Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Forked from kurtdekker/MakeGeometryDoubleSided.cs
Created September 22, 2022 10:53
Show Gist options
  • Select an option

  • Save unitycoder/1c893591c38456b100debb8ce6da69df to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/1c893591c38456b100debb8ce6da69df to your computer and use it in GitHub Desktop.

Revisions

  1. @kurtdekker kurtdekker revised this gist Sep 21, 2022. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions MakeGeometryDoubleSided.cs
    Original file line number Diff line number Diff line change
    @@ -12,19 +12,21 @@ 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>();

    // If this blows up you failed to meet the documented
    // requirement above that a MeshFilter be present.
    int vertCount = mf.mesh.vertices.Length;
    Vector3[] verts = new Vector3[ vertCount * 2];

    // 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];

  2. @kurtdekker kurtdekker created this gist Sep 21, 2022.
    50 changes: 50 additions & 0 deletions MakeGeometryDoubleSided.cs
    Original 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 ();
    }
    }