Search Unity

Output o.albedo as a function of vertex normal and vertex position?

Discussion in 'Shaders' started by frogsbo, Aug 29, 2014.

  1. frogsbo

    frogsbo

    Joined:
    Jan 16, 2014
    Posts:
    79
    This is a tessellation code based on Unity tessellation example code page.

    I want to write albedo based on the vertex normal and world position and tessellation vertex position, but i have no idea how to pass the vertex and normal variables from struct to surface shader.

    Please help.


    Code (csharp):
    1.  
    2. Shader "Test Tessellation" {
    3.  
    4.         Properties {
    5.             _Tess ("Tessellation", Range(1,32)) = 4
    6.             _MainTex ("Base (RGB)", 2D) = "white" {}
    7.             _DispTex ("Disp Texture", 2D) = "gray" {}
    8.             _NormalMap ("Normalmap", 2D) = "bump" {}
    9.             _Displacement ("Displacement", Range(0, 1.0)) = 0.3
    10.             _Color ("Color", color) = (1,1,1,0)
    11.             _SpecColor ("Spec color", color) = (0.5,0.5,0.5,0.5)
    12.         }
    13.         SubShader {
    14.             Tags { "RenderType"="Opaque" }
    15.             LOD 300
    16.          
    17.             CGPROGRAM
    18.             #pragma surface surf BlinnPhong addshadow fullforwardshadows vertex:disp tessellate:tessDistance nolightmap
    19.             #pragma target 5.0
    20.             #include "Tessellation.cginc"
    21.  
    22.             struct appdata {
    23.                 float4 vertex : POSITION;
    24.                 float4 tangent : TANGENT;
    25.                 float3 normal : NORMAL;
    26.                 float2 texcoord : TEXCOORD0;
    27.             };
    28.  
    29.             float _Tess;
    30.  
    31.             float4 tessDistance (appdata v0, appdata v1, appdata v2) {
    32.                 float minDist = 10.0;
    33.                 float maxDist = 25.0;
    34.                 return UnityDistanceBasedTess(v0.vertex, v1.vertex, v2.vertex, minDist, maxDist, _Tess);
    35.             }
    36.  
    37.             sampler2D _DispTex;
    38.             float _Displacement;
    39.  
    40.             void disp (inout appdata v)
    41.             {
    42.                 float d =sin(v.vertex.z*.021)*111;
    43.                 v.vertex.xz += v.normal.xz ;
    44.                 v.vertex.y += v.normal.y * d;
    45.             }
    46.  
    47.             struct Input {
    48.                 float2 uv_MainTex;
    49.                 float3 normal;
    50.              
    51.             };
    52.  
    53.             sampler2D _MainTex;
    54.             sampler2D _NormalMap;
    55.             fixed4 _Color;
    56.  
    57.             void surf (Input IN, inout SurfaceOutput o) {
    58.                 half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    59.                 o.Albedo = c.rgb;
    60.                 o.Specular = 0.2;
    61.                 o.Gloss = 1.0;
    62.                 o.Normal = IN.normal;//UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
    63.             }
    64.             ENDCG
    65.         }
    66.         FallBack "Diffuse"
    67.     }
    68.  
     
    Last edited: Aug 29, 2014
  2. frogsbo

    frogsbo

    Joined:
    Jan 16, 2014
    Posts:
    79
    I did this for fun to make normals based on the fragment position.

    It was blindingly white and black so i devided the output normal by 1000, and it gives me some kind of strange world shading... it's almost right, except that that the upwards facing vertices are always dark, so it's like the normal is skewed.

    Why did i have to devide the normals by 1000 to get something not crazily bright and why is the normal not at the same angle as unitynormals from the mesh?

    Is it very angular because i have to do Blinn Phong rounding on it?
    Code (csharp):
    1.  
    2.             void surf (Input IN, inout SurfaceOutput o) {
    3.                 half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    4.                 float3 objectPos = mul(_World2Object,float4(IN.worldPos,1)).xyz;
    5.                 float3 w = IN.worldPos;
    6.          
    7.                 o.Albedo = c.rgb;//sin(w*0.01);
    8.                 o.Specular = 0.0;
    9.                 o.Gloss = 0.0;
    10.          
    11.                 float3 wps = normalize(cross(ddx(w)-w , ddy(w) -w ))*.001;
    12.          
    13.                 o.Normal = wps;
    14.             }
    15.             ENDCG
    16.  
    2014-08-30_013157.jpg
    ddx.jpg
     
    Last edited: Aug 30, 2014