15
2014
Unity indie: Soft Shadows (Directional Light)

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

—
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..

Related Posts
5 Comments + Add Comment
Leave a comment
Recent posts
- Favorites in PackageManager
- LudumDare59 : Signal
- Unity Editor: Tree Generator
- Leaf/Foliage Generator Tools (Runs in Browser)
- Testing Unity AI Beta
- Ways to Support UnityCoder Development
- Using UI Slider to Create 5-Star Rating Element
- Game Music Library For Unity (editor plugin)
- Fontastic : Easily Test Fonts in Unity Editor!
- GeoTiff Importer & Terrain Generator for Unity
- Create Baked DropShadow for UI images
- .JP2 Ortho Image Converter to PNG/JPG/TIFF
Recent Comments
- on Mesh Exploder (sources)
- on Sprite Sheet Flip Book Shader
- on Sprite Sheet Flip Book Shader
- on [Asset Store] PolygonCollider2D Optimizer
- on Trajectory Test Scene 2.0
- on Vector3 maths for dummies!
- on UnityHub 3.6.0: Remove Version Control & Cloud Dashboard columns
- on Using RenderDoc with Unity (graphics debugger)
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by












someone suggested this shader also,
http://armedunity.com/topic/8368-better-soft-shadows-wip/
*seems to be for deferred (which is pro only), but maybe those calculations can be used?
** this shader is same or similar to the ones in builtin shaders zip.
also interesting find, some random texture from unity:
uniform sampler2D unity_RandomRotation16;
modified cgincludes can be put on resources folder, must restart unity after that so that they take effect:
http://forum.unity3d.com/threads/builtin-unity-shaders-source.2085/#post-1702265
smoother autolight.cginc version available here:
http://forum.unity3d.com/threads/free-directional-light-soft-shadow-unity-indie.257432/#post-1708062
about softshadows:
http://codeflow.org/entries/2013/feb/15/soft-shadow-mapping/
[…] Soft Shadows (Directional Light) | Unity Coding […]