Search Unity

Changing the Heightmap of the Shader during rendering

Discussion in 'Scripting' started by Pascal_Kelm, Oct 8, 2015.

  1. Pascal_Kelm

    Pascal_Kelm

    Joined:
    Sep 24, 2015
    Posts:
    24
    I am using a 'Tessellation/Bumped Specular (displacment)' Shader and I want to change the heightmap during the rendering by C# code. I could not find a method to change the heightmap. I am a beginner... please give me some help by code.

    Code (CSharp):
    1. Shader tx = GetComponent<Renderer> ().sharedMaterial.shader as Shader;
    At this moment my first frame looks like this... done by drag and drop.
     
  2. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    Pascal_Kelm likes this.
  3. Pascal_Kelm

    Pascal_Kelm

    Joined:
    Sep 24, 2015
    Posts:
    24
  4. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    Code (CSharp):
    1. public Texture2D normalMapTexture;
    2.  
    3. ...
    4. // assign texture
    5. Renderer renderer = GetComponent<Renderer>();        
    6. renderer.material.EnableKeyword ("_NORMALMAP");
    7. renderer.material.SetTexture("_BumpMap", normalMapTexture);
    8. ...
    9.  
     
    Pascal_Kelm likes this.
  5. Pascal_Kelm

    Pascal_Kelm

    Joined:
    Sep 24, 2015
    Posts:
    24
    @Jordi Bonastre It is not updating the heightmap. Do you see a mistake
    Code (CSharp):
    1. void Update () {
    2.         if (vr_objects.Count () > 0) {
    3.             int index = (int)(Time.time * 30);      
    4.             index = index % vr_objects.Count ();
    5.             if (index < vr_objects.Count ()) {
    6.  
    7.                 // Displacement map - not working                      
    8.                 renderer.material.EnableKeyword ("_PARALLAXMAP");
    9.                 renderer.material.SetTexture("_PARALLAXMAP", disp_tex[index]);
    10.  
    11.                 //Moving plane - works
    12.                 Vector3 vec = new Vector3 (0, transform.position.y, (float) vr_objects[index].Rel_Position); // transform.position.z + 1);
    13.                 transform.position = vec;
    14.             }
    15.         }
    16.     }


     
  6. Pascal_Kelm

    Pascal_Kelm

    Joined:
    Sep 24, 2015
    Posts:
    24
    Ahhh... Thanks @Jordi Bonastre
    Code (CSharp):
    1. renderer.material.EnableKeyword ("_PARALLAXMAP");
    2.                 renderer.material.SetTexture("_ParallaxMap", disp_tex[index]);