Unity 3D/球体中的翻转法线 [英] Flip Normals in Unity 3D/spheres

查看:203
本文介绍了Unity 3D/球体中的翻转法线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的游戏编写了这段代码,我想要的是统一翻转纹理上的法线.我有一个模型和一个纹理,希望该纹理位于球体模型内部,而不是外部.我想通过在翻转的纹理上方的球体内部的图像周围移动相机来创建360度全景效果.

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.

看来我尝试播放每2次就可以了.我对此很陌生,不知道我的错误在哪里.

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

推荐答案

您正在编辑sharedMesh,这会使更改继续存在.发生的情况是,当您第二次点击运行时,您正在反转已经反转的网格,使其再次朝右侧显示.

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.

代替

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

尝试

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

有关更多信息,请参见此处

See here for more information

这篇关于Unity 3D/球体中的翻转法线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