How can I check if the UV forms a triangle, and get their UV-coordinates?
I have an FBX I have imported from the Blenders, and that FBX has a lot of individually unwrapped triangles that are assigned to coloured squares on the texture. By using texture offset I can change the palette of the mesh. However, I want to be able to offset just parts of the unwrapped UV on the texture.
The things I have looked at are here:
https://docs.unity3d.com/ScriptReference/Mesh-uv.html https://docs.unity3d.com/ScriptReference/Mesh-subMeshCount.html https://answers.unity.com/questions/46788/painting-on-a-texture-without-crossing-mesh-triang.html?childToView=46816#answer-46816 https://answers.unity.com/questions/1105729/find-uv-coordinates-of-mesh-without-a-raycast.html
I found that UV is a Vector2, but to get an area you can texture, you need an area, which is a Vector3. So I want to be able to find which UVs on the texture fall within a certain area, and change their offset position, leaving the rest in their current positions.
It would seem to be possible to do, but this is not my area of expertise. Thank you all very much in advance for helping me out.
Answer by nyanpath · Sep 03, 2018 at 02:12 PM
I solved this myself. The example code to offset one triangle is as follows (this uses a tetrahedron as a base mesh imported from an .FBX into the Unities):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PartiallyOffsetUV : MonoBehaviour {
private Mesh _pacito;
private Vector3[] _vertices;
private Vector2[] _uvs;
void Start ()
{
_pacito = GetComponent<MeshFilter>().mesh;
_vertices = _pacito.vertices;
_uvs = new Vector2[_vertices.Length];
for (int c = 0; c < _uvs.Length; c++)
{
Debug.Log(_pacito.uv[c].x + " : " + _pacito.uv[c].y);
//if (c > 2 && c < 6)
if (_pacito.uv[c].x < 0.5f && _pacito.uv[c].y < 0.5f)
{
_uvs[c] = new Vector2(_pacito.uv[c].x + 0.5f, _pacito.uv[c].y);
}
else
{
_uvs[c] = new Vector2(_pacito.uv[c].x, _pacito.uv[c].y);
}
}
_pacito.uv = _uvs;
}
}
Your answer
Follow this Question
Related Questions
Assigning uv's to multiple faces 1 Answer
Help setting UV coordinates for code-generated plane 0 Answers
Animated rigged blender model generating colliders problem 1 Answer
Tiling a noise texture 1:1 over each triangle without creating extra vertices 1 Answer
Mesh.uv assign in Unity 5.6 is 5x slower than in 5.5 0 Answers