【问题标题】:Unity3d - How to fill plane divided into two with two different color each?Unity3d - 如何用两种不同的颜色填充一分为二的平面?
【发布时间】: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 内置功能造成的,请提交错误。这是一个不可恢复的错误,编辑器将关闭。

标签: unity3d shader


【解决方案1】:

实际上有一种更简单的方法可以解决这个问题。

尝试使用纹理将旗帜颜色应用于您的飞机。只需导入应在平面上显示的标志图像,并在导入设置检查器中将其标记为纹理。之后,您应该能够将纹理拖放到名为例如的材质上。 “旗帜材料”。将材料应用到飞机上,就完成了。

如果由于某种原因这不起作用,您可以轻松地将调色板导入 Blender,将其细分多次,这样您就有了三个面,然后转到 Shading,然后进入 编辑模式 face 选择您要绘制的面并在左侧的 Shading Tab 中指定颜色(默认)。 将其导入 Unity(参见工作流程)并将其放入您的场景中。

希望这会有所帮助。??

【讨论】:

    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    相关资源
    最近更新 更多