Example #1
0
        private void ParseFile_Files(ref I3DModel model, XmlReader xml)
        {
            xml.ReadAndMove();

            Stack <I3DFile> files = new Stack <I3DFile>();

            while (xml.NotAtEnd())
            {
                string sFileId       = xml.GetAttribute("fileId");
                string sRelativePath = xml.GetAttribute("relativePath");
                if (sFileId != null && sRelativePath != null)
                {
                    I3DFile file = new I3DFile
                    {
                        FileName     = xml.GetAttribute("filename"),
                        Id           = int.Parse(sFileId),
                        RelativePath = bool.Parse(sRelativePath)
                    };

                    string abspath = Path.Combine(model.Path, file.FileName);

                    if (!File.Exists(abspath))
                    {
                        string newname = ReplaceExtension(file.FileName, "dds");
                        abspath = Path.Combine(model.Path, newname);

                        /*if(!File.Exists(abspath))
                         *  throw new FileNotFoundException(abspath);*/
                        file.FileName = newname;
                    }

                    file.AbsolutePath = abspath;

                    files.Push(file);
                }

                xml.ReadAndMove();
            }

            model.Files = files.ToArray();
        }
Example #2
0
        private static void ParseFile_SceneShapesAttributes(XmlReader xml, ref I3DModel model, Entity part)
        {
            Entity.ShapeType type;
            if (!Enum.TryParse(xml.LocalName, out type))
            {
                throw new Exception($"Unrecognized shape type {xml.LocalName}");
            }
            part.Type = type;

            string name = ParseString(xml.GetAttribute("name"));

            //I3DTransform
            I3DTransform t = part.gameObject.AddComponent <I3DTransform>();

            t.Name            = name;
            t.Id              = ParseInt(xml.GetAttribute("nodeId"));
            t.IndexPath       = ""; //TODO: Make this
            t.Visibility      = ParseBool(xml.GetAttribute("visibility"), true);
            t.ClipDistance    = ParseInt(xml.GetAttribute("clipDistance"), 1000000);
            t.MinClipDistance = ParseInt(xml.GetAttribute("minClipDistance"));
            t.ObjectMask      = ParseInt(xml.GetAttribute("objectMask"), 0xffff);
            t.LOD             = ParseBool(xml.GetAttribute("lodDistance"));

            //Transform
            part.gameObject.transform.localPosition    = ParseVector3(xml.GetAttribute("translation"));
            part.gameObject.transform.localEulerAngles = ParseVector3(xml.GetAttribute("rotation"));
            Vector3 scale = xml.GetAttribute("scale") != null?ParseVector3(xml.GetAttribute("scale")) : Vector3.one;

            //scale.x *= -1;
            part.gameObject.transform.localScale = scale;

            //Rigidbody
            bool bStatic    = ParseBool(xml.GetAttribute("static"));
            bool bDynamic   = ParseBool(xml.GetAttribute("dynamic"));
            bool bKinematic = ParseBool(xml.GetAttribute("kinematic"));

            if (bStatic || bDynamic || bKinematic)
            {
                t.RigidBody = true;

                I3DRigidBody r = part.gameObject.AddComponent <I3DRigidBody>();
                if (bStatic)
                {
                    r.Type = RigidBodyType.Static;
                }
                else if (bDynamic)
                {
                    r.Type = RigidBodyType.Dynamic;
                }
                else
                {
                    r.Type = RigidBodyType.Kinematic;
                }

                r.Compound         = ParseBool(xml.GetAttribute("compound"));
                r.CompoundChild    = ParseBool(xml.GetAttribute("compoundChild"));
                r.Collision        = ParseBool(xml.GetAttribute("collision"), true);
                r.Trigger          = ParseBool(xml.GetAttribute("trigger"));
                r.CollisionMask    = ParseInt(xml.GetAttribute("collisionMask"));
                r.Restitution      = ParseFloat(xml.GetAttribute("restitution"));
                r.StaticFriction   = ParseFloat(xml.GetAttribute("staticFriction"), 0.5f);
                r.DynamicFriction  = ParseFloat(xml.GetAttribute("dynamicFriction"), 0.5f);
                r.LinearDamping    = ParseFloat(xml.GetAttribute("linearDamping"), 0.5f);
                r.AngularDamping   = ParseFloat(xml.GetAttribute("angularDamping"), 0.5f);
                r.Density          = ParseFloat(xml.GetAttribute("density"), 1);
                r.SolverIterations = ParseInt(xml.GetAttribute("solverIterationCount"), 4);
                //r.Mass = ParseFloat(xml.GetAttribute("mass"), 0.5f);
            }

            //Shape
            if (part.Type == Entity.ShapeType.Shape)
            {
                if (name.EndsWith("_lod0") || name.EndsWith("_lod1") || name.EndsWith("_lod2"))
                {
                    Transform parent   = part.transform.parent;
                    LODGroup  lodGroup = parent.GetComponent <LODGroup>();
                    if (lodGroup == null)
                    {
                        lodGroup = parent.gameObject.AddComponent <LODGroup>();
                        lodGroup.SetLODs(new LOD[] {});
                    }

                    int lodIndex = ParseInt(name.Substring(name.Length - 1));

                    LOD lod = new LOD {
                        renderers = new Renderer[] { part.GetComponent <MeshRenderer>() }
                    };

                    List <LOD> lods = lodGroup.GetLODs().ToList();

                    if (lodIndex > lods.Count)
                    {
                        lods.Add(lod);
                    }
                    else
                    {
                        lods.Insert(lodIndex, lod);
                    }

                    // Update the transition point for all LODs
                    float lodGap = 1f / lods.Count;
                    for (int i = 0; i < lods.Count; i++)
                    {
                        lods[i] = new LOD((lods.Count - i - 1) * lodGap, lods[i].renderers);
                    }

                    lodGroup.SetLODs(lods.ToArray());
                    lodGroup.RecalculateBounds();
                }

                Shape s = part.gameObject.AddComponent <Shape>();
                s.ID               = ParseInt(xml.GetAttribute("shapeId"));
                s.CastShadows      = ParseBool(xml.GetAttribute("castsShadows"));
                s.ReceiveShadows   = ParseBool(xml.GetAttribute("receiveShadows"));
                s.NonRenderable    = ParseBool(xml.GetAttribute("nonRenderable"));
                s.BuildNavMeshMask = ParseInt(xml.GetAttribute("buildNavMeshMask"));
                s._Materials       = Shape.ParseMaterialString(ParseString(xml.GetAttribute("materialIds")));

                part.Shape = s;

                //Assign the shape object according to ID
                foreach (I3DShapeData sh in model.ShapeDatas)
                {
                    if (part.Shape.ID != sh.ID)
                    {
                        continue;
                    }

                    part.Shape.ID   = sh.ID;
                    part.Shape.Name = sh.Name;
                    part.Shape.Mesh = sh.Mesh;

                    part.GetComponent <MeshFilter>().mesh = sh.Mesh;
                    break;
                }

                //Assign material according to ID
                part.Shape.Materials = new I3DMaterial[part.Shape._Materials.Length];

                for (int i = 0; i < part.Shape._Materials.Length; i++)
                {
                    int shapeMatId = part.Shape._Materials[i];
                    foreach (I3DMaterial mat in model.Materials)
                    {
                        if (mat.Id != shapeMatId)
                        {
                            continue;
                        }

                        part.Shape.Materials[i] = mat;
                    }
                }
            }
            else if (part.Type == Entity.ShapeType.TerrainTransformGroup)
            {
                int heightMapId = ParseInt(xml.GetAttribute("heightMapId"));
                //int patchSize = ParseInt(xml.GetAttribute("patchSize"));
                float heightScale   = ParseFloat(xml.GetAttribute("heightScale"));
                float unitsPerPixel = ParseFloat(xml.GetAttribute("unitsPerPixel"));

                I3DFile heightMapFile = model.GetFile(heightMapId);
                if (heightMapFile != null)
                {
                    I3DTerrain i3DTerrain = part.gameObject.AddComponent <I3DTerrain>();
                    i3DTerrain.AlphaWidth             = ParseInt(xml.GetAttribute("lodTextureSize"));
                    i3DTerrain.AlphaHeight            = ParseInt(xml.GetAttribute("lodTextureSize"));
                    i3DTerrain.Terrain                = part.gameObject.AddComponent <Terrain>();
                    i3DTerrain.TerrainData            = new TerrainData();
                    i3DTerrain.TerrainCollider        = part.gameObject.AddComponent <TerrainCollider>();
                    i3DTerrain.HeightMapScale         = heightScale;
                    i3DTerrain.HeightMapUnitsPerPixel = unitsPerPixel;
                    i3DTerrain.HeightMap              = TextureLoader.GetTexture(heightMapFile.AbsolutePath);

                    i3DTerrain.BuildHeightmap();
                }
            }
            else if (part.Type == Entity.ShapeType.Layer)
            {
                I3DFile detailMapFile = model.GetFile(ParseInt(xml.GetAttribute("detailMapId"))); // Detail map = diffuse texture
                I3DFile normalMapFile = model.GetFile(ParseInt(xml.GetAttribute("normalMapId"))); // Normal map = normal texture
                I3DFile weightMapFile = model.GetFile(ParseInt(xml.GetAttribute("weightMapId"))); // Weight map = splat map?
                //I3DFile distanceMapFile = model.GetFile(ParseInt(xml.GetAttribute("distanceMapId"))); // Distance map = low-res diffuse texture

                int unitSize = ParseInt(xml.GetAttribute("unitSize"));
                //int distanceMapUnitSize = ParseInt(xml.GetAttribute("distanceMapUnitSize")); // Unknown

                SplatPrototype splat = new SplatPrototype
                {
                    texture    = TextureLoader.GetTexture(detailMapFile.AbsolutePath),
                    normalMap  = TextureLoader.GetTexture(normalMapFile.AbsolutePath),
                    tileSize   = new Vector2(unitSize, unitSize),
                    tileOffset = Vector2.zero
                };

                I3DTerrain i3Dterrain = part.GetComponentInParent <I3DTerrain>();
                i3Dterrain.Layers.Add(new I3DTerrainLayer
                {
                    Priority   = ParseInt(xml.GetAttribute("priority")),
                    Attributes = ParseVector4(xml.GetAttribute("attributes")),
                    SplatMap   = splat,
                    Weights    = I3DTerrainUtil.Parse8BitMap(TextureLoader.GetTexture(weightMapFile.AbsolutePath))
                });
            }
        }
