【问题标题】:Specular + Metallic Shader高光 + 金属着色器
【发布时间】:2019-02-08 22:47:40
【问题描述】:

Unity 标准着色器如何具有“镜面高光”同时又具有“金属”?

我正在尝试做完全相同的事情,但我无法弄清楚。到目前为止,我有:

// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Custom/MetallicHighlights" {
    Properties {
        _Color ("Color", Color) = (1, 1, 1, 1)
        _MainTex ("Albedo", 2D) = "white" {}

        _GlossMapScale ("Smoothness", Range(0.0, 1.0)) = 0.0
        _MetallicFactor("Metallic Factor", Range(0.0, 1.0)) = 0
        _MetallicGlossMap ("Metallic", 2D) = "gloss" {}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        // And generate the shadow pass with instancing support
        #pragma surface surf Standard fullforwardshadows addshadow

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        struct Input {
            float4 color: Color;
            float2 uv_MainTex;
            float3 viewDir;
        };

        sampler2D _MainTex;
        sampler2D _MetallicGlossMap;
        fixed4 _Color;
        half _GlossMapScale;
        half _MetallicFactor;

        void surf (Input IN, inout SurfaceOutputStandard o) {
            // Albedo comes from a texture tinted by color
            fixed4 albedo = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = albedo.rgb;
            o.Alpha = albedo.a;

            half rim = _RimCap - saturate(dot(normalize(IN.viewDir), o.Normal));
            o.Emission = _RimColor.rgb * pow(rim, _RimIntensity);

            // Metallic and smoothness come from slider variables
            // o.Metallic = _MetallicFactor;
            // o.Smoothness = _GlossMapScale;

            fixed4 metal_colour = tex2D(_MetallicGlossMap, IN.uv_MainTex);
            o.Metallic = metal_colour.r * _MetallicFactor;
            o.Smoothness = metal_colour.a * _GlossMapScale;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

但当我检查 Unity 标准着色器时,我注意到它有“金属”(滑块 + 纹理),还有“高光”(复选框)和“前向渲染选项”的“反射”(复选框)..

他们究竟是如何做到的?如果我使用SurfaceOutputStandardSpecular,那么我会在结构中丢失Metallic 选项。

有什么想法吗?

【问题讨论】:

    标签: c# unity3d shader


    【解决方案1】:

    它应该可以工作:

    // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    
    Shader "Custom/MetallicHighlights" {
        Properties {
            _Color ("Color", Color) = (1, 1, 1, 1)
            _MainTex ("Albedo", 2D) = "white" {}
    
            _GlossMapScale ("Smoothness", Range(0.0, 1.0)) = 0.0
            _MetallicFactor("Metallic Factor", Range(0.0, 1.0)) = 0
            _MetallicGlossMap ("Metallic", 2D) = "gloss" {}
            [ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
        }
        SubShader {
            Tags { "RenderType"="Opaque" }
            LOD 200
    
            CGPROGRAM
            // Physically based Standard lighting model, and enable shadows on all light types
            // And generate the shadow pass with instancing support
            #pragma surface surf Standard fullforwardshadows addshadow
            #pragma shader_feature _SPECULARHIGHLIGHTS_OFF
    
            // Use shader model 3.0 target, to get nicer looking lighting
            #pragma target 3.0
    
            struct Input {
                float4 color: Color;
                float2 uv_MainTex;
                float3 viewDir;
            };
    
            sampler2D _MainTex;
            sampler2D _MetallicGlossMap;
            fixed4 _Color;
            half _GlossMapScale;
            half _MetallicFactor;
    
            void surf (Input IN, inout SurfaceOutputStandard o) {
                // Albedo comes from a texture tinted by color
                fixed4 albedo = tex2D (_MainTex, IN.uv_MainTex) * _Color;
                o.Albedo = albedo.rgb;
                o.Alpha = albedo.a;
    
                half rim = _RimCap - saturate(dot(normalize(IN.viewDir), o.Normal));
                o.Emission = _RimColor.rgb * pow(rim, _RimIntensity);
    
                // Metallic and smoothness come from slider variables
                // o.Metallic = _MetallicFactor;
                // o.Smoothness = _GlossMapScale;
    
                fixed4 metal_colour = tex2D(_MetallicGlossMap, IN.uv_MainTex);
                o.Metallic = metal_colour.r * _MetallicFactor;
                o.Smoothness = metal_colour.a * _GlossMapScale;
            }
            ENDCG
        }
        FallBack "Diffuse"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 2018-04-21
      • 2016-10-08
      • 2015-09-05
      • 2017-12-20
      • 2018-06-26
      • 1970-01-01
      相关资源
      最近更新 更多