您的位置:首页 > 移动开发 > Unity3D

unity3d场景导入webgl/three.js

2020-06-03 06:09 811 查看

比如到UnityAssetStore买了一个PolygonCity的asset资源,打开里面的Demo.scene。

用这个C#脚本可以导出xml,

脚本参考了《Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景(四十二)》http://www.xuanyusong.com/archives/1919

!!!会对场景对象的树形结构进行修改,注意备份。

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;

public class MyEditor : Editor
{
    //将所有游戏场景导出为XML格式
    [MenuItem ("GameObject/cake")]
    static void ExportXML ()
    {
        string filepath = Application.dataPath + @"/StreamingAssets/my.xml";
        if(!File.Exists (filepath))
        {
            File.Delete(filepath);
        }
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement root = xmlDoc.CreateElement("gameObjectsrt");
        //遍历所有的游戏场景

        foreach (GameObject xx in Object.FindObjectsOfType(typeof(GameObject))) {
            xx.transform.SetParent (null);
        }

        foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
        {
            if (obj.name.IndexOf ("SM_") == -1)
                continue;
            if (obj.transform.parent != null) {
            }else{
                XmlElement gameObject = xmlDoc.CreateElement ("gameObjects");
                string ps = obj.GetComponent<MeshFilter> ().sharedMesh.name;
                //Debug.Log (ps);
                gameObject.SetAttribute ("name", ps);
                gameObject.SetAttribute ("asset", obj.name + ".prefab");
                XmlElement transform = xmlDoc.CreateElement ("transform");
                XmlElement position = xmlDoc.CreateElement ("position");
                XmlElement position_x = xmlDoc.CreateElement ("x");
                position_x.InnerText = obj.transform.position.x + "";
                XmlElement position_y = xmlDoc.CreateElement ("y");
                position_y.InnerText = obj.transform.position.y + "";
                XmlElement position_z = xmlDoc.CreateElement ("z");
                position_z.InnerText = obj.transform.position.z + "";
                position.AppendChild (position_x);
                position.AppendChild (position_y);
                position.AppendChild (position_z);

                XmlElement rotation = xmlDoc.CreateElement ("rotation");
                XmlElement rotation_x = xmlDoc.CreateElement ("x");
                rotation_x.InnerText = obj.transform.rotation.eulerAngles.x + "";
                XmlElement rotation_y = xmlDoc.CreateElement ("y");
                rotation_y.InnerText = obj.transform.rotation.eulerAngles.y + "";
                XmlElement rotation_z = xmlDoc.CreateElement ("z");
                rotation_z.InnerText = obj.transform.rotation.eulerAngles.z + "";
                rotation.AppendChild (rotation_x);
                rotation.AppendChild (rotation_y);
                rotation.AppendChild (rotation_z);

                XmlElement scale = xmlDoc.CreateElement ("scale");
                XmlElement scale_x = xmlDoc.CreateElement ("x");
                scale_x.InnerText = obj.transform.localScale.x + "";
                XmlElement scale_y = xmlDoc.CreateElement ("y");
                scale_y.InnerText = obj.transform.localScale.y + "";
                XmlElement scale_z = xmlDoc.CreateElement ("z");
                scale_z.InnerText = obj.transform.localScale.z + "";

                scale.AppendChild (scale_x);
                scale.AppendChild (scale_y);
                scale.AppendChild (scale_z);

                transform.AppendChild (position);
                transform.AppendChild (rotation);
                transform.AppendChild (scale);    

                gameObject.AppendChild (transform);
                root.AppendChild (gameObject);
            }
        }

        xmlDoc.AppendChild(root);
        xmlDoc.Save(filepath);
        Debug.Log(">>>>> FILEPATH = " + filepath);
        //刷新Project视图, 不然需要手动刷新哦
        //AssetDatabase.Refresh();
        AssetDatabase.Refresh();
    }
}

简单转换后拿到一串这样的...

add_model('SM_Env_WaterEdge_Straight_03',[-50.0, 0.0, -155.0],[0.0, 0.0, 0.0],[1.0, 1.0, 1.0]);
add_model('SM_Env_Sidewalk_01',[-85.0, 0.0, 10.0],[0.0, 0.0, 0.0],[1.0, 1.0, 1.0]);
add_model('SM_Env_Sidewalk_01',[-80.0, 0.0, 35.0],[0.0, 0.0, 0.0],[1.0, 1.0, 1.0]);

......

用THREE里面的FBXLoader就可以载入了,坐标系需要调整。

var loader = new THREE.FBXLoader();
var defaultmap = (new THREE.TextureLoader()).load('models/PolygonCity_Texture.png');
function add_model(modname, pos, rot, sca){
    console.log("add model " + modname);
    loader.load( 'models/'+Kname(modname), function ( xinst ) {
        xinst.scale.set(-sca[0],sca[1],-sca[2]);
        var radi = Math.PI*2/360;
        xinst.rotation.set(rot[0]*radi, (180-rot[1])*radi, rot[2]*radi);
        xinst.position.set(-pos[0],pos[1],pos[2]);
        xinst.children[0].material.map = defaultmap;
        scene.add( xinst );});}

 

转载于:https://my.oschina.net/u/3151032/blog/3000748

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: