【Unity Shader】第二节 为上一节课的shader增加属性

来源:互联网 发布:手机注册淘宝店铺 编辑:程序博客网 时间:2024/04/28 23:16

昨天的代码里讲到了Properties这个里面显示的是给Unity GUI上显示的名字,到底可以显示哪些呢?

今天的内容就是给昨天的shader增加新的功能


In our  Properties block of our Shader, remove the current property by deleting the
following code from our current Shader.
_MainTex ("Base (RGB)", 2D) = "white" {}
2. Now enter the following code, save the Shader, and re-enter the Unity editor.
_EmissiveColor ("Emissive Color", Color) = (1,1,1,1)
3. When you return to Unity, the Shader will compile and you will see that our Material's
Inspector tab now has a color swatch, named Emissive Color, instead of a texture
swatch. Let's add one more and see what happens. Enter the following code:
_AmbientColor ("Ambient Color", Range(0,10)) = 2
Chapter 1
4. We have added another Color Swatch to the Material's Inspector tab. Now let's
add one more to get a feel for other kinds of properties that we can create. Add the
following code to the properties block:
_MySliderValue ("This is a Slider", Range(0,10)) = 2.5


可以增加:

_MainTex ("Base (RGB)", 2D) = "white" {}

_AmbientColor ("Ambient Color", Color) = (1,1,1,1)

_EmissiveColor ("Emissive Color", Color) = (1,1,1,1)

_MySliderValue ("This is a Slider", Range(0,10)) = 2.5


Shader "CookbookShaders/BasicDiffuse"{//We define Properties in the properties blockProperties{_EmissiveColor ("Emissive Color", Color) = (1,1,1,1)_AmbientColor ("Ambient Color", Color) = (1,1,1,1)_MySliderValue ("This is a Slider", Range(0,10)) = 2.5}SubShader  Diffuse Shading{Tags { "RenderType"="Opaque" }LOD 200CGPROGRAM#pragma surface surf Lambert//We need to declare the properties variable type inside of theCGPROGRAM so we can access its value from the properties block.float4 _EmissiveColor;float4 _AmbientColor;float _MySliderValue;struct Input{float2 uv_MainTex;};void surf (Input IN, inout SurfaceOutput o){//We can then use the properties values in our shaderfloat4 c;c = pow((_EmissiveColor + _AmbientColor), _MySliderValue);o.Albedo = c.rgb;o.Alpha = c.a;}ENDCG}FallBack "Diffuse"}



0 0
原创粉丝点击