Skip to content

Instantly share code, notes, and snippets.

@thelucre
Created March 17, 2017 02:13
Show Gist options
  • Save thelucre/b45efb856375b275228904bd71408501 to your computer and use it in GitHub Desktop.
Save thelucre/b45efb856375b275228904bd71408501 to your computer and use it in GitHub Desktop.
Color From Normals
Shader "Custom/ColorFromNormals" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// include file that contains UnityObjectToWorldNormal helper function
#include "UnityCG.cginc"
struct v2f {
// we'll output world space normal as one of regular ("texcoord") interpolators
half3 worldNormal : TEXCOORD0;
float4 pos : SV_POSITION;
};
// vertex shader: takes object space normal as input too
v2f vert (float4 vertex : POSITION, float3 normal : NORMAL)
{
v2f o;
o.pos = UnityObjectToClipPos(vertex);
// UnityCG.cginc file contains function to transform
// normal from object to world space, use that
o.worldNormal = UnityObjectToWorldNormal(normal);
return o;
}
fixed4 _Color;
fixed4 frag (v2f i) : SV_Target
{
fixed4 c = 0;
// normal is a 3D vector with xyz components; in -1..1
// range. To display it as color, bring the range into 0..1
// and put into red, green, blue components
c.rgb = _Color * i.worldNormal*0.5+0.5;
return c;
}
ENDCG
}
}
FallBack "Diffuse"
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorNormalsShaderScript : ShaderBaseScript {
void Update()
{
mat.SetColor("_Color", Color.white * new Vector4(
0.5f * Mathf.Sin(Time.timeSinceLevelLoad) + 0.5f,
0.5f * -Mathf.Sin(Time.timeSinceLevelLoad) + 0.5f,
0.5f * Mathf.Cos(Time.timeSinceLevelLoad) + 0.5f,
1)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment