gpt4 book ai didi

unity3d - 从 DX9 到 DX11 的顶点着色器编译错误(Unity 5.6 到 2017.4)

转载 作者:行者123 更新时间:2023-12-01 23:38:00 24 4
gpt4 key购买 nike

我有一个使用自定义着色器的 Unity 图像后期效果,在从 Unity 5.6 迁移到 Unity 2017.4 时抛出错误

Assertion failed: Failed to create DX11 vertex declaration; something wrong with vertex shader input data? (hr=80070057)

这似乎是这个脚本中的错误。

Shader "SG/Stretch Shadows" {
Properties {
_MainTex ("Light texture", 2D) = "white" {}
}
SubShader {
LOD 100
Blend One Zero
Cull Off
ZWrite Off
Lighting Off

Pass { // 0 Blacken source
CGPROGRAM
#pragma target 3.0

#include "UnityCG.cginc"
#pragma vertex processVerts
#pragma fragment drawFrag

#pragma glsl_no_auto_normalization

struct vertdata {
float2 uv : TEXCOORD0;
float2 uvScaled : TEXCOORD1;
float4 vertex : POSITION;
};

struct frag_v2f {
float4 vertex : SV_POSITION;
half2 uv : TEXCOORD0;
};

uniform sampler2D _MainTex;

frag_v2f processVerts (vertdata v){
frag_v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}

half4 drawFrag (frag_v2f i) : SV_Target {
fixed4 shadow = tex2D(_MainTex, i.uv);
half intensity = max(shadow.r, max(shadow.g, shadow.b));
intensity = step(intensity,0.1);
return 1-intensity;
}

ENDCG
}

Pass { // 1 Stretch
CGPROGRAM
#pragma target 3.0

#include "UnityCG.cginc"
#pragma vertex processVerts
#pragma fragment drawFrag

#pragma glsl_no_auto_normalization

struct vertdata {
float2 uv : TEXCOORD0;
float2 uvScaled : TEXCOORD1;
float4 vertex : SV_POSITION;
};

struct frag_v2f {
float4 vertex : SV_POSITION;
half2 basePos : TEXCOORD0;
half2 zoomPos : TEXCOORD1;
};

uniform sampler2D _ObstacleTex;
uniform sampler2D _MainTex;
half4 _SunPos;
#ifdef UNITY_HALF_TEXEL_OFFSET
#endif

frag_v2f processVerts (vertdata v){
frag_v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.basePos = v.uv;
o.zoomPos = v.uv * _SunPos.zw + (_SunPos.xy -_SunPos.zw*0.5);
return o;
}

half4 drawFrag (frag_v2f i) : SV_Target {
half2 basePos = i.basePos;
half2 zoomPos = i.zoomPos;
half4 tex = tex2D(_MainTex, i.basePos);

half sub = 1.0/60;
half len = length((basePos - zoomPos));

half4 col = tex*tex.a;
col = half4(0,0,0,0);
half pos = 1;
half power = 0.5;

for(int i = 0; i < 60; i++){
pos -= sub;
half4 obstacle = tex2D(_MainTex, lerp(zoomPos, basePos, pos));
obstacle *= pow(pos, power);
col = max(col, obstacle);
}

return 1-col;//half4(half3((basePos - zoomPos).x*20), 1);//tex2D(_ObstacleTex, basePos);
}

ENDCG
}

Pass { // 2 Blacken alpha
CGPROGRAM
#pragma target 3.0

#include "UnityCG.cginc"
#pragma vertex processVerts
#pragma fragment drawFrag

#pragma glsl_no_auto_normalization

struct vertdata {
float2 uv : TEXCOORD0;
float2 uvScaled : TEXCOORD1;
float4 vertex : SV_POSITION;
};

struct frag_v2f {
float4 vertex : SV_POSITION;
half2 uv : TEXCOORD0;
};

uniform sampler2D _MainTex;

frag_v2f processVerts (vertdata v){
frag_v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}

half4 drawFrag (frag_v2f i) : SV_Target {
fixed4 shadow = tex2D(_MainTex, i.uv);
half4 intensity = half4(0,0.2,0.2,shadow.a);
intensity = lerp(shadow, intensity, shadow.a);

return intensity;
}

ENDCG
}

Pass { // 3 Draw fully zoomed shadow and half zoomed shadow
// Each pass duplicates previous pass, doubling drawn shadows
CGPROGRAM
#pragma target 3.0

#include "UnityCG.cginc"
#pragma vertex processVerts
#pragma fragment drawFrag

#pragma glsl_no_auto_normalization

struct vertdata {
float2 uv : TEXCOORD0;
float2 uvScaled : TEXCOORD1;
float4 vertex : SV_POSITION;
};

struct frag_v2f {
float4 vertex : SV_POSITION;
half2 basePos : TEXCOORD0;
half2 zoomPos : TEXCOORD1;
};

uniform sampler2D _ObstacleTex;
uniform sampler2D _MainTex;
half4 _SunPos;
half _Offset; // frational lerp value. Decrease by powers of two each pass
#ifdef UNITY_HALF_TEXEL_OFFSET
#endif

frag_v2f processVerts (vertdata v){
frag_v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.basePos = v.uv;
o.zoomPos = v.uv * _SunPos.zw + (_SunPos.xy -_SunPos.zw*0.5);
return o;
}

half4 drawFrag (frag_v2f i) : SV_Target {
half2 basePos = i.basePos;
half2 zoomPos = i.zoomPos;
half firstPass = tex2D(_MainTex, basePos).a;
half secondPass = tex2D(_MainTex, zoomPos).a-0.85;
half4 output = half4(0,0,0,max(firstPass, secondPass));
secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.65)).a-0.3;
output.a = max(output.a, secondPass);
secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.4)).a-0.45;
output.a = max(output.a, secondPass);
secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.25)).a-0.6;
output.a = max(output.a, secondPass);
secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.1)).a-0.7;
output.a = max(output.a, secondPass);

