Flip Normals in Unity 3D/spheres

2019-04-12 23:42发布

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 () {

    }
}

2条回答
Viruses.
2楼-- · 2019-04-13 00:09

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

        Vector3[] normals = SferaPanoramica.GetComponent<MeshFilter>().sharedMesh.normals;

try

        Vector3[] normals = SferaPanoramica.GetComponent<MeshFilter>().mesh.normals;

See here for more information

查看更多
聊天终结者
3楼-- · 2019-04-13 00:14

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

Shader "Flip Normals" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader {

        Tags { "RenderType" = "Opaque" }

        Cull Off

        CGPROGRAM

        #pragma surface surf Lambert vertex:vert
        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
            float4 color : COLOR;
        };

        void vert(inout appdata_full v) {
            v.normal.xyz = v.normal * -1;
        }

        void surf (Input IN, inout SurfaceOutput o) {
             fixed3 result = tex2D(_MainTex, IN.uv_MainTex);
             o.Albedo = result.rgb;
             o.Alpha = 1;
        }

        ENDCG

    }

      Fallback "Diffuse"
}
查看更多
登录 后发表回答