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

Creating data texture to sample in vertex shader gives odd values?

Discussion in 'Shaders' started by Bas-Smit, Jul 8, 2019.

  1. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    Hi, Im trying to pass some data to a shader using a texture created from code. I need one float value per vertex so TextureFormat.RFloat seems to make sense:

    var tex = new Texture2D(32, 32, TextureFormat.RFloat, false, true); // (no mips, linear)


    To be on the safe side I make sure all values are set to 0:

    var t = tex.GetRawTextureData<float>();
    for (int i = 0; i < t.Length; i++)
    t[i] = 0;
    tex.LoadRawTextureData(t);
    tex.Apply(false);


    I then assign the texture to the material:

    material.SetTexture("_Data", tex);


    In the vertex shader I sample the texture:

    v.vertex.y = tex2Dlod(_Data, float4(v.texcoord.xy, 0, 0));


    I expect the value to be 0, but it ends up being 0.5. I've tried many other texture formats, but I can't manage to get even a 0 out. Setting the y to any number yields the expected result:

    v.vertex.y = 5


    Am I doing something wrong?

    Cheers, Bas
     
  2. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    I had to use MaterialPropertyBlock.SetTexture