예제 #1
0
        private IEnumerator LoadModels(string mapname, List <RSW.ModelDescriptor> modelDescriptors, Action <string, string, object> callback)
        {
            /**
             * Divide by 3 because of each loop in which we increase progress
             */
            float remainingProgress = 100 - Progress;
            float modelProgress     = remainingProgress / modelDescriptors.Count / 3;

            for (int i = 0; i < modelDescriptors.Count; i++)
            {
                var model = modelDescriptors[i];
                model.filename = "data/model/" + model.filename;

                FileManager.Load(model.filename);
                Progress += modelProgress;

                if (i % BATCH_SIZE == 0)
                {
                    yield return(new WaitForEndOfFrame());
                }
            }

            //create model instances
            HashSet <RSM> objectsSet = new HashSet <RSM>();

            for (int i = 0; i < modelDescriptors.Count; ++i)
            {
                RSM model = (RSM)FileManager.Load(modelDescriptors[i].filename);
                if (model != null)
                {
                    model.CreateInstance(modelDescriptors[i]);
                    objectsSet.Add(model);
                }
                Progress += modelProgress;

                if (i % BATCH_SIZE == 0)
                {
                    yield return(new WaitForEndOfFrame());
                }
            }
            FileCache.ClearAllWithExt("rsm");
            RSM[] objects = new RSM[objectsSet.Count];
            objectsSet.CopyTo(objects);

            yield return(CompileModels(objects, modelProgress, (compiledModels) =>
            {
                callback.Invoke(mapname, "MAP_MODELS", compiledModels);
            }));
        }
예제 #2
0
    //returns a collection of nodes and its meshes generated from an RSM.
    public static RSM.CompiledModel Compile(RSM rsm)
    {
        var nodesData = new Dictionary <long, RSM.NodeMeshData> [rsm.nodes.Length];

        for (int i = 0; i < rsm.nodes.Length; i++)
        {
            //mesh = union of nodes meshes
            nodesData[i] = rsm.nodes[i].Compile();
        }

        return(new RSM.CompiledModel()
        {
            nodesData = nodesData,
            rsm = rsm,
        });
    }
예제 #3
0
    private static void CalcBoundingBox(RSM rsm)
    {
        var matrix = Mat4.Identity;
        var count  = rsm.nodes.Length;
        var box    = rsm.box;

        CalcNodeBoundingBox(rsm.mainNode, matrix);

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < count; j++)
            {
                box.max[i] = Math.Max(box.max[i], rsm.nodes[j].box.max[i]);
                box.min[i] = Math.Min(box.min[i], rsm.nodes[j].box.min[i]);
            }
            box.offset[i] = (box.max[i] + box.min[i]) / 2.0f;
            box.range[i]  = (box.max[i] - box.min[i]) / 2.0f;
            box.center[i] = box.min[i] + box.range[i];
        }
    }
예제 #4
0
    private List <RSM.CompiledModel> LoadModels(List <RSW.ModelDescriptor> modelDescriptors, GND ground)
    {
        FileManager.InitBatch();

        //queue list of models to load
        for (int i = 0; i < modelDescriptors.Count; i++)
        {
            var model = modelDescriptors[i];
            model.filename = "data/model/" + model.filename;

            FileManager.Load(model.filename);
        }

        //load models
        FileManager.EndBatch();

        //create model instances
        HashSet <RSM> objectsSet = new HashSet <RSM>();

        for (int i = 0; i < modelDescriptors.Count; i++)
        {
            RSM model = (RSM)FileManager.Load(modelDescriptors[i].filename);
            if (model != null)
            {
                model.CreateInstance(modelDescriptors[i]);
                objectsSet.Add(model);
            }
        }
        FileCache.ClearAllWithExt("rsm");
        RSM[] objects = new RSM[objectsSet.Count];
        objectsSet.CopyTo(objects);

        var compiledModels = CompileModels(objects);

        LoadModelsTextures(compiledModels);
        return(compiledModels);
    }
