TerrainTransparency

From Unify Community Wiki
Jump to: navigation, search


Description

This is a shader to allow transparent textures on terrains. It does this by overriding the terrain shader, checking for a transparent texture, and, if it finds one, adjusting the terrain's visibility to the transparent texture's splatmap opacity data.

Features:

  • Enables the use of a transparent texture on the terrain.

The main use of this shader is for adding 'holes' or 'caves' to terrains, although you can also use it for making terrain areas partially transparent, or for making a terrain non-rectangular.

Usage

  • Copy the shader code below and paste it into a new shader file within your project's Assets folder. (you can name it whatever you want)
  • Create a completely transparent image and add it as one of your terrain's textures.
  • Paint the transparent texture onto your terrain, and wherever you paint, the terrain will become transparent!

Notes:

  • For the transparency to work, you must place the transparent texture into one of the first four slots. (technical reason; the 'first pass' shader that is overridden can only access the first four splatmaps)
  • If you apply the transparent texture with only partial opacity, you can make existing textures 'partially transparency'.
  • To remove the transparency, just paint over it with another texture.
  • The terrain collider will still be active on transparent parts of the terrain. To fix this, you would need to make a script that disables the collider when the player is over a transparent area.

Shader - TerrainTransparency.shader

Shader "Nature/Terrain/Diffuse"
{
    Properties
    {
        [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {}
        [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
        [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
        [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
        [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
        //Used in fallback on old cards & base map
        [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {}
        [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
    }
 
    SubShader
    {
        Tags
        {
            "SplatCount" = "4"
            "Queue" = "Geometry-100"
            "RenderType" = "Opaque"
        }
        Blend SrcAlpha OneMinusSrcAlpha
        CGPROGRAM
        #pragma surface surf Lambert
        struct Input
        {
            float2 uv_Control : TEXCOORD0;
            float2 uv_Splat0 : TEXCOORD1;
            float2 uv_Splat1 : TEXCOORD2;
            float2 uv_Splat2 : TEXCOORD3;
            float2 uv_Splat3 : TEXCOORD4;
        };
 
        sampler2D _Control;
        sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
 
        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 splat_control = tex2D (_Control, IN.uv_Control);
            fixed4 firstSplat = tex2D (_Splat0, IN.uv_Splat0);
            fixed3 col;
            col = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
            col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
            col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
            col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
            o.Albedo = col;
            o.Alpha = 1;
            if(tex2D(_Splat0, IN.uv_Splat0).a == 0)
                o.Alpha = 1 - splat_control.r;
            else if(tex2D(_Splat1, IN.uv_Splat1).a == 0)
                o.Alpha = 1 - splat_control.g;
            else if(tex2D(_Splat2, IN.uv_Splat2).a == 0)
                o.Alpha = 1 - splat_control.b;
            else if(tex2D(_Splat3, IN.uv_Splat3).a == 0)
                o.Alpha = 1 - splat_control.a;
        }
        ENDCG
    }
 
    Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Lightmap-AddPass"
    Dependency "BaseMapShader" = "Diffuse"
 
    //Fallback to Diffuse
    Fallback "Diffuse"
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools