You copied the Doc URL to your clipboard.

The WorldSpaceNormalCreator shader code

The following is the code of the WorldSpaceNormalCreator shader.

Shader "Custom/WorldSpaceNormalCreator" {
	Properties {
	}
	SubShader {
		
		Cull off
		
		Pass
    	{ 
			CGPROGRAM
			#pragma target 3.0
			#pragma glsl		
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"
						 	         
			uniform sampler2D _BumpMapGlobal;

			struct vin
			{
				half4 tex : TEXCOORD0;
				half3 normal   : NORMAL;
                half4 tangent  : TANGENT;
			};

			struct vout
			{
			half4 pos : POSITION;
			half2 tc : TEXCOORD0;
			half3 normalInWorld : TEXCOORD1;
			half3 tangentWorld : TEXCOORD2;
			half3 bitangentWorld : TEXCOORD3;
			};
			
			vout vert (vin input )
			{
			vout output;
			output.pos = half4(input.tex.x*2.0 - 1.0,((1.0-input.tex.y)*2.0 - 1.0), 
					   0.0, 1.0);  
			output.tc  = input.tex;
			output.normalInWorld  = normalize(mul(half4(input.normal, 0.0), 
					   		  _World2Object).xyz);
			output.tangentWorld   = normalize(mul(_Object2World, 
								half4(input.tangent.xyz, 0.0)).xyz);
			output.bitangentWorld = normalize(cross(output.normalInWorld,
								output.tangentWorld) * input.tangent.w);
                
			return output;
			}
			
			float4 frag( vout input ) : COLOR
			{
			half3 normalInWorld = half3(0.0,0.0,0.0);
			half3 bumpNormal = UnpackNormal(tex2D(_BumpMapGlobal, input.tc));
			half3x3 local2WorldTranspose = half3x3(
                       input.tangentWorld,
                       input.bitangentWorld,
                       input.normalInWorld);
			normalInWorld = normalize(mul(bumpNormal, local2WorldTranspose));
			normalInWorld = normalInWorld*0.5 + 0.5;
				
			return  half4(normalInWorld,1.0);
			}
			ENDCG
		}
	} 
}
Was this page helpful? Yes No