Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sampling NoiseModule from script and/or shader?

Discussion in 'General Graphics' started by chrismarch, Nov 10, 2017.

  1. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    470
    I'd like to sample the output of a NoiseModule being used with a ParticleSystem from a C# script. Is this possible? I think I remember seeing that it is now possible to do this from shaders, but I might be misremembering, and I can't find any mention of this in the docs for NoiseModule.

    I'd also like to do this with ColorOverLifetimeModule.

    (I want to use output from existing particle system modules, instead of separate code, so that I can match the particle system appearance with some secondary effects)
     
    Last edited: Nov 10, 2017
  2. ifurkend

    ifurkend

    Joined:
    Sep 4, 2012
    Posts:
    350
    I am kinda sad that there is no example code in GetActiveVertexStream to explain how to convert the ParticleSystemVertexStream output to useful data type like Vector3 for other script. This is the farthest I can get but it still does nothing meaningful:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class getVertexStream : MonoBehaviour {
    5.    ParticleSystemRenderer psr;
    6.    ParticleSystemVertexStream[] psvsa;
    7.    void Awake() {
    8.        psr = GetComponent<ParticleSystemRenderer>();
    9.        psvsa = new ParticleSystemVertexStream[psr.activeVertexStreamsCount];
    10.        psr.GetActiveVertexStreams(new List<ParticleSystemVertexStream>(psvsa));// Assuming you have enabled Custom Vertex Streams in Particle System Renderer and added a 5th stream like NoiseSum.x
    11.        Debug.Log(psvsa[4]);// This generates string "Position" only.
    12.    }
    13. }
     
  3. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    You can use ParticleSystem.GetParticles and ParticleSystem.Particle.GetCurrentColor to do this.

    For noise, there is nothing, but we can add it to our roadmap. Thanks for the idea.
     
    chrismarch likes this.
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    What the you trying to do?
     
  5. ifurkend

    ifurkend

    Joined:
    Sep 4, 2012
    Posts:
    350
    Since Chrismarch and I share this same question, I thought there were promise in the previous official blog post about being able fetch the noise (sum/impulse) data from particle system for other script instead of just shader. I can't find that specific post, so let's assume we have collective hallucination.
     
  6. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    I don’t recall anything like that :)

    But it’s on our roadmap now.
     
  7. kvam

    kvam

    Joined:
    Sep 28, 2019
    Posts:
    9
    Any updates on this?
     
  8. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    Yes - you can fetch the noise data in a shader - there is a Custom Vertex Stream for it. Probably since Unity 2017.something, I think.

    It's still not possible to fetch the noise data in a script, and this probably won't get added now, because Unity is focusing on developing the Visual Effect Graph for particle simulations.
     
  9. kvam

    kvam

    Joined:
    Sep 28, 2019
    Posts:
    9
    Ok, thanks! I'm a total beginner to shaders and shader graph. Do you know if I could use the noise generated in a shader to control the position of an object, like a light? I want to create custom lit particles (ish) as that is not supported in 2D URP atm.

    @richardkettlewell
     
  10. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    I suspect not, because shader graph noise is for use on the GPU, and a light position would most likely be set by the CPU.
     
  11. kvam

    kvam

    Joined:
    Sep 28, 2019
    Posts:
    9
    Ok, thank you! On a different note, is it possible to expose values from VFX-graph? I know you can do it the other way around, and expose variables from the graph to the script for manipulation, but can I do it the other way as an output?

    Have to give you kudos for these replies btw. Thank you so much!
     
    richardkettlewell likes this.
  12. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    I’m not sure of the answer to that! @JulienF_Unity can you help?
     
  13. kvam

    kvam

    Joined:
    Sep 28, 2019
    Posts:
    9
    Either way, I think it would be extremely powerful if I could, because it makes it possible (or at least a lot easier) for other systems or game objects to react to the VFX and not only the other way around.