You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity URP曲面细分着色器仅打包后失效问题求助

Unity URP草地曲面细分着色器打包后失效问题

问题详情

  • URP项目中自定义的草地曲面细分着色器,在Unity编辑器内运行完全正常,但打包后彻底失效
  • 打包过程中出现编译错误,运行时通过以下代码检测到着色器编译失败:
renderer.material.shader = grassShader;
if (grassShader == null)
{
    Debug.LogError("Shader is missing in the Material!");
}
if (!grassShader.isSupported)
{
    Debug.LogError("Shader compilation failed: " + grassShader.name);
    return;
}
  • 尝试查看Player.log未找到相关错误详情,仅显示简单运行时错误提示
  • 设备为联想NVIDIA 3050显卡,使用DX11,理论上支持曲面细分特性,怀疑问题出在CG与HLSL API差异或平台兼容性配置上

相关代码

Tessellation.hlsl

#ifndef MY_TESSELLATION_INCLUDED
#define MY_TESSELLATION_INCLUDED

struct a2v
{
    float4 vertex : POSITION;
    float3 normal : NORMAL;
    float4 tangent : TANGENT;
};

struct v2f
{
    float4 pos : SV_POSITION;
    float3 normal : NORMAL;
    float4 tangent : TANGENT;
};
struct TessellationFactors
{
    float edge[3] : SV_TessFactor;
    float inside : SV_InsideTessFactor;
};

a2v vert(a2v v)
{
    return v;
}

v2f tessVert(a2v v)
{
    v2f o;
    o.pos = v.vertex;
    o.normal = v.normal;
    o.tangent = v.tangent;
    return o;
}
float _TessellationUniform;
TessellationFactors patchConstantFunction(InputPatch<a2v, 3> patch, uint patchID: SV_PrimitiveID)
{
    TessellationFactors f;
    f.edge[0] = _TessellationUniform;
    f.edge[1] = _TessellationUniform;
    f.edge[2] = _TessellationUniform;
    f.inside = _TessellationUniform;
    return f;
}

[domain("tri")]
[outputcontrolpoints(3)]
[outputtopology("triangle_cw")]
[partitioning("integer")]
[patchconstantfunc("patchConstantFunction")]
[maxtessfactor(64.0f)]
a2v hull(InputPatch<a2v, 3> patch, uint id : SV_OutputControlPointID)
{
    // return one a2v patch
    return patch[id];
}
[domain("tri")]
v2f domain(TessellationFactors factors, OutputPatch<v2f, 3> patch, float3 barycentricCoordinates : SV_DomainLocation)
{
    a2v v;

    #define MY_DOMAIN_PROGRAM_INTERPOLATE(a2vName, v2fName) v.a2vName = \
        patch[0].v2fName * barycentricCoordinates.x + \
        patch[1].v2fName * barycentricCoordinates.y + \
        patch[2].v2fName * barycentricCoordinates.z;

    MY_DOMAIN_PROGRAM_INTERPOLATE(vertex, pos)
    MY_DOMAIN_PROGRAM_INTERPOLATE(normal, normal)
    MY_DOMAIN_PROGRAM_INTERPOLATE(tangent, tangent)

    return tessVert(v);
}
#endif // MY_TESSELLATION_INCLUDED

草着色器核心代码

// sample the FlowMap by time
float2 uv = pos.xz * _WindDistortionMap_ST.xy + _WindDistortionMap_ST.zw
    + _WindFrequency * _Time.y;
float2 windSample = (SAMPLE_TEXTURE2D_LOD(_WindDistortionMap, sampler_WindDistortionMap, uv, 0.0).xy*2 - 1) * _WindStrength;
// flowmap(windMap) calculates the wind direction
float3 wind = normalize(float3(windSample.x + _WindTargetModifier.x, windSample.y + _WindTargetModifier.y, 0));
// Rotate by windMap
float3x3 windRotation = RotateAroundAxis3x3(C_HALF_PI * length(windSample), wind);

// z = Unity_y; x = -Unity_z; y = Unity_x
// Rotate one random angle in the tangent space
float3x3 facingRotationMatrix = RotateAroundAxis3x3((GetRandomNum(pos.xy)*C_2PI), float3(0, 0, 1));
// Take a random bend
float3x3 bendRotationMatrix = RotateAroundAxis3x3(GetRandomNum(pos.xzx) * _BendRotationRandom * C_HALF_PI, float3(-1, 0, 0));

float3x3 windMatrix = mul(tangentToLocal, windRotation);
float3x3 localMatrix = mul(windMatrix, facingRotationMatrix);
float3x3 transformationMatrix = mul(localMatrix, bendRotationMatrix);
float3x3 transformationMatrixFacing = mul(tangentToLocal, facingRotationMatrix);

float height =  _BladeHeightRandom * (GetRandomNum(pos.zyx)*2 - 1) + _BladeHeight;
float width = _BladeWidthRandom * (GetRandomNum(pos.xzy)*2 - 1) + _BladeWidth;
float forward = GetRandomNum(pos.yyz) * _BladeForward;

请求

希望能得到该问题的具体原因分析及可行解决方案。

内容的提问来源于stack exchange,提问作者EauDouce

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 12:58:21