Search Unity

Surface Shader 'vert' function texture lookups

Discussion in 'Shaders' started by teammetablast, Nov 23, 2010.

  1. teammetablast

    teammetablast

    Joined:
    Nov 22, 2010
    Posts:
    13
    Hello!

    I'm trying to write a surface shader that uses a noise texture for vertex displacement. However, I am getting a compile error when trying to do a texture lookup in the 'vert' function.


    Here is the code for the shader (obviously not finished, don't point and laugh! ;) )

    Code (csharp):
    1.   Shader "Test/Noise" {
    2.     Properties {
    3.       _MainTex ("Texture", 2D) = "white" {}
    4.       _NoiseTex("Noise", 2D) = "white" {}
    5.       _TimeScale ("Time Scaling", Range(0,10)) = 1.0
    6.       _Displacement("Displacement", Range(0,5)) = 1.0
    7.     }
    8.     SubShader {
    9.       Tags { "RenderType" = "Opaque" }
    10.       CGPROGRAM
    11.  
    12.       #pragma target 3.0
    13.           #pragma surface surf Lambert vertex:vert
    14.          
    15.       float _Sharpness;
    16.       float _Displacement;
    17.       float _TimeScale;
    18.       sampler2D _NoiseTex;
    19.      
    20.       void vert(inout appdata_full v)
    21.       {
    22.         float2 uvHack = float2(frac(v.vertex.x + v.vertex.y + v.vertex.z),
    23.                                 frac(v.vertex.x * v.vertex.y * v.vertex.z));
    24.        
    25.         float3 noise = tex2D(_NoiseTex,uvHack).rgb;
    26.        
    27.         v.vertex.xyz = v.vertex.xyz - (v.normal.xyz * (noise.x * _Displacement));
    28.       }
    29.      
    30.           sampler2D _MainTex;
    31.      
    32.           struct Input {
    33.               float2 uv_MainTex;
    34.           };
    35.  
    36.           void surf (Input IN, inout SurfaceOutput o) {
    37.          
    38.               o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    39.           }
    40.  
    41.       ENDCG
    42.     }
    43.     Fallback "Diffuse"
    44.   }

    Is it even possible to do this with a surface shader? I've never even tried this, and some googling informed me that it is possible (dependant on hardware) if I write out the vertex and fragment shaders separately, but I would like the functionality of using the surface shader to handle lighting.

    I'm new to unity/ShaderLab, but I have some experience with CgFX, so I have a fairly good grasp of what is going on, I'm just not sure if what I want to do is possible or not.
     
  2. teammetablast

    teammetablast

    Joined:
    Nov 22, 2010
    Posts:
    13
    Devilwhale likes this.