Beispiel #1
0
        // One ModelMeshPart for each unique shader in a W3D_CHUNK_MATERIAL_PASS.
        private ModelMeshPart CreateModelMeshPart(
            ContentManager contentManager,
            uint startIndex,
            uint indexCount,
            W3dMesh w3dMesh,
            FixedFunction.VertexMaterial[] vertexMaterials,
            FixedFunction.ShadingConfiguration[] shadingConfigurations,
            uint vertexMaterialID,
            uint shaderID,
            uint numTextureStages,
            uint?textureIndex0,
            uint?textureIndex1)
        {
            var w3dShader = w3dMesh.Shaders[shaderID];

            var rasterizerState = RasterizerStateDescriptionUtility.DefaultFrontIsCounterClockwise;

            rasterizerState.CullMode = w3dMesh.Header.Attributes.HasFlag(W3dMeshFlags.TwoSided)
                ? FaceCullMode.None
                : FaceCullMode.Back;

            var depthState = DepthStencilStateDescription.DepthOnlyLessEqual;

            depthState.DepthWriteEnabled = w3dShader.DepthMask == W3dShaderDepthMask.WriteEnable;
            depthState.DepthComparison   = w3dShader.DepthCompare.ToComparison();

            var blendState = new BlendStateDescription(
                RgbaFloat.White,
                new BlendAttachmentDescription(
                    w3dShader.SrcBlend != W3dShaderSrcBlendFunc.One || w3dShader.DestBlend != W3dShaderDestBlendFunc.Zero,
                    w3dShader.SrcBlend.ToBlend(),
                    w3dShader.DestBlend.ToBlend(false),
                    BlendFunction.Add,
                    w3dShader.SrcBlend.ToBlend(),
                    w3dShader.DestBlend.ToBlend(true),
                    BlendFunction.Add));

            var effectMaterial = new FixedFunctionMaterial(contentManager, contentManager.EffectLibrary.FixedFunction)
            {
                PipelineState = new EffectPipelineState(
                    rasterizerState,
                    depthState,
                    blendState,
                    RenderPipeline.GameOutputDescription)
            };

            var materialConstantsBuffer = AddDisposable(contentManager.GraphicsDevice.CreateStaticBuffer(
                                                            new FixedFunction.MaterialConstantsType
            {
                Material         = vertexMaterials[vertexMaterialID],
                Shading          = shadingConfigurations[shaderID],
                NumTextureStages = numTextureStages
            },
                                                            BufferUsage.UniformBuffer));

            effectMaterial.SetMaterialConstants(materialConstantsBuffer);
            effectMaterial.SetTexture0(CreateTexture(contentManager, w3dMesh, textureIndex0));
            effectMaterial.SetTexture1(CreateTexture(contentManager, w3dMesh, textureIndex1));

            return(new ModelMeshPart(
                       startIndex,
                       indexCount,
                       effectMaterial));
        }
Beispiel #2
0
        // One ModelMeshPart for each unique shader in a W3D_CHUNK_MATERIAL_PASS.
        private ModelMeshPart CreateModelMeshPart(
            ContentManager contentManager,
            uint startIndex,
            uint indexCount,
            W3dMesh w3dMesh,
            FixedFunctionMaterial.VertexMaterial[] vertexMaterials,
            FixedFunctionMaterial.ShadingConfiguration[] shadingConfigurations,
            uint vertexMaterialID,
            uint shaderID,
            uint numTextureStages,
            uint?textureIndex0,
            uint?textureIndex1)
        {
            var w3dShader = w3dMesh.Shaders[shaderID];

            var rasterizerState = RasterizerStateDescription.CullBackSolid;

            rasterizerState.CullMode = w3dMesh.Header.Attributes.HasFlag(W3dMeshFlags.TwoSided)
                ? CullMode.None
                : CullMode.CullBack;

            var depthState = DepthStencilStateDescription.Default;

            depthState.IsDepthEnabled      = true;
            depthState.IsDepthWriteEnabled = w3dShader.DepthMask == W3dShaderDepthMask.WriteEnable;
            depthState.DepthComparison     = w3dShader.DepthCompare.ToComparison();

            var blendState = BlendStateDescription.Opaque;

            blendState.Enabled = w3dShader.SrcBlend != W3dShaderSrcBlendFunc.One ||
                                 w3dShader.DestBlend != W3dShaderDestBlendFunc.Zero;
            blendState.SourceBlend           = w3dShader.SrcBlend.ToBlend();
            blendState.SourceAlphaBlend      = w3dShader.SrcBlend.ToBlend();
            blendState.DestinationBlend      = w3dShader.DestBlend.ToBlend(false);
            blendState.DestinationAlphaBlend = w3dShader.DestBlend.ToBlend(true);

            var effectMaterial = new FixedFunctionMaterial(contentManager.EffectLibrary.FixedFunction);

            effectMaterial.PipelineState = new EffectPipelineState(
                rasterizerState,
                depthState,
                blendState);

            var materialConstantsBuffer = AddDisposable(Buffer <FixedFunctionMaterial.MaterialConstants> .CreateStatic(
                                                            contentManager.GraphicsDevice,
                                                            new FixedFunctionMaterial.MaterialConstants
            {
                Material         = vertexMaterials[vertexMaterialID],
                Shading          = shadingConfigurations[shaderID],
                NumTextureStages = numTextureStages
            },
                                                            BufferBindFlags.ConstantBuffer));

            effectMaterial.SetMaterialConstants(materialConstantsBuffer);
            effectMaterial.SetTexture0(CreateTexture(contentManager, w3dMesh, textureIndex0));
            effectMaterial.SetTexture1(CreateTexture(contentManager, w3dMesh, textureIndex1));

            return(new ModelMeshPart(
                       startIndex,
                       indexCount,
                       effectMaterial));
        }