首页 > 解决方案 > 有没有办法在 Unity 3D 对象上重复纹理而不指定瓷砖数量?

问题描述

我正在尝试使用相同大小的纹理/相同材质制作两个不同的对象,这应该是重复的;无需为我创建的每个新对象制作新材料。下面是我想要实现的一个例子。

两个对象上的材料 A 看起来应该是一样的,右边的立方体代表它应该看起来的样子。

标签: unity3d

解决方案


这是固定的并且有效!

我必须制作一个 Unity 着色器,它使用世界空间而不是本地空间(默认)。这是我使用的着色器的代码:

Shader "Legacy Shaders/Diffuse - Worldspace" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Scale("Texture Scale", Float) = 1.0
}
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200

CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;
fixed4 _Color;
float _Scale;

struct Input {
    float3 worldNormal;
    float3 worldPos;
};

void surf (Input IN, inout SurfaceOutput o) {
    float2 UV;
    fixed4 c;

    if (abs(IN.worldNormal.x) > 0.5) {
        UV = IN.worldPos.yz; // side
        c = tex2D(_MainTex, UV* _Scale); // use WALLSIDE texture
    }
    else if (abs(IN.worldNormal.z) > 0.5) {
        UV = IN.worldPos.xy; // front
        c = tex2D(_MainTex, UV* _Scale); // use WALL texture
    }
    else {
        UV = IN.worldPos.xz; // top
        c = tex2D(_MainTex, UV* _Scale); // use FLR texture
    }

    o.Albedo = c.rgb * _Color;
}
ENDCG
}

Fallback "Legacy Shaders/VertexLit"
}

固定版本:

固定的

我使用的知识来源:

PushyPixels 的视频教程

取自此博客的代码块

@derHugo 让我走上了正轨,谢谢!!


推荐阅读