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"
}
相关推荐
雪儿waii6 小时前
Unity 中的 Resources 详解
unity·游戏引擎
RReality17 小时前
【Unity UGUI】Toggle / ToggleGroup 与 Dropdown
ui·unity·游戏引擎·图形渲染·材质
雪儿waii18 小时前
Unity 中的 InvokeRepeating 详解
unity·游戏引擎
mxwin18 小时前
Unity Shader 程序化生成:Shader 中的数学宇宙
unity·游戏引擎
雪儿waii19 小时前
Unity 中的 Quaternion(四元数)详解
unity·游戏引擎
RReality20 小时前
【Unity UGUI】ScrollRect 与 Scrollbar 深度用法
unity·游戏引擎
人邮异步社区20 小时前
如何自学游戏引擎的开发?
unity·程序员·游戏引擎
郝学胜-神的一滴21 小时前
[简化版 Games 101] 计算机图形学 05:二维变换下
c++·unity·图形渲染·three.js·opengl·unreal
mxwin1 天前
Unity URP 热更新兼容性:Shader 在 IL2CPP 打包下的注意事项
unity·游戏引擎
AIminminHu2 天前
OpenGL渲染与几何内核那点事-项目实践理论补充(一-3-(7):从“显卡不听话”到“GPU秒懂你”:一个CAD老兵的着色器驯服史))
着色器·编译流程·着色器语言 glsl·创建着色器对象·glcreateshader·gluseprogram·glcreateprogram