查看: 2926|回复: 2
打印 上一主题 下一主题

[其他] 生成Cubemap所需要的六面图代码分享

[复制链接]
may    

8830

主题

81

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52336
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-4-29 06:43:29 |只看该作者 |倒序浏览
class SkyBoxGenerator extends ScriptableWizard
{
var renderFromPosition : Transform;

var skyBoxImage = new Array ("frontImage", "rightImage", "backImage", "leftImage", "upImage", "downImage");

var skyDirection = new Array (Vector3 (0,0,0), Vector3 (0,-90,0), Vector3 (0,180,0), Vector3 (0,90,0), Vector3 (-90,0,0), Vector3 (90,0,0));


function OnWizardUpdate()
{
helpstring = "Select transform to render from";
isValid = (renderFromPosition != null);
}

function OnWizardCreate()
{
var go = new GameObject ("SkyboxCamera", Camera);

go.camera.backgroundColor = Color.black;
go.camera.clearFlags = CameraClearFlags.Skybox;
go.camera.fieldOfView = 90;
go.camera.aspect = 1.0;

go.transform.position = renderFromPosition.position;

if (renderFromPosition.renderer)
{
go.transform.position = renderFromPosition.renderer.bounds.center;
}

go.transform.rotation = Quaternion.identity;

for (var orientation = 0; orientation < skyDirection.length ; orientation++)
{
renderSkyImage(orientation, go);
}

DestroyImmediate (go);
}

@MenuItem("Custom/Render Skybox", false, 4)
static function RenderSkyBox()
{
ScriptableWizard.DisplayWizard ("Render SkyBox", SkyBoxGenerator, "Render!");
}

function renderSkyImage(orientation : int, go : GameObject)
{
go.transform.eulerAngles = skyDirection[orientation];
var screenSize = 1024;
var rt = new RenderTexture (screenSize, screenSize, 24);
go.camera.targetTexture = rt;
var screenShot = new Texture2D (screenSize, screenSize, TextureFormat.RGB24, false);
go.camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels (Rect (0, 0, screenSize, screenSize), 0, 0);

RenderTexture.active = null;
DestroyImmediate (rt);
var bytes = screenShot.EncodeToPNG();

var directory = "Assets/Skyboxes";
if (!System.IO.Directory.Exists(directory))
System.IO.Directory.CreateDirectory(directory);
System.IO.File.WriteAllBytes (System.IO.Path.Combine(directory, skyBoxImage[orientation] + ".png"), bytes);
}

}
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏1 支持支持0 反对反对0
回复

使用道具 举报

0

主题

1

听众

56

积分

设计初学者

Rank: 1

纳金币
25
精华
0

活跃会员 灌水之王

沙发
发表于 2014-1-14 23:43:01 |只看该作者
赞一个!
回复

使用道具 举报

115

主题

3

听众

5676

积分

高级设计师

Rank: 6Rank: 6

纳金币
7268
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2014-1-15 00:00:56 |只看该作者
Thanks for this ! Very useful !
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-11 10:36 , Processed in 0.083182 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部