5

我正在使用文本网格将文本放置在 3D 对象上,但众所周知,文本网格没有任何法线......

http://docs.unity3d.com/Documentation/Components/class-TextMesh.html

...所以它不能正确点亮。我进行了搜索,发现很多人在照明 3D 文本网格时遇到问题,因为它没有任何法线,但我还没有找到将法线添加到文本网格对象的解决方案,所以这是我的问题。

如何向文本网格添加法线以使其正确点亮?

非常感谢您的智慧!

4

1 回答 1

6

我做了类似的事情来照亮 3D 纹理。我希望我的回答不会矫枉过正。所以这段代码是我不久前写的一个hack,它效率低下,只支持一个定向光(有用的cg lighting tuts here)。希望这足以让您入门。

要使用我下面的代码创建 3D 纹理,您必须完成更多工作:

  1. 创建一个空的游戏对象
  2. 附加组件->网格->文本网格
  3. 附加组件 TextMeshNormals(包括在下面)
  4. 附加组件->网格->网格渲染器
  5. 将材质分配给使用下面我的 GUI/LitText 着色器的网格渲染器

在着色器中,您会注意到一个文本法线属性。在实践中,您可以让 gameObject 的 Update() 方法_Normal使用文本所面对的方向更新属性,以便它反映方向的变化。文本是平面的,所以我们只需要 1 个法线。为了测试,我手动将法线设置为 (0,0,-1,1),因为默认的文本网格朝下 -Z。

因为此脚本不在编辑器中运行,所以在您在预览中运行场景之前,您的文本不会显示。

着色器:

Shader "GUI/LitText" { 
Properties { 
   _MainTex ("Font Texture", 2D) = "white" {} 
   _Color ("Text Color", Color) = (1,1,1,1) 
   _Normal ("Text Normal",Vector) = (0,0,0,1)
} 

SubShader {
    Blend SrcAlpha OneMinusSrcAlpha
    Pass { 
        Color [_Color] 
        SetTexture [_MainTex] { 
            combine primary, texture * primary 
        } 
    } 
    pass {
         Tags { "LightMode" = "ForwardBase" } 

         CGPROGRAM

         #pragma vertex vert  
         #pragma fragment frag 

         uniform sampler2D _MainTex;  
         uniform float4 _Color; // define shader property for shaders
         uniform float4 _Normal;

         // The following built-in uniforms (apart from _LightColor0) 
         // are defined in "UnityCG.cginc", which could be #included 
         uniform float4 unity_Scale; // w = 1/scale; see _World2Object
         uniform float3 _WorldSpaceCameraPos;
         uniform float4x4 _Object2World; // model matrix
         uniform float4x4 _World2Object; // inverse model matrix 
            // (all but the bottom-right element have to be scaled 
            // with unity_Scale.w if scaling is important) 
         uniform float4 _WorldSpaceLightPos0; 
            // position or direction of light source
         uniform float4 _LightColor0; 
            // color of light source (from "Lighting.cginc")

         struct vertexInput {
            float4 vertex : POSITION;
            float3 normal : NORMAL;
            float4 texcoord : TEXCOORD0;
         };
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float4 col : COLOR;
            float4 tex : TEXCOORD0;
         };

         vertexOutput vert(vertexInput input) 
         {
            vertexOutput output;

            float4x4 modelMatrix = _Object2World;
            float4x4 modelMatrixInverse = _World2Object; 

            float3 normalDirection = normalize(float3(mul(_Normal, modelMatrixInverse)));
            float3 lightDirection = normalize(float3(_WorldSpaceLightPos0));

            float3 diffuseReflection = float3(_LightColor0) * float3(_Color)
               * max(0.0, dot(normalDirection, lightDirection));

            output.col = float4(diffuseReflection, 1.0);
            output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
            output.tex = input.texcoord;
            return output;
         }

         float4 frag(vertexOutput input) : COLOR
         {
            half4 color = tex2D(_MainTex, float2(input.tex));
            // use color.a to get alpha from text texture, rgb comes from vertex shader                        
            return float4(input.col.r,input.col.g,input.col.b,color.a);
         }

         ENDCG
    }
}
}

和帮助脚本:

public class TextMeshNormals : MonoBehaviour {
    private TextMesh textMesh;

    // Use this for initialization
    void Start () {
        // reassign font texture to our material
        textMesh = transform.GetComponent<TextMesh>();
        renderer.material.mainTexture = textMesh.font.material.mainTexture;
    }
}

更新 Unity 4.5.X 使用这个稍微更新的版本:

Shader "GUI/LitText" { 
Properties { 
   _MainTex ("Font Texture", 2D) = "white" {} 
   _Color ("Text Color", Color) = (1,1,1,1) 
   _Normal ("Text Normal",Vector) = (0,0,0,1)
} 

SubShader {
    Blend SrcAlpha OneMinusSrcAlpha
    Pass { 
        Color [_Color] 
        SetTexture [_MainTex] { 
            combine primary, texture * primary 
        } 
    } 
    pass {
         Tags { "LightMode" = "ForwardBase" } 

         CGPROGRAM

         #pragma vertex vert  
         #pragma fragment frag 

         uniform sampler2D _MainTex;  
         uniform float4 _Color; // define shader property for shaders
         uniform float4 _Normal;
         uniform float4 _LightColor0; 
             // color of light source (from "Lighting.cginc")
         struct vertexInput {
            float4 vertex : POSITION;
            float3 normal : NORMAL;
            float4 texcoord : TEXCOORD0;
         };
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float4 col : COLOR;
            float4 tex : TEXCOORD0;
         };

         vertexOutput vert(vertexInput input) 
         {
            vertexOutput output;

            float4x4 modelMatrix = _Object2World;
            float4x4 modelMatrixInverse = _World2Object; 

            float3 normalDirection = normalize(float3(mul(_Normal, modelMatrixInverse)));
            float3 lightDirection = normalize(float3(_WorldSpaceLightPos0));

            float3 diffuseReflection = float3(_LightColor0) * float3(_Color)
               * max(0.0, dot(normalDirection, lightDirection));

            output.col = float4(diffuseReflection, 1.0);
            output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
            output.tex = input.texcoord;
            return output;
         }

         float4 frag(vertexOutput input) : COLOR
         {
            half4 color = tex2D(_MainTex, float2(input.tex));
            // use color.a to get alpha from text texture, rgb comes from vertex shader                        
            return float4(input.col.r,input.col.g,input.col.b,color.a);
         }

         ENDCG
    }
}
}
于 2013-04-27T05:53:13.447 回答