private void GenerateMeshes() { if (msh == null) { msh = new Mesh(); } CombineInstance[] combine = new CombineInstance[Paths.Count]; int it = 0; foreach (GenPath path in Paths) { SVGGenerals.OptimizePoints(ref path.points); // Use the triangulator to get indices for creating triangles Triangulator tr = new Triangulator(path.points); int[] indices = tr.Triangulate(); // Create the Vector3 vertices Vector3[] vertices = new Vector3[path.points.Length]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new Vector3(path.points[i].x, path.points[i].y, 0); } // Create the mesh Mesh _msh = new Mesh(); _msh.vertices = vertices; _msh.triangles = indices; if (fillColor != null) { // Use vertex colors Color[] colors = new Color[path.points.Length]; for (int i = 0; i < colors.Length; i++) { colors[i] = new Color(fillColor.R, fillColor.G, fillColor.B, fillOpacity); } _msh.colors = colors; } //Normals Vector3[] normals = new Vector3[path.points.Length]; for (int i = 0; i < normals.Length; i++) { normals[i] = new Vector3(0, 0, 1); } _msh.RecalculateBounds(); combine[it].mesh = _msh; it++; } msh.CombineMeshes(combine, false, false, false); }
public override void Render(SVGElement parent, Material baseMaterial, onRenderCallback cb) { int i = 0; Paths = new List <GenPath>(); generatedPoints = new List <Vector2>(); foreach (SVGPathSeg seg in segList) { i++; Vector2[] newPoints = seg.GetPoints(SVGGenerals.pathNSegments); if (generatedPoints.Count > 0 && newPoints != null) { if (newPoints[0] == generatedPoints[generatedPoints.Count - 1]) { generatedPoints.RemoveAt(generatedPoints.Count - 1); Debug.Log("Remuevo duplicado!"); } } if (newPoints != null) { generatedPoints.AddRange(newPoints); } if (seg.GetType() == typeof(SVGPathSegClose)) { GenPath path = new GenPath(); SVGGenerals.OptimizePoints(ref generatedPoints); path.points = generatedPoints.ToArray(); path.closed = true; Paths.Add(path); generatedPoints.Clear(); } } if (generatedPoints.Count > 0) { GenPath path = new GenPath(); SVGGenerals.OptimizePoints(ref generatedPoints); path.points = generatedPoints.ToArray(); path.closed = false; Paths.Add(path); generatedPoints.Clear(); } if (Paths.Count == 0) { Debug.LogWarning("Un path sin segmentos?"); return; } vectors_2d = Paths[0].points; //foreach (Vector2 vec in vectors_2d) { // Debug.Log(name + "-Vector:" + vec); //} //base.Render(parent, baseMaterial); if (_gameobject == null) { _gameobject = new GameObject(name); _gameobject.AddComponent(typeof(MeshRenderer)); if (parent != null) { _gameobject.transform.parent = parent.gameObject.transform; } } GenerateMeshes(); if (filter == null) { filter = gameObject.GetComponent <MeshFilter>(); } if (filter == null) { filter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter; } filter.mesh = msh; if (renderer == null) { renderer = gameObject.GetComponent <Renderer>(); } //renderer.sharedMaterials[0] = baseMaterial; renderer.sharedMaterial = baseMaterial; //renderer. = "USVG"; renderer.sortingOrder = element_number * 2; //renderer.material.name = this.name + "-material"; //renderer.material.color = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value); //if (fillColor != null) { // renderer.material.color = new Color(fillColor.R, fillColor.G, fillColor.B, fillOpacity); // renderer.material.renderQueue = 3000 + element_number; //} if (cb != null) { cb.Invoke(this.name, msh); //cb.Invoke(this.name + "-material", renderer.material); } }
public override void Render(SVGElement parent, Material baseMaterial, onRenderCallback cb) { if (_gameobject == null) { _gameobject = new GameObject(name); _gameobject.AddComponent(typeof(MeshRenderer)); if (parent != null) { _gameobject.transform.parent = parent.gameObject.transform; } } if (vectors_2d != null && vectors_2d.Length > 0) { SVGGenerals.OptimizePoints(ref vectors_2d); // Use the triangulator to get indices for creating triangles Triangulator tr = new Triangulator(vectors_2d); int[] indices = tr.Triangulate(); // Create the Vector3 vertices Vector3[] vertices = new Vector3[vectors_2d.Length]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new Vector3(vectors_2d[i].x, vectors_2d[i].y, 0); } // Create the mesh if (msh == null) { msh = new Mesh(); } msh.vertices = vertices; msh.triangles = indices; if (fillColor != null) { // Use vertex colors Color[] colors = new Color[vertices.Length]; for (int i = 0; i < colors.Length; i++) { colors[i] = new Color(fillColor.R, fillColor.G, fillColor.B, fillOpacity); } msh.colors = colors; } //Normals //Vector3[] normals = new Vector3[vectors_2d.Length]; //for (int i = 0; i < normals.Length; i++) { // normals[i] = new Vector3(0, 0, 1); //} msh.RecalculateNormals(); msh.RecalculateBounds(); msh.RecalculateTangents(); //Generate Path mesh if (stroke != null) { stroke.GenerateMesh(vertices); Mesh strokeMesh = stroke.GetMesh(); CombineInstance combine = new CombineInstance(); combine.mesh = strokeMesh; combine.transform = _gameobject.transform.localToWorldMatrix; msh.CombineMeshes(new CombineInstance[] { combine }); } if (filter == null) { filter = gameObject.GetComponent <MeshFilter>(); } if (filter == null) { filter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter; } filter.sharedMesh = msh; if (renderer == null) { renderer = gameObject.GetComponent <Renderer>(); } renderer.sharedMaterial = baseMaterial; //renderer.material = baseMaterial; //renderer.material.name = name + "-material"; //renderer.sortingLayerName = "USVG"; renderer.sortingOrder = element_number; //renderer.material.color = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value); //if (fillColor != null){ // renderer.material.color = new Color(fillColor.R, fillColor.G, fillColor.B, fillOpacity); // renderer.material.renderQueue = 3000 + element_number; //} if (cb != null) { cb.Invoke(this.name + "_msh", msh); //cb.Invoke(renderer.material.name, renderer.material); } } }