Search Unity

Blog Post: ParticleSystem modules FAQ

Discussion in 'General Graphics' started by karl_jones, Apr 20, 2016.

  1. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Hello.
    I wanted to explain how scripting access to the particle system’s modules work and what our future plans for its development are. Check out the blog post with code examples here

    Feel free to add comments/questions here.
     

    Attached Files:

    Last edited: Apr 20, 2016
    AmirArdy, ZJP and hippocoder like this.
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    You are too quick! I posted the fourm post and then the blog. 1-2 minute delay
     
    theANMATOR2b likes this.
  4. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Thanks for confirming what was going on.

    I think the change is class is worthwhile even at the expense of a bit of GC; right now, the implementation goes against every bit of .NET/C# standards.

    You should be pooling any particle effects spawned at runtime anyways. Therefore, the allocations is no issue if handled correctly.
     
    karl_jones likes this.
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Thanks.
    The change to class does sound simple although it will likely introduce binary incompatibility, so it needs further discussion with the scripting team on how to do this safely.
     
  6. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,090
    Agreed. I actually had to search Google earlier this week to figure out how to change the emission rate because standard C#/.NET stuff wouldn't work. Things made sense once I read about the interface stuff on some other post (not the blog entry) but that still took way too much effort to figure out - the instincts are to treat structs as structs and when you try to do that you get build errors because setters don't exist...
     
  7. Cenda

    Cenda

    Joined:
    Jun 3, 2010
    Posts:
    66
    Hello I am just playing with particle and new system of modules. I wonder how can I change second color of Start Color paramater in script.
     

    Attached Files:

  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
  9. Cenda

    Cenda

    Joined:
    Jun 3, 2010
    Posts:
    66
    GetComponent<ParticleSystem>().startColor is type Color, so if I assign MinMaxGradient I get:

    Cannot implicitly convert type `UnityEngine.ParticleSystem.MinMaxGradient' to `UnityEngine.Color'
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Ok some values do not have support for setting the curve.
    We have added this support and it should be in an upcoming version.
     
    SonicBloomEric likes this.
  11. Cenda

    Cenda

    Joined:
    Jun 3, 2010
    Posts:
    66
    Thanks for info Karl. I will wait for newer version.
     
  12. FriendAndFoeMatt

    FriendAndFoeMatt

    Joined:
    Jan 27, 2015
    Posts:
    11
    Just came across the module documentation; is the change to class (reference types) a given?
     
  13. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Yes the plan is still to do this. We will need to deprecate the old structs and introduce new versions which are classes. Should start to happen in the next few versions (eta 5.6~)
     
  14. mahdi_jeddi

    mahdi_jeddi

    Joined:
    Jul 18, 2016
    Posts:
    246
    would've been interesting to see how difference the SIMD optimizations made to the performance results.
     
  15. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Yeah im sure we will do some comparisons in the future.
     
  16. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    I profiled some example systems as we did the work - generally the new SIMD code performs 2-3x faster. (4x is generally unachievable due to other bottlenecks such as memory/cache)

    But there is lots of "other" non SIMD code that can affect overall particle system performance (memory management, sub emitter logic, physx collision, etc) so results vary a lot depending on what the system is doing.
     
    mahdi_jeddi and karl_jones like this.
  17. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Can I set the custom mesh ,and make my self buildboard particle shader in particle system now?
     
  18. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Yes, set the mesh in the renderer module, add some vertex streams so you have the info you need in your custom shader, and it should work :)

    You probably want the Size, Rotation and CenterAndVertexID streams.
     
    karl_jones likes this.
  19. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Wow, any example about vertex streams? I want make a big atlas for different particles and share material~
     
  20. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
  21. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    As Karl says, it will be easier to use the Texture Animation Module to do that, as long as your particle textures are the same size and can be placed on a grid-based texture sheet.

    We don't have any good examples in the User Manual yet for vertex streams - I'm actually working on getting stuff into the manual this week though :)
     
    karl_jones likes this.
  22. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Texture Animation Module is not good ,will waste a lot of memory.So i want to use atlas which includes not the same size textures,this can set uv stream to achieve
     
    richardkettlewell likes this.
  23. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    dreamerflyer is right, the animation module is not that good, its flexability /w mesh in new v. is good....but large art depts' need a way to author single textures, exchanged in version control, and on bundle the system organizes atlases (tagged/LOD'd) so primary, secondary, tertiary (etc...) textures can auto feed to shuriken without artists manually re-mapping every particle component.

    if a project deprecates a particle texture @1,1 in a 2x2 atlas, then adds FOUR particles (this quadrant becomes it's own 2x2),,,, artists have to hunt for every use of 1,1, convert to 4x4 and identify location 0,1,4,5 to access them....tracking these adjustments >.< risk of error accelerates within teams.

    the 2D sprite atlas system I wish fed into the shuriken module, a low-level atlas tool that is used by both particles and 2D sprite render seems ideal.
     
  24. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    This kind of suggestion has come up a few times before. I'll make sure we dedicate some time to evaluating what we can do here. Thanks for the feedback!
     
    Torbach78 and karl_jones like this.
  25. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
  26. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    We have an update to the script documentation on the way, which adds a lot of example scripts. But, until that is released publicly, here is one of the examples that we have added:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections.Generic;
    4.  
    5. public class ExampleClass : MonoBehaviour {
    6.  
    7.     private ParticleSystem ps;
    8.     private List<Vector4> customData = new List<Vector4>();
    9.     private int uniqueID;
    10.  
    11.     void Start() {
    12.  
    13.         ps = GetComponent<ParticleSystem>();
    14.     }
    15.  
    16.     void Update() {
    17.  
    18.         ps.GetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
    19.  
    20.         for (int i = 0; i < customData.Count; i++)
    21.         {
    22.             // set custom data to the next ID, if it is in the default 0 state
    23.             if (customData[i].x == 0.0f)
    24.             {
    25.                 customData[i] = new Vector4(++uniqueID, 0, 0, 0);
    26.             }
    27.         }
    28.  
    29.         ps.SetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
    30.     }
    31. }
     
    karl_jones and dreamerflyer like this.
  27. Jean-Moreno

    Jean-Moreno

    Joined:
    Jul 23, 2012
    Posts:
    593
    Hi @karl_jones and @richardkettlewell!
    I was wondering if you were able to share more details on the optimized curves part of the particle systems.
    Reason is I'd like to use this optimization for another thing unrelated to particle systems, and I'm not so good at maths.
    I think I understood that internally you store polynomial coefficients and that you then just sample the math function directly without the AnimationCurve overhead, so... say I have an AnimationCurve that answers to the polynomial constraints ("no more than 3 keys with one at each end" as your doc says), how can I convert it to the polynomial coefficients that will give me the same curve with just the math function?
    And is the polynomial a cubic function in the form of ax³+bx²+cx+d?
    Thanks for any insight!
     
  28. wutongtongtong

    wutongtongtong

    Joined:
    Jan 11, 2018
    Posts:
    8
    @richardkettlewell!
    I've tried this simple example you provided but when I step through the code GetCustomParticleData always returns vectors of 0,0,0,0
    why?
    Has anyone tested this lately? I'm on 2017.3.0
     
  29. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Maybe you have the Custom Data Module enabled in the Inspector? That will overwrite the data with whatever is configured in there.
     
  30. wutongtongtong

    wutongtongtong

    Joined:
    Jan 11, 2018
    Posts:
    8

    Attached Files:

    richardkettlewell likes this.
  31. wutongtongtong

    wutongtongtong

    Joined:
    Jan 11, 2018
    Posts:
    8
    Hi @richardkettlewell!
    I want to take the color of particles as a vertex stream into the shader.Why to display "not match"? Thank you!
    upload_2018-1-18_10-43-14.png
     

    Attached Files:

  32. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    At a glance, it looks correct. Some minor tweaks: vertex can be float3, and color can be fixed4. I wouldn't expect either to fix the issue though.

    We would need more info to offer more help. Please start a new thread here: http://forum.unity3d.com/forums/general-graphics.76/ Please include the entire shader, if possible, and even better would be a minimal reproduction project.
     
    karl_jones likes this.
  33. wutongtongtong

    wutongtongtong

    Joined:
    Jan 11, 2018
    Posts:
    8
  34. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Is there a simple example somewhere of controlling a particle size over lifetime from code?
    Think a spaceship thruster changing size based on thruster input from user, so at 0 or completely braked it's barely ticking over and at full thrust it's full bore?
     
  35. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
  36. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Thanks. So changing the scalar size value is the best way to control size, keeping the curve constant?
    So you need to
    sz.size = new ParticleSystem.MinMaxCurve(scalingValue, curve); in update?
     
  37. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    sizeMultiplier is a faster way to only change the curve scale
     
    karl_jones likes this.