Esempio n. 1
0
        public void initialize(GameObject _static_gameobject, DynamicBundle dynamicBundle)
        {
            static_gameobject = _static_gameobject;
            if (static_gameobject.transform.parent != null)
            {
                parent_instance_id = static_gameobject.transform.parent.GetInstanceID();
            }
            instance_id       = static_gameobject.transform.GetInstanceID();
            name              = static_gameobject.name;
            position          = static_gameobject.transform.position.ToString("F4");
            localScale        = static_gameobject.transform.localScale.ToString("F4");
            localEulerAngles  = static_gameobject.transform.localEulerAngles.ToString("F4");
            has_mesh_collider = static_gameobject.GetComponent <MeshCollider>() != null;
            MeshFilter   mf = static_gameobject.GetComponent <MeshFilter>();
            MeshRenderer mr = static_gameobject.GetComponent <MeshRenderer>();

            material_names = new List <string>();
            if (mf != null)
            {
                mesh_name = mf.sharedMesh.name;
                if (!dynamicBundle.meshes_table.ContainsKey(mesh_name))
                {
                    DynamicMesh dynamicMesh = new DynamicMesh();
                    dynamicMesh.initialize(mf.sharedMesh);
                    dynamicMesh.static_mesh_filter   = mf;
                    dynamicMesh.static_mesh_renderer = mr;
                    dynamicBundle.meshes_table.Add(mesh_name, dynamicMesh);
                }
            }
            if (mr != null)
            {
                foreach (var material in mr.sharedMaterials)
                {
                    material_names.Add(material.name);
                    if (!dynamicBundle.materials_table.ContainsKey(material.name))
                    {
                        DynamicMaterial dynamicMaterial = new DynamicMaterial();
                        dynamicMaterial.initialize(material);
                        dynamicBundle.materials_table.Add(material.name, dynamicMaterial);
                        Texture2D texture = (Texture2D)material.mainTexture;
                        if (texture != null)
                        {
                            if (!dynamicBundle.textures_table.ContainsKey(texture.name))
                            {
                                DynamicTexture dynamicTexture = new DynamicTexture();
                                dynamicTexture.initialize(texture);
                                dynamicBundle.textures_table.Add(texture.name, dynamicTexture);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public void Load()
        {
            // Create Meshes
            foreach (string name in meshes_table.Keys)
            {
                DynamicMesh mesh  = meshes_table[name];
                Mesh        _mesh = mesh.GenerateMesh();
                _mesh.name = name;
                mesh.mesh  = _mesh;
            }

            // Create Textures
            foreach (string name in textures_table.Keys)
            {
                DynamicTexture texture = textures_table[name];
                texture.static_texture      = new Texture2D(texture.w, texture.h);
                texture.static_texture.name = name;
                if (texture.texture_data != null)
                {
                    texture.static_texture.LoadImage(Convert.FromBase64String(texture.texture_data));
                }
                else
                {
                    Mod.Settings.Log("Missing texture data for " + name);
                }
                texture.static_texture.Apply();
            }

            // Create Materials
            foreach (string name in materials_table.Keys)
            {
                DynamicMaterial material = materials_table[name];
                material.static_material       = new Material(Shader.Find(material.shader_name));
                material.static_material.name  = name;
                material.static_material.color = new Color(material.colors[0], material.colors[1], material.colors[2], material.colors[3]);
                if (material.texture_name != null)
                {
                    material.static_material.mainTexture = textures_table[material.texture_name].static_texture;
                }
            }

            // Create GameObjects
            foreach (int instance_id in gameobjects_table.Keys)
            {
                var gameobject = gameobjects_table[instance_id];
                gameobject.static_gameobject = new GameObject(gameobject.name);
                if (gameobject.mesh_name != null)
                {
                    var mf = gameobject.static_gameobject.AddComponent <MeshFilter>();
                    var mr = gameobject.static_gameobject.AddComponent <MeshRenderer>();
                    try
                    {
                        mf.mesh = meshes_table[gameobject.mesh_name].mesh;
                    }
                    catch
                    {
                        Debug.Log("Failed to load mesh for " + gameobject.name);
                    }
                    List <Material> materials = new List <Material>();
                    foreach (var material_name in gameobject.material_names)
                    {
                        materials.Add(materials_table[material_name].static_material);
                    }
                    mr.materials = materials.ToArray();
                    if (gameobject.has_mesh_collider)
                    {
                        gameobject.static_gameobject.AddComponent <MeshCollider>();
                    }
                }
            }
            // Configure Hierarchy
            foreach (int instance_id in gameobjects_table.Keys)
            {
                var gameobject = gameobjects_table[instance_id];
                if (gameobject.parent_instance_id != 0)
                {
                    gameobject.static_gameobject.transform.parent = gameobjects_table[gameobject.parent_instance_id].static_gameobject.transform;
                }
            }
            // Set Transformations
            foreach (int instance_id in gameobjects_table.Keys)
            {
                var gameobject = gameobjects_table[instance_id];
                gameobject.static_gameobject.transform.position         = Mod.Helper.ParseVector3(gameobject.position);
                gameobject.static_gameobject.transform.localScale       = Mod.Helper.ParseVector3(gameobject.localScale);
                gameobject.static_gameobject.transform.localEulerAngles = Mod.Helper.ParseVector3(gameobject.localEulerAngles);
            }
        }