Next we will start today's unity3d game development skill transparent shader skill training. We have learned the unity3d training goal: to allow U3D beginners to quickly master U3D technology, create and modify materials on their own, and develop small-scale 2D and 3D games and web games independently.
[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)
- [Hideininspector] _ cutoff ("Alpha cutoff", range (0, 1) = 0.5
- }
- Subshader {
- Tags {
- "Ignoreprojector" = "true"
- "Queue" = "Transparent"
- "Rendertype" = "Transparent"
- }
- 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
- # Maid 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
- Zwrite off
- Fog {color (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
- # Maid 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"
- }
For more exciting unity3d technical articles, please click http://www.gopedu.com/article
Unity3d game development-simple transparent shader Skill Training