private MaterialPtr createFromDescription(MaterialDescription description, bool alpha)
        {
            String name = description.Name;

            if (description.CreateAlphaMaterial && alpha) //Is this an automatic alpha material?
            {
                name += "Alpha";
            }
            MaterialPtr material = MaterialManager.getInstance().create(name, GroupName, false, null);

            CreateMaterial createMaterial = createUnifiedMaterial;

            if (description.IsSpecialMaterial && !specialMaterialFuncs.TryGetValue(description.SpecialMaterial, out createMaterial))
            {
                Logging.Log.Error("Could not find special material creation function {0} for material {1} in {2}.", description.SpecialMaterial, description.Name, description.SourceFile);
                createMaterial = createUnifiedMaterial; //Attempt to create something, out above clears this variable
            }

            IndirectionTexture indirectionTex = createMaterial(material.Value.getTechnique(0), description, alpha, true);

            if (alpha)
            {
                //Create no depth check technique
                Technique technique = material.Value.createTechnique();
                technique.setLodIndex(1);
                technique.createPass();
                createMaterial(technique, description, alpha, false);
            }

            //If we have an indirection texture we need to seup the virtual texturing, if not no additional techniques will be created and the
            //entity must disable itself for feedback buffer rendering somehow (likely visibility mask).
            if (indirectionTex != null)
            {
                String vertexShaderName   = shaderFactory.createFeedbackVertexProgram(indirectionTex.FeedbackBufferVPName, description.NumHardwareBones, description.NumHardwarePoses);
                String fragmentShaderName = shaderFactory.createFeedbackBufferFP(indirectionTex.FeedbackBufferFPName);
                indirectionTex.setupFeedbackBufferTechnique(material.Value, vertexShaderName);

                int count = 0;
                if (!indirectionTextureUsageCounts.TryGetValue(indirectionTex.Id, out count))
                {
                    indirectionTextureUsageCounts.Add(indirectionTex.Id, 0);
                }
                else
                {
                    indirectionTextureUsageCounts[indirectionTex.Id] = count + 1;
                }
            }

            material.Value.compile();
            material.Value.load();

            createdMaterials.Add(material.Value, new MaterialInfo(material.Value, indirectionTex));

            return(material);
        }
        internal void destroyMaterial(MaterialPtr material, String builderName)
        {
            MaterialBuilder builder;

            if (materialBuilders.TryGetValue(builderName, out builder))
            {
                builder.destroyMaterial(material);
            }
            else
            {
                Logging.Log.Error("Could not find material builder '{0}' for the destruction of material '{1}'. This builder may have been removed too early.", builderName, material.Value.Name);
            }
        }
        public override void destroyMaterial(MaterialPtr materialPtr)
        {
            MaterialInfo info;

            if (createdMaterials.TryGetValue(materialPtr.Value, out info))
            {
                int count = 0;
                if (info.IndirectionTexture != null && indirectionTextureUsageCounts.TryGetValue(info.IndirectionTexture.Id, out count))
                {
                    --count;
                    if (count == 0)
                    {
                        virtualTextureManager.destroyIndirectionTexture(info.IndirectionTexture);
                        indirectionTextureUsageCounts.Remove(info.IndirectionTexture.Id);
                    }
                    else
                    {
                        indirectionTextureUsageCounts[info.IndirectionTexture.Id] = count;
                    }
                }

                PhysicalTexture physicalTexture;
                foreach (var technique in materialPtr.Value.Techniques)
                {
                    foreach (var pass in technique.Passes)
                    {
                        foreach (var textureUnitState in pass.TextureUnitStates)
                        {
                            if (virtualTextureManager.tryGetPhysicalTexture(textureUnitState.Name, out physicalTexture))
                            {
                                physicalTexture.removeTextureUnit(textureUnitState);
                            }
                        }
                    }
                }

                createdMaterials.Remove(materialPtr.Value);
                MaterialManager.getInstance().remove(materialPtr.Value.Name);
                materialPtr.Dispose();
            }
        }
        public UnifiedMaterialBuilder(VirtualTextureManager virtualTextureManager, CompressedTextureSupport textureFormat, ResourceManager liveResourceManager)
        {
            PixelFormat      otherFormat;
            PixelFormat      normalFormat;
            NormaMapReadMode normalMapReadMode = NormaMapReadMode.RG;

            switch (textureFormat)
            {
            case CompressedTextureSupport.DXT_BC4_BC5:
                textureFormatExtension       = ".dds";
                normalTextureFormatExtension = "_bc5.dds";
                normalMapReadMode            = NormaMapReadMode.RG;
                createOpacityTexture         = true;
                otherFormat  = PixelFormat.PF_DXT5;
                normalFormat = PixelFormat.PF_BC5_UNORM;
                break;

            case CompressedTextureSupport.DXT:
                textureFormatExtension       = ".dds";
                normalTextureFormatExtension = ".dds";
                normalMapReadMode            = NormaMapReadMode.AG;
                createOpacityTexture         = true;
                otherFormat  = PixelFormat.PF_DXT5;
                normalFormat = PixelFormat.PF_DXT5;
                break;

            default:
            case CompressedTextureSupport.None:
                textureFormatExtension       = PagedImage.FileExtension;
                normalTextureFormatExtension = PagedImage.FileExtension;
                normalMapReadMode            = NormaMapReadMode.RG;
                createOpacityTexture         = false;
                otherFormat  = PixelFormat.PF_A8R8G8B8;
                normalFormat = PixelFormat.PF_A8R8G8B8;
                break;
            }

            this.virtualTextureManager = virtualTextureManager;

            if (OgreInterface.Instance.RenderSystem.isShaderProfileSupported("hlsl"))
            {
                shaderFactory = new HlslUnifiedShaderFactory(liveResourceManager, normalMapReadMode, createOpacityTexture);
            }
            else if (OgreInterface.Instance.RenderSystem.isShaderProfileSupported("glsl"))
            {
                shaderFactory = new GlslUnifiedShaderFactory(liveResourceManager, normalMapReadMode, createOpacityTexture);
            }
            else if (OgreInterface.Instance.RenderSystem.isShaderProfileSupported("glsles"))
            {
                shaderFactory = new GlslesUnifiedShaderFactory(liveResourceManager, normalMapReadMode, createOpacityTexture);
            }
            else
            {
                throw new OgreException("Cannot create Unified Material Builder, device must support shader profiles hlsl, glsl, or glsles.");
            }

            diffuseTexture  = virtualTextureManager.createPhysicalTexture("Diffuse", otherFormat);
            normalTexture   = virtualTextureManager.createPhysicalTexture("NormalMap", normalFormat);
            specularTexture = virtualTextureManager.createPhysicalTexture("Specular", otherFormat);

            if (createOpacityTexture)
            {
                opacityTexture = virtualTextureManager.createPhysicalTexture("Opacity", otherFormat);
            }

            specialMaterialFuncs.Add("EyeOuter", createEyeOuterMaterial);
            specialMaterialFuncs.Add("ColorVertex", createColorVertexMaterial);

            OgreResourceGroupManager.getInstance().createResourceGroup(GroupName);

            MaterialPtr hiddenMaterial = MaterialManager.getInstance().create("HiddenMaterial", GroupName, false, null);

            setupHiddenMaterialPass(hiddenMaterial.Value.getTechnique(0).getPass(0), false, false);
            createdMaterials.Add(hiddenMaterial.Value, new MaterialInfo(hiddenMaterial.Value, null));
            builtInMaterials.Add(hiddenMaterial);

            //Delete stock resources
            MaterialManager.getInstance().remove("BaseWhite");
            MaterialManager.getInstance().remove("BaseWhiteNoLighting");
            MaterialManager.getInstance().remove("Ogre/Debug/AxesMat");

            //Rebuild with our materials
            var baseWhite = createFromDescription(new MaterialDescription()
            {
                Name = "BaseWhite"
            }, false);

            builtInMaterials.Add(baseWhite);

            var baseWhiteNoLighting = createFromDescription(new MaterialDescription()
            {
                Name = "BaseWhiteNoLighting"
            }, false);

            builtInMaterials.Add(baseWhiteNoLighting);

            MaterialPtr axesMat = MaterialManager.getInstance().create("Ogre/Debug/AxesMat", GroupName, false, null);

            axesMat.Value.setLightingEnabled(false);
            axesMat.Value.setSceneBlending(SceneBlendType.SBT_TRANSPARENT_ALPHA);
            axesMat.Value.setCullingMode(CullingMode.CULL_NONE);
            axesMat.Value.setDepthWriteEnabled(false);
            axesMat.Value.setDepthCheckEnabled(false);
            builtInMaterials.Add(axesMat);
            createdMaterials.Add(axesMat.Value, new MaterialInfo(axesMat.Value, null));
        }
Exemple #5
0
 /// <summary>
 /// This will be called for all materials when they need to be cleaned up. This includes all variants
 /// made during buildMaterial.
 /// </summary>
 /// <param name="materialPtr">The MaterialPtr to destroy.</param>
 public abstract void destroyMaterial(MaterialPtr materialPtr);
Exemple #6
0
 public void addMaterial(MaterialPtr material, MaterialDescription description)
 {
     loadedMaterials.Add(new MaterialEntry(material, description.Builder));
 }
Exemple #7
0
 public MaterialEntry(MaterialPtr ptr, String builder)
 {
     this.ptr     = ptr;
     this.builder = builder;
 }