Unity Shader编程(2)Unity Shader初步
2015-10-03 15:21
555 查看
Unity Shader编程(2)Unity Shader初步
Unity通过ShaderLab来组织Shader。下面我们通过分析上一节中的Shader代码来学习Unity Shader的组织形式。
Shader "husheng/Shader" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} _Glossiness ("Smoothness", Range(0,1)) = 0.5 _Metallic ("Metallic", Range(0,1)) = 0.0 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; struct Input { float2 uv_MainTex; }; half _Glossiness; half _Metallic; fixed4 _Color; void surf (Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; // Metallic and smoothness come from slider variables o.Metallic = _Metallic; o.Smoothness = _Glossiness; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
1、关键字Shader
Unity Shader是通过Shader关键字开始的。2、使用SubShader组织Shader的不同实现
一个Shader文件里,可以有多个SubShader。多个SubShader可以从上至下选择最适合你的显卡的SubShader。3、Properties属性
Properties属性相当于Shader程序的接口,用来设置贴图,参数之类。如:Properties代码块:
Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} _Glossiness ("Smoothness", Range(0,1)) = 0.5 _Metallic ("Metallic", Range(0,1)) = 0.0 }
我们可以在Shader以及绑定了该Shader的Material上,看到我们的设置。
4、标签和通用状态
Tags { "RenderType"="Opaque" }
Tags为标签指定值,可以设置多个标签。
Queue标签就是渲染队列。
RenderType标签指的是渲染类型。
IgnoreProjector标签。
ForceNoShadowCasting标签。
当然,我们也可以自定义标签。
5、SubShader中的Pass块
SubShader包装了一个渲染方案,而这个方案就是由一个个Pass块来执行。Pass块的Name一般用来引用此Pass。这就是说可以定义一个Pass块,然后在其他Shader的Pass块中多次引用它。
Pass块内的标签都是针对渲染路径的,告诉引擎这个Pass应该在什么渲染路径下被渲染。
Shader "husheng/BaseForm" { Properties { _Color ("Main Color", Color) = (1,1,1,0.5) _SpecColor ("Spec Color", Color) = (1,1,1,1) _Emission ("Emmisive Color", Color) = (0,0,0,0) _Shininess ("Shininess", Range (0.01, 1)) = 0.7 _MainTex ("Base (RGB)", 2D) = "white" { } } SubShader { pass { Material { Diffuse [_Color] Ambient [_Color] Shininess [_Shininess] Specular [_SpecColor] Emission [_Emission] } Lighting On//vertex lighting SeparateSpecular On SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant } } } }
6、FallBack
如果所有的SubShader都失败了。就会执行FallBack代码。FallBack所指定的Shader一般是能够在所有显卡上执行的。===================================================================================
结束。
相关文章推荐
- unity3D:IL2CPP 内部构建 – 生成代码的调试技巧
- Unity mesh 合并
- Unity 下简易状态机的实现
- unity3d发布Android程序
- Unity 5 中的全局光照技术详解(建议收藏)
- unity3d 中 transform.position 与Rigidbody.position的同步问题
- 【Unity3D】3D角色换装++ Advance
- Unity3d 3d角色换装实现原理及步骤
- unity3d 学习笔记(三)
- Unity 3D飞机大战制作心得
- unity调用安卓打包apk时的错误unable to convert classes into dex format
- 测试-一个unity的编译bug,初始化器
- unity 代码运行时间 和 反射的使用
- 安卓调用unity的3D界面
- Unity3D之Vector3.Dot和Vector3.Cross采用
- Unity 获得Log
- Unity3D-RPG项目实战(1):发动机的特殊文件夹
- Unity3D专访——真正的面试
- Unity 3D 控制物体上下左右均匀移动脚本
- 【Unity3d开发基础】Mathf中的数学运算