- Home /
Question by
irrumabote · Dec 05, 2016 at 07:29 AM ·
submesh
Submeshes with LineStrip
Just a simple test to draw two lines using submeshes and MeshTopology.LineStrip. Only the first one (points {0,1,2} ) gets drawn:
void Start()
{
List<Vector3> verts = new List<Vector3>();
verts.Add(new Vector3(-50f, 50f, 0f));
verts.Add(new Vector3(50f, 50f, 0f));
verts.Add(new Vector3(50f, -50f, 0f));
verts.Add(new Vector3(-50f, -50f, 0f));
Mesh newMesh = new Mesh();
newMesh.SetVertices(verts);
newMesh.subMeshCount = 2;
newMesh.SetIndices(new int[] { 0, 1, 2 }, MeshTopology.LineStrip, 0);
newMesh.SetIndices(new int[] { 2, 3, 0 }, MeshTopology.LineStrip, 1);
GameObject testObject = new GameObject("SubMeshTestObject");
MeshRenderer renderer = testObject.AddComponent<MeshRenderer>();
renderer.material = new Material(Shader.Find("Diffuse"));
MeshFilter filter = testObject.AddComponent<MeshFilter>();
filter.mesh = newMesh;
}
What am I doing wrong?
Comment
Answer by MESJM1 · Aug 17, 2019 at 08:10 PM
Try this:
public class LineMaker : MonoBehaviour
{
void Start()
{
List<Vector3> verts = new List<Vector3>();
verts.Add(new Vector3(-50f, 50f, 0f));
verts.Add(new Vector3(50f, 50f, 0f));
verts.Add(new Vector3(50f, -50f, 0f));
verts.Add(new Vector3(-50f, -50f, 0f));
Material Material1 = new Material(Shader.Find("Diffuse"));
Material Material2 = new Material(Shader.Find("Diffuse"));
Mesh newMesh = new Mesh();
newMesh.SetVertices(verts);
newMesh.subMeshCount = 2;
newMesh.SetIndices(new int[] { 0, 1, 2 }, MeshTopology.LineStrip, 0);
newMesh.SetIndices(new int[] { 2, 3, 0 }, MeshTopology.LineStrip, 1);
GameObject testObject = new GameObject("SubMeshTestObject");
MeshRenderer renderer = testObject.AddComponent<MeshRenderer>();
renderer.materials = new Material[] { Material1, Material2 };
MeshFilter filter = testObject.AddComponent<MeshFilter>();
filter.mesh = newMesh;
}
}
Also see:
Your answer
![](https://koobas.hobune.stream/wayback/20220612101944im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Assign new material to submesh? 1 Answer
Best way to split up procedurally generated mesh 0 Answers
Multiple materials for multiple submeshes 3 Answers
Handling large vertex counts. 0 Answers
How to know which material is used by a submesh in script ? 0 Answers