Skip to content

Instantly share code, notes, and snippets.

@jeromemaurey
Forked from solkar/BakeStaticCubemap.cs
Last active November 5, 2019 00:18
Show Gist options
  • Select an option

  • Save jeromemaurey/a33e2f14537ce2c8fdd55e2d91b2a701 to your computer and use it in GitHub Desktop.

Select an option

Save jeromemaurey/a33e2f14537ce2c8fdd55e2d91b2a701 to your computer and use it in GitHub Desktop.

Revisions

  1. jeromemaurey revised this gist Nov 5, 2019. 1 changed file with 19 additions and 17 deletions.
    36 changes: 19 additions & 17 deletions BakeStaticCubemap.cs
    Original file line number Diff line number Diff line change
    @@ -64,16 +64,17 @@ void OnWizardUpdate()

    void OnWizardCreate ()
    {

    // Create temporary camera for rendering.
    GameObject go = new GameObject( "CubemapCam", typeof(Camera) );
    // Camera settings.
    go.camera.depth = cameraDepth;
    go.camera.backgroundColor = cameraBackgroundColor;
    go.camera.cullingMask = cameraLayerMask;
    go.camera.nearClipPlane = cameraNearPlane;
    go.camera.farClipPlane = cameraFarPlane;
    go.camera.useOcclusionCulling = cameraUseOcclusion;
    var cam = go.GetComponent<Camera>();
    cam.depth = cameraDepth;
    cam.backgroundColor = cameraBackgroundColor;
    cam.cullingMask = cameraLayerMask;
    cam.nearClipPlane = cameraNearPlane;
    cam.farClipPlane = cameraFarPlane;
    cam.useOcclusionCulling = cameraUseOcclusion;
    // Cubemap settings
    cubemap.filterMode = cubemapFilterMode;
    // Set antialiasing
    @@ -84,7 +85,7 @@ void OnWizardCreate ()
    go.transform.rotation = Quaternion.identity;

    // Bake the cubemap
    go.camera.RenderToCubemap(cubemap);
    cam.RenderToCubemap(cubemap);

    // Rendering individual images
    if(createIndividualImages)
    @@ -96,18 +97,19 @@ void OnWizardCreate ()

    RenderIndividualCubemapImages(go);
    }


    // Destroy the camera after rendering.
    DestroyImmediate(go);

    }

    void RenderIndividualCubemapImages(GameObject go)
    {
    go.camera.backgroundColor = Color.black;
    go.camera.clearFlags = CameraClearFlags.Skybox;
    go.camera.fieldOfView = 90;
    go.camera.aspect = 1.0f;
    var cam = go.GetComponent<Camera>();
    cam.backgroundColor = Color.black;
    cam.clearFlags = CameraClearFlags.Skybox;
    cam.fieldOfView = 90;
    cam.aspect = 1.0f;

    go.transform.rotation = Quaternion.identity;

    @@ -116,11 +118,11 @@ void RenderIndividualCubemapImages(GameObject go)
    {
    string imageName = Path.Combine(imageDirectory, cubemap.name + "_"
    + cubemapImage[camOrientation] + ".png");
    go.camera.transform.eulerAngles = eulerAngles[camOrientation];
    cam.transform.eulerAngles = eulerAngles[camOrientation];
    RenderTexture renderTex = new RenderTexture(cubemap.height,
    cubemap.height, cameraDepth);
    go.camera.targetTexture = renderTex;
    go.camera.Render();
    cam.targetTexture = renderTex;
    cam.Render();
    RenderTexture.active = renderTex;

    Texture2D img = new Texture2D(cubemap.height, cubemap.height,
    @@ -144,4 +146,4 @@ static void RenderCubemap ()
    {
    ScriptableWizard.DisplayWizard("Bake CubeMap", typeof(BakeStaticCubemap),"Bake!");
    }
    }
    }
  2. @solkar solkar created this gist Jul 17, 2017.
    147 changes: 147 additions & 0 deletions BakeStaticCubemap.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,147 @@
    /*
    * This confidential and proprietary software may be used only as
    * authorised by a licensing agreement from ARM Limited
    * (C) COPYRIGHT 2014 ARM Limited
    * ALL RIGHTS RESERVED
    * The entire notice above must be reproduced on all authorised
    * copies and copies may only be made to the extent permitted
    * by a licensing agreement from ARM Limited.
    */

    using UnityEngine;
    using UnityEditor;
    using System.IO;

    /**
    * This script must be placed in the Editor folder.
    * The script renders the scene into a cubemap and optionally
    * saves each cubemap image individually.
    * The script is available in the Editor mode from the
    * Game Object menu as "Bake Cubemap" option.
    * Be sure the camera far plane is enough to render the scene.
    */

    public class BakeStaticCubemap : ScriptableWizard
    {

    public Transform renderPosition;
    public Cubemap cubemap;
    // Camera settings.
    public int cameraDepth = 24;
    public LayerMask cameraLayerMask = -1;
    public Color cameraBackgroundColor;
    public float cameraNearPlane = 0.1f;
    public float cameraFarPlane = 2500.0f;
    public bool cameraUseOcclusion = true;
    // Cubemap settings.
    public FilterMode cubemapFilterMode = FilterMode.Trilinear;
    // Quality settings.
    public int antiAliasing = 4;

    public bool createIndividualImages = false;

    // The folder where individual cubemap images will be saved
    static string imageDirectory = "Assets/CubemapImages";
    static string[] cubemapImage
    = new string[]{"front+Z", "right+X", "back-Z", "left-X", "top+Y", "bottom-Y"};
    static Vector3[] eulerAngles = new Vector3[]{new Vector3(0.0f,0.0f,0.0f),
    new Vector3(0.0f,-90.0f,0.0f), new Vector3(0.0f,180.0f,0.0f),
    new Vector3(0.0f,90.0f,0.0f), new Vector3(-90.0f,0.0f,0.0f),
    new Vector3(90.0f,0.0f,0.0f)};

    void OnWizardUpdate()
    {
    helpString = "Set the position to render from and the cubemap to bake.";
    if(renderPosition != null && cubemap != null)
    {
    isValid = true;
    }
    else
    {
    isValid = false;
    }
    }

    void OnWizardCreate ()
    {

    // Create temporary camera for rendering.
    GameObject go = new GameObject( "CubemapCam", typeof(Camera) );
    // Camera settings.
    go.camera.depth = cameraDepth;
    go.camera.backgroundColor = cameraBackgroundColor;
    go.camera.cullingMask = cameraLayerMask;
    go.camera.nearClipPlane = cameraNearPlane;
    go.camera.farClipPlane = cameraFarPlane;
    go.camera.useOcclusionCulling = cameraUseOcclusion;
    // Cubemap settings
    cubemap.filterMode = cubemapFilterMode;
    // Set antialiasing
    QualitySettings.antiAliasing = antiAliasing;

    // Place the camera on the render position.
    go.transform.position = renderPosition.position;
    go.transform.rotation = Quaternion.identity;

    // Bake the cubemap
    go.camera.RenderToCubemap(cubemap);

    // Rendering individual images
    if(createIndividualImages)
    {
    if (!Directory.Exists(imageDirectory))
    {
    Directory.CreateDirectory(imageDirectory);
    }

    RenderIndividualCubemapImages(go);
    }


    // Destroy the camera after rendering.
    DestroyImmediate(go);
    }

    void RenderIndividualCubemapImages(GameObject go)
    {
    go.camera.backgroundColor = Color.black;
    go.camera.clearFlags = CameraClearFlags.Skybox;
    go.camera.fieldOfView = 90;
    go.camera.aspect = 1.0f;

    go.transform.rotation = Quaternion.identity;

    //Render individual images
    for (int camOrientation = 0; camOrientation < eulerAngles.Length ; camOrientation++)
    {
    string imageName = Path.Combine(imageDirectory, cubemap.name + "_"
    + cubemapImage[camOrientation] + ".png");
    go.camera.transform.eulerAngles = eulerAngles[camOrientation];
    RenderTexture renderTex = new RenderTexture(cubemap.height,
    cubemap.height, cameraDepth);
    go.camera.targetTexture = renderTex;
    go.camera.Render();
    RenderTexture.active = renderTex;

    Texture2D img = new Texture2D(cubemap.height, cubemap.height,
    TextureFormat.RGB24, false);
    img.ReadPixels(new Rect(0, 0, cubemap.height, cubemap.height), 0, 0);

    RenderTexture.active = null;
    GameObject.DestroyImmediate(renderTex);

    byte[] imgBytes = img.EncodeToPNG();
    File.WriteAllBytes(imageName, imgBytes);

    AssetDatabase.ImportAsset(imageName, ImportAssetOptions.ForceUpdate);
    }

    AssetDatabase.Refresh();
    }

    [MenuItem("GameObject/Bake Cubemap")]
    static void RenderCubemap ()
    {
    ScriptableWizard.DisplayWizard("Bake CubeMap", typeof(BakeStaticCubemap),"Bake!");
    }
    }