【问题标题】:Unity - how to make material double sidedUnity - 如何使材料双面
【发布时间】:2022-04-03 18:41:33
【问题描述】:

搜索问题提供了许多解决方案,但由于某种原因,它们在我的 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 属性。

如何制作双面材料?

【问题讨论】:

    标签: unity3d


    【解决方案1】:

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

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

    【讨论】:

    • 感谢分享!在我的尝试中,它给了背面一种透视,几乎透明的感觉,它后面的每种颜色都有点颠倒。
    • 是的,这不会正确渲染灯光,即灯光必须被镜像!
    • 如果您只是将视图方向应用于法线,照明将正常工作。 ie:基于普通点viewdir,正常翻转。这个想法是法线只在一个方向上是正确的 - 但你基本上需要来自前后视图的照明。
    【解决方案2】:

    使用或创建一个着色器

    剔除

    在这个简单的 2 面着色器中可以看到:

    Shader "Custom/NewSurfaceShader" {
        Properties {
            
        }
        SubShader {
             Cull off   
             Pass {
                 ColorMaterial AmbientAndDiffuse
             }
            
        }
    }
    

    【讨论】:

    • 与之前的答案基本相同,但从默认着色器开始可能更有用。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      只需创建一个 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
              } }}
      

      【讨论】:

        【解决方案5】:

        您可以使用带有 Cull Off 的自定义表面着色器,但相对的面将无法获得正确的光线,因为法线仅对正面有效,对于背面,法线与面相反.如果您希望将背面视为正面,并且不想制作具有双面网格的模型以消耗双倍内存,您可以绘制 2 个 Passes、1 个 Pass 表示正面,1 表示背面,您可以在 vertex shader 中为每个顶点反转法线。您可以使用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
                   ...
             }
        }
        

        【讨论】:

          【解决方案6】:

          此外,在 Unity 2020.3 中,URP/Lit 着色器有一个渲染面选项。

          【讨论】:

            猜你喜欢
            • 2011-09-03
            • 2011-02-05
            • 1970-01-01
            • 2021-05-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多