Hello! Ive been looking through unity references and googling my brains out trying to figure out how to do this. Any help would be greatly appreciated!
Here is an example of what I would like to do.
I have a webcamTexture being applied to the Texture of a 10x10 scaled quad gameObject. I would like to take this texture and cut it up into 100 small game objects.
The problem I am having is how to apply just the section of the image I want into a new Texture. How would I make this Texture into a grid of 10x10 and access each grid of 1x1?
Again thanks for reading this and if I did poorly explaining my situation please let me know and Ill try to go into more detail.
Just to get it right: You want 10x10 game objects, where each game object has its own MeshRenderer and MeshFilter and each mesh filter has its own Mesh (a quad) and each quad shows a specific part (Mesh.uv) of the webcam texture.
So all you need is to create a new mesh with the proper texture coordinates and assign it to the mesh filter of each game object. It’s pretty straight forward.
I haven’t tested it but basically you want something like this:
Mesh CreateQuad( Vector3 normal, Vector3 right, Vector2 size, Vector2 uvFrom, Vector2 uvTo, Vector3 positionOffset, Color color )
{
Vector3 up = -Vector3.Cross( normal, right );
right = right * size.x * 0.5f;
up = up * size.y * 0.5f;
Vector3[] vertices = new Vector3[ 4 ];
vertices[ 0 ] = -right - up + positionOffset;
vertices[ 1 ] = right - up + positionOffset;
vertices[ 2 ] = right + up + positionOffset;
vertices[ 3 ] = -right + up + positionOffset;
int[] triangles = new int[ 6 ];
triangles[ 0 ] = 0;
triangles[ 1 ] = 3;
triangles[ 2 ] = 1;
triangles[ 3 ] = 1;
triangles[ 4 ] = 3;
triangles[ 5 ] = 2;
Vector2[] uvs = new Vector2[ 4 ];
uvs[ 0 ] = uvFrom;
uvs[ 1 ] = new Vector2( uvTo.x, uvFrom.y );
uvs[ 2 ] = uvTo;
uvs[ 3 ] = new Vector2( uvFrom.x, uvTo.y );
Color[] colors = new Color[ 4 ];
for ( int i = 0; i < 4; ++i )
{
colors[ i ] = color;
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
mesh.colors = colors;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
return mesh;
}
BTW, if you only want quads that are always “camera-aligned”, it might be totally sufficient to use sprites that you create at runtime.
Then each sprite would show a specific part of your webcam texture. Have a look at Sprite.Create.
(Sorry for kinda spamming your thread.)