Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Unity 2017.2 beta is now available for download.
    Dismiss Notice

*Shocked*: Particle Emitter Not Scalable? [SOLVED!]

Discussion in 'Editor & General Support' started by MrBurns, Sep 17, 2011.

  1. MrBurns

    MrBurns

    Joined:
    Aug 16, 2011
    Posts:
    379
    Someone might wonder why I am shocked about that. Frankly, I didn't have time to investigate particle emitters up to now. Before was just happy when they looked good :). Numerous threads about this topic indicate that I am not the only one who is scared... Before you click away, I managed to solve this problem right away, but only partially (see below)...

    (EDIT: I have now validated my approach with all of my [also compound with hierarchies and mixed with real meshes] particle emitters, it works like a charm :))

    So besides the fact that EllipsoidParticleEmitter and MeshParticleEmitter are not exposed (and they are not even really existing, must be runtime generated or something like that probably from US) this is a MAJOR drawback!! Please UT fix this as soon as possible. What the hell shall I do with a non-scalable particle system? Anyone got an idea? Both special particle emitters can be setup quite easily in code so this is not really a problem, but scaling really is!

    So the (partial) solution, which is only for non-moving (or only very slowly moving) particle systems. No wait... how awesome is that, it works for local space particle system without any restrictions!! So only world space particle system look weird when moving and scaled... For me this is not much of a problem, since world space PS don't move usually... But local space are fired out of weapons for example...

    What do you need:

    1) A script attached to the particle renderer:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class ParticleScaling : MonoBehaviour
    5. {
    6.     public void OnWillRenderObject()
    7.     {
    8.         GetComponent<ParticleRenderer>().material.SetVector("_Center", transform.position);
    9.         GetComponent<ParticleRenderer>().material.SetVector("_Scaling", transform.lossyScale);
    10.         GetComponent<ParticleRenderer>().material.SetMatrix("_Camera", Camera.current.worldToCameraMatrix);
    11.         GetComponent<ParticleRenderer>().material.SetMatrix("_CameraInv", Camera.current.worldToCameraMatrix.inverse);
    12.     }
    13. }
    14.  
    This may seem strange but actually the PS are billboards and get transformed on CPU like it seems. The GPU just applies the projection matrix. Thus we have to pass the "real" camera vectors ourselves as wells as the parts of the object transform...

    2) A modified particle shader:
    Code (csharp):
    1.  
    2. float4 _MainTex_ST;
    3.  
    4. // new 4 lines
    5. float3 _Scaling;
    6. float4 _Center;
    7. float4x4 _Camera;
    8. float4x4 _CameraInv;
    9.  
    10. v2f vert (appdata_t v)
    11. {
    12.     v2f o;
    13.  
    14.     // START OF CHANGE
    15.     float4 objV = mul(_CameraInv, v.vertex);
    16.  
    17.     objV.xyz -= _Center;
    18.     objV.xyz = float3(_Scaling.x * objV.x, _Scaling.y * objV.y, _Scaling.z * objV.z);
    19.     objV.xyz += _Center;
    20.  
    21.     o.vertex = mul(_Camera, objV);
    22.     // END OF CHANGE
    23.  
    24.     o.vertex = mul(UNITY_MATRIX_P, o.vertex);
    25.  
    26.     #ifdef SOFTPARTICLES_ON
    27.     o.projPos = ComputeScreenPos (o.vertex);
    28.     COMPUTE_EYEDEPTH(o.projPos.z);
    29.     #endif
    30.     o.color = v.color;
    31.     o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    32.     return o;
    33. }
    34.  
    This simply transform the particle vertex back into world space, applies the concentric scaling to all particles, and then just transforms the result back into camera space...

    Maybe someone advanced can shed some light on this mess??? I don't for the life of me understand why this isn't implemented by default... Sorry!

    PS: Someone could also solve the world space problem partially by passing some sort of a "MaxEmitterRange" to the shader. Then you could adjust your scaling and take care of that particles do not leave this sphere but it may look weird either...
     

    Attached Files:

    Last edited: Sep 18, 2011
    Deltaflux and Baste like this.
  2. MrBurns

    MrBurns

    Joined:
    Aug 16, 2011
    Posts:
    379
    Doesn't anybody have the same problem ^^? Or is there a better way to scale these things?
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
    Oh, I have the problem. I just haven't bothered to try to solve it for the general case. I've only needed to scale by negative 1, which I've done via negating transform.position and worldVelocity.
     
  4. MrBurns

    MrBurns

    Joined:
    Aug 16, 2011
    Posts:
    379
    Ok, I took the liberty to necro your thread ^^... But I have a collection of different emitters and they shall be attach to objects... When these objects are scaled everything is screwed up! I have now updated the script and the shader. It will work out of the box and take the scaling out of the PS tranform...
     
    Last edited: Sep 18, 2011
  5. MrBurns

    MrBurns

    Joined:
    Aug 16, 2011
    Posts:
    379
    Still feeling like an outcast ^^... So how do you scale your particle emitters? I mean especially if you buy one the chance is good that it won't fit perfectly into your scene??
     
  6. imphy

    imphy

    Joined:
    Jun 28, 2011
    Posts:
    339
    I've just encountered this as well I think. I want to scale an explosion prefab (contains 4 child particle systems) and I was surprised to see that altering the transform.localScale didn't affect the size of the explosion.
     
  7. MrBurns

    MrBurns

    Joined:
    Aug 16, 2011
    Posts:
    379
    Yes this is also my main application if you speak of Detonator framework ;). They are just way too big...
     
  8. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    533
    2014... Particle Systems still don't react to transform scale in an intuitive manner?
    It is one thing to apply scale to a world space system - that's just prone to errors.
    But not being able to scale a local space system is somewhat ... 90ish.
     
  9. pachermann

    pachermann

    Joined:
    Dec 18, 2013
    Posts:
    35
    Hi mister Burns

    thanks for this clearing post...
    Since iam not the coder but the scenen editor i wll delegate this to our programmer.

    But are there any news about the scaling problems?

    I bought a very nice particle package and of coarse we can't scale.

    kind regards
    p
     
  10. Deltaflux

    Deltaflux

    Joined:
    May 29, 2013
    Posts:
    2
    2015.. Still not officially supported as far as I can tell. Seems like a big oversight.
     
  11. zeblackness

    zeblackness

    Joined:
    Jun 13, 2015
    Posts:
    3