在Unity 3D / spheres

时间:2016-04-05 10:30:56

标签: unity3d unityscript

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

现在,当我第一次按下播放按钮时,它工作得很好,但是当我停下来并想再次播放时,我看不到电路板,也看不到周围环境。

它似乎每2次尝试一次。我对此很陌生,我不知道我的错误在哪里。

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 个答案:

答案 0 :(得分:8)

我意识到你找到了修复脚本的方法。

如果您对替代方案感兴趣,可以使用着色器翻转法线。

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

答案 1 :(得分:2)

您正在编辑sharedMesh,这会导致更改持续存在。发生的事情是,当你第二次跑步时,你正在反转已经倒置的网状物,使其再次向右侧移动。而不是

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

尝试

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

有关详细信息,请参阅here