I've written this code for my game and what I want is to flip the normals on a texture in unity. I have a model and a texture and wish for the texture to be inside the sphere model and not on the outside. I want to create a 360 panoramic effect by moving the camera around the images inside the sphere on top of the flipped texture.
Now, when I first hit the play button, it works perfectly, but then, when I stop it and want to play again, I don't see the board and neither the surroundings.
It seems that it works every 2 times that I try to play. I'm kind of new at this, and I have no idea where my mistake is.
using UnityEngine;
using System.Collections;
public class InvertObjectNormals : MonoBehaviour
{
public GameObject SferaPanoramica;
void Awake()
{
InvertSphere();
}
void InvertSphere()
{
Vector3[] normals = SferaPanoramica.GetComponent<MeshFilter>().sharedMesh.normals;
for(int i = 0; i < normals.Length; i++)
{
normals[i] = -normals[i];
}
SferaPanoramica.GetComponent<MeshFilter>().sharedMesh.normals = normals;
int[] triangles = SferaPanoramica.GetComponent<MeshFilter>().sharedMesh.triangles;
for (int i = 0; i < triangles.Length; i+=3)
{
int t = triangles[i];
triangles[i] = triangles[i + 2];
triangles[i + 2] = t;
}
SferaPanoramica.GetComponent<MeshFilter>().sharedMesh.triangles= triangles;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
You're editing the sharedMesh, which is causing the changes to persist. What's happening is that when you hit run the second time, you are inverting the already inverted mesh, making it right side out again. instead of
try
See here for more information
I realize you found a fix for your script.
If you're interested in an alternative, you can use a shader to flip the normals.
Flip Normals.shader