Search Unity

Get Input struct data from displacement function

Discussion in 'Shaders' started by Dolzen, Mar 16, 2017.

  1. Dolzen

    Dolzen

    Joined:
    Mar 26, 2014
    Posts:
    90
    I'm just beginning with surface shaders and I'm trying to figure out how to acces Input struct from the displacement function "disp", within the surf function I can just type IN. to acces any input struct I want (UV), but from disp function wich is the function that manage the displacement, I cant. Can some one help me to figure out how to do that? Thanks!
    Edit: I would like to use this function tex2D (_MainTex, IN.uv_MainTex) inside disp function is it possible?
    Code (CSharp):
    1. Shader "Planet Generator/Test" {
    2.         Properties {
    3.             _Color ("Color", color) = (1,1,1,0)
    4.             _MainTex ("Base (RGB)", 2D) = "white" {}
    5.             _DispTex ("Disp Texture", 2D) = "gray" {}
    6.             _Displacement ("Displacement", Range(0, 1.0)) = 0.3
    7.             _Tess ("Tessellation", Range(1,32)) = 4
    8.         }
    9.         SubShader {
    10.             Tags { "RenderType"="Opaque" }
    11.             LOD 300
    12.          
    13.             CGPROGRAM
    14.             #pragma surface surf Lambert vertex:disp tessellate:tessDistance
    15.             #pragma target 4.6
    16.             #include "Tessellation.cginc"
    17.  
    18.             struct appdata {
    19.                 float4 vertex : POSITION;
    20.                 float4 tangent : TANGENT;
    21.                 float3 normal : NORMAL;
    22.                 float2 texcoord : TEXCOORD0;
    23.             };
    24.  
    25.             float _Tess;
    26.  
    27.             float4 tessDistance (appdata v0, appdata v1, appdata v2) {
    28.                 float minDist = 5.0;
    29.                 float maxDist = 5.0;
    30.                 return UnityDistanceBasedTess(v0.vertex, v1.vertex, v2.vertex, minDist, maxDist, _Tess);
    31.             }
    32.  
    33.             sampler2D _DispTex;
    34.             float _Displacement;
    35.  
    36.             void disp (inout appdata v)
    37.             {
    38.                 float d = tex2Dlod(_DispTex, float4(v.texcoord.xy,0,0)).r * _Displacement;
    39.                 v.vertex.xyz += v.normal * d;
    40.             }
    41.  
    42.             struct Input {
    43.                 float2 uv_MainTex;
    44.             };
    45.  
    46.             sampler2D _MainTex;
    47.             fixed4 _Color;
    48.  
    49.             void surf (Input IN, inout SurfaceOutput o) {
    50.                 half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    51.                 o.Albedo = c.rgb;
    52.             }
    53.             ENDCG
    54.         }
    55.         FallBack "Diffuse"
    56.     }
     
    Last edited: Mar 16, 2017