Select your preferred scripting language. All code snippets will be displayed in this language.
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closecubemap | The cube map to render to. |
faceMask | A bitmask which determines which of the six faces are rendered to. |
bool False is rendering fails, else true.
Render into a static cubemap from this camera.
This function is mostly useful in the editor for "baking" static cubemaps of your scene. See
wizard example below. If you want a realtime-updated cubemap, use RenderToCubemap variant that uses
a RenderTexture with a cubemap dimension, see below.
Camera's position, clear flags and clipping plane distances will be used to
render into cubemap faces. faceMask
is a bitfield indicating which cubemap faces should be rendered into. Each
bit that is set corresponds to a face. Bit numbers are integer values of CubemapFace enum.
By default all six cubemap faces will be rendered (default value 63 has six lowest bits on).
This function will return false
if rendering to cubemap fails. Some graphics hardware does not
support the functionality.
Note also that ReflectionProbes are a more advanced way of performing realtime reflections. Cubemaps can be created in the editor by selecting the Create->Legacy option.
See Also: Cubemap assets, Reflective shaders.
// Render scene from a given point into a static cube map. // Place this script in Editor folder of your project. // Then use the cubemap with one of Reflective shaders! class RenderCubemapWizard extends ScriptableWizard { var renderFromPosition : Transform; var cubemap : Cubemap;
function OnWizardUpdate () { helpString = "Select transform to render from and cubemap to render into"; isValid = (renderFromPosition != null) && (cubemap != null); }
function OnWizardCreate () { // create temporary camera for rendering var go = new GameObject("CubemapCamera", Camera); // place it on the object go.transform.position = renderFromPosition.position; go.transform.rotation = Quaternion.identity;
// render into cubemap go.GetComponent.<Camera>().RenderToCubemap(cubemap);
// destroy temporary camera DestroyImmediate(go); }
@MenuItem("GameObject/Render into Cubemap") static function RenderCubemap () { ScriptableWizard.DisplayWizard.<RenderCubemapWizard>( "Render cubemap", "Render!"); } }
using UnityEngine; using UnityEditor; using System.Collections;
public class RenderCubemapWizard : ScriptableWizard { public Transform renderFromPosition; public Cubemap cubemap;
void OnWizardUpdate() { string helpString = "Select transform to render from and cubemap to render into"; bool isValid = (renderFromPosition != null) && (cubemap != null); }
void OnWizardCreate() { // create temporary camera for rendering GameObject go = new GameObject("CubemapCamera"); go.AddComponent<Camera>(); // place it on the object go.transform.position = renderFromPosition.position; go.transform.rotation = Quaternion.identity; // render into cubemap go.GetComponent<Camera>().RenderToCubemap(cubemap);
// destroy temporary camera DestroyImmediate(go); }
[MenuItem("GameObject/Render into Cubemap")] static void RenderCubemap() { ScriptableWizard.DisplayWizard<RenderCubemapWizard>( "Render cubemap", "Render!"); } }
faceMask | A bitfield indicating which cubemap faces should be rendered into. |
cubemap | The texture to render to. |
bool False is rendering fails, else true.
Render into a cubemap from this camera.
This is used for real-time reflections into cubemap render textures. It can be quite expensive though,
especially if all six cubemap faces are rendered each frame.
The Camera's position, clear flags and clipping plane distances will be used to
render into cubemap faces. faceMask
is a bitfield indicating which cubemap faces should be rendered into. Each
bit that is set corresponds to a face. Bit numbers are integer values of CubemapFace enum.
By default all six cubemap faces will be rendered (default value 63 has six lowest bits on).
This function will return false
if rendering to cubemap fails. Some graphics hardware does not
support the functionality.
Note that the RenderTexture must have RenderTexture.dimension set to TextureDimension.Cube. This is illustrated in the example following.
See Also: RenderTexture.isCubemap, Reflective shaders.
// Attach this script to an object that uses a Reflective shader. // Realtime reflective cubemaps!
@script ExecuteInEditMode
var cubemapSize = 128; var oneFacePerFrame = false; private var cam : Camera; private var rtex : RenderTexture;
function Start () { // render all six faces at startup UpdateCubemap( 63 ); }
function LateUpdate () { if (oneFacePerFrame) { var faceToRender = Time.frameCount % 6; var faceMask = 1 << faceToRender; UpdateCubemap (faceMask); } else { UpdateCubemap (63); // all six faces } }
function UpdateCubemap (faceMask : int) { if (!cam) { var go = new GameObject ("CubemapCamera", Camera); go.hideFlags = HideFlags.HideAndDontSave; go.transform.position = transform.position; go.transform.rotation = Quaternion.identity; cam = go.GetComponent.<Camera>(); cam.farClipPlane = 100; // don't render very far into cubemap cam.enabled = false; }
if (!rtex) { rtex = new RenderTexture (cubemapSize, cubemapSize, 16); rtex.dimension = UnityEngine.Rendering.TextureDimension.Cube; rtex.hideFlags = HideFlags.HideAndDontSave; GetComponent.<Renderer>().sharedMaterial.SetTexture ("_Cube", rtex); }
cam.transform.position = transform.position; cam.RenderToCubemap (rtex, faceMask); }
function OnDisable () { DestroyImmediate (cam); DestroyImmediate (rtex); }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!