Skip to content

Instantly share code, notes, and snippets.

@sugi-cho
Last active August 29, 2015 14:06
Show Gist options
  • Save sugi-cho/985bdc86603c50438940 to your computer and use it in GitHub Desktop.
Save sugi-cho/985bdc86603c50438940 to your computer and use it in GitHub Desktop.
v2f shader with lighting in unity simple
Shader "Custom/FragmentLit" {
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
fixed4 _Color;
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR;
float4 vPos : TEXCOORD0;
float3 normal : TEXCOORD1;
};
v2f vert (appdata_full v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
o.vPos = v.vertex;
o.normal = v.normal;
return o;
}
half4 frag (v2f i) : COLOR
{
half3 lightColor = ShadeVertexLights(i.vPos, i.normal);
return half4(lightColor,1);
}
ENDCG
SubShader {
Tags {"LightMode" = "Vertex"}
Pass {
CGPROGRAM
#pragma glsl
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
}
}
Shader "Custom/VertexLit" {
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
fixed4 _Color;
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert (appdata_full v)
{
half3 lightColor = ShadeVertexLights(v.vertex, v.normal);
v.color.rgb = lightColor.rgb;
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
return o;
}
half4 frag (v2f i) : COLOR
{
return i.color;
}
ENDCG
SubShader {
Tags {"LightMode" = "Vertex"}
Pass {
CGPROGRAM
#pragma glsl
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
ENDCG
}
}
}
vertex shader内で、ライティングを計算する、ShadeVertexLights(v.vertex,v.normal)が、fragment shader内でも使えてうれしい。
ShadeVertexLights内でMatrixの計算とかしてるっぽいから、vert内で先に計算しておいたほうが、良いのかも。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment