下面我们开始今天的Unity3D游戏开发技能透明shader技能培训。 我们学习Unity3D培训目标:让U3D初学者可以更快速的掌握U3D技术,自行制作修改素材,可以独立完成2D、3D小规模游戏及网页游戏开发。
[plain] view plaincopy
// Shader created with Shader Forge Beta 0.34
// Shader Forge (c) Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:0.34;sub:START;pass:START;ps:flbk:,lico:1,lgpr:1,nrmq:1,limd:1,uamb:True,mssp:True,lmpd:False,lprd:False,enco:False,frtr:True,vitr:True,dbil:False,rmgx:True,rpth:0,hqsc:True,hqlp:False,blpr:1,bsrc:3,bdst:7,culm:0,dpts:2,wrdp:False,ufog:True,aust:True,igpj:True,qofs:0,qpre:3,rntp:2,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,ofsf:0,ofsu:0,f2p0:False;n:type:ShaderForge.SFN_Final,id:3,x:32038,y:32598|diff-227-RGB,alpha-226-OUT;n:type:ShaderForge.SFN_Fresnel,id:226,x:32459,y:32705;n:type:ShaderForge.SFN_Color,id:227,x:32398,y:32551,ptlb:Color,ptin:_Color,glob:False,c1:1,c2:1,c3:1,c4:1;proporder:227;pass:END;sub:END;*/
Shader "Custom/Shader1" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="Transparent"
"RenderType"="Transparent"
}
LOD 200
Pass {
Name "ForwardBase"
Tags {
"LightMode"="ForwardBase"
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#pragma exclude_renderers xbox360 ps3 flash d3d11_9x
#pragma target 3.0
uniform float4 _LightColor0;
uniform float4 _Color;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD0;
float3 normalDir : TEXCOORD1;
};
VertexOutput vert (VertexInput v) {
VertexOutput o;
o.normalDir = mul(float4(v.normal,0), _World2Object).xyz;
o.posWorld = mul(_Object2World, v.vertex);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
fixed4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
/////// Normals:
float3 normalDirection = i.normalDir;
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
////// Lighting:
float attenuation = 1;
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = dot( normalDirection, lightDirection );
float3 diffuse = max( 0.0, NdotL) * attenColor + UNITY_LIGHTMODEL_AMBIENT.rgb;
float3 finalColor = 0;
float3 diffuseLight = diffuse;
finalColor += diffuseLight * _Color.rgb;
/// Final Color:
return fixed4(finalColor,(1.0-max(0,dot(normalDirection, viewDirection))));
}
ENDCG
}
Pass {
Name "ForwardAdd"
Tags {
"LightMode"="ForwardAdd"
}
Blend One One
ZWrite Off
Fog { Color (0,0,0,0) }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDADD
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fwdadd
#pragma exclude_renderers xbox360 ps3 flash d3d11_9x
#pragma target 3.0
uniform float4 _LightColor0;
uniform float4 _Color;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD0;
float3 normalDir : TEXCOORD1;
LIGHTING_COORDS(2,3)
};
VertexOutput vert (VertexInput v) {
VertexOutput o;
o.normalDir = mul(float4(v.normal,0), _World2Object).xyz;
o.posWorld = mul(_Object2World, v.vertex);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
fixed4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
/////// Normals:
float3 normalDirection = i.normalDir;
float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
////// Lighting:
float attenuation = LIGHT_ATTENUATION(i);
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = dot( normalDirection, lightDirection );
float3 diffuse = max( 0.0, NdotL) * attenColor;
float3 finalColor = 0;
float3 diffuseLight = diffuse;
finalColor += diffuseLight * _Color.rgb;
/// Final Color:
return fixed4(finalColor * (1.0-max(0,dot(normalDirection, viewDirection))),0);
}
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "ShaderForgeMaterialInspector"
}
更多精彩unity3d技术文章请点击 http://www.gopedu.com/article