使用自定义特性关键字,可以动态对Shader某一部分代码进行开关操作

shader(定义了KEYWORD1特性):

定义:#pragma shader_feature KEYWORD1

判断:#ifdef KEYWORD1

Shader "Custom/NewSurfaceShader" {
Properties {
_Color ("Color", Color) = (,,,)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(,)) = 0.5
_Metallic ("Metallic", Range(,)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 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 #pragma shader_feature KEYWORD1 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;
#ifdef KEYWORD1
o.Albedo = float3(,,);
#else
o.Albedo = float3(,,);
#endif
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}

脚本:

using UnityEngine;
using System.Collections; public class ShaderKeywordTest : MonoBehaviour
{
public Material mat; void OnEnable()
{
mat.EnableKeyword("KEYWORD1");
} void OnDisable()
{
mat.DisableKeyword("KEYWORD1");
}
}

测试效果如下:

最新文章

  1. Windows on Device 项目实践 4 - 智能风扇制作
  2. 微信授权步骤与详解 -- c#篇
  3. equals和=,==的区别
  4. 关于js中的几个小问题。
  5. HDU 3308 线段树 最长连续上升子序列 单点更新 区间查询
  6. VS2010升级VS2012必备(MVC4 WebPage2.0 Razor2.0资料汇集)
  7. hdoj 2102 A计划
  8. Solr与Tomcat的整合
  9. C++中vector的实现
  10. js中年份、月份下拉框
  11. Linux shell 编程文件比较
  12. [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number
  13. python __all__
  14. winSCP无法连接虚拟机Linux解决
  15. python 新式类的 __getattribute__
  16. window.open子窗口获取父窗口的值
  17. **CI创建类库(创建自己的工具类等)
  18. k8s之Service
  19. LeetCode--110--平衡二叉树
  20. node.js和JavaScript的关系

热门文章

  1. SQL 语句调用这个存储过程,生成顺序编码
  2. BizTalk开发系列(三十一)配置和使用HTTP适配器
  3. PHP魔术方法使用总结
  4. svn服务配置和日常维护命令
  5. IOS网络第二天 - 03-JSON显示数据,调用本地视频播放,数据转模型
  6. Sharepoint 2013 网站集的删除与还原
  7. 浏览器中Javascript的加载和执行
  8. smaller programs should improve performance
  9. Flink -- Failover
  10. yii 基础版用rbac-plus