Last active
February 18, 2021 15:19
-
-
Save jagt/b74aff76e31ff616a87e to your computer and use it in GitHub Desktop.
Sample Shaders http://en.wikibooks.org/wiki/Cg_Programming/Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.meta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook Additive" { | |
Properties { | |
_Color ("Main Color", Color) = (1.0, 0.0, 0.0, 1.0) | |
} | |
SubShader { | |
Tags { "Queue" = "Transparent" } | |
Pass { | |
Cull Off | |
ZWrite Off | |
Blend SrcAlpha One | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform float4 _Color; | |
float4 vert(float4 vertex : POSITION) : SV_POSITION | |
{ | |
return mul(UNITY_MATRIX_MVP, vertex); | |
} | |
float4 frag(void) : COLOR | |
{ | |
return _Color; | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook AddMult" { | |
Properties { | |
_Color ("Main color", Color) = (0.0, 0.0, 1.0, 1.0) | |
} | |
SubShader { | |
Tags { "Queue"="Transparent" } | |
Pass { | |
Cull Off | |
ZWrite Off | |
Blend Zero OneMinusSrcAlpha // attenuation by frag's alpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform float4 _Color; | |
float4 vert(float4 vertex : POSITION) : SV_POSITION | |
{ | |
return mul(UNITY_MATRIX_MVP, vertex); | |
} | |
float4 frag(void) : COLOR | |
{ | |
return _Color; | |
} | |
ENDCG | |
} | |
Pass { | |
Cull Off | |
ZWrite Off | |
Blend SrcAlpha One | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform float4 _Color; | |
float4 vert(float4 vertex : POSITION) : SV_POSITION | |
{ | |
return mul(UNITY_MATRIX_MVP, vertex); | |
//return mul(vertex, UNITY_MATRIX_MVP); | |
} | |
float4 frag(void) : COLOR | |
{ | |
return _Color; | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Cg shader using discard" { | |
SubShader { | |
Pass { | |
Cull Off // turn off triangle culling, alternatives are: | |
// Cull Back (or nothing): cull only back faces | |
// Cull Front : cull only front faces | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform float4x4 _world2Local; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 posInObjectCoords : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
output.posInObjectCoords = mul(_world2Local, input.vertex); | |
//output.posInObjectCoords = input.vertex; | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
if (length(input.posInObjectCoords) < 1.2) | |
{ | |
//return float4(0.0, 0.0, 0.0, 1.0); // green | |
discard; // drop the fragment if y coordinate > 0 | |
} | |
return float4(0.0, 1.0, 0.0, 1.0); // green | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook Diffuse" { | |
Properties { | |
_Color ("Diffuse Material Color", Color) = (1,1,1,1) | |
} | |
SubShader { | |
Pass { | |
Tags { "LightMode"="ForwardBase" } | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
uniform float4 _Color; | |
struct VertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct VertexOutput { | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
VertexOutput vert(VertexInput input) | |
{ | |
VertexOutput output; | |
float3 normalDir = normalize(float3(mul(float4(input.normal, 0.0), _World2Object).xyz)); | |
float3 lightDir; | |
float attenuation; | |
if (_WorldSpaceLightPos0.w == 0) // directional light | |
{ | |
attenuation = 1.0; | |
lightDir = normalize(float3(_WorldSpaceLightPos0.xyz)); | |
} | |
else | |
{ | |
float3 vertex2LightSrc = float3(_WorldSpaceLightPos0 - mul(_Object2World, input.vertex)); | |
float distance = length(vertex2LightSrc); | |
attenuation = 1.0 / distance; | |
lightDir = normalize(vertex2LightSrc); | |
} | |
float3 diffuseReflection = attenuation * float(_Color.rgb) * float3(_LightColor0.rgb) * max(0.0, dot(normalDir, lightDir)); | |
output.col = float4(diffuseReflection.xyz, 1.0); | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(VertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
Pass { | |
Tags { "LightMode"="ForwardAdd" } | |
Blend One One | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
uniform float4 _Color; | |
struct VertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct VertexOutput { | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
VertexOutput vert(VertexInput input) | |
{ | |
VertexOutput output; | |
float3 normalDir = normalize(float3(mul(float4(input.normal, 0.0), _World2Object).xyz)); | |
float3 lightDir; | |
float attenuation; | |
if (_WorldSpaceLightPos0.w == 0) // directional light | |
{ | |
attenuation = 1.0; | |
lightDir = normalize(float3(_WorldSpaceLightPos0.xyz)); | |
} | |
else | |
{ | |
float3 vertex2LightSrc = float3(_WorldSpaceLightPos0 - mul(_Object2World, input.vertex)); | |
float distance = length(vertex2LightSrc); | |
attenuation = 1.0 / distance; | |
lightDir = normalize(vertex2LightSrc); | |
} | |
float3 diffuseReflection = attenuation * float(_Color.rgb) * float3(_LightColor0.rgb) * max(0.0, dot(normalDir, lightDir)); | |
output.col = float4(diffuseReflection.xyz, 1.0); | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(VertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Cg shader with all built-in vertex input parameters" { | |
SubShader { | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float4 tangent : TANGENT; | |
float3 normal : NORMAL; | |
float4 texcoord : TEXCOORD0; | |
float4 texcoord1 : TEXCOORD1; | |
fixed4 color : COLOR; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 col : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
output.col = radians(input.texcoord * 90); | |
// other possibilities to play with: | |
// output.col = input.vertex; | |
// output.col = input.tangent; | |
// output.col = float4(input.normal, 1.0); | |
// output.col = input.texcoord; | |
// output.col = input.texcoord1; | |
// output.col = input.color; | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook MultipleLights" { | |
Properties { | |
_Color ("Diffuse Material Color", Color) = (1,1,1,1) | |
_SpecColor ("Specular Material Color", Color) = (1,1,1,1) | |
_Shininess ("Shininess", Float) = 10 | |
} | |
SubShader { | |
Pass { | |
Tags { "LightMode" = "ForwardBase" } // pass for | |
// 4 vertex lights, ambient light & first pixel light | |
CGPROGRAM | |
#pragma multi_compile_fwdbase | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 posWorld : TEXCOORD0; | |
float3 normalDir : TEXCOORD1; | |
float3 vertexLighting : TEXCOORD2; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// unity_Scale.w is unnecessary here | |
output.posWorld = mul(modelMatrix, input.vertex); | |
output.normalDir = normalize( | |
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
// Diffuse reflection by four "vertex lights" | |
output.vertexLighting = float3(0.0, 0.0, 0.0); | |
#ifdef VERTEXLIGHT_ON | |
for (int index = 0; index < 4; index++) | |
{ | |
float4 lightPosition = float4(unity_4LightPosX0[index], | |
unity_4LightPosY0[index], | |
unity_4LightPosZ0[index], 1.0); | |
float3 vertexToLightSource = | |
lightPosition.xyz - output.posWorld.xyz; | |
float3 lightDirection = normalize(vertexToLightSource); | |
float squaredDistance = | |
dot(vertexToLightSource, vertexToLightSource); | |
float attenuation = 1.0 / (1.0 + | |
unity_4LightAtten0[index] * squaredDistance); | |
float3 diffuseReflection = attenuation | |
* unity_LightColor[index].rgb * _Color.rgb | |
* max(0.0, dot(output.normalDir, lightDirection)); | |
output.vertexLighting = | |
output.vertexLighting + diffuseReflection; | |
} | |
#endif | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float3 normalDirection = normalize(input.normalDir); | |
float3 viewDirection = normalize( | |
_WorldSpaceCameraPos - input.posWorld.xyz); | |
float3 lightDirection; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) // directional light? | |
{ | |
attenuation = 1.0; // no attenuation | |
lightDirection = | |
normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else // point or spot light | |
{ | |
float3 vertexToLightSource = | |
_WorldSpaceLightPos0.xyz - input.posWorld.xyz; | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; // linear attenuation | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 ambientLighting = | |
UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb; | |
float3 diffuseReflection = | |
attenuation * _LightColor0.rgb * _Color.rgb | |
* max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation * _LightColor0.rgb | |
* _SpecColor.rgb * pow(max(0.0, dot( | |
reflect(-lightDirection, normalDirection), | |
viewDirection)), _Shininess); | |
} | |
return float4(input.vertexLighting + ambientLighting | |
+ diffuseReflection + specularReflection, 1.0); | |
} | |
ENDCG | |
} | |
} | |
// The definition of a fallback shader should be commented out | |
// during development: | |
// Fallback "Specular" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook Multiplicative" { | |
SubShader { | |
Tags { "Queue"="Transparent" } | |
Pass | |
{ | |
Cull Off // draw front and back faces | |
ZWrite Off // don't write to depth buffer | |
// in order not to occlude other objects | |
Blend Zero SrcColor // multiplicative blending | |
// for attenuation by the fragment's alpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
float4 vert(float4 vertexPos : POSITION) : SV_POSITION | |
{ | |
return mul(UNITY_MATRIX_MVP, vertexPos); | |
} | |
float4 frag(void) : COLOR | |
{ | |
return float4(1.0, 0.0, 0.0, 0.3); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook Phong" { | |
Properties { | |
_Color ("Diffuse Material Color", Color) = (1,1,1,1) | |
_SpecColor ("Specular Material Color", Color) = (1,1,1,1) | |
_Shininess ("Shininess", Float) = 10 | |
} | |
SubShader { | |
Pass { | |
Tags { "LightMode" = "ForwardBase" } | |
// pass for ambient light and first light source | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 posWorld : TEXCOORD0; | |
float3 normalDir : TEXCOORD1; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
output.posWorld = mul(modelMatrix, input.vertex); | |
output.normalDir = normalize(mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float3 normalDir = normalize(input.normalDir); | |
float3 viewDir = normalize(_WorldSpaceCameraPos - input.posWorld.xyz); | |
float3 lightDir; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) | |
{ | |
attenuation = 1.0; | |
lightDir = normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else | |
{ | |
float3 vertex2LightSrc = _WorldSpaceLightPos0.xyz - input.posWorld.xyz; | |
float distance = length(vertex2LightSrc); | |
attenuation = 1.0 / distance; | |
lightDir = normalize(vertex2LightSrc); | |
} | |
float3 ambientLit = float3(UNITY_LIGHTMODEL_AMBIENT) * _Color.rgb; | |
float3 diffuseReflection = attenuation * float3(_LightColor0) * float3(_Color) * max(0.0, dot(normalDir, lightDir)); | |
float3 specularReflection; | |
if (dot(normalDir, lightDir) < 0.0) | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
} | |
else | |
{ | |
specularReflection = attenuation * float3(_LightColor0) * float3(_SpecColor) * pow(max(0.0, dot(reflect(-lightDir, normalDir), viewDir)), _Shininess); | |
} | |
return float4(ambientLit + diffuseReflection + specularReflection, 1.0); | |
} | |
ENDCG | |
} | |
Pass { | |
Tags { "LightMode" = "ForwardAdd" } | |
// pass for ambient light and first light source | |
Blend One One | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 posWorld : TEXCOORD0; | |
float3 normalDir : TEXCOORD1; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
output.posWorld = mul(modelMatrix, input.vertex); | |
output.normalDir = normalize(mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float3 normalDir = normalize(input.normalDir); | |
float3 viewDir = normalize(_WorldSpaceCameraPos - input.posWorld.xyz); | |
float3 lightDir; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) | |
{ | |
attenuation = 1.0; | |
lightDir = normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else | |
{ | |
float3 vertex2LightSrc = _WorldSpaceLightPos0.xyz - input.posWorld.xyz; | |
float distance = length(vertex2LightSrc); | |
attenuation = 1.0 / distance; | |
lightDir = normalize(vertex2LightSrc); | |
} | |
float3 diffuseReflection = attenuation * float3(_LightColor0) * float3(_Color) * max(0.0, dot(normalDir, lightDir)); | |
float3 specularReflection; | |
if (dot(normalDir, lightDir) < 0.0) | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
} | |
else | |
{ | |
specularReflection = attenuation * float3(_LightColor0) * float3(_SpecColor) * pow(max(0.0, dot(reflect(-lightDir, normalDir), viewDir)), _Shininess); | |
} | |
return float4(diffuseReflection + specularReflection, 1.0); | |
} | |
ENDCG | |
} | |
// The definition of a fallback shader should be commented out | |
// during development: | |
// Fallback "Specular" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Cg shading in world space" { | |
Properties { | |
_Point ("a point in world space", Vector) = (0., 0., 0., 1.0) | |
_DistanceNear ("threshold distance", Float) = 5.0 | |
_ColorNear ("color near to point", Color) = (0.0, 1.0, 0.0, 1.0) | |
_ColorFar ("color far from point", Color) = (0.3, 0.3, 0.3, 1.0) | |
} | |
SubShader { | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
// defines _Object2World and _World2Object | |
// uniforms corresponding to properties | |
uniform float4 _Point; | |
uniform float _DistanceNear; | |
uniform float4 _ColorNear; | |
uniform float4 _ColorFar; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 position_in_world_space : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
output.position_in_world_space = | |
mul(_Object2World, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float dist = abs(distance(input.position_in_world_space, | |
_Point)); | |
// computes the distance between the fragment position | |
// and the position _Point. | |
if (dist < _DistanceNear) | |
{ | |
return _ColorNear; | |
} | |
else | |
{ | |
return _ColorFar; | |
} | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Cg Shader for RGB Cube" { | |
SubShader { | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
void vert(float4 vertexPos : POSITION, out float4 pos : SV_POSITION, out float4 col : TEXCOORD0) | |
{ | |
pos = mul(UNITY_MATRIX_MVP, float4(1.0, 1.0, 1.0, 1.0) * vertexPos); | |
col = vertexPos + float4(0.5, 0.5, 0.5, 0.0); | |
} | |
float4 frag(float4 pos : SV_POSITION, float4 col : TEXCOORD0) : COLOR | |
{ | |
return col; | |
} | |
ENDCG | |
} | |
} | |
FallBack "Diffuse" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook RGB" { | |
Properties { | |
_Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0) | |
_Offset ("Offset", Float) = 0.1 | |
} | |
SubShader { | |
Pass { | |
Cull Off | |
Blend One One | |
CGPROGRAM | |
// red channel | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _Color; | |
uniform float _Offset; | |
struct vertexOutput | |
{ | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
vertexOutput vert(float4 vertex : POSITION) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, vertex) + float4(-_Offset, 0, 0, 0); | |
output.col = float4(_Color.r, 0.0, 0.0, 1.0); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
Pass { | |
Cull Off | |
Blend One One | |
CGPROGRAM | |
// green channel | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _Color; | |
struct vertexOutput | |
{ | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
vertexOutput vert(float4 vertex : POSITION) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, vertex); | |
output.col = float4(0.0, _Color.g, 0.0, 1.0); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
Pass { | |
Cull Off | |
Blend One One | |
CGPROGRAM | |
// blue channel | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _Color; | |
uniform float _Offset; | |
struct vertexOutput | |
{ | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
vertexOutput vert(float4 vertex : POSITION) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, vertex) + float4(_Offset, 0.0, 0.0, 0.0); | |
output.col = float4(0.0, 0.0, _Color.b, 1.0); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook ScreenPos" { | |
SubShader { | |
Tags { "RenderType"="Opaque" } | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct v2f | |
{ | |
float4 pos : SV_POSITION; | |
float4 screenPos : TEXCOORD0; | |
}; | |
v2f vert(float4 vertex : POSITION) | |
{ | |
v2f output; | |
output.pos = mul(UNITY_MATRIX_MVP, vertex); | |
//output.screenPos = ComputeScreenPos(output.pos); | |
output.screenPos = output.pos; | |
return output; | |
} | |
float4 frag(v2f input) : COLOR | |
{ | |
// ComputeScreenPos results ranges in [0, 1], origin is at left bottom corner | |
// z and w is always 1 | |
//return float4(input.screenPos.xy, 0.0, 1.0); | |
return (float4(normalize(input.screenPos).xy, 0.0, 1.0) + float4(1.0, 1.0, 0, 1.0)) / 2; | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook Silhouette" { | |
Properties { | |
_Color ("Color", Color) = (1, 1, 1, 0.5) | |
_Enhance ("Enhance Silhouette", Float) = 1.0 | |
} | |
SubShader { | |
Tags { "Queue"="Transparent" } | |
Pass { | |
ZWrite Off // don't occlude other objects | |
Blend SrcAlpha OneMinusSrcAlpha // standard alpha blending | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _Color; // define shader property for shaders | |
uniform float _Enhance; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float3 normal : TEXCOORD; | |
float3 viewDir : TEXCOORD1; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
output.normal = normalize( | |
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
output.viewDir = normalize(_WorldSpaceCameraPos | |
- mul(modelMatrix, input.vertex).xyz); | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float3 normalDirection = normalize(input.normal); | |
float3 viewDirection = normalize(input.viewDir); | |
float newOpacity = min(1.0, _Color.a | |
/ abs(pow(dot(viewDirection, normalDirection), _Enhance))); | |
return float4(_Color.rgb, newOpacity); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook Specular" { | |
Properties { | |
_Color ("Diffuse Material Color", Color) = (1,1,1,1) | |
_SpecColor ("Specular Material Color", Color) = (1,1,1,1) | |
_Shininess ("Shininess", Float) = 10 | |
} | |
SubShader { | |
Pass { | |
Tags { "LightMode" = "ForwardBase" } | |
// pass for ambient light and first light source | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
float3 normalDirection = normalize( | |
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
float3 viewDirection = normalize(_WorldSpaceCameraPos | |
- mul(modelMatrix, input.vertex).xyz); | |
float3 lightDirection; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) // directional light? | |
{ | |
attenuation = 1.0; // no attenuation | |
lightDirection = normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else // point or spot light | |
{ | |
float3 vertexToLightSource = _WorldSpaceLightPos0.xyz | |
- mul(modelMatrix, input.vertex).xyz; | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; // linear attenuation | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 ambientLighting = | |
UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb; | |
float3 diffuseReflection = | |
attenuation * _LightColor0.rgb * _Color.rgb | |
* max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation * _LightColor0.rgb | |
* _SpecColor.rgb * pow(max(0.0, dot( | |
reflect(-lightDirection, normalDirection), | |
viewDirection)), _Shininess); | |
} | |
output.col = float4(ambientLighting + diffuseReflection | |
+ specularReflection, 1.0); | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
Pass { | |
Tags { "LightMode" = "ForwardAdd" } | |
// pass for additional light sources | |
Blend One One // additive blending | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
float3 normalDirection = normalize( | |
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
float3 viewDirection = normalize(_WorldSpaceCameraPos | |
- mul(modelMatrix, input.vertex).xyz); | |
float3 lightDirection; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) // directional light? | |
{ | |
attenuation = 1.0; // no attenuation | |
lightDirection = normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else // point or spot light | |
{ | |
float3 vertexToLightSource = _WorldSpaceLightPos0.xyz | |
- mul(modelMatrix, input.vertex).xyz; | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; // linear attenuation | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 diffuseReflection = | |
attenuation * _LightColor0.rgb * _Color.rgb | |
* max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation * _LightColor0.rgb | |
* _SpecColor.rgb * pow(max(0.0, dot( | |
reflect(-lightDirection, normalDirection), | |
viewDirection)), _Shininess); | |
} | |
output.col = float4(diffuseReflection | |
+ specularReflection, 1.0); | |
// no ambient contribution in this pass | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
} | |
// The definition of a fallback shader should be commented out | |
// during development: | |
// Fallback "Specular" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/CG per-vertext lighting with texture (glossing)" { | |
Properties { | |
_MainTex ("Base (RGB)", 2D) = "white" {} | |
_Color ("Overall Diffuse Color", Color) = (1,1,1,1) | |
_SpecColor ("Specular Color", Color) = (1,1,1,1) | |
_Shininess ("Shininess", Float) = 10 | |
} | |
SubShader { | |
Pass { | |
Tags { "LightMode" = "ForwardBase" } | |
// pass for ambient light and first light source | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform sampler2D _MainTex; | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 posWorld : TEXCOORD0; | |
float3 normalDir : TEXCOORD1; | |
float4 tex : TEXCOORD2; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
output.posWorld = mul(modelMatrix, input.vertex); | |
output.normalDir = normalize( | |
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
output.tex = input.texcoord; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float3 normalDirection = normalize(input.normalDir); | |
float3 viewDirection = normalize( | |
_WorldSpaceCameraPos - input.posWorld.xyz); | |
float3 lightDirection; | |
float attenuation; | |
float4 textureColor = tex2D(_MainTex, input.tex.xy); | |
if (0.0 == _WorldSpaceLightPos0.w) // directional light? | |
{ | |
attenuation = 1.0; // no attenuation | |
lightDirection = | |
normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else // point or spot light | |
{ | |
float3 vertexToLightSource = | |
_WorldSpaceLightPos0.xyz - input.posWorld.xyz; | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; // linear attenuation | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 ambientLighting = textureColor.rgb | |
* UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb; | |
float3 diffuseReflection = textureColor.rgb | |
* attenuation * _LightColor0.rgb * _Color.rgb | |
* max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation * _LightColor0.rgb | |
* _SpecColor.rgb * (1.0 - textureColor.a) | |
// for usual gloss maps: "... * textureColor.a" | |
* pow(max(0.0, dot( | |
reflect(-lightDirection, normalDirection), | |
viewDirection)), _Shininess); | |
} | |
return float4(ambientLighting + diffuseReflection | |
+ specularReflection, 1.0); | |
} | |
ENDCG | |
} | |
Pass { | |
Tags { "LightMode" = "ForwardAdd" } | |
// pass for additional light sources | |
Blend One One // additive blending | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform sampler2D _MainTex; | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 posWorld : TEXCOORD0; | |
float3 normalDir : TEXCOORD1; | |
float4 tex : TEXCOORD2; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
output.posWorld = mul(modelMatrix, input.vertex); | |
output.normalDir = normalize( | |
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
output.tex = input.texcoord; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float3 normalDirection = normalize(input.normalDir); | |
float3 viewDirection = normalize( | |
_WorldSpaceCameraPos - input.posWorld.xyz); | |
float3 lightDirection; | |
float attenuation; | |
float4 textureColor = tex2D(_MainTex, input.tex.xy); | |
if (0.0 == _WorldSpaceLightPos0.w) // directional light? | |
{ | |
attenuation = 1.0; // no attenuation | |
lightDirection = | |
normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else // point or spot light | |
{ | |
float3 vertexToLightSource = | |
_WorldSpaceLightPos0.xyz - input.posWorld.xyz; | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; // linear attenuation | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 diffuseReflection = textureColor.rgb | |
* attenuation * _LightColor0.rgb * _Color.rgb | |
* max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation * _LightColor0.rgb | |
* _SpecColor.rgb * (1.0 - textureColor.a) | |
// for usual gloss maps: "... * textureColor.a" | |
* pow(max(0.0, dot( | |
reflect(-lightDirection, normalDirection), | |
viewDirection)), _Shininess); | |
} | |
return float4(diffuseReflection | |
+ specularReflection, 1.0); | |
// no ambient lighting in this pass | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/CG per-vertext lighting with texture" { | |
Properties { | |
_MainTex ("Base (RGB)", 2D) = "white" {} | |
_Color ("Overall Diffuse Color", Color) = (1,1,1,1) | |
_SpecColor ("Specular Color", Color) = (1,1,1,1) | |
_Shininess ("Shininess", Float) = 10 | |
} | |
SubShader { | |
Pass { | |
Tags { "LightMode"="ForwardBase" } | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; // in Lighting.cginc | |
uniform sampler2D _MainTex; | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 tex : TEXCOORD0; | |
float3 diffuseColor : TEXCOORD1; | |
float3 specularColor : TEXCOORD2; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float3 normalDirection = normalize(mul(float4(input.normal, 0.0), _World2Object).xyz); | |
float3 viewDirection = normalize(float3( float4(_WorldSpaceCameraPos, 1.0) - mul(_Object2World, input.vertex) )); | |
float3 lightDirection; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) | |
{ | |
// directional light | |
attenuation = 1.0; | |
lightDirection = normalize(float3(_WorldSpaceLightPos0)); | |
} | |
else | |
{ | |
// point light | |
float3 vertexToLightSource = float3(_WorldSpaceLightPos0 - mul(_Object2World, input.vertex)); | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 ambientLighting = float3(UNITY_LIGHTMODEL_AMBIENT) * float3(_Color); | |
float3 diffuseReflection = attenuation * float3(_LightColor0) * float3(_Color) * max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
} | |
else | |
{ | |
specularReflection = attenuation * float3(_LightColor0) * float3(_SpecColor) * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)), _Shininess); | |
} | |
output.diffuseColor = ambientLighting + diffuseReflection; | |
output.specularColor = specularReflection; | |
output.tex = input.texcoord; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return float4(input.specularColor + input.diffuseColor * tex2D(_MainTex, float2(input.tex)), 1.0); | |
} | |
ENDCG | |
} | |
Pass { // pasted pass from wiki | |
Tags { "LightMode" = "ForwardAdd" } | |
// pass for additional light sources | |
Blend One One // additive blending | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform sampler2D _MainTex; | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 tex : TEXCOORD0; | |
float3 diffuseColor : TEXCOORD1; | |
float3 specularColor : TEXCOORD2; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
float3 normalDirection = normalize( | |
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
float3 viewDirection = normalize(_WorldSpaceCameraPos | |
- mul(modelMatrix, input.vertex).xyz); | |
float3 lightDirection; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) // directional light? | |
{ | |
attenuation = 1.0; // no attenuation | |
lightDirection = normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else // point or spot light | |
{ | |
float3 vertexToLightSource = _WorldSpaceLightPos0.xyz | |
- mul(modelMatrix, input.vertex).xyz; | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; // linear attenuation | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 diffuseReflection = | |
attenuation * _LightColor0.rgb * _Color.rgb | |
* max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation * _LightColor0.rgb | |
* _SpecColor.rgb * pow(max(0.0, dot( | |
reflect(-lightDirection, normalDirection), | |
viewDirection)), _Shininess); | |
} | |
output.diffuseColor = diffuseReflection; // no ambient | |
output.specularColor = specularReflection; | |
output.tex = input.texcoord; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return float4(input.specularColor + | |
input.diffuseColor * tex2D(_MainTex, input.tex.xy), | |
1.0); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Cg semi transparent on alpha" { | |
Properties { | |
_MainTex ("RGBA Texture Image", 2D) = "white" {} | |
} | |
SubShader { | |
Tags {"Queue" = "Transparent"} | |
Pass { | |
Cull Front | |
ZWrite Off | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform sampler2D _MainTex; | |
uniform float _Cutoff; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 tex : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.tex = input.texcoord; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float4 color = tex2D(_MainTex, float2(input.tex)); | |
if (color.a > 0.5) | |
{ | |
color = float4(0.0, 0.0, 0.2, 1.0); | |
} | |
else | |
{ | |
color = float4(0.0, 0.0, 1.0, 0.3); | |
} | |
return color; | |
} | |
ENDCG | |
} | |
Pass { | |
Cull Back | |
ZWrite Off | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform sampler2D _MainTex; | |
uniform float _Cutoff; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 tex : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.tex = input.texcoord; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float4 color = tex2D(_MainTex, float2(input.tex)); | |
if (color.a > 0.5) | |
{ | |
color = float4(0.0, 1.0, 0.0, 1.0); | |
} | |
else | |
{ | |
color = float4(0.0, 0.0, 1.0, 0.3); | |
} | |
return color; | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Cg texturing with alpha discard" { | |
Properties { | |
_MainTex ("RGBA Texture Image", 2D) = "white" {} | |
_Cutoff ("Alpha Cutoff", Float) = 0.5 | |
} | |
SubShader { | |
Pass { | |
Cull Off // front is transparent, don't cull | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform sampler2D _MainTex; | |
uniform float _Cutoff; | |
uniform float4x4 _world2Local; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 tex : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
output.tex = input.texcoord; | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float4 textureColor = tex2D(_MainTex, float2(input.tex)); | |
if (textureColor.a < _Cutoff) | |
{ | |
return float4(0.0, 0.0, 0.0, 0.0); // this with the correct Blend will have basically the same effects as discard (maybe more efficient?) | |
//discard; | |
} | |
else | |
{ | |
return textureColor; | |
} | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Cg texturing with alpha test" { | |
Properties { | |
_MainTex ("RGBA Texture Image", 2D) = "white" {} | |
_Cutoff ("Alpha Cutoff", Float) = 0.5 | |
} | |
SubShader { | |
Pass { | |
Cull Off // front is transparent, don't cull | |
AlphaTest Greater [_Cutoff] // fragment passes if alpha greater than _Cutoff | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform sampler2D _MainTex; | |
uniform float _Cutoff; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 tex : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.tex = input.texcoord; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return tex2D(_MainTex, float2(input.tex)); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook Texturing" { | |
Properties { | |
_MainTex ("Texture Image", 2D) = "white" {} | |
} | |
SubShader { | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform sampler2D _MainTex; | |
uniform float4 _MainTex_ST; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 tex : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.tex = input.texcoord; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return tex2D(_MainTex, float2(_MainTex_ST.xy * input.tex.xy + _MainTex_ST.zw)); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook Transparent" { | |
SubShader { | |
Tags { "Queue"="Transparent" } | |
Pass { | |
ZWrite On | |
Cull Off | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
struct vertexInput | |
{ | |
float4 vertex : POSITION; | |
}; | |
struct vertexOutput | |
{ | |
float4 pos : SV_POSITION; | |
float4 posDup : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
output.posDup = output.pos; | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return normalize(input.posDup) + float4(0.0, 1.0, 0.0, 0.3); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook TwoFace" { | |
Properties { | |
_Color ("Front Material Diffuse Color", Color) = (1,1,1,1) | |
_SpecColor ("Front Material Specular Color", Color) = (1,1,1,1) | |
_Shininess ("Front Material Shininess", Float) = 10 | |
_BackColor ("Back Material Diffuse Color", Color) = (1,1,1,1) | |
_BackSpecColor ("Back Material Specular Color", Color) | |
= (1,1,1,1) | |
_BackShininess ("Back Material Shininess", Float) = 10 | |
} | |
SubShader { | |
Pass { | |
Tags { "LightMode" = "ForwardBase" } | |
// pass for ambient light and first light source | |
Cull Back // render only front faces | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
uniform float4 _BackColor; | |
uniform float4 _BackSpecColor; | |
uniform float _BackShininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
float3 normalDirection = normalize( | |
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
float3 viewDirection = normalize(_WorldSpaceCameraPos | |
- mul(modelMatrix, input.vertex).xyz); | |
float3 lightDirection; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) // directional light? | |
{ | |
attenuation = 1.0; // no attenuation | |
lightDirection = normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else // point or spot light | |
{ | |
float3 vertexToLightSource = _WorldSpaceLightPos0.xyz | |
- mul(modelMatrix, input.vertex).xyz; | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; // linear attenuation | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 ambientLighting = | |
UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb; | |
float3 diffuseReflection = | |
attenuation * _LightColor0.rgb * _Color.rgb | |
* max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation * _LightColor0.rgb | |
* _SpecColor.rgb * pow(max(0.0, dot( | |
reflect(-lightDirection, normalDirection), | |
viewDirection)), _Shininess); | |
} | |
output.col = float4(ambientLighting + diffuseReflection | |
+ specularReflection, 1.0); | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
Pass { | |
Tags { "LightMode" = "ForwardAdd" } | |
// pass for additional light sources | |
Blend One One // additive blending | |
Cull Back // render only front faces | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
uniform float4 _BackColor; | |
uniform float4 _BackSpecColor; | |
uniform float _BackShininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
float3 normalDirection = normalize( | |
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
float3 viewDirection = normalize(_WorldSpaceCameraPos | |
- mul(modelMatrix, input.vertex).xyz); | |
float3 lightDirection; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) // directional light? | |
{ | |
attenuation = 1.0; // no attenuation | |
lightDirection = normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else // point or spot light | |
{ | |
float3 vertexToLightSource = _WorldSpaceLightPos0.xyz | |
- mul(modelMatrix, input.vertex).xyz; | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; // linear attenuation | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 diffuseReflection = | |
attenuation * _LightColor0.rgb * _Color.rgb | |
* max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation * _LightColor0.rgb | |
* _SpecColor.rgb * pow(max(0.0, dot( | |
reflect(-lightDirection, normalDirection), | |
viewDirection)), _Shininess); | |
} | |
output.col = float4(diffuseReflection | |
+ specularReflection, 1.0); | |
// no ambient contribution in this pass | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
Pass { | |
Tags { "LightMode" = "ForwardBase" } | |
// pass for ambient light and first light source | |
Cull Front// render only back faces | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
uniform float4 _BackColor; | |
uniform float4 _BackSpecColor; | |
uniform float _BackShininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
float3 normalDirection = normalize( | |
mul(float4(-input.normal, 0.0), modelMatrixInverse).xyz); | |
float3 viewDirection = normalize(_WorldSpaceCameraPos | |
- mul(modelMatrix, input.vertex).xyz); | |
float3 lightDirection; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) // directional light? | |
{ | |
attenuation = 1.0; // no attenuation | |
lightDirection = normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else // point or spot light | |
{ | |
float3 vertexToLightSource = _WorldSpaceLightPos0.xyz | |
- mul(modelMatrix, input.vertex).xyz; | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; // linear attenuation | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 ambientLighting = | |
UNITY_LIGHTMODEL_AMBIENT.rgb * _BackColor.rgb; | |
float3 diffuseReflection = | |
attenuation * _LightColor0.rgb * _BackColor.rgb | |
* max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation * _LightColor0.rgb | |
* _BackSpecColor.rgb * pow(max(0.0, dot( | |
reflect(-lightDirection, normalDirection), | |
viewDirection)), _BackShininess); | |
} | |
output.col = float4(ambientLighting + diffuseReflection | |
+ specularReflection, 1.0); | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
Pass { | |
Tags { "LightMode" = "ForwardAdd" } | |
// pass for additional light sources | |
Blend One One // additive blending | |
Cull Front // render only back faces | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
uniform float4 _LightColor0; | |
// color of light source (from "Lighting.cginc") | |
// User-specified properties | |
uniform float4 _Color; | |
uniform float4 _SpecColor; | |
uniform float _Shininess; | |
uniform float4 _BackColor; | |
uniform float4 _BackSpecColor; | |
uniform float _BackShininess; | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
float4x4 modelMatrix = _Object2World; | |
float4x4 modelMatrixInverse = _World2Object; | |
// multiplication with unity_Scale.w is unnecessary | |
// because we normalize transformed vectors | |
float3 normalDirection = normalize( | |
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz); | |
float3 viewDirection = normalize(_WorldSpaceCameraPos | |
- mul(modelMatrix, input.vertex).xyz); | |
float3 lightDirection; | |
float attenuation; | |
if (0.0 == _WorldSpaceLightPos0.w) // directional light? | |
{ | |
attenuation = 1.0; // no attenuation | |
lightDirection = normalize(_WorldSpaceLightPos0.xyz); | |
} | |
else // point or spot light | |
{ | |
float3 vertexToLightSource = _WorldSpaceLightPos0.xyz | |
- mul(modelMatrix, input.vertex).xyz; | |
float distance = length(vertexToLightSource); | |
attenuation = 1.0 / distance; // linear attenuation | |
lightDirection = normalize(vertexToLightSource); | |
} | |
float3 diffuseReflection = | |
attenuation * _LightColor0.rgb * _BackColor.rgb | |
* max(0.0, dot(normalDirection, lightDirection)); | |
float3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
specularReflection = float3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation * _LightColor0.rgb | |
* _BackSpecColor.rgb * pow(max(0.0, dot( | |
reflect(-lightDirection, normalDirection), | |
viewDirection)), _BackShininess); | |
} | |
output.col = float4(diffuseReflection | |
+ specularReflection, 1.0); | |
// no ambient contribution in this pass | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
} | |
FallBack "Diffuse" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook Twopass Blending" { | |
Properties { | |
_ColorBack ("Transparent color", Color) = (1.0, 0.0, 0.0, 0.3) | |
_ColorFront ("Transparent color", Color) = (0.0, 1.0, 0.0, 0.3) | |
} | |
SubShader { | |
Tags { "Queue" = "Transparent" } | |
Pass { | |
Cull Front | |
ZWrite Off | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform float4 _ColorBack; | |
float4 vert(float4 vertexPos : POSITION) : SV_POSITION | |
{ | |
return mul(UNITY_MATRIX_MVP, vertexPos); | |
} | |
float4 frag(void) : COLOR | |
{ | |
return _ColorBack; | |
} | |
ENDCG | |
} | |
Pass { | |
Cull Back | |
ZWrite Off | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform float4 _ColorFront; | |
float4 vert(float4 vertexPos : POSITION) : SV_POSITION | |
{ | |
return mul(UNITY_MATRIX_MVP, vertexPos); | |
} | |
float4 frag(void) : COLOR | |
{ | |
return _ColorFront; | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook Twopass Discard" { | |
SubShader { | |
Pass { | |
Cull Back | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
struct vertexInput { | |
float4 vertex : POSITION; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 posInObjectCoords : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
output.posInObjectCoords = input.vertex; | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
if (input.posInObjectCoords.y > 0.0) | |
{ | |
discard; | |
} | |
return float4(0.0, 1.0, 0.0, 1.0); | |
} | |
ENDCG | |
} | |
Pass { | |
Cull Front | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
struct vertexInput { | |
float4 vertex : POSITION; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 posInObjectCoords : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
output.posInObjectCoords = input.vertex; | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
if (input.posInObjectCoords.y > 0.0) | |
{ | |
discard; | |
} | |
return float4(1.0, 0.0, 0.0, 1.0); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Cg shading in world space" { | |
SubShader { | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
// uniform float4x4 _Object2World; | |
// automatic definition of a Unity-specific uniform parameter | |
struct vertexInput { | |
float4 vertex : POSITION; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 position_in_world_space : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
output.position_in_world_space = | |
mul(_Object2World, input.vertex); | |
// transformation of input.vertex from object | |
// coordinates to world coordinates; | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
float dist = distance(input.position_in_world_space, | |
float4(0.0, 0.0, 0.0, 1.0)); | |
// computes the distance between the fragment position | |
// and the origin (the 4th coordinate should always be | |
// 1 for points). | |
float4 output = (sin(_Time * 10) + float4(1.0, 1.0, 1.0, 1.0)) / 2; | |
return float4(output.xyz, 1.0); | |
if (dist < 5.0) | |
{ | |
return float4(0.0, 1.0, 0.0, 1.0); | |
// color near origin | |
} | |
else | |
{ | |
return float4(0.3, 0.3, 0.3, 1.0); | |
// color far from origin | |
} | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Wikibook UVCoords" { | |
SubShader { | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
struct vertexInput { | |
float4 vertex : POSITION; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput { | |
float4 pos : SV_POSITION; | |
float4 tex : TEXCOORD0; | |
}; | |
vertexOutput vert(vertexInput input) | |
{ | |
vertexOutput output; | |
output.tex = input.texcoord; | |
output.pos = mul(UNITY_MATRIX_MVP, input.vertex); | |
return output; | |
} | |
float4 frag(vertexOutput input) : COLOR | |
{ | |
return float4(input.tex.xy, 0.0, 1.0); | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment