- Home /
Best way to generate texture coordinates?
I'm generating some meshes at runtime (actually reading from a file), which may or may not have texture coords. I assign the Transparent/Specular map to the objects. In some cases I get this on my console:
Shader wants texture coordinates, but the mesh doesn't have them UnityEditor.DockArea:OnGUI() Shader wants secondary texture coordinates, but the mesh doesn't have any UnityEditor.DockArea:OnGUI()
So, do I have to generate texture coordinates?, and secondary ones (???) Is there any facility for this in Unity, if not, any good resources on how to generate these? Thanks
you only NEED to to assign UV coordinates for shaders that actually USE them. I suspect that Shaders that do not take a texture will not generate this error. I'd bet it's because there are so many different ways one can choose to map textures to a shape, that there is no default function for this (like there is for say.. RecomputeNormals) ooo... http://wiki.unity3d.com/index.php/Scripts/General... check out the section "mesh helpers"
Answer by Bunny83 · Jan 27, 2011 at 08:58 AM
The example shown at Mesh.uv does exactly what you want. You may need to assign .uv2 too.
ps. Read more Unity Script Reference ;)
Just to be clear on that: Doing what they did in the example will just add UVs to your mesh but they won't fit any unwrap you might have done for those meshes. As long as you don't use textures or when it's not important that they don't fit on the triangles, you'll be fine
Ah I should have been more precise (and that script reference has been my daily bible this past year or more!). I would like to generate something of a default projection, such that any given face will have one 'tile' stretched across it (any direction, but not distorted). Or even better, if all faces had the same sized tile. Like if I'm importing a building with a brick texture, that those bricks would tile across all faces where uv's are undefined. I imagine this would entail a) getting the extents (bounds) of each face, b) (optional) deter$$anonymous$$e tile size, c) generate uv per vertex, (more..)
probably aligning to either local or world x/z plane, or first segment on the face.
hmm you talk about buildings with simple geometry? If your faces are always quads (2 triangles) you could calculate the uvs according to the position of the vertices on the face-plane, but when you have unorderd triangles it's getting difficult. As long as your building is aligned to the 3 axis (parallel to the axis) you could try what you've just mentioned. $$anonymous$$inda project the geometry onto your texture to get the uvs ;)
Yeah, that's what I was thinking. Simple case of a unit cube aligned to world axes, trivial. Same cube, rotated, there must be a simple transform I could use to get those UV's. On a non-unit cube, I'd be scaling the UV's according to the size of the bounding square in the plane of the triangle. I was hoping there was some handy utility for this, but if it needs to be written, by golly, that's what I'll do (I actually did something similar years ago for an Unreal Editor exporter)