Example #3
0
        private static void ParseFile_Materials(ref I3DModel model, XmlReader xml)
        {
            xml.ReadAndMove();

            Stack <I3DMaterial> mats = new Stack <I3DMaterial>();

            while (xml.NotAtEnd())
            {
                string sName  = xml.GetAttribute("name");
                string sMatId = xml.GetAttribute("materialId");
                if (sName != null && sMatId != null)
                {
                    I3DMaterial mat = new I3DMaterial
                    {
                        Name           = sName,
                        Id             = ParseInt(sMatId),
                        CosPower       = ParseInt(xml.GetAttribute("cosPower")),
                        AlphaBlending  = ParseBool(xml.GetAttribute("alphaBlending")),
                        CustomShaderId = ParseInt(xml.GetAttribute("customShaderId")),
                        AmbientColor   = ParseColor(xml.GetAttribute("ambientColor")),
                        DiffuseColor   = ParseColor(xml.GetAttribute("diffuseColor")),
                        SpecularColor  = ParseColor(xml.GetAttribute("specularColor"))
                    };

                    xml.ReadAndMove();

                    Dictionary <string, I3DFile> customMaps       = new Dictionary <string, I3DFile>();
                    Dictionary <string, string>  customParameters = new Dictionary <string, string>();

                    while (xml.NodeType != XmlNodeType.EndElement)
                    {
                        int fileId = ParseInt(xml.GetAttribute("fileId"));

                        I3DFile file = model.GetFile(fileId);

                        switch (xml.LocalName)
                        {
                        case "Emissivemap":
                            mat.EmissiveMapFile = file;
                            break;

                        case "Texture":
                            mat.TextureFile = file;
                            break;

                        case "Normalmap":
                            mat.NormalMapFile = file;
                            break;

                        case "Glossmap":
                            mat.GlossMapFile = file;
                            break;

                        case "Reflectionmap":
                            mat.ReflectionMap = file;
                            break;

                        case "Custommap":
                            customMaps[ParseString(xml.GetAttribute("name"))] = file;
                            break;

                        case "CustomParameter":
                            customParameters[ParseString(xml.GetAttribute("name"))] = ParseString(xml.GetAttribute("value"));
                            break;
                        }

                        xml.ReadAndMove();
                    }

                    mat.CustomMaps       = customMaps;
                    mat.CustomParameters = customParameters;

                    mats.Push(mat);
                }

                xml.ReadAndMove();
            }

            model.Materials = mats.ToArray();
        }