Unity indie: Soft Shadows (Directional Light)


Using Unity Indie, modified directional hard light shadows.

Forum thread:


How to:
– Download builtin shader: http://unity3d.com/unity/download/archive
– copied UnityCG, UnityShaderVariables, HLSLSupport, AutoLight to assets folder
– renamed these as: UnityCG2, ​​UnityShaderVariables2, ​​HLSLSupport2​​
– created new shader file (that default diffuse)
– add this line to shader: #include “UnityCG2.cginc”
​​- modified it to use: #include “UnityShaderVariables2.cginc”​​
– modified that to use: #include “HLSLSupport2.cginc”
– then modified ​​AutoLight for the soft shadow​​ (basically changed all returns to 1, until founded which one it is, then added shadow “blur” there)
– *** Seems that you only need the AutoLight.cginc (in the same folder as the shader, others were not needed)

https://github.com/unitycoder/IndieSoftShadow **there are no comments or tutorial yet, check AutoLight.cginc for the modifications..

Webplayer demo:

v2:  Some progress on getting custom shadows, without any cginc modifications, but something is still broken..

Shader "Custom/IndieShadow1" {
        Properties {
            _Color ("Main Color", Color) = (1,1,1,0.5)
            _MainTex ("Texture", 2D) = "white" { }
        SubShader {
            Pass {

// Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct v2f members shadowCoord)
#pragma exclude_renderers d3d11 xbox360

        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"

        float4 _Color;
        sampler2D _MainTex;
        sampler2D _ShadowMapTexture;

        struct v2f {
            float4 pos : SV_POSITION;
            float2 uv : TEXCOORD0;
            float4 shadowCoord;

        float4 _MainTex_ST;

        v2f vert (appdata_base v)
            v2f o;
            o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
            o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
            // FIXME: problem could be here?
            o.shadowCoord =  mul( unity_World2Shadow[0], mul( _Object2World, v.vertex ) );
            return o;

        half4 frag (v2f i) : COLOR
            half4 texcol = tex2D (_MainTex, i.uv);
            // Hard Shadow
              fixed shadow = tex2D(_ShadowMapTexture, i.shadowCoord.xyz).r;
            shadow = _LightShadowData.r + shadow * (1-_LightShadowData.r);    
            return texcol * _Color * shadow;

        Fallback "VertexLit"



Image#2: closeup


Image#3: Inverted shadow (shadow is light), could be useful for some effects?


Image#4: Custom shader with inline unitySampleShadow(), without using autolight.inc..currently only works from certain angles..


4 Comments + Add Comment

Leave a comment


Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.