EIYOOO 2025-03-29 20:46 采纳率: 25%
浏览 6

庄懂12课透明剪切投影修正

我在做庄懂十二课最后透明剪切投影修正出了问题

img

以下是我的代码

//Shader路径名
Shader "AP01/L12/OgreMagi_Code" {
    //材质面板参数
    Properties {
        [header(Texture)]
        _Maintex        ("RGB:颜色 A:透贴",2d) = "white"{}
        _MaskTex        ("R:高光强度 G:边缘光强度 B:高光染色 A:高光次幂",2d) = "balck"{}
        _NormalTex      ("RGB:法线贴图",2d) = "bump"{}
        _MetalnessMask  ("金属度遮罩",2d) = "black"{}
        _EmissionMask   ("自发光遮罩",2d) = "black"{}
        _DiffWarpTex    ("颜色Warp图",2d) = "gray"{}
        _FresWarpTex    ("菲涅尔Warp图",2d) = "gray"{}
        _Cubemap        ("环境球",cube) = "_Skybox"{}
        [header(LightDiff)]
        _LightCol       ("光颜色",Color) = (1,1,1,1)
        [header(LightSpec)]
        _SpecPow        ("高光次幂",Range(0,30.0)) = 5
        _SpecInt        ("高光强度",Range(0,10.0)) = 5
        [header(EnvDiff)]
        _EnvCol         ("环境光颜色",Color) = (1,1,1,1)
        [header(EnvSpec)]
        _EnvSpecInt     ("环境镜面反射强度",Range(0,10)) = 0.5
        [header(Rim)]
        [hdr] _RimCol   ("轮廓光颜色",Color) = (1,1,1,1)
        [header(Emit)]
        _EmitInt        ("自发光强度",Range(0,10)) = 1
        [HideInInspector]
        _Cutoff         ("Alpha cutoff", Range(0,1)) = 0.5
        [HideInInspector]
         _Color          ("Main Color", Color) = (1.0, 1.0, 1.0, 1.0)
    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            Cull Off
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            #include "AutoLight.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0
            //输入参数
            uniform sampler2D _Maintex;
            uniform sampler2D _MaskTex;
            uniform sampler2D _NormalTex;
            uniform sampler2D _MetalnessMask;
            uniform sampler2D _EmissionMask;
            uniform sampler2D _DiffWarpTex;
            uniform sampler2D _FresWarpTex;
            uniform samplerCUBE _Cubemap;
            uniform half3 _LightCol;
            uniform half _SpecPow;
            uniform half _SpecInt;
            uniform half3 _EnvCol;
            uniform half _EnvSpecInt;
            uniform half3 _RimCol;
            uniform half _EmitInt;
            uniform half _Cutoff;
            //输入结构
            struct VertexInput {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
                float4 tangent : TANGENT; 
            };
            //输出结构
            struct VertexOutput {
                float4 pos : SV_POSITION;
                float3 posWS : TEXCOORD0;
                float2 uv : TEXCOORD1;
                float3 nDirWS : TEXCOORD2;
                float3 tDirWS : TEXCOORD3;
                float3 bDirWS : TEXCOORD4;
                LIGHTING_COORDS(5,6)
            };
            //顶点Shader
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.pos = UnityObjectToClipPos( v.vertex );
                o.posWS = mul(unity_ObjectToWorld,v.vertex).xyz;
                o.uv = v.uv;
                o.nDirWS = normalize(UnityObjectToWorldNormal(v.normal));
                o.tDirWS = normalize(mul(unity_ObjectToWorld,float4(v.tangent.xyz,0)));
                o.bDirWS = normalize(cross(o.nDirWS,o.tDirWS) * v.tangent.w);
                TRANSFER_VERTEX_TO_FRAGMENT(o)
                return o;
            }
            //像素Shader
            float4 frag(VertexOutput i) : COLOR {
                //准备向量
                half3 nDirTS = UnpackNormal(tex2D(_NormalTex,i.uv));
                half3x3 TBN = half3x3(i.tDirWS,i.bDirWS,i.nDirWS);
                half3 nDirWS = normalize(mul(nDirTS,TBN));
                half3 vDirWS = normalize(_WorldSpaceCameraPos.xyz - i.posWS);
                half3 vrDirWS = normalize(reflect(-vDirWS,nDirWS));
                half3 lDirWS = normalize(_WorldSpaceLightPos0.xyz);
                half3 lrDirWS = normalize(reflect(-lDirWS,nDirWS));
                //中间向量
                half3 nDotl = dot(nDirWS,lDirWS);
                half3 nDotv = dot(nDirWS,vDirWS);
                half3 vDotlr = dot(vDirWS,lrDirWS);
                //纹理采样
                half4 var_Maintex = tex2D(_Maintex,i.uv);
                half4 var_MaskTex = tex2D(_MaskTex,i.uv);
                half var_MetalnessMask = tex2D(_MetalnessMask,i.uv).r;
                half var_EmissionMask = tex2D(_EmissionMask,i.uv).r;
                half3 var_FresWarpTex = tex2D(_FresWarpTex,nDotv).rgb;
                half3 var_Cubemap = texCUBElod(_Cubemap,float4(vrDirWS,lerp(8.0,0.0,var_MaskTex.a)));
                //提取信息
                half3 baseCol = var_Maintex.rgb;
                half opacity = var_Maintex.a;
                half specInt = var_MaskTex.r;
                half rimInt = var_MaskTex.g;
                half soecTint = var_MaskTex.b;
                half specPow = var_MaskTex.a;
                half metallic = var_MetalnessMask;
                half emitInt = var_EmissionMask;
                half3 envCube = var_Cubemap;
                half shadow = LIGHT_ATTENUATION(i);
                //光照模型
                    //漫反射颜色 
                    half3 diffCol = lerp(baseCol,float3(0.0,0.0,0.0),metallic);
                    //镜面反射颜色
                    half3 specCol = lerp(baseCol,float3(0.3,0.3,0.3),soecTint)* specInt;
                    //菲涅尔
                    half3 fresnel = lerp(var_FresWarpTex,0,metallic);
                    half fresnelCol = var_FresWarpTex.r;
                    half fresnelRim = var_FresWarpTex.g;
                    half fresnelSpec = var_FresWarpTex.b;
                    //漫反射光源
                    half halflambert = nDotl * 0.5 + 0.5;
                    half3 var_DiffWarpTex = tex2D(_DiffWarpTex,half2 (halflambert,0.2));
                    half3 lightDiff = diffCol * var_DiffWarpTex * _LightCol;
                    //镜面反射光源
                    half phong = pow(max(0,vDotlr),specPow * _SpecPow);
                    half spec = phong * max(0,nDotl);
                    spec = max(spec,fresnelSpec);
                    spec = spec * _SpecInt;
                    half3 lightSpec = specCol * spec * _LightCol;
                    //漫反射环境
                    half3 envDiff = diffCol * _EnvCol;
                    //镜面反射环境
                    half reflectInt = max(fresnelSpec,metallic) * specInt;
                    half3 envSpec = specCol * reflectInt * envCube * _EnvSpecInt;
                    //轮廓光
                    half3 rimLight = _RimCol * fresnelRim * rimInt * max(0.0,nDirWS.g);
                    //自发光
                    half3 emission = diffCol * emitInt *_EmitInt;
                //混合
                half3 finalRGB = (lightDiff + lightSpec) * shadow + (envDiff + envSpec) + rimLight + emission;
                //透明剪切
                clip(opacity - _Cutoff);
                //返回值
                return float4(finalRGB,1.0);
            }
            ENDCG
        }
    }
    FallBack "Legacy Shaders/Transparent/Cutout/VertexLit"
}


  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2025-03-29 21:05
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    庄懂12课透明剪切投影修正问题

    你遇到了透明剪切投影修正的问题,可能是因为Shader的写法不正确或 Unity 的设置不对导致的。

    首先,检查你的Shader代码是否正确,特别是 Proper 关键字是否正确使用。如果你使用的是 Unity 的 built-in Shader,可以检查 Unity 的官方文档以确保你的代码正确。

    其次,检查 Unity 的设置是否正确,特别是渲染模式、透明度和剪切投影等设置是否正确。如果你使用的是 Unity 的 built-in Shader,可以检查 Unity 的官方文档以确保你的设置正确。

    最后,如果你仍然遇到问题,可以提供更多的代码和设置信息,以便我更好地帮助你解决问题。

    核心部分代码:

    Shader "AP01/L12/OgreMagi_Code" {
        //材质面板参数
        Properties {
            _MainTex ("Albedo (RGBA)", 2D) = "white" {}
            _Cutoff ("Alpha Cutoff", Range (0, 1)) = 0.5
        }
        //材质面板
        SubShader {
            Tags {"Queue"="Transparent" "RenderType"="Transparent"}
            LOD 200
            // Alpha blending
            blend SrcAlpha OneMinusSrcAlpha
            // Cull Back
            Cull Back
            // ZWrite Off
            ZWrite Off
            // Pass
            Pass {
                // Set the alpha cutoff
                SetAlphaCutoff _Cutoff
                // Set the texture
                SetTexture [_MainTex] {
                    // Set the texture coordinates
                    coordgenmode 0
                }
            }
        }
    }
    

    这个代码片段是一个简单的透明材质Shader,使用 Alpha blending 和 Alpha cutoff 来实现透明度的控制。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月29日