Unity3d game development-simple transparent shader Skill Training

Source: Internet
Author: User

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

 

    1. // Shader created with shader forge beta 0.34
    2. // Shader forge (c) Joachim holmer-http://www.acegikmo.com/shaderforge/
    3. // Note: manually altering this data may prevent you from opening it in shader forge
    4. /* 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 ;*/
    5. Shader "Custom/shader1 "{
    6. Properties {
    7. _ Color ("color", color) = (1, 1, 1)
    8. [Hideininspector] _ cutoff ("Alpha cutoff", range (0, 1) = 0.5
    9. }
    10. Subshader {
    11. Tags {
    12. "Ignoreprojector" = "true"
    13. "Queue" = "Transparent"
    14. "Rendertype" = "Transparent"
    15. }
    16. 200
    17. Pass {
    18. Name "forwardbase"
    19. Tags {
    20. "Lightmode" = "forwardbase"
    21. }
    22. Blend srcalpha oneminussrcalpha
    23. Zwrite off
    24. Cgprogram
    25. # Pragma vertex vert
    26. # Pragma fragment frag
    27. # Define unity_pass_forwardbase
    28. # Include "unitycg. cginc"
    29. # Pragma multi_compile_fwdbase
    30. # Pragma exclude_renderers xbox360 PS3 flash d3d11_9x
    31. # Maid target 3.0
    32. Uniform float4 _ lightcolor0;
    33. Uniform float4 _ color;
    34. Struct vertexinput {
    35. Float4 vertex: position;
    36. Float3 normal: normal;
    37. };
    38. Struct vertexoutput {
    39. Float4 pos: sv_position;
    40. Float4 posworld: texcoord0;
    41. Float3 normaldir: texcoord1;
    42. };
    43. Vertexoutput Vert (vertexinput v ){
    44. Vertexoutput O;
    45. O. normaldir = MUL (float4 (V. Normal, 0), _ world2object). XYZ;
    46. O. posworld = MUL (_ object2world, V. vertex );
    47. O. Pos = MUL (unity_matrix_mvp, V. vertex );
    48. Return O;
    49. }
    50. Fixed4 frag (vertexoutput I): Color {
    51. I. normaldir = normalize (I. normaldir );
    52. Float3 viewdirection = normalize (_ worldspacecamerapos. XYZ-I. posworld. XYZ );
    53. ////// Normals:
    54. Float3 normaldirection = I. normaldir;
    55. Float3 lightdirection = normalize (_ worldspacelightpos0.xyz );
    56. ///// Lighting:
    57. Float attenuation = 1;
    58. Float3 attencolor = attenuation * _ lightcolor0.xyz;
    59. ///// Diffuse:
    60. Float ndotl = dot (normaldirection, lightdirection );
    61. Float3 diffuse = max (0.0, ndotl) * attencolor + unity_lightmodel_ambient.rgb;
    62. Float3 finalcolor = 0;
    63. Float3 diffuselight = diffuse;
    64. Finalcolor + = diffuselight * _ color. RGB;
    65. /// Final color:
    66. Return fixed4 (finalcolor, (1.0-max (0, dot (normaldirection, viewdirection ))));
    67. }
    68. Endcg
    69. }
    70. Pass {
    71. Name "forwardadd"
    72. Tags {
    73. "Lightmode" = "forwardadd"
    74. }
    75. Blend one
    76. Zwrite off
    77. Fog {color (0, 0, 0 )}
    78. Cgprogram
    79. # Pragma vertex vert
    80. # Pragma fragment frag
    81. # Define unity_pass_forwardadd
    82. # Include "unitycg. cginc"
    83. # Include "autolight. cginc"
    84. # Pragma multi_compile_fwdadd
    85. # Pragma exclude_renderers xbox360 PS3 flash d3d11_9x
    86. # Maid target 3.0
    87. Uniform float4 _ lightcolor0;
    88. Uniform float4 _ color;
    89. Struct vertexinput {
    90. Float4 vertex: position;
    91. Float3 normal: normal;
    92. };
    93. Struct vertexoutput {
    94. Float4 pos: sv_position;
    95. Float4 posworld: texcoord0;
    96. Float3 normaldir: texcoord1;
    97. Lighting_coords (2, 3)
    98. };
    99. Vertexoutput Vert (vertexinput v ){
    100. Vertexoutput O;
    101. O. normaldir = MUL (float4 (V. Normal, 0), _ world2object). XYZ;
    102. O. posworld = MUL (_ object2world, V. vertex );
    103. O. Pos = MUL (unity_matrix_mvp, V. vertex );
    104. Transfer_vertex_to_fragment (o)
    105. Return O;
    106. }
    107. Fixed4 frag (vertexoutput I): Color {
    108. I. normaldir = normalize (I. normaldir );
    109. Float3 viewdirection = normalize (_ worldspacecamerapos. XYZ-I. posworld. XYZ );
    110. ////// Normals:
    111. Float3 normaldirection = I. normaldir;
    112. Float3 lightdirection = normalize (lerp (_ worldspacelightpos0.xyz, _ worldspacelightpos0.xyz-I. posworld. XYZ, _ worldspacelightpos0.w ));
    113. ///// Lighting:
    114. Float attenuation = light_attenuation (I );
    115. Float3 attencolor = attenuation * _ lightcolor0.xyz;
    116. ///// Diffuse:
    117. Float ndotl = dot (normaldirection, lightdirection );
    118. Float3 diffuse = max (0.0, ndotl) * attencolor;
    119. Float3 finalcolor = 0;
    120. Float3 diffuselight = diffuse;
    121. Finalcolor + = diffuselight * _ color. RGB;
    122. /// Final color:
    123. Return fixed4 (finalcolor * (1.0-max (0, dot (normaldirection, viewdirection), 0 );
    124. }
    125. Endcg
    126. }
    127. }
    128. Fallback "diffuse"
    129. Customeditor "shaderforgematerialinspector"
    130. }

      For more exciting unity3d technical articles, please click http://www.gopedu.com/article

       

       

Unity3d game development-simple transparent shader Skill Training

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.