Exemple #1
0
        //scene 全局数据
        private static void SerializeSceneData(Scene scene, ObjPack objPack)
        {
            if (RenderSettings.ambientMode == AmbientMode.Trilight)
            {
                var d = new ObjDataHemisphereLight
                {
                    skyColor    = ObjPack.Color(RenderSettings.ambientSkyColor),
                    groundColor = ObjPack.Color(RenderSettings.ambientGroundColor),
                };
                objPack.sceneDatas.Add(d);
            }
            else if (RenderSettings.ambientMode == AmbientMode.Flat)
            {
                var d = new ObjDataAmbientLight()
                {
                    color = ObjPack.Color(RenderSettings.ambientLight)
                };
                objPack.sceneDatas.Add(d);
            }

            if (RenderSettings.skybox != null)
            {
                if (RenderSettings.skybox.shader.name == "Skybox/6 Sided")
                {
                    var d = new ObjDataSkybox(RenderSettings.skybox);
                    objPack.sceneDatas.Add(d);
                }
            }
        }
Exemple #2
0
 public static void Serialize(GameObject[] gameObjects, ObjPack objPack)
 {
     foreach (var go in gameObjects)
     {
         if (go.activeSelf == false)
         {
             continue;
         }
         Serialize(go, objPack);
     }
 }
Exemple #3
0
        public static void SaveToThreejs()
        {
            var objPack        = new ObjPack();
            var scene          = SceneManager.GetActiveScene();
            var allGameObjects = scene.GetRootGameObjects();

            CreateObjectID(allGameObjects);
            SerializeSceneData(scene, objPack);
            Serialize(allGameObjects, objPack);
            var json = JsonConvert.SerializeObject(objPack);// JsonUtility.ToJson(objPack);

            AssetDatabase.SaveScene(json, scene.name);
            EditorUtility.DisplayDialog("Threejs Editor", "saved", "OK");
        }
        public ObjDataDirectionalLight(Light light)
        {
            type       = TypeConst.DirectionalLight;
            castShadow = light.shadows != LightShadows.None;
            intensity  = light.intensity;
            color      = ObjPack.Color(light.color);
            var v3 = light.transform.rotation * Vector3.back * 2;

            position = ObjPack.Vector3(v3);


            if (light.transform.TryGetComponent <ShadowSettings>(out var settings))
            {
                shadowNear     = settings.shadowNear;
                shadowFar      = settings.shadowFar;
                shadowMapSizeW = settings.shadowMapSizeW;
                shadowMapSizeH = settings.shadowMapSizeH;

                shadowCameraLeft   = settings.shadowCameraLeft;
                shadowCameraRight  = settings.shadowCameraRight;
                shadowCameraTop    = settings.shadowCameraTop;
                shadowCameraBottom = settings.shadowCameraBottom;
            }
        }
Exemple #5
0
        public static void Serialize(GameObject gameObject, ObjPack objPack, string parent = "")
        {
            if (!gameObject.TryGetComponent <ObjectID>(out var objectID))
            {
                return;
            }
            var obj = new Obj();

            obj.type = "group";
            obj.id   = objectID.ID.ToString();
            var pos = gameObject.transform.localPosition;

            obj.position = new [] { pos.x, pos.y, pos.z };
            var q = gameObject.transform.localRotation;

            obj.quaternion = new[] { q.x, q.y, q.z, q.w };
            var s = gameObject.transform.localScale;

            obj.scale  = new[] { s.x, s.y, s.z };
            obj.parent = parent;

            //var m = Matrix4x4.identity;
            //m.SetTRS(translation, rotation, scale);

            if (gameObject.TryGetComponent <MeshFilter>(out var meshFilter))
            {
                var geometrie = AssetDatabase.TrySave <Geometrie>(meshFilter.sharedMesh);
                if (geometrie != null)
                {
                    obj.type = "mesh";
                    objPack.Add(geometrie);
                    obj.geometry = geometrie.id;
                }
            }
            if (gameObject.TryGetComponent <MeshRenderer>(out var meshRenderer))
            {
                if (meshRenderer.sharedMaterials.Length > 1)
                {
                    var list = new List <string>();
                    for (var i = 0; i < meshRenderer.sharedMaterials.Length; i++)
                    {
                        var mat = AssetDatabase.TrySave <Mat>(meshRenderer.sharedMaterials[i]);
                        if (mat != null)
                        {
                            objPack.Add(mat);
                            list.Add(mat.id);
                        }
                    }
                    obj.materials = list.ToArray();
                }
                else
                {
                    var mat = AssetDatabase.TrySave <Mat>(meshRenderer.sharedMaterial);
                    if (mat != null)
                    {
                        objPack.Add(mat);
                        obj.material = mat.id;
                    }
                }
            }

            SaveData(gameObject, obj);

            objPack.objects.Add(obj);

            var childCount = gameObject.transform.childCount;

            if (childCount > 0)
            {
                for (var i = 0; i < childCount; i++)
                {
                    var childGo = gameObject.transform.GetChild(i).gameObject;
                    Serialize(childGo, objPack, obj.id);
                }
            }
        }