return output;//half4(half3((basePos - zoomPos).x*20), 1);//tex2D(_ObstacleTex, basePos);
}

ENDCG
}

Pass { // 4 Single pass. Ping-pong
// Each pass duplicates previous pass, doubling drawn shadows
CGPROGRAM
#pragma target 3.0

#include "UnityCG.cginc"
#pragma vertex processVerts
#pragma fragment drawFrag

#pragma glsl_no_auto_normalization

struct vertdata {
float2 uv : TEXCOORD0;
float2 uvScaled : TEXCOORD1;
float4 vertex : SV_POSITION;
};

struct frag_v2f {
float4 vertex : SV_POSITION;
half2 basePos : TEXCOORD0;
half2 zoomPos : TEXCOORD1;
};

uniform sampler2D _ObstacleTex;
uniform sampler2D _MainTex;
half4 _SunPos;
half _Offset; // frational lerp value. Decrease by powers of two each pass
#ifdef UNITY_HALF_TEXEL_OFFSET
#endif

frag_v2f processVerts (vertdata v){
frag_v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.basePos = v.uv;
o.zoomPos = v.uv * _SunPos.zw + (_SunPos.xy -_SunPos.zw*0.5);
return o;
}

half4 drawFrag (frag_v2f i) : SV_Target {
half firstPass = tex2D(_MainTex, i.basePos).a;
half secondPass = tex2D(_MainTex, lerp(i.zoomPos, i.basePos, _Offset)).a;
half4 output = half4(0,0,0,max(firstPass, secondPass));
return output;
}

ENDCG
}
}

构建工具不提供任何有关错误脚本或行号的详细信息。有关 Unity 后期效果的文档未涵盖任何细节。谷歌没有发现其他人有同样的问题。我发现的唯一一件事是有人建议 SV_POSITION 应该替换为 POSITION,我尝试了各种组合但没有成功。

我还在 Unity 中创建了一个新的图像效果着色器来进行比较,代码看起来是一样的。

为后期效果创建合适的顶点着色器的正确形式是什么?或者这甚至是我的代码的问题?

最佳答案

你很接近! SV_POSITION 语义用于传递给片段着色器的顶点位置。

vertdata 结构中的 SV_POSITION 中删除所有 SV_ 前缀。例如:

struct vertdata
{
float2 uv : TEXCOORD0;
float2 uvScaled : TEXCOORD1;
float4 vertex : POSITION; // <- Remove here
};

struct frag_v2f
{
float4 vertex : SV_POSITION; // <- Keep here
half2 uv : TEXCOORD0;
};

关于unity3d - 从 DX9 到 DX11 的顶点着色器编译错误(Unity 5.6 到 2017.4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49859495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com