Flip Normals in Unity 3D/spheres

若如初见. 提交于 2019-12-18 18:13:13

问题


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;
    }
}

回答1:


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




回答2:


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"
}


来源:https://stackoverflow.com/questions/36423846/flip-normals-in-unity-3d-spheres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!