纳金网

标题: [转载]在U3D中生成Cubemap所需要的六面图 [打印本页]

作者: yeu1233    时间: 2012-8-23 14:19
标题: [转载]在U3D中生成Cubemap所需要的六面图

功能:生成Cubemap所需要的六面图

脚本命:SkyBoxGenerator.js



使用方法:脚本必须放置在项目文件中名为“Editor”的文件夹;该脚本将创建一个名为“Custom/Render Skybox”菜单。当选定会弹出一个对话框,设置一个渲染位置然后点击“Render”,并等待几秒钟,刷新项目窗口,生成的6张图片将出现在名为“Skyboxes”的文件夹。警告,每次运行这个脚本,它会覆盖以前生成的图像。



脚本内容:

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);  

    }
}


文章转载于:http://u3dpro.com/read.php?tid=714





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5