我正在尝试制作两个具有相同尺寸纹理/相同材质的不同物体,这应该是重复的;无需为我创建的每个新对象制作新材料。下面是我想要实现的目标的示例。
两个对象上的材质 A 应该看起来相同,右侧的立方体代表它应该是什么样子。
这是固定的并且可以工作!
我必须制作一个 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"
}
Run Code Online (Sandbox Code Playgroud)
固定版本:
我使用的知识来源:
@derHugo 让我走上了正轨,谢谢!!