码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D Camera RenderToCubemap 渲染到立方体纹理

时间:2017-08-05 14:04:11      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:engine   nio   height   for   isa   position   val   des   temporary   

Unity圣典 传送门:http://www.ceeger.com/Script/Camera/Camera.RenderToCubemap.html

Camera.RenderToCubemap 有4个重载

(1)bool  RenderToCubemap(Cubemap cubemap)

    bool  RenderToCubemap(Cubemap cubemap,int faceMask)

  可以用于在编辑器中生成场景静态立方体贴图。faceMask是一个bitfield比特数,表示那个立方贴图面应该被渲染,每个位对

应于一个面。比特数是Cubemapface枚举的整型值。默认的所有六个立方贴图面都被渲染(默认值63的低6位是打开的)。

 1 // Render scene from a given point into a static cube map.
 2 //从给定的点渲染场景到以静态立方贴图
 3 // Place this script in Editor folder of your project.
 4 //放置这个脚本到工程的Editor文件夹中
 5 // Then use the cubemap with one of Reflective shaders!
 6 //然后用一个Reflective shaders 来使用这个立方贴图
 7 class RenderCubemapWizard extends ScriptableWizard {
 8     var renderFromPosition : Transform;
 9     var cubemap : Cubemap;
10 
11     function OnWizardUpdate () {
12         helpString = "Select transform to render from and cubemap to render into";
13         isValid = (renderFromPosition != null) && (cubemap != null);
14     }
15 
16     function OnWizardCreate () {
17         // create temporary camera for rendering
18         //为渲染创建临时相机
19         var go = new GameObject( "CubemapCamera", Camera );
20         // place it on the object
21         //放置它到物体上
22         go.transform.position = renderFromPosition.position;
23         go.transform.rotation = Quaternion.identity;
24 
25         // render into cubemap
26         //渲染到立方贴图    
27         go.camera.RenderToCubemap( cubemap );
28 
29         // destroy temporary camera
30         //销毁临时相机
31         DestroyImmediate( go );
32     }
33 
34     @MenuItem("GameObject/Render into Cubemap")
35     static function RenderCubemap () {
36         ScriptableWizard.DisplayWizard.<RenderCubemapWizard>(
37         "Render cubemap", "Render!");
38     }
39 }

(2)bool  RenderToCubemap(RenderTexture cubemap)

         bool  RenderToCubemap(RenderTexture cubemap,int faceMask)

        这个用于实时反射到立方贴图渲染纹理。这是非常耗时的,尤其是所有六个立方贴图面在每一帧中都被渲染。

如果渲染失败这个函数将返回false,某些显卡不支持这个函数。

  同时,一定要将 RenderTexture.isCubemap = true

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.UI;
 4 using UnityEditor;
 5 
 6 // Attach this script to an object that uses a Reflective shader.
 7 // Realtime reflective cubemaps!
 8 //附加这个脚本到使用了Reflective shader的物体上 
 9 //实时反射立方贴图
10 [ExecuteInEditMode]
11 public class RealtimeCubemap :MonoBehaviour
12 {
13     int cubemapSize = 128;
14     //一帧渲染一面
15     bool oneFacePerFrame = false; 
16     Camera cam = null;
17     RenderTexture cubemap = null;
18     Material mat = null;
19 
20     void Start()
21     {
22         //在启动时渲染所有六个面
23         UpdateCubemap(63);
24     }
25 
26     void LateUpdate()
27     {
28         if (oneFacePerFrame)
29         {
30             int faceToRender = Time.frameCount % 6;
31             int faceMask = 1 << faceToRender;
32             UpdateCubemap(faceMask);
33         }
34         else
35         {
36             //所有六个面
37             UpdateCubemap(63);
38         }
39     }
40 
41     
42 void UpdateCubemap (int faceMask )
43 {
44     if (cam == null)
45     {
46         GameObject go = new GameObject("CubemapCamera", typeof(Camera));
47 
48         //隐藏在场景中
49         go.hideFlags = HideFlags.HideAndDontSave;
50         go.transform.position = transform.position;
51         go.transform.rotation = Quaternion.identity;
52         cam = go.GetComponent<Camera>();
53 
54         ///不要渲染较远的部分
55         cam.farClipPlane = 100;
56         cam.enabled = false;
57     }
58     if (mat == null)
59     {
60         mat = new Material(Shader.Find("Custom/RealtimeCubemap"));
61         mat.hideFlags = HideFlags.HideAndDontSave;
62         GetComponent<Renderer>().material = mat;
63     }
64 
65     if (cubemap == null)
66     {
67         cubemap = new RenderTexture(cubemapSize, cubemapSize, 16);
68         cubemap.isPowerOfTwo = true;
69         cubemap.isCubemap = true;
70         cubemap.hideFlags = HideFlags.HideAndDontSave;
71         mat.SetTexture("_Cubemap", cubemap);
72     }
73 
74     cam.transform.position = transform.position;
75     cam.RenderToCubemap(cubemap, faceMask);
76 }
77 
78     void OnDisable()
79     {
80         DestroyImmediate(cam);
81         DestroyImmediate(cubemap);
82     }
83 
84 }

 

Unity3D Camera RenderToCubemap 渲染到立方体纹理

标签:engine   nio   height   for   isa   position   val   des   temporary   

原文地址:http://www.cnblogs.com/nanwei/p/7289960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!