Search Unity

how to store height data for shader use?

Discussion in 'Shaders' started by stationx, Oct 1, 2015.

  1. stationx

    stationx

    Joined:
    Jul 9, 2012
    Posts:
    251
    hello!
    Is there a way that I can store and reuse height map data into a shader?

    I know I can read out world position of vertex but I need to store that data ones and reuse it, instead of that it constant updates with new information.
    Basically, 'bake' Y info into a variable.

    Regards!
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I'm not sure I understand the question... but if you want to make it so your shader runs only when you specifically tell it to, use the Graphics.Blit() function.
     
  3. stationx

    stationx

    Joined:
    Jul 9, 2012
    Posts:
    251
    Heey, i am more looking for a function something like 'init()' inside a shader.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Shaders inherently do not do this. They take data from one end and spit out data on the other. They save no state between frames on their own. If you want this you need to supply the data on your own.

    There's many ways to go about this, depending on what's needed and what changes in your mesh.

    You could bake out the information you need into the vertex information (either color or use a UV channel) on the CPU side or in an external art tool.

    You could render a depth buffer using a replacement shader and render to texture and feed that into the shader.

    You could store off the object2world matrix transform from the moment you want to recall and supply it to the shader as another input to use it to transform the vertex data into the correct position.
     
  5. stationx

    stationx

    Joined:
    Jul 9, 2012
    Posts:
    251
    That last one....that sounds interesting...how does that look? You mind providing an example?
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    MeshRenderer meshRen = gameObject.GetComponent<MeshRenderer>();
    meshRen.material.SetMatrix("_FrozenMatrix", meshRen.localToWorldMatrix);

    Run that once at the time you want to store, then use that matrix in your shader to mul the vertex info and pull the .y from that.

    This won't work on skinned meshes or any mesh that is modified dynamically on the CPU side. This basically just lets you recall an orientation and offset position at a specific point in time.