Jul
15
2014

Unity indie: Soft Shadows (Directional Light)

fake_soft_shadows_unity_indie_1

Using Unity Indie, modified directional hard light shadows.

Forum thread:
http://forum.unity3d.com/threads/free-directional-light-soft-shadow-unity-indie.257432/

 

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)

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

Webplayer demo:
http://unitycoder.com/upload/demos/HardShadowSoftener/

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 {

        CGPROGRAM
// 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;
        }
        ENDCG

            }
        }
        Fallback "VertexLit"
    }

 

 

Image#2: closeup

fake_soft_shadows_unity_indie_2

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

inverted_shadow_cast_light_unity_3

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

custom_shadow_shader_unity1


5 Comments + Add Comment

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

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