예제 #1
0
        private static ThreeDSFileData ReadEDIT3DS(FileStream file, Chunk thischunk)
        {
            ThreeDSFileData data = new ThreeDSFileData();

            uint bytesRead = 6;

            while (bytesRead < thischunk.length)
            {
                Chunk chunk = ReadChunk(file);

                switch ((ChunkCodes)chunk.ID)
                {
                case ChunkCodes.BLANK:
                    break;

                case ChunkCodes.NAMED_OBJECT:
                    data.objects.Add(ReadNAMED_OBJECT(file, chunk));
                    break;

                case ChunkCodes.MATERIAL:
                    data.materials.Add(ReadMATERIAL(file, chunk));
                    break;

                default:
                    file.Seek(chunk.length - 6, SeekOrigin.Current);
                    break;
                }

                bytesRead += chunk.length;
            }

            return(data);
        }
예제 #2
0
        public static ThreeDSFileData ParseFile(string filename)
        {
            FileStream file = new FileStream(filename, FileMode.Open);

            Chunk first = ReadChunk(file);

            // Check file
            if (first.ID != (ushort)ChunkCodes.PRIMARY)
            {
                throw new Exception("Not a valid 3ds file");
            }

            ThreeDSFileData data = null;

            // Read top level
            uint bytesRead = 6;

            while (bytesRead < first.length)
            {
                Chunk chunk = ReadChunk(file);

                switch ((ChunkCodes)chunk.ID)
                {
                case ChunkCodes.BLANK:
                    break;

                case ChunkCodes.EDIT3DS:
                    data = ReadEDIT3DS(file, chunk);
                    break;

                /*case ChunkCodes.KEYF3DS:
                 *  ReadKEYF3DS(file, chunk);
                 *  break;*/
                default:
                    file.Seek(chunk.length - 6, SeekOrigin.Current);
                    break;
                }

                if (chunk.length == 0)
                {
                    break;
                }
                bytesRead += chunk.length;
            }

            file.Close();

            return(data);
        }
예제 #3
0
        private static void Parse3DSData(DataReader3DS dataSegment, ThreeDSFileData file)
        {
            DataReader3DS subSegment = dataSegment.GetNextSubSegment();

            while (subSegment != null)
            {
                switch ((ChunkCodes)subSegment.Tag)
                {
                case ChunkCodes.MATERIAL:
                    file.materials.Add(ParseMaterialData(subSegment));
                    break;

                case ChunkCodes.NAMED_OBJECT:
                    file.objects.Add(ParseMeshData(subSegment));
                    break;

                default:
                    break;
                }
                subSegment = dataSegment.GetNextSubSegment();
            }
        }
예제 #4
0
        static ThreeDSFileData ParseFromData(DataReader3DS data)
        {
            DataReader3DS dataSubSegment = null;

            if ((ChunkCodes)data.Tag == ChunkCodes.PRIMARY)
            {
                dataSubSegment = data.GetNextSubSegment();
            }
            else
            {
                return(null);
            }

            ThreeDSFileData file = new ThreeDSFileData();

            while (dataSubSegment != null)
            {
                // Check the tag to see what sort of data is in this subsegment (or "chunk")
                switch ((ChunkCodes)dataSubSegment.Tag)
                {
                case ChunkCodes.VERSION:
                    file.version = dataSubSegment.GetUShort();
                    break;

                case ChunkCodes.EDIT3DS:
                    Parse3DSData(dataSubSegment, file);
                    break;

                default:
                    break;
                }
                dataSubSegment = data.GetNextSubSegment();
            }

            return(file);
        }
