Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to repeat texture on Unity 3D object without specifying amount of tiles?

I am trying to make two different objects with the same size of texture / same material, which should be repeating; Without making new material for every new object I create. Below is an example of what I am trying to achieve.

Material A on both objects should look the same, the cube on the right represents how it should look.

like image 513
BloodyGoldfish Avatar asked Aug 31 '25 03:08

BloodyGoldfish


1 Answers

This is fixed and working!

I had to make a Unity shader, which uses world space instead of local space (default). This is the code of a shader I used:

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"
}

Fixed version:

Fixed

Sources of Knowledge I used:

Video tutorial by PushyPixels

Blocks of code taken from this blog

@derHugo set me on the right track, thank you!!

like image 116
BloodyGoldfish Avatar answered Sep 04 '25 05:09

BloodyGoldfish