Camera.RenderToCubemap

Switch to Manual
public bool RenderToCubemap (Cubemap cubemap, int faceMask);

Parameters

cubemap要渲染到的立方体贴图。
faceMask位掩码,用于确定渲染到六个面中的哪一个。

Returns

bool 如果渲染失败,返回 false,否则返回 true。

Description

从该摄像机渲染到一个静态立方体贴图。

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.

将使用摄像机的位置、清除标记和裁剪面距离渲染到立方体贴图面。 faceMask 是一个位域,指示应渲染到立方体贴图的哪些面。设置的每个位 对应一个面。位数为 CubemapFace 枚举的整数值。 默认情况下,将渲染立方体贴图的所有六个面(默认值为 63 - 即最低六位均被置位)。

如果渲染到立方体贴图失败,该函数将返回 /false/。某些图形硬件不支持 该功能。

另请注意,ReflectionProbes 是执行实时反射的更高级的方法。通过选择 Create > Legacy 选项,可以在 Editor 中创建立方体贴图。

另请参阅:立方体贴图资源反光着色器

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!"); } }

public bool RenderToCubemap (RenderTexture cubemap, int faceMask);

Parameters

faceMask位域,指示应渲染到立方体贴图的哪些面。
cubemap要渲染到的纹理。

Returns

bool 如果渲染失败,返回 false,否则返回 true。

Description

从该摄像机渲染到一个立方体贴图。

用于将实时反光渲染到立方体贴图渲染纹理。该操作开销很大, 如果每帧都需要渲染立方体贴图的所有六个面,代价将极其高昂。

将使用摄像机的位置、清除标记和裁剪面距离渲染到立方体贴图面。 faceMask 是一个位域,指示应渲染到立方体贴图的哪些面。设置的每个位 对应一个面。位数为 CubemapFace 枚举的整数值。 默认情况下,将渲染立方体贴图的所有六个面(默认值为 63 - 即最低六位均被置位)。

如果渲染到立方体贴图失败,该函数将返回 /false/。某些图形硬件不支持 该功能。

注意,必须将 RenderTexture 的 RenderTexture.dimension 设置为 TextureDimension.Cube。下面的示例将对此进行说明。

See Also: RenderTexture.isCubemap, Reflective shaders.

using UnityEngine;

[ExecuteInEditMode] public class Example : MonoBehaviour { // Attach this script to an object that uses a Reflective shader. // Realtime reflective cubemaps!

int cubemapSize = 128; bool oneFacePerFrame = false; Camera cam; RenderTexture renderTexture;

void Start() { // render all six faces at startup UpdateCubemap(63); }

void OnDisable() { DestroyImmediate(cam); DestroyImmediate(renderTexture); }

void LateUpdate() { if (oneFacePerFrame) { var faceToRender = Time.frameCount % 6; var faceMask = 1 << faceToRender; UpdateCubemap(faceMask); } else { UpdateCubemap(63); // all six faces } }

void UpdateCubemap(int faceMask) { if (!cam) { GameObject obj = new GameObject("CubemapCamera", typeof(Camera)); obj.hideFlags = HideFlags.HideAndDontSave; obj.transform.position = transform.position; obj.transform.rotation = Quaternion.identity; cam = obj.GetComponent<Camera>(); cam.farClipPlane = 100; // don't render very far into cubemap cam.enabled = false; }

if (!renderTexture) { renderTexture = new RenderTexture(cubemapSize, cubemapSize, 16); renderTexture.dimension = UnityEngine.Rendering.TextureDimension.Cube; renderTexture.hideFlags = HideFlags.HideAndDontSave; GetComponent<Renderer>().sharedMaterial.SetTexture("_Cube", renderTexture); }

cam.transform.position = transform.position; cam.RenderToCubemap(renderTexture, faceMask); } }

public bool RenderToCubemap (RenderTexture cubemap, int faceMask, Camera.MonoOrStereoscopicEye stereoEye);

Parameters

cubemap要渲染到的纹理。
faceMask位域,指示应渲染到立方体贴图的哪些面。设置为整数值 63 将渲染所有面。
stereoEye摄像机眼,对应于左眼或右眼(用于立体渲染),或不对应于眼睛(用于非立体渲染)。

Returns

bool 如果渲染失败,返回 false,否则返回 true。

Description

从该摄像机将一个 360 度立体图像的一侧渲染到一个立方体贴图。

Setting the stereoEye parameter to Camera.MonoOrStereoscopicEye.Left or Camera.MonoOrStereoscopicEye.Right renders the left or right eye point-of-view of a stereo 360 image with proper world space transform. Setting stereoEye to Camera.MonoOrStereoscopicEye.Mono renders a monoscopic view of the Scene. After rendering the separate left and right cubemaps, you can convert them into equirectangular panoramic images that occupy one texture.

在渲染立体视图的任一侧时,除非启用了 VR 支持,否则摄像机将使用其 stereoSeparation 值作为瞳孔间距 (IPD)。使用 VR 摄像机时,VR 设备 IPD 会重写 stereoSeparation 值。

Unity uses the Camera's position, clear flags and clipping plane distances to render into the cubemap faces. The camera is rotated for each face. 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. For 360 stereo image capture, all six cubemap faces should be rendered (set facemask to 63).

This function will return false if rendering to the cubemap fails. Some graphics hardware does not support the functionality.

Note that the RenderTexture must have RenderTexture.dimension set to TextureDimension.Cube.

See Also: RenderTexture.isCubemap, Cubemap.

using UnityEngine;
using UnityEngine.Rendering;

//attach this script to your camera object public class CreateStereoCubemaps : MonoBehaviour { public RenderTexture cubemapLeftEye; public RenderTexture cubemapRightEye; public RenderTexture equirect; public bool renderStereo = true; public float stereoSeparation = 0.064f;

void Start() { cubemapLeftEye = new RenderTexture(1024, 1024, 24, RenderTextureFormat.ARGB32); cubemapLeftEye.dimension = TextureDimension.Cube; cubemapRightEye = new RenderTexture(1024, 1024, 24, RenderTextureFormat.ARGB32); cubemapRightEye.dimension = TextureDimension.Cube; //equirect height should be twice the height of cubemap equirect = new RenderTexture(1024, 2048, 24, RenderTextureFormat.ARGB32); }

void LateUpdate() { Camera cam = GetComponent<Camera>();

if (cam == null) { cam = GetComponentInParent<Camera>(); }

if (cam == null) { Debug.Log("stereo 360 capture node has no camera or parent camera"); }

if (renderStereo) { cam.stereoSeparation = stereoSeparation; cam.RenderToCubemap(cubemapLeftEye, 63, Camera.MonoOrStereoscopicEye.Left); cam.RenderToCubemap(cubemapRightEye, 63, Camera.MonoOrStereoscopicEye.Right); } else { cam.RenderToCubemap(cubemapLeftEye, 63, Camera.MonoOrStereoscopicEye.Mono); }

//optional: convert cubemaps to equirect

if (equirect == null) return;

if (renderStereo) { cubemapLeftEye.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Left); cubemapRightEye.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Right); } else { cubemapLeftEye.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Mono); } } }