Beispiel #1
0
 public bool Hit(ModuleMesh tankMesh)
 {
     return(_hitMeshs.Contains(tankMesh));
 }
        public ModuleMesh(ModuleModel model, IFileLocator fileLocator, Device device, GraphicsSettings graphicsSettings)
        {
            _graphicsSettings = graphicsSettings;

            this.LogInfo("load tank mesh {0} from {1}", model.ModelName, model.TankInstance.Tank.Name);

            this.Tank = model.TankInstance.Tank;
            _device   = device;

            if (model.Visual == null)
            {
                throw new ArgumentException("model.Visual is null", nameof(model));
            }

            if (model.Primitives == null)
            {
                throw new ArgumentException("model.Primitives is null", nameof(model));
            }

            var verticesMap = model.Primitives.Vertices.ToDictionary(k => k.Key, k => ModuleMesh.ConvertToVertexBuffer(k.Value, device));
            var indicesMap  = model.Primitives.Indices.ToDictionary(k => k.Key, k => ModuleMesh.ConvertToIndexBuffer(k.Value, device));

            foreach (var renderSet in model.Visual.RenderSets)
            {
                //renderSet.Geometry.PrimitiveName
                var vState      = verticesMap[renderSet.Geometry.VerticesName];
                var indices     = indicesMap[renderSet.Geometry.IndicesName];
                var rawVertices = model.Primitives.Vertices[renderSet.Geometry.VerticesName].Vertices;
                var rawIndices  = model.Primitives.Indices[renderSet.Geometry.IndicesName];

                foreach (var group in renderSet.Geometry.ModelPrimitiveGroups.Values)
                {
                    RenderGroup renderGroup;

                    if (group.Sectioned)
                    {
                        renderGroup = new RenderGroup(graphicsSettings)
                        {
                            MinVertexIndex = (int)group.StartVertex,
                            VerticesCount  = (int)group.VerticesCount,
                            StartIndex     = (int)group.StartIndex,
                            PrimitiveCount = (int)group.PrimitiveCount,
                        };
                    }
                    else
                    {
                        renderGroup = new RenderGroup(graphicsSettings)
                        {
                            MinVertexIndex = 0,
                            VerticesCount  = vState.Count,
                            StartIndex     = 0,
                            PrimitiveCount = ((int)indices.Tag) / 3,
                        };
                    }

                    renderGroup.VertexState = vState;
                    renderGroup.Indices     = indices;
                    renderGroup.RawVertices = rawVertices;
                    renderGroup.RawIndices  = rawIndices;

                    if (group.Material.ShowArmor)
                    {
                        renderGroup.RenderArmor = true;
                        renderGroup.Textures    = null;
                        renderGroup.ArmorGroup  = group.Material.Armor;
                    }
                    else
                    {
                        renderGroup.RenderArmor = false;

                        var textures = new Dictionary <string, Texture>();

                        foreach (var property in group.Material.Properties)
                        {
                            var texturePath = property.Texture;

                            if (string.IsNullOrWhiteSpace(texturePath))
                            {
                                if (property.Name == "alphaTestEnable" && group.Material.Fx != "shaders/std_effects/PBS_tank.fx")
                                {
                                    renderGroup.AlphaTestEnable = property.BoolValue;
                                }
                                else if (property.Name == "alphaReference")
                                {
                                    renderGroup.AlphaReference = property.IntValue;
                                }
                                else if (property.Name == "g_useNormalPackDXT1")
                                {
                                    renderGroup.UseNormalPackDXT1 = property.BoolValue;
                                }
                                else if (property.Name == "g_detailPower")
                                {
                                    renderGroup.DetailPower = property.FloatValue;
                                }
                                else if (property.Name == "g_metallicDetailUVTiling")
                                {
                                    renderGroup.DetailUVTiling = property.Vector4Value;
                                }
                            }
                            else
                            {
                                try
                                {
                                    using (var stream = ModuleMesh.OpenTexture(fileLocator, texturePath))
                                    {
                                        var info    = ImageInformation.FromStream(stream, true);
                                        var texture = Texture.FromStream(device, stream);
                                        textures[property.Name] = texture;

                                        if (property.Name == "normalMap" && !renderGroup.UseNormalBC1)
                                        {
                                            if (info.Format == Format.Dxt1)
                                            {
                                                renderGroup.UseNormalBC1 = true;
                                            }
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    this.LogInfo("can't load texture {0} for {1}", texturePath, property.Name);
                                }
                            }
                        }

                        renderGroup.Textures = textures;
                    }

                    _renderGroups.Add(renderGroup);
                }
            }
        }