1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

Get Input struct data from displacement function

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

  1. Dolzen

    Dolzen

    Joined:
    Mar 26, 2014
    Posts:
    43
    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