团结 - 如何使材料双面

时间:2017-02-15 15:18:47

标签: unity3d

搜索问题提供了许多解决方案,但在我的Unity3D 5.4中,它们由于某种原因不起作用。喜欢 camera inside a sphere

我在Unity编辑器中的材料中看不到cull和/或sides。 在C#

rend = GetComponent<Renderer>();
mater = rend.material;
rend.setFaceCulling( "front", "ccw" );
mater.side = THREE.DoubleSide;

不提供此类setFaceCullingside属性。

如何制作双面材料?

5 个答案:

答案 0 :(得分:8)

您需要使用自定义着色器才能使用Cull Off启用双面材质 最简单/最快速的测试方法是在编辑器中创建一个新的Standard Surface Shader并打开它。在Cull Off下面添加第LOD 200行。

现在要考虑的一件事是闪电不能正确渲染背面。如果你想要,我建议你做两面模型。

答案 1 :(得分:1)

使用或创建带有 Cull Off 的着色器。 一个简单的2面着色器:

Shader "Custom/NewSurfaceShader" {
    Properties {

    }
    SubShader {
         Cull off   
         Pass {
             ColorMaterial AmbientAndDiffuse
         }

    }
}

答案 2 :(得分:1)

只需创建一个 Unlitshader 并对其进行编辑:

<块引用> <块引用>

你应该写下 Cull off LOD 100 然后将它拖到一个新的材质上并设置一张图片进行测试——现在将材质拖到一个对象上。 闪电会正确渲染!!! (我的统一是2019.4)

enter code here

Shader "Unlit/unlit"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100
        Cull off
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog
            #include "UnityCG.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };
            sampler2D _MainTex;
            float4 _MainTex_ST;
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        } }}

答案 3 :(得分:0)

也许我对您的 Unity 版本的回答不起作用,但这里是下图中 HDRP 中较新版本的解决方案
enter image description here

答案 4 :(得分:0)

您可以使用带有剔除的自定义表面着色器,但相对的面将无法获得正确的光线,因为法线仅对正面有效,对于背面,法线与面相反.如果你想让背面像正面一样处理,又不想做一个双面网格的模型来消耗双倍内存,你可以画2个Passes,1个Pass< /strong> 用于正面,1 用于背面,您可以在 顶点着色器 中为每个顶点反转法线。您可以使用Cull backCull front

SubShader
{
     //Here start first Pass, if you are using standard surface shader passes are created automated, 
     //else you should specify Pass {  }
     Tags { ... }
     LOD 200
     Cull Back

     ...
     struct Input 
     {
         ...
     }
     ...

     ...
     //or vert & frag shaders
     void surf(Input IN, inout SurfaceOutputStandard p)
     {
           //processing front face, culling back face
           ...
     }
     ...

     //Here start second pass put automated by unity
     Tags {...}
     LOD 200
     Cull Front

     #pragma vertex vert
     #pragma surface ...

     ...
     struct Input 
     {
         ...
     }
     ...

     void vert(inout appdata_full v)
     {
           v.normal = -v.normal;//flip
     }

     void surf(Input IN, inout SurfaceOutputStandard p)
     {
           //processing back face, culling front face
           ...
     }
}