Ejemplo n.º 1
0
        public void RecreateMaterials()
        {
            lock (instanceMutationLock) {
                if (!materialsOutOfDate)
                {
                    return;
                }
            }

            FragmentShader fs = GetRecommendedFragmentShader();

            using (RenderingModule.RenderStateBarrier.AcquirePermit(withLock: instanceMutationLock)) {
                currentMaterials.ForEach(kvp => kvp.Value.Dispose());
                currentMaterials.Clear();
                textureViews.ForEach(kvp => {
                    AssetLocator.UnloadTexture(kvp.Key);
                    kvp.Value.Dispose();
                });
                textureViews.Clear();

                foreach (LevelMaterial levelMaterial in materials)
                {
                    Material           material = new Material(levelMaterial.Name + " [" + levelMaterial.ID + "]", fs);
                    ShaderResourceView srv;
                    if (textureViews.ContainsKey(levelMaterial.TextureFileName))
                    {
                        srv = textureViews[levelMaterial.TextureFileName];
                    }
                    else
                    {
                        ITexture2D loadedTexture = AssetLocator.LoadTexture(levelMaterial.TextureFileName, levelMaterial.GenerateMips);
                        srv = loadedTexture.CreateView();
                        textureViews[levelMaterial.TextureFileName] = srv;
                    }
                    material.SetMaterialResource((ResourceViewBinding)fs.GetBindingByIdentifier("DiffuseMap"), srv);
                    if (fs.ContainsBinding("MaterialProperties"))
                    {
                        material.SetMaterialConstantValue(
                            (ConstantBufferBinding)fs.GetBindingByIdentifier("MaterialProperties"),
                            new Vector4(0f, 0f, 0f, levelMaterial.SpecularPower)
                            );
                    }

                    if (fs.ContainsBinding("NormalMap"))
                    {
                        if (levelMaterial.NormalFileName != null)
                        {
                            var loadedNormalMap = AssetLocator.LoadTexture(levelMaterial.NormalFileName, levelMaterial.GenerateMips);
                            srv = loadedNormalMap.CreateView();
                            textureViews[levelMaterial.NormalFileName] = srv;
                        }
                        else
                        {
                            srv = AssetLocator.DefaultNormalMapView;
                        }
                        material.SetMaterialResource((ResourceViewBinding)fs.GetBindingByIdentifier("NormalMap"), srv);
                    }

                    if (fs.ContainsBinding("SpecularMap"))
                    {
                        if (levelMaterial.SpecularFileName != null)
                        {
                            var loadedSpecularMap = AssetLocator.LoadTexture(levelMaterial.SpecularFileName, levelMaterial.GenerateMips);
                            srv = loadedSpecularMap.CreateView();
                            textureViews[levelMaterial.SpecularFileName] = srv;
                        }
                        else
                        {
                            srv = AssetLocator.DefaultSpecularMapView;
                        }
                        material.SetMaterialResource((ResourceViewBinding)fs.GetBindingByIdentifier("SpecularMap"), srv);
                    }

                    if (fs.ContainsBinding("EmissiveMap"))
                    {
                        if (levelMaterial.EmissiveFileName != null)
                        {
                            var loadedEmissiveMap = AssetLocator.LoadTexture(levelMaterial.EmissiveFileName, levelMaterial.GenerateMips);
                            srv = loadedEmissiveMap.CreateView();
                            textureViews[levelMaterial.EmissiveFileName] = srv;
                        }
                        else
                        {
                            srv = AssetLocator.DefaultEmissiveMapView;
                        }
                        material.SetMaterialResource((ResourceViewBinding)fs.GetBindingByIdentifier("EmissiveMap"), srv);
                    }

                    currentMaterials.Add(levelMaterial, material);
                }

                materialsOutOfDate = false;
            }
        }