- Home /
Flip a mesh inside-out?
Basically, I need to have a inside-out cube to use for a cloud skybox overlay. So as we al know, triangles are only rendered on one side. So I thought I could simply flip the tris over, but it didn't seem to work. Here is my script:
using UnityEngine;
using System.Collections;
public class FlipInsideOut: MonoBehaviour {
public int[] Tris = new int[100];
int temp;
void Start () {
Tris = this.GetComponent().mesh.triangles;
for(int i = 0; i < 100; i++){
if(Tris[3*i] != null){
temp = Tris[3*i+1];
Tris[3*i+1] = Tris[3*i+2];
Tris[3*i+2] = temp;
}
}
this.GetComponent().mesh.triangles = Tris;
}
}
Does anybody have any idea why it isnt working? I shouldn't see the mesh from the outside. If anybody could help me I'd be very happy.
Answer by Jessy · Jun 18, 2013 at 08:24 PM
Your method should work fine. This is easier:
using System.Linq;
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.triangles = mesh.triangles.Reverse().ToArray();
This is beautiful. Anyway to get the geometry saved, so in the Editor it shows flipped?
Instead of triggering it in playmode you use
[ContextMenu( "Flip" )]
public void FlipMeshContext ( ) {
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh. triangles = mesh. triangles. Reverse( ). ToArray( );
}
This will add the "flip" option to the context menu of your script, you can then trigger it without being in playmode.
This is ok if you are just working with geometry. If you have any UV maps, this will create problems with your uv indexes. Remember a triangle will be indexed like so: TRIANGLE 1 = VERTEXidx1 + UVidx1 and VERTEXidx2 + UVidx2 and VERTEXidx3 and UVidx3 If you rearrange your geometry (vertex list) then you will need to fix your uv list and normal (for faces) as well. https://docs.unity3d.com/ScriptReference/$$anonymous$$esh-triangles.html
The UVs are actually tied to the vertex and they don't change at all if you flip the triangles. $$anonymous$$eep in $$anonymous$$d that we flip / reverse the index array, not the vertices array. There is only one index array.
Of course the image will actually be mirrored (since you look at it from the other side). However there's no "generally valid" way to "flip the UV" as it highly depends on the topography of the mesh and the UV layout. $$anonymous$$ost unwraps are not symetrically laid out and therefore can't be simply flipped in texture space. Though it probably would work for a cube if all sides are mapped to the whole texture. Though this is not what you would use for a skybox.
Though you are right about the vertex normals and tangents when a lit shader is used. They would need to be flipped as well to get the correct lighting. Though if you use an unlit shader the vertex normals don't matter (the mesh doesn't need to have normals in this case). $$anonymous$$ost skyboxes are unlit or calculate the lighting in a different way. Using vertex or face normals wouldn't make much sense as they often cause visible seems at the edges
Answer by ExplodingCookie · Jun 18, 2013 at 08:40 PM
There are two ways to render the inside of a cube.
Use the Flip Normals in the modeling application of your choice. (Not sure if the function is universally flip normals) This makes the visible side of the mesh only visible from within the cube
Use a shader that renders both sides of the mesh(exe Toon Shaders and the Particle Shaders)
Your answer
![](https://koobas.hobune.stream/wayback/20220613104425im_/https://answers.unity.com/themes/thub/images/avi.jpg)