Search Unity

How to Terrain paint with a shader?

Discussion in 'Shaders' started by Korosenai_Mod, Oct 27, 2016.

  1. Korosenai_Mod

    Korosenai_Mod

    Joined:
    Jan 24, 2016
    Posts:
    3
    Hi,
    I'm new to the forums and this is my first post. Apologies in advance if I'm posting wrong.

    Goal: I'm trying to get this simple tessellation code to work with Unity terrain so that I can paint with it. How do I do this?

    Note: the code works with objects but not Unity Terrain. I'm I missing something obvious?

    Not C# Code:
    Code (CSharp):
    1. Shader "Nature/Terrain/GroundH"
    2. {
    3.    Properties {
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _BumpMap ("Normal Map (RGB)", 2D) = "bump" {}
    7.         _BumpScale ("Normal Power" , Float) = 1
    8.         _Occlusion ("Occlusion Map (R)" , 2D) = "white" {}
    9.         _Metallic ("Metallic (R) , Smoothness (A)" , 2D) = "black" {}
    10.         _ParallaxMap ("Heightmap (A)", 2D) = "black" {}
    11.         _EdgeLength ("Edge length", Range(3,50)) = 10
    12.         _Parallax ("Height", Range (0.0, 1)) = .5
    13.     }
    14.     SubShader {
    15.         Tags { "RenderType"="Opaque" }
    16.         LOD 200
    17.      
    18.         CGPROGRAM
    19.         // Physically based Standard lighting model, and enable shadows on all light types
    20.      
    21.         #pragma surface surf Standard fullforwardshadows vertex:disp tessellate:tessEdge
    22.         #include "Tessellation.cginc"
    23.      
    24.         sampler2D _ParallaxMap;
    25.         float _EdgeLength;
    26.         fixed _Parallax;
    27.         float _BumpScale;
    28.         sampler2D _MainTex;
    29.         sampler2D _Occlusion;
    30.         sampler2D _BumpMap;
    31.         sampler2D _Metallic;
    32.      
    33.         struct appdata {
    34.             float4 vertex : POSITION;
    35.             float4 tangent : TANGENT;
    36.             float3 normal : NORMAL;
    37.             float2 texcoord : TEXCOORD0;
    38.             float2 texcoord1 : TEXCOORD1;
    39.             float2 texcoord2 : TEXCOORD2;
    40.         };
    41.      
    42.         float4 tessEdge (appdata v0, appdata v1,appdata v2)
    43.         {
    44.             return UnityEdgeLengthBasedTessCull (v0.vertex, v1.vertex, v2.vertex, _EdgeLength, _Parallax * 1.5f);
    45.         }
    46.  
    47.         void disp (inout appdata v)
    48.         {
    49.             float d = tex2Dlod(_ParallaxMap, float4(v.texcoord.xy,0,0)).a * _Parallax;
    50.             v.vertex.xyz += v.normal * d;
    51.         }
    52.         // Use shader model 3.0 target, to get nicer looking lighting
    53.         #pragma target 3.0
    54.         struct Input {
    55.             float2 uv_MainTex;
    56.         };
    57.         fixed4 _Color;
    58.         void surf (Input IN, inout SurfaceOutputStandard o) {
    59.             // Albedo comes from a texture tinted by color
    60.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    61.             o.Albedo = c.rgb;
    62.             // Metallic and smoothness come from slider variables
    63.             fixed4 m = tex2D(_Metallic,IN.uv_MainTex);
    64.             o.Metallic = m.r;
    65.             o.Occlusion = tex2D(_Occlusion,IN.uv_MainTex).x;
    66.             o.Smoothness = m.a;
    67.             o.Normal = normalize(UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale));
    68.             o.Alpha = c.a;
    69.         }
    70.         ENDCG
    71.     }
    72.     FallBack "Diffuse"
    73. }