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

查看:118
本文介绍了在 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;

请参阅此处了解更多信息

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

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