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"
}
相关推荐
Hody9113 小时前
【XR开发系列】2025 年 XR 开发入门,我该选择 Unity 还是 Unreal Engine?
unity·xr·虚幻
DvLee102415 小时前
UnityGLTF 材质创建与赋值流程
unity·材质
华硕广东17 小时前
电脑被捆绑软件缠上?3 步根治卡顿弹窗~
科技·安全·技术美术
HahaGiver66618 小时前
从0到1做一个“字母拼词”Unity小游戏(含源码/GIF)- 字母拼词正确错误判断
unity·游戏引擎·游戏程序
华硕广东1 天前
怎么才能去定义自己的生活呢?
科技·安全·技术美术
一个小狼娃2 天前
Android集成Unity避坑指南
android·游戏·unity
极客柒2 天前
Unity 协程GC优化记录
java·unity·游戏引擎
黄思搏2 天前
Unity SpriteRenderer 进度条 Shader 实现
unity·游戏引擎
猫屋小鱼丸2 天前
手把手教你在unity中实现一个视觉小说系统(一)
unity
国服第二切图仔2 天前
Rust开发实战之简单游戏开发(piston游戏引擎)
开发语言·rust·游戏引擎