【问题标题】:Unity: Apply shader to object to curve its textureUnity:将着色器应用于对象以弯曲其纹理
【发布时间】:2019-04-09 16:46:05
【问题描述】:

我正在 Unity 中制作一个无尽的跑步游戏,我想像本教程中一样添加一个弯曲着色器 https://youtu.be/6_e_GoWlZOo

但是当我将该材质添加到一个对象时,它会替换旧的。当我将它作为第二个添加到网格渲染器中时,它不会影响第一个。 https://drive.google.com/open?id=1-kP4A0Xh3P9xVjrwjTiTPlh_WcHnBA1d

这是着色器代码:

Shader "PPP/BendWorld"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Curvature("Curvature", Float) = 0.001
    }
        SubShader
        {
            Tags { "RenderType" = "Opaque" }
            LOD 200

            CGPROGRAM
            #pragma surface surf Lambert vertex:vert addshadow
            uniform sampler2D _MainTex;
            uniform float _Curvature;

            struct Input 
            {
                float uv_MainTex;
            };

            void vert(inout appdata_full v) 
            {
                float4 worldSpace = mul(unity_ObjectToWorld, v.vertex);
                worldSpace.xyz -= _WorldSpaceCameraPos.xyz;
                worldSpace = float4(0.0f, (worldSpace.x * worldSpace.x) * -_Curvature, 0.0f, 0.0f);

                v.vertex += mul(unity_WorldToObject, worldSpace);
            }

            void surf(Input IN, inout SurfaceOutput o) 
            {
                half4 c = tex2D(_MainTex, IN.uv_MainTex);
                o.Albedo = c.rgb;
                o.Alpha = c.a;
            }

            ENDCG
    }
            Fallback "Mobile/Diffuse"
}

我想把物体弯曲并在上面贴上纹理,有什么办法吗?

【问题讨论】:

  • 着色器有它自己的纹理属性。因此,您应该将此着色器与您想要的纹理一起添加到对象 - 制作新材质。正如您所注意到的,向对象添加两个材质/着色器将不起作用。 (对不起,如果我误解了这个问题。)
  • 那么我应该为每个具有纹理的对象创建新的着色器材质吗?我已经试过了,但是纹理坏了这是普通纹理drive.google.com/open?id=1mipTfiobyB7PDTT6qiw9udX9HgiA2zZ9这是与具有相同纹理的着色器drive.google.com/open?id=1uMafa5qbh-g4gFh6E1SHsJ3-McJjJG8x
  • 那是带流动水的水着色器吗?如果是这样,你需要做一些工作。然后您必须修改该着色器以使用曲率。您不能只是添加另一个着色器并期望它们一起工作。
  • 纹理已损坏,因为您将 uv_MainTex 定义为 float 而不是 float2。 UV 坐标是二维的。
  • @KalleHalvorsson 非常感谢,它有效

标签: unity3d textures shader


【解决方案1】:

关于纹理问题:纹理看起来已损坏,因为您将 uv_MainTex 定义为 float 而不是 float2。 UV 坐标是二维的 - 如果将其定义为标量,它将仅使用 UV 的 U 轴。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    相关资源
    最近更新 更多