Material texture tile with object scale

I’m trying to make a simple level editor for a game. To make the walls and grounds i use simple cube that I rescale. I want the material to tile with the scale of the cube so it doesn’t look stretched.
I have no idea how to do it.
I know I can modify the tiling of the material but I have 2 problem with that, first is that i need to create a material for each potential size, and the second is that this tiling is only on 2 axis, so there are two face well tiled and the third one completely strectched.
Ex :

I’ve heard I can do it with shaders but I know absolutely nothing in that.
Thanks for your help.

Okay so afer a bit of research I think that i found what I need, it’s called an Object Space Triplanar shader, but i can’t find how to do one. I’ve found how to do a world space triplanar shader like so

It make the texture tile perfectly, but if the cube is not perfectly aligned, the texture has an offset

9774393--1401075--upload_2024-4-16_17-34-26.png

I’ve tried doing that to make it an object space triplanar, but it did’nt work and the texture was back to stretching

9774393--1401078--upload_2024-4-16_17-36-15.png

So if someone know how I can do it, your welcome !

Your object-space shader works perfectly, but scaling (like in a transform) happens outside of object space. There is no way to get a purely object-space mapping to work how you want.

I suppose you could write a script to pass the scale to the shader as a parameter and you could multiply by that or whatever. I’m not sure what sort of math you need but you need to take either the scale or the world-space into account.

Scaling is kind-of a fudge, though. You’d be better off creating meshes procedurally if you can manage it.

I’ve never touched procedural mesh generation in unity, do you know something where I could learn how to do what I want ?

If you want to do it all yourself:

Although I imagine the UnityEngine.Probuilder API probably has some tools that would make it a lot easier. You need to have Probuilder installed, btw.

https://docs.unity3d.com/Packages/com.unity.probuilder@4.0/manual/api.html

For example, if your level editor is only meant to make simple, rectangular walls or platforms you could use ShapeGenerator.GeneratePrism and create a box with arbitrary dimenstions. I think it should generate proportional U,Vs even, if Probuilder works like I’m remembering.
https://docs.unity3d.com/Packages/com.unity.probuilder@4.0/api/UnityEngine.ProBuilder.ShapeGenerator.html

I’ve also found this script that appears to do exactly what I want, but as I’ve not seen anyone mention this way of doing, I was wondering if it was a good way for aproaching the problem. As I understand, it modifies the UV of the cune to match its scale

Oh I didn’t know I could use probuilder in script, I’ll try taking a look at that too

That should work fine too. If you use this on a mesh that you’ve imported in to your project, you’ll need to check the checkbox that makes the mesh read/writable in the import settings.

Okay I see, I’ll make sure to remember that.
Thank you for all your help !