Uniform texture scale based on object

I’m pretty sure this has been already asked, I found some info here and there, but no real solution, or just different type of solutions but that for my case won’t help. So let’s start.

I’m writing an ingame editor to build maps, think of DoomBuilder or something like that. The process of creating meshes isn’t a problem, my main problem is the scaling of textures based on object size. Now to apply the uv map I do a simple calculation considering that for me 1 unity unit is 32 pixels, I calculate the 32 * mesh.localScale.x / texture.width, same for height, and the result goes on the uv (all walls are just planes.

So why that, I just want to keep textures uniform on size giving the user no ability to resize textures, thats a design choice to make all textures even on pixel size, like it would be Doom.

Now the problem with this approach is that when I may end up in the scene with big meshes that have still a localscale of 1, so the above math won’t apply. I then tried to use a shader that take a reference world space coordinates instead of messing with the uv, but failed in this case as Have to take in consideration the texture pixel size, and have no clue on how to get this info in a shader (I’m a noob on shaders).

So how can I go about this? There is another reference on mesh size I can take in account other than scale? Tried also with the bounds, but on a diagonal plane, is not really precise, I was thinking to calculate distance between vertices, but is kinda overkill.

Just as note, I’m not asking for fully functional scripts or anything, just a direction or some thougths on the matter, I’m going insane with this.

EDIT: And please I’m not here to buy assets or anything, so don’t point me to the asset store, I would like to find a solution instead of spending money.

Part of your problem is that you’re tying the texture calculations to localScale. Local scale has no direct correlation to Unity units. Unity units only pertain to the position of vertices. If you don’t have access to the vertices, you won’t be able to calculate out their relation to Unity units.

The localScale values come into play after you’ve already made certain that the original model’s UV coordinates are the correct proportion.

Yeah thats why I’m having problems in this case, I would like another reference, probably like you hinted is to get vertices position in world space and calculate the distance between them from there. Is a thing I should give a try.

Any insight or feedback on the outcome of your tests? I’d like to do pretty much the same thing. I’m thinking of untextured props from Valve Hammer Editor or even for a better example, http://twvideo01.ubm-us.net/o1/vault/gdc07/slides/S3736i1.pdf on page 32

Not really, just theory for now. I was thinking to calculate the distance between vertices, then take the higher number and use it as base for uv calculation, but I have no code yet for this.