Home Unity

unlit with shadows?

polycounter lvl 17
Offline / Send Message
bugo polycounter lvl 17
Has anyone ever tried achieving an unlit shader that receives dynamic shadows in Unity? I heard you can change on #pragma, but all the attributes noted on unity docs don't work. Any ideas?

Replies

  • Brendan
    Options
    Offline / Send Message
    Brendan polycounter lvl 8
    Why not use a custom lighting ramp that is all white for faces up to 90 degrees away from the sun, and all gray for faces from 90 to 180 degrees?

    Find a lighting ramp shader online, and make the ramp all white with 1 pixel of black at the dark edge.

    Actually, the Unity Toon (Ramp) Shader (No Outline) would be perfect for this.
  • Farfarer
    Options
    Offline / Send Message
    This should do it;
    Shader "Unlit With Shadows" {
    	Properties {
    		_Color ("Main Color", Color) = (1,1,1,1)
    		_MainTex ("Base (RGB)", 2D) = "white" {}
    	}
    	SubShader {
    		Tags {"Queue" = "Geometry" "RenderType" = "Opaque"}
    
    		Pass {
    			Tags {"LightMode" = "ForwardBase"}
    			CGPROGRAM
    				#pragma vertex vert
    				#pragma fragment frag
    				#pragma multi_compile_fwdbase
    				#pragma fragmentoption ARB_fog_exp2
    				#pragma fragmentoption ARB_precision_hint_fastest
    				
    				#include "UnityCG.cginc"
    				#include "AutoLight.cginc"
    				
    				struct v2f
    				{
    					float4	pos			: SV_POSITION;
    					float2	uv			: TEXCOORD0;
    					LIGHTING_COORDS(1,2)
    				};
    
    				float4 _MainTex_ST;
    
    				v2f vert (appdata_tan v)
    				{
    					v2f o;
    					
    					o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    					o.uv = TRANSFORM_TEX (v.texcoord, _MainTex).xy;
    					TRANSFER_VERTEX_TO_FRAGMENT(o);
    					return o;
    				}
    
    				sampler2D _MainTex;
    
    				fixed4 frag(v2f i) : COLOR
    				{
    					fixed atten = LIGHT_ATTENUATION(i);	// Light attenuation + shadows.
    					//fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    					return tex2D(_MainTex, i.uv) * atten;
    				}
    			ENDCG
    		}
    
    		Pass {
    			Tags {"LightMode" = "ForwardAdd"}
    			Blend One One
    			CGPROGRAM
    				#pragma vertex vert
    				#pragma fragment frag
    				#pragma multi_compile_fwdadd_fullshadows
    				#pragma fragmentoption ARB_fog_exp2
    				#pragma fragmentoption ARB_precision_hint_fastest
    				
    				#include "UnityCG.cginc"
    				#include "AutoLight.cginc"
    				
    				struct v2f
    				{
    					float4	pos			: SV_POSITION;
    					float2	uv			: TEXCOORD0;
    					LIGHTING_COORDS(1,2)
    				};
    
    				float4 _MainTex_ST;
    
    				v2f vert (appdata_tan v)
    				{
    					v2f o;
    					
    					o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    					o.uv = TRANSFORM_TEX (v.texcoord, _MainTex).xy;
    					TRANSFER_VERTEX_TO_FRAGMENT(o);
    					return o;
    				}
    
    				sampler2D _MainTex;
    
    				fixed4 frag(v2f i) : COLOR
    				{
    					fixed atten = LIGHT_ATTENUATION(i);	// Light attenuation + shadows.
    					//fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    					return tex2D(_MainTex, i.uv) * atten;
    				}
    			ENDCG
    		}
    	}
    	FallBack "VertexLit"
    }
    
  • bugo
    Options
    Offline / Send Message
    bugo polycounter lvl 17
    Oh man, this is perfect! thank you so much!
  • IsraelAlves
    Options
    Offline / Send Message
    Hi!

    Is there any way to use lights intensity in this shader? When I tried to change this parameter, nothing happens ( Only when I put 0 intensity, shadows dissapear ).

    I want to change color intensity only ( white to black )... but without a 3D effect, only planar color. If you know how to add normal map too, I really appreciate it!

    thanks!
  • Lansiculus
    Options
    Offline / Send Message
    So, I tried to implement the shadow pass into my own shader, but its not working? It is still a bit messy, but this is my first shader. This is my code:
    Shader "Custom/NoobShader_04" {
    	Properties {
    		_Color ("Color", Color) = (0,0.55,0.83,1)
    		_Diffuse ("Diffuse Map", 2D) = "white" {}
    		_Displacement ("Displacement Map", 2D) = "white" {}
    		_Normal ("Normal Map", 2D) = "bump" {}
    		_Cube ("Cube Map", cube) = ""{}
    		_SpecColor ("Specular Color", Color) = (1,1,.1,1)
    		_Shininess ("Shininess", float) = 10
    		_Bumpiness ("Bumpiness", float) = 1
    		_CubeStrength ("Cube Map Strength", float) = 1
    		
    	
    		_Scale ("Wave Scale", float) = 0.7
    		_Speed ("Speed", float) = 0.5
    
    	}
    	SubShader {
    		Pass{
    		Tags { "LightMode" = "ForwardBase"}
    		CGPROGRAM
    		#pragma vertex vert
    		#pragma fragment frag
    		
    		float4 _Color;
    		sampler2D _Displacement;
    		sampler2D _Diffuse;
    		sampler2D _Normal;
    		samplerCUBE _Cube;
    		float4 _SpecColor;
    		float _Shininess;
    		float _Bumpiness;
    		float _CubeStrength;
    		
    		float _Scale;
    		float _Speed;
    		
    		float4 _LightColor0;
    			
    		struct VertexOutput
    		{
    			float4 pos : SV_POSITION;
    			float4 posWorld : TEXCOORD1;
    			float4 tex : TEXCOORD0;
    			float3 normalWorld : TEXCOORD2;
    			float3 tangentWorld : TEXCOORD3;
    			float3 binormalWorld : TEXCOORD4;
    		};
    		
    		struct VertexInput
    		{
    			float4 vertex : POSITION;
    			float3 normal : NORMAL;
    			float4 texcoord : TEXCOORD0;
    			float4 tangent : TANGENT;
    		};
    		
    		struct FragmentOutput
    		{
    			float4 color : COLOR;
    		};
    
    		VertexOutput vert (VertexInput i)
    		{
    			VertexOutput VOUT;
    			
    			float4 disp = tex2Dlod(_Displacement, float4(i.texcoord.x + (_Time.x * _Speed), i.texcoord.y + (_Time.x * _Speed),0.0,0.0));
    			float4 newPos = i.vertex;
    			newPos.y += _Scale * disp.y; 
    			
    			VOUT.posWorld = mul(_Object2World, newPos);
    			VOUT.pos = mul(UNITY_MATRIX_MVP,newPos);
    			VOUT.tex = i.texcoord;
    			
    			VOUT.normalWorld = normalize( mul(float4(i.normal,0.0),_World2Object).xyz);
    			VOUT.tangentWorld = normalize( mul(_Object2World,i.tangent).xyz);
    			VOUT.binormalWorld = normalize( cross(VOUT.normalWorld, VOUT.tangentWorld) * i.tangent.w);
    			
    			return VOUT;
    		}
    		
    		FragmentOutput frag(VertexOutput v) 
    		{
    			FragmentOutput FOUT;
    			
    			float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    			float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - v.posWorld.xyz);
    		
    			float4 texN = tex2D(_Normal,float4(v.tex.x + (_Time.x * _Speed),v.tex.y + (_Time.x * _Speed) ,0.0,0.0));
    			
    			float3 localCoords = float3(2.0 * texN.ag - float2(1.0,1.0), 0.0);
    			localCoords.z = _Bumpiness;
    			
    			float3x3 local2WorldTranspose = float3x3(
    				v.tangentWorld,
    				v.binormalWorld,
    				v.normalWorld
    			);
    			
    			float3 normalDirection = normalize(mul(localCoords,local2WorldTranspose));
    			
    			float3 reflectDir = reflect(viewDirection,v.normalWorld);
    			
    			float4 cubeRef = texCUBE(_Cube, reflectDir);
    			
    			
    			//
    			float3 diffuseReflection = (cubeRef * _CubeStrength) * _LightColor0 * saturate(dot(normalDirection,lightDirection));
    			float3 specularReflection = _SpecColor.xyz * pow(saturate(dot(reflect(-lightDirection, normalDirection), viewDirection)), _Shininess);
    			
    			float3 lightFinal = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseReflection + specularReflection;
    			
    			
    			//float4 tex = tex2D(_Diffuse,float4(v.tex.x * _Frequency + (_Time.x * _Speed), v.tex.y * _Frequency + (_Time.x * _Speed),0.0,0.0));
    			//FOUT.color = float4(cubeRef * _Color.xyz * lightFinal , 1.0);
    			FOUT.color = _Color * float4(lightFinal,0.0);
    			return FOUT;
    		}
    		ENDCG
    		}
    		
    		Pass {
    			Tags {"LightMode" = "ForwardAdd"}
    			Blend One One
    			CGPROGRAM
    				#pragma vertex vert
    				#pragma fragment frag
    				#pragma multi_compile_fwdadd_fullshadows
    				#pragma fragmentoption ARB_fog_exp2
    				#pragma fragmentoption ARB_precision_hint_fastest
    				
    				#include "UnityCG.cginc"
    				#include "AutoLight.cginc"
    				
    				sampler2D _Displacement;
    				float _Scale;
    				float _Speed;
    				
    				struct v2f
    				{
    					float4	pos			: SV_POSITION;
    					float2	uv			: TEXCOORD0;
    					LIGHTING_COORDS(1,2)
    				};
    
    				float4 _MainTex_ST;
    
    				v2f vert (appdata_tan v)
    				{
    					v2f o;
    					float4 disp = tex2Dlod(_Displacement, float4(v.texcoord.x + (_Time.x * _Speed), v.texcoord.y + (_Time.x * _Speed),0.0,0.0));
    					float4 newPos = v.vertex;
    					newPos.y += _Scale * disp.y; 
    					o.pos = mul( UNITY_MATRIX_MVP, newPos);
    					o.uv = TRANSFORM_TEX (v.texcoord, _MainTex).xy;
    					TRANSFER_VERTEX_TO_FRAGMENT(o);
    					return o;
    				}
    
    				sampler2D _MainTex;
    
    				fixed4 frag(v2f i) : COLOR
    				{
    					fixed atten = LIGHT_ATTENUATION(i);	// Light attenuation + shadows.
    					//fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    					return tex2D(_MainTex, i.uv) * atten;
    				}
    			ENDCG
    		}
    	} 
    	FallBack "Diffuse"
    }
    
  • Farfarer
    Options
    Offline / Send Message
    There's nothing in your forward base shader to do with shadows. By default, only the base pass in forward rendering gets shadows in Unity (i.e. your most powerful direct light).
Sign In or Register to comment.