Exemplo n.º 1
0
    void UpdateCubemap(int faceMask)
    {
        Camera mainCamera = CameraHelper.Main();

        if (mainCamera == null || planet == null)
        {
            return;
        }

        if (!renderCamera)
        {
            GameObject go = new GameObject("CubemapCamera", typeof(Camera));
            go.hideFlags          = HideFlags.HideAndDontSave;
            go.transform.position = transform.position;
            go.transform.rotation = Quaternion.identity;
            go.transform.parent   = mainCamera.transform;

            renderCamera                 = go.GetComponent <Camera>();
            renderCamera.cullingMask     = layerMask;
            renderCamera.nearClipPlane   = mainCamera.nearClipPlane;
            renderCamera.farClipPlane    = mainCamera.farClipPlane;
            renderCamera.backgroundColor = mainCamera.backgroundColor;
            renderCamera.enabled         = false;
        }

        if (cubeRenderTexture == null)
        {
            cubeRenderTexture = new RenderTexture(cubemapSize, cubemapSize, 16);
            cubeRenderTexture.isPowerOfTwo = true;
            cubeRenderTexture.dimension    = UnityEngine.Rendering.TextureDimension.Cube;
            cubeRenderTexture.hideFlags    = HideFlags.HideAndDontSave;

            foreach (Renderer r in GetComponentsInChildren <Renderer>())
            {
                foreach (Material m in r.sharedMaterials)
                {
                    if (m.HasProperty("_Cube"))
                    {
                        m.SetTexture("_Cube", cubeRenderTexture);
                    }
                }
            }
        }

        planet.Render(renderCamera);

        renderCamera.transform.position = transform.position;

        renderCamera.RenderToCubemap(cubeRenderTexture, faceMask);
    }