예제 #5
0
        static ThreeDSFileData ParseFromData(DataReader3DS data)
        {
            DataReader3DS dataSubSegment = null;

            if ((ChunkCodes)data.Tag == ChunkCodes.PRIMARY)
            {
                dataSubSegment = data.GetNextSubSegment();
            }
            else
                return null;

            ThreeDSFileData file = new ThreeDSFileData();

            while (dataSubSegment != null)
            {
                // Check the tag to see what sort of data is in this subsegment (or "chunk")
                switch ((ChunkCodes)dataSubSegment.Tag)
                {
                    case ChunkCodes.VERSION:
                        file.version = dataSubSegment.GetUShort();
                        break;
                    case ChunkCodes.EDIT3DS:
                        Parse3DSData(dataSubSegment, file);
                        break;
                    default:
                        break;
                }
                dataSubSegment = data.GetNextSubSegment();
            }

            return file;
        }
예제 #6
0
        private static void Parse3DSData(DataReader3DS dataSegment, ThreeDSFileData file)
        {
            DataReader3DS subSegment = dataSegment.GetNextSubSegment();

            while (subSegment != null)
            {
                switch ((ChunkCodes)subSegment.Tag)
                {
                    case ChunkCodes.MATERIAL:
                        file.materials.Add(ParseMaterialData(subSegment));
                        break;
                    case ChunkCodes.NAMED_OBJECT:
                        file.objects.Add(ParseMeshData(subSegment));
                        break;
                    default:
                        break;
                }
                subSegment = dataSegment.GetNextSubSegment();
            }
        }
예제 #7
0
        public ABScene3D LoadScene(string file)
        {
            string path = file.Substring(0, file.Length - Path.GetFileName(file).Length);
            // parse file into 3ds structures
            ThreeDSFileData data = ThreeDSParser.ParseFromFile(file);

            // convert to AB
            ABScene3D scene = new ABScene3D();

            ABMaterial[] materials = null;
            if (data.materials != null && data.materials.Count > 0)
            {
                materials = new ABMaterial[data.materials.Count];
                for (int mIdx = 0; mIdx < data.materials.Count; mIdx++)
                {
                    ABMaterial material = materials[mIdx] = new ABMaterial();
                    material.Name = data.materials[mIdx].name;

                    material.Ambient   = new ABColorARGB();
                    material.Ambient.A = 255;
                    material.Ambient.R = data.materials[mIdx].ambient.r;
                    material.Ambient.G = data.materials[mIdx].ambient.g;
                    material.Ambient.B = data.materials[mIdx].ambient.b;

                    material.Diffuse   = new ABColorARGB();
                    material.Diffuse.A = 255;
                    material.Diffuse.R = data.materials[mIdx].diffuse.r;
                    material.Diffuse.G = data.materials[mIdx].diffuse.g;
                    material.Diffuse.B = data.materials[mIdx].diffuse.b;

                    // convert to absolute path
                    if (data.materials[mIdx].texture == null || Path.IsPathRooted(data.materials[mIdx].texture))
                    {
                        material.TextureName = data.materials[mIdx].texture;
                    }
                    else
                    {
                        material.TextureName = path + data.materials[mIdx].texture;
                    }

                    // match file
                    if (material.TextureName != null)
                    {
                        string   filename = Path.GetFileNameWithoutExtension(material.TextureName).ToLower();
                        string   ext      = Path.GetExtension(material.TextureName).ToLower();
                        string[] files    = Directory.GetFiles(path);
                        foreach (string fn in files)
                        {
                            string fno = Path.GetFileNameWithoutExtension(fn).ToLower();
                            if (fno.StartsWith(filename) && Path.GetExtension(fn).ToLower() == ext)
                            {
                                material.TextureName = fn;
                                break;
                            }
                        }
                    }
                }
            }

            int numMeshes = data.GetNumFaceGroups();

            if (numMeshes > 0)
            {
                scene.Models = new ABModel3D[numMeshes];
                int meshIdx = 0;
                foreach (ThreeDSObject obj in data.objects)
                {
                    foreach (ThreeDSMesh mesh in obj.meshes)
                    {
                        scene.Models[meshIdx++] = ConvertMesh(mesh, materials);
                    }
                }
            }
            return(scene);
        }