Пример #1
0
 public void ImportedGeometryUpdate(ImportedGeometry geometry, ImportedGeometryInfo info)
 {
     ImportedGeometryUpdate(new[] { new ImportedGeometryUpdateInfo(geometry, info) });
 }
Пример #2
0
        public void Update(LevelSettings settings, Dictionary <string, Texture> absolutePathTextureLookup, ImportedGeometryInfo info)
        {
            Info          = info;
            LoadException = null;
            DirectXModel  = null;
            Textures.Clear();

            try
            {
                string importedGeometryPath      = settings.MakeAbsolute(info.Path);
                string importedGeometryDirectory = Path.GetDirectoryName(importedGeometryPath);

                // Invoke the TombLib geometry import code
                var settingsIO = new IOGeometrySettings
                {
                    Scale          = info.Scale,
                    SwapXY         = info.SwapXY,
                    SwapXZ         = info.SwapXZ,
                    SwapYZ         = info.SwapYZ,
                    FlipX          = info.FlipX,
                    FlipY          = info.FlipY,
                    FlipZ          = info.FlipZ,
                    FlipUV_V       = info.FlipUV_V,
                    InvertFaces    = info.InvertFaces,
                    UseVertexColor = true
                };

                BaseGeometryImporter importer = BaseGeometryImporter.CreateForFile(importedGeometryPath, settingsIO, absoluteTexturePath =>
                {
                    return(GetOrAddTexture(absolutePathTextureLookup, importedGeometryDirectory, absoluteTexturePath));
                });
                var tmpModel = importer.ImportFromFile(importedGeometryPath);

                SynchronizationContext.Current.Post(unused => // Synchronize DirectX, we can't 'send' because that may deadlock with the level settings reloader
                {
                    if (Device == null)
                    {
                        return;
                    }

                    // Create a new static model
                    DirectXModel             = new Model(Device, info.Scale);
                    DirectXModel.BoundingBox = tmpModel.BoundingBox;

                    // Create materials
                    foreach (var tmpMaterial in tmpModel.Materials)
                    {
                        var material              = new Material(tmpMaterial.Name);
                        material.Texture          = tmpMaterial.Texture;
                        material.AdditiveBlending = tmpMaterial.AdditiveBlending;
                        material.DoubleSided      = tmpMaterial.DoubleSided;
                        DirectXModel.Materials.Add(material);
                    }

                    // Loop for each mesh loaded in scene
                    foreach (var mesh in tmpModel.Meshes)
                    {
                        var modelMesh = new ImportedGeometryMesh(Device, mesh.Name);

                        modelMesh.HasVertexColors = (mesh.Colors.Count != 0);

                        var currentIndex = 0;
                        var currPoly     = 0;
                        foreach (var tmpSubmesh in mesh.Submeshes)
                        {
                            var material = DirectXModel.Materials[tmpModel.Materials.IndexOf(tmpSubmesh.Value.Material)];
                            var submesh  = new Submesh(material);

                            foreach (var tmpPoly in tmpSubmesh.Value.Polygons)
                            {
                                if (tmpPoly.Shape == IOPolygonShape.Quad)
                                {
                                    var vertexList = new List <ImportedGeometryVertex>();

                                    for (var i = 0; i < 4; i++)
                                    {
                                        var vertex      = new ImportedGeometryVertex();
                                        vertex.Position = mesh.Positions[tmpPoly.Indices[i]];
                                        vertex.Color    = tmpPoly.Indices[i] < mesh.Colors.Count ? mesh.Colors[tmpPoly.Indices[i]].To3() : Vector3.One;
                                        vertex.UV       = tmpPoly.Indices[i] < mesh.UV.Count ? mesh.UV[tmpPoly.Indices[i]] : Vector2.Zero;
                                        vertex.Normal   = tmpPoly.Indices[i] < mesh.Normals.Count ? mesh.Normals[tmpPoly.Indices[i]] : Vector3.Zero;
                                        vertexList.Add(vertex);
                                    }

                                    // HACK: Triangulate and disjoint quad faces for imported geometry, because otherwise another hack which joints
                                    // disjointed vertices together will fail in Rooms.cs

                                    submesh.Indices.Add(currentIndex);
                                    submesh.Indices.Add(currentIndex + 1);
                                    submesh.Indices.Add(currentIndex + 2);
                                    submesh.Indices.Add(currentIndex + 3);
                                    submesh.Indices.Add(currentIndex + 4);
                                    submesh.Indices.Add(currentIndex + 5);

                                    modelMesh.Vertices.Add(vertexList[0]);
                                    modelMesh.Vertices.Add(vertexList[1]);
                                    modelMesh.Vertices.Add(vertexList[2]);
                                    modelMesh.Vertices.Add(vertexList[0]);
                                    modelMesh.Vertices.Add(vertexList[2]);
                                    modelMesh.Vertices.Add(vertexList[3]);

                                    currentIndex += 6;
                                }
                                else
                                {
                                    for (var i = 0; i < 3; i++)
                                    {
                                        var vertex      = new ImportedGeometryVertex();
                                        vertex.Position = mesh.Positions[tmpPoly.Indices[i]];
                                        vertex.Color    = tmpPoly.Indices[i] < mesh.Colors.Count ? mesh.Colors[tmpPoly.Indices[i]].To3() : Vector3.One;
                                        vertex.UV       = tmpPoly.Indices[i] < mesh.UV.Count ? mesh.UV[tmpPoly.Indices[i]] : Vector2.Zero;
                                        vertex.Normal   = tmpPoly.Indices[i] < mesh.Normals.Count ? mesh.Normals[tmpPoly.Indices[i]] : Vector3.Zero;
                                        modelMesh.Vertices.Add(vertex);
                                        submesh.Indices.Add(currentIndex);
                                        currentIndex++;
                                    }
                                }

                                currPoly++;
                            }

                            modelMesh.Submeshes.Add(material, submesh);
                        }

                        DirectXModel.Meshes.Add(modelMesh);
                    }

                    DirectXModel.UpdateBuffers();
                }, null);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception exc)
            {
                LoadException = exc;
                DirectXModel  = null;
                logger.Warn(exc, "Unable to load model \"" + info.Name + "\" from \"" + info.Path + "\" because an exception occurred during loading.");
            }
        }