- Home /
How to slice texture for 3D models?
hi, I have a simple cube with a darker edge outline. When i scale the cube in Y axis, the outline in top edge also gets stretched. I want its edge to be of initial width. Is there any workaround for this? I am using a simple black and white texture. In 3D, is there something similar to 9 slice method we use in 2D sprites to efficiently handle textures while scaling?
Answer by ArseneySorokin · Jun 09, 2020 at 08:06 AM
The easiest ways would be to set three cubes and stretch only the middle one. Otherwise you will have to morph the vertices of your 3D model, which is far more complicated.
Answer by BastianUrbach · Jun 09, 2020 at 09:26 AM
You could make a "9 slice-aware mesh" with edge loops marking the borders, then scale it by only expanding the middle portion but not the borders. Since texture coordinates are stored in vertices and simply interpolated linearly across the faces, this should scale the texture as intended:
The problem with this is that unity doesn't have this functionality built-in but there are a couple other ways to achieve it:
Write a script that scales the mesh correctly by manipulating the mesh's vertex array
Like 1. but do it in a vertex shader instead of a script
If it's static, simply build the entire level or the relevant part of it in a 3d modelling application (where this kind of manipulation would be easy) instead of the Unity editor
Like 3. but maybe you could use the ProBuilder Package to do it inside the Unity Editor (not sure if this works, I haven't used it yet but I would expect it to be able to do that kind of thing)
Use a skinned mesh and a skinned mesh renderer where each side has it's own bone which you can then move independently.
Use blend shapes to interpolate between different versions of the mesh