【发布时间】:2021-08-12 08:51:26
【问题描述】:
我正在尝试将飞机分成几个部分,并用不同的颜色填充每个部分。 (如德国国旗)
我以为我可以使用“for循环”来完成这项工作,但我选择了错误的工具。
我将着色器应用到平面后,Unity 就会卡住。
因为我是 Shader 的新手,所以我找不到任何方法来解决这个问题。
希望你告诉我如何分割平面,并用不同的颜色填充它们。
这是我的着色器代码。
'''Shader "Custom/NewSurfaceShader"
{
Properties
{
_UR("Upper Red", Range(0,1)) = 0.0
_UG("Upper Green", Range(0,1)) = 1.0
_UB("Upper Blue", Range(0,1)) = 1.0
_UA("Upper Alpha", Range(0,1)) = 1.0
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
fixed _UR;
fixed _UG;
fixed _UB;
fixed _UA;
fixed4 _AlterColor;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
_AlterColor = fixed4(_UR, _UG, _UB, _UA);
fixed4 c;
for (int i = 0; i < 1920; i++) {
for (int j = 0; j < 2160; j++) {
fixed2 hv = fixed2(i / 1920, j / 2160);
if (hv.x < 0.5) {
c = tex2D(_MainTex, (hv))*_AlterColor;
}
else
c = tex2D(_MainTex, (hv)) * _Color;
}
}
o.Emission = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
'''
【问题讨论】:
-
统一卡住时出现错误。由于设备重置/删除,无法呈现 D3D11 交换链。如果您将非常昂贵的工作负载绘制或分派到 GPU,则可能会发生此错误,这可能会导致 Windows 检测到 GPU 超时并重置设备。(请参阅docs.microsoft.com/en-us/windows-hardware/drivers/display/…)。如果您认为此错误是由于 Unity 内置功能造成的,请提交错误。这是一个不可恢复的错误,编辑器将关闭。