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"
}
相关推荐
★YUI★11 小时前
学习游戏制作记录(克隆技能)7.25
学习·游戏·unity·c#
不绝19115 小时前
ARPG开发流程第一章——方法合集
算法·游戏·unity·游戏引擎
玩代码15 小时前
Unity里的加力
开发语言·unity
贵州晓智信息科技16 小时前
Unity 性能优化全攻略
unity·性能优化·游戏引擎
UWA18 小时前
UWA DAY 2025 游戏开发者大会|全议程
游戏·unity·性能优化·游戏开发·uwa·unreal engine
unicrom_深圳市由你创科技18 小时前
Unity 的UI动画调节
ui·unity·游戏引擎
咩咩觉主1 天前
Unity编辑器拓展 IMGUI与部分Utility知识总结(代码+思维导图)
unity·c#·编辑器·游戏引擎
龚子亦1 天前
【Unity开发】数据存储——XML
xml·unity·游戏引擎·数据存储·游戏开发
write_the_code2 天前
Unity国际版下载链接分享(非c1国内版)
unity·游戏引擎
Bulling_2 天前
unity动态背景制作
unity·游戏引擎