Forked from kurtdekker/MakeGeometryDoubleSided.cs
Created
September 22, 2022 10:53
-
-
Save unitycoder/1c893591c38456b100debb8ce6da69df to your computer and use it in GitHub Desktop.
Revisions
-
kurtdekker revised this gist
Sep 21, 2022 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,19 +12,21 @@ public class MakeGeometryDoubleSided : MonoBehaviour { void Awake() { MeshFilter mf = GetComponent<MeshFilter>(); // If this blows up you failed to meet the documented // requirement above that a MeshFilter be present. int vertCount = mf.mesh.vertices.Length; // we need to dupe all verts so we can have lighting / UV info Vector3[] verts = new Vector3[ vertCount * 2]; for (int i = 0; i < vertCount; i++) { verts [i] = mf.mesh.vertices [i]; verts [i + vertCount] = mf.mesh.vertices [i]; } // and of course dupe all triangles int triCount = mf.mesh.triangles.Length; int[] tris = new int[ triCount * 2]; -
kurtdekker created this gist
Sep 21, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ using UnityEngine; // @kurtdekker - quick double-siding mechanism. // // Place or add this Component to things with at // least a MeshFilter on them. // // Presently only for single-submesh geometry. [RequireComponent( typeof( MeshFilter))] public class MakeGeometryDoubleSided : MonoBehaviour { void Awake() { // If this blows up you failed to meet the documented // requirement above that a MeshFilter be present. MeshFilter mf = GetComponent<MeshFilter>(); int vertCount = mf.mesh.vertices.Length; Vector3[] verts = new Vector3[ vertCount * 2]; for (int i = 0; i < vertCount; i++) { verts [i] = mf.mesh.vertices [i]; verts [i + vertCount] = mf.mesh.vertices [i]; } int triCount = mf.mesh.triangles.Length; int[] tris = new int[ triCount * 2]; for (int i = 0; i < triCount; i += 3) { // re-add the original triangles, no change tris[i + 0] = mf.mesh.triangles[i + 0]; tris[i + 1] = mf.mesh.triangles[i + 1]; tris[i + 2] = mf.mesh.triangles[i + 2]; // now add triangles flipped the opposite way tris[i + triCount + 0] = mf.mesh.triangles[i + 0] + vertCount; tris[i + triCount + 1] = mf.mesh.triangles[i + 2] + vertCount; tris[i + triCount + 2] = mf.mesh.triangles[i + 1] + vertCount; } mf.mesh.vertices = verts; mf.mesh.triangles = tris; mf.mesh.RecalculateNormals (); } }