Unity图形学之着色器之间传递参数

1.初始化 struct:

UNITY_INITIALIZE_OUTPUT(type,name)

Type: struct 名字

Name :变量的名字

cs 复制代码
 struct Input
        {
            float2 uv_MainTex;
            float3 myColor;
        };
        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
        void MyVertx(inout appdata_base v,out Input o){
            //宏定义 初始化struct
            UNITY_INITIALIZE_OUTPUT(Input,o);
            o.myColor = _Color.rgb;
        }

2.顶点着色器 和片段着色器 的运算次数不是一个量级的,能放在顶点着色器里面运算的,尽量放在顶点里面运算

3.abs(v.naormal) : 法线取值是 -1到 1 ,所以abs是取绝对值 (0-1)

cs 复制代码
Shader "Custom/TestParameter"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200
        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all  light types
        #pragma surface surf Lambert vertex:MyVertx
        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0
        sampler2D _MainTex;
        struct Input
        {
            float2 uv_MainTex;
            float3 myColor;
        };
        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
        void MyVertx(inout appdata_base v,out Input o){
            //宏定义 初始化struct
            UNITY_INITIALIZE_OUTPUT(Input,o);
            o.myColor = _Color.rgb * abs(v.normal);
        }
        void surf (Input IN, inout SurfaceOutput o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb * IN.myColor;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
相关推荐
辰辉创聚6 小时前
禽流感病毒亚型与HA/NA重组蛋白结构解析:分子机制与科研应用综述
wireshark·基带工程·技术美术·重组蛋白表达·重组蛋白·单克隆抗体
小小数媒成员6 小时前
如何优化代码适应托管内存?
游戏·unity·游戏引擎
狂人开飞机13 小时前
21、游戏架构设计模式
游戏·游戏引擎·godot
新的瑞拉公主2 天前
Unity游戏发布微信小游戏:从构建到提审
unity·webgl·微信小游戏·开放数据域
狂人开飞机2 天前
14、游戏手感与视觉打磨
游戏·游戏引擎·godot
狂人开飞机3 天前
15、音频系统
游戏引擎·godot
狂人开飞机3 天前
16、粒子线条与视觉特效
游戏引擎·godot
心前阳光3 天前
Unity发布PC软件图标不能改变
unity·游戏引擎
an86950013 天前
unity如何在visual studio中打断点debug
unity·游戏引擎·visual studio
xcLeigh3 天前
Unity基础:使用Transform控制物体移动——Translate与position详解
unity·游戏引擎