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 小时前
使用Unity引擎开发Rokid主机应用的模型交互操作
游戏·ui·unity·c#·游戏引擎·交互
淡海水7 小时前
【URP】Unity[内置Shader]光照着色器Lit
unity·游戏引擎·shader·urp·着色器·lit
爱吃小胖橘7 小时前
Lua语法(2)
开发语言·unity·lua
心疼你的一切12 小时前
使用Unity引擎开发Rokid主机应用的全面配置交互操作
学习·游戏·unity·c#·游戏引擎·交互
qq_428639611 天前
虚幻基础:角色受击
游戏引擎·虚幻
ellis19701 天前
LuaC API知识点汇总
unity·lua
maki0771 天前
虚幻版Pico大空间VR入门教程 05 —— 原点坐标和项目优化技巧整理
android·游戏引擎·vr·虚幻·pico·htc vive·大空间
一线灵1 天前
跨平台游戏引擎 Axmol-2.9.0 发布
游戏引擎
maki0772 天前
VR大空间资料 01 —— 常用VR框架对比
android·ue5·游戏引擎·vr·虚幻·pico
maki0772 天前
VR大空间资料 02 —— 常用Body IK对比
android·游戏引擎·vr·虚幻·pico·ik