Small Submesh Example
After three days of frustration, I finally got a subMesh example working. May not be the cleanest code, and if you see any improvements, please reply with them. An empty GameObject is created C# script is added. A Resources folder is added to the assets, and two images are placed into it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
//---------- class SubMeshScript
public class SubMeshScript : MonoBehaviour {
Material[] mat = new Material[2];
Vector3[] vertices = new Vector3[8];
Vector2[] uvs = new Vector2[8];
Vector2[] uvs2 = new Vector2[8];
Renderer rend;
Mesh mesh;
int[][] triDex = new int[2][];
//---------- Start
void Start () {
mesh = GetComponent<MeshFilter> ().mesh;
mesh.Clear ();
rend = GetComponent<Renderer> ();
mat [0] = new Material (Shader.Find("Unlit/Texture"));
mat [1] = new Material (Shader.Find("Unlit/Texture"));
mat[0].mainTexture = Resources.Load ("colorTest") as Texture2D;
mat[1].mainTexture = Resources.Load ("MyTest") as Texture2D;
rend.materials = mat;
//----------
vertices [0] = new Vector3 (0f, 0f, 0f);
vertices [1] = new Vector3 (0f, 0f, 8f);
vertices [2] = new Vector3 (0f, 8f, 0f);
vertices [3] = new Vector3 (0f, 8f, 8f);
vertices [4] = new Vector3 (8f, 0f, 0f);
vertices [5] = new Vector3 (0f, 0f, 0f);
vertices [6] = new Vector3 (8f, 8f, 0f);
vertices [7] = new Vector3 (0f, 8f, 0f);
//----------
uvs [0] = new Vector2 (0f, 0f);
uvs [1] = new Vector2 (1f, 0f);
uvs [2] = new Vector2 (0f, 1f);
uvs [3] = new Vector2 (1f, 1f);
uvs [4] = new Vector2 (0f, 0f);
uvs [5] = new Vector2 (1f, 0f);
uvs [6] = new Vector2 (0f, 1f);
uvs [7] = new Vector2 (1f, 1f);
// y mesh0
// 7 2 ------ 3
//mesh1 / | | |
// / | | |
// 6 5 0 ------ 1 z
// | /
// | /
// 4
// x
mesh.subMeshCount = 2;
mesh.vertices = vertices;
mesh.uv = uvs;
triDex [0] = new int[6] { 0, 3, 1, 0, 2, 3 };
triDex [1] = new int[6] { 4, 7, 5, 4, 6, 7 };
mesh.SetTriangles (triDex[0], 0);
mesh.SetTriangles (triDex[1], 1);
mesh.RecalculateNormals();
}//Start
}//class SubMeshScript
looks fine. Couple suggestions:
Shader.Find("Unlit/Texture")
only needs to called once. Store the value in a variable, and pass it to both your material creation functions.
Also, I would avoid using constant values (for flexibility), and would try to use named shortcuts, when possible (for readability). e.g.
public float size=8.0f; //default value of 8, but can be changed.
//now we can write vertices [1] = new Vector3 (0f, 0f, 8f); as
vertices [1] = Vector3.forward * size;
(https://docs.unity3d.com/ScriptReference/Vector3-forward.html)
(Then again, I would also use size=1.0f, and ins$$anonymous$$d let the $$anonymous$$eshFilter/$$anonymous$$eshRederer object's transform do the scaling.)
Same for the UV's
new Vector2 (1f, 0f);
is equivalent to Vector2.right
FYI: This Q would probabalybe better suited for the forum. UA is generally for finding solutions, rather than code review.
Thank you so much. Your answer is the actual useful one on the entire internet on this subject. Love you my boy =).
thanks man, I got stuck too and it helped me understand the idea.
Answer by zeropage · Dec 15, 2018 at 09:49 PM
@kendavistoo - thanks for posting this. Super helpful.
Your answer