예제 #5
0
    public static RSM Load(BinaryReader data)
    {
        var header = data.ReadBinaryString(4);

        if (header != RSM.Header)
        {
            throw new Exception("ModelLoader.Load: Header (" + header + ") is not \"GRSM\"");
        }

        RSM rsm = new RSM();

        //read infos
        string version    = Convert.ToString(data.ReadUByte());
        string subversion = Convert.ToString(data.ReadUByte());

        version += "." + subversion;
        double dversion = double.Parse(version, CultureInfo.InvariantCulture);

        rsm.version   = version;
        rsm.animLen   = data.ReadLong();
        rsm.shadeType = (RSM.SHADING)data.ReadLong();

        rsm.alpha = dversion >= 1.4 ? data.ReadUByte() / 255f : 1;
        data.Seek(16, System.IO.SeekOrigin.Current);

        //read textures
        int textureCount = data.ReadLong();

        rsm.textures = new string[textureCount];
        for (int i = 0; i < textureCount; i++)
        {
            rsm.textures[i] = data.ReadBinaryString(40);
        }

        //read nodes (meshes)
        rsm.name = data.ReadBinaryString(40);
        int nodeCount = data.ReadLong();

        rsm.nodes = new RSM.Node[nodeCount];

        for (int i = 0; i < nodeCount; i++)
        {
            var node = rsm.nodes[i] = LoadNode(rsm, data, dversion);
            if (string.Equals(node.name, rsm.name))
            {
                rsm.mainNode = node;
            }
        }

        //fallback for non defined main node
        if (rsm.mainNode == null)
        {
            rsm.mainNode = rsm.nodes[0];
        }

        //read poskeyframes
        if (dversion < 1.5)
        {
            int count = data.ReadLong();
            rsm.posKeyframes = new RSM.PositionKeyframe[count];
            for (int i = 0; i < count; i++)
            {
                rsm.posKeyframes[i] = new RSM.PositionKeyframe()
                {
                    frame = data.ReadLong(),
                    p     = new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat())
                };
            }
        }
        else
        {
            rsm.posKeyframes = new RSM.PositionKeyframe[0];
        }

        //read volume box
        int vbCount = data.ReadLong();

        rsm.volumeBoxes = new RSM.VolumeBox[vbCount];

        for (int i = 0; i < vbCount; i++)
        {
            rsm.volumeBoxes[i] = new RSM.VolumeBox()
            {
                size = new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat()),
                pos  = new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat()),
                rot  = new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat()),
                flag = dversion >= 1.3 ? data.ReadLong() : 0
            };
        }

        rsm.instances = new List <RSW.ModelDescriptor>();
        rsm.box       = new RSM.Box();

        CalcBoundingBox(rsm);

        return(rsm);
    }
예제 #6
0
    private static RSM.Node LoadNode(RSM rsm, BinaryReader data, double version)
    {
        RSM.Node node = new RSM.Node();

        node.model  = rsm;
        node.isOnly = rsm.nodes.Length == 1;

        node.name       = data.ReadBinaryString(40);
        node.parentName = data.ReadBinaryString(40);

        //read textures
        int textureCount = data.ReadLong();

        node.textures = new long[textureCount];

        for (int i = 0; i < textureCount; i++)
        {
            node.textures[i] = data.ReadLong();
        }

        //read options
        node.mat3 = new Vector3[] {
            new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat()),
            new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat()),
            new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat())
        };

        node.offset   = new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat());
        node.pos      = new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat());
        node.rotAngle = data.ReadFloat();
        node.rotAxis  = new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat());
        node.scale    = new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat());

        //read vertices
        int verticeCount = data.ReadLong();

        node.vertices = new List <Vector3>();
        for (int i = 0; i < verticeCount; i++)
        {
            node.vertices.Add(new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat()));
        }

        //read textures vertices
        int tverticeCount = data.ReadLong();

        node.tVertices = new float[tverticeCount * 6];
        for (int i = 0; i < tverticeCount; i++)
        {
            if (version >= 1.2)
            {
                node.tVertices[(i * 6) + 0] = data.ReadUByte() / 255f;
                node.tVertices[(i * 6) + 1] = data.ReadUByte() / 255f;
                node.tVertices[(i * 6) + 2] = data.ReadUByte() / 255f;
                node.tVertices[(i * 6) + 3] = data.ReadUByte() / 255f;
            }
            node.tVertices[(i * 6) + 4] = data.ReadFloat() * 0.98f + 0.01f;
            node.tVertices[(i * 6) + 5] = data.ReadFloat() * 0.98f + 0.01f;
        }

        //read faces
        int faceCount = data.ReadLong();

        node.faces = new RSM.Face[faceCount];
        for (int i = 0; i < faceCount; i++)
        {
            node.faces[i] = new RSM.Face()
            {
                vertidx     = new Vector3Int(data.ReadUShort(), data.ReadUShort(), data.ReadUShort()),
                tvertidx    = new Vector3Int(data.ReadUShort(), data.ReadUShort(), data.ReadUShort()),
                texid       = data.ReadUShort(),
                padding     = data.ReadUShort(),
                twoSided    = data.ReadLong(),
                smoothGroup = version >= 1.2 ? data.ReadLong() : 0
            };
        }

        //read position keyframes
        if (version >= 1.5)
        {
            int pkfCount = data.ReadLong();
            for (int i = 0; i < pkfCount; i++)
            {
                node.posKeyframes.Add(data.ReadLong(), new Vector3(data.ReadFloat(), data.ReadFloat(), data.ReadFloat()));
            }
        }

        //read rotation keyframes
        int rkfCount = data.ReadLong();

        for (int i = 0; i < rkfCount; i++)
        {
            int        time = data.ReadLong();
            Quaternion quat = new Quaternion(data.ReadFloat(), data.ReadFloat(), data.ReadFloat(), data.ReadFloat());

            if (!node.rotKeyframes.ContainsKey(time))
            {
                //some models have multiple keyframes with the
                //same timestamp, here we just keep the first one
                //and throw out the rest.
                node.rotKeyframes.Add(time, quat);
            }
        }

        node.box = new RSM.Box();

        return(node);
    }
예제 #7
0
 public ModelCompiler(RSM obj)
 {
     _obj = obj;
 }