public MdfMaterialFactory(IFileSystem fs, RenderingDevice device)
 {
     _fs       = fs;
     _device   = device;
     _textures = device.GetTextures();
 }
示例#2
0
    public static Material CreateMaterial(RenderingDevice device,
                                          PartSysEmitter emitter,
                                          bool pointSprites)
    {
        var blendState = new BlendSpec();

        blendState.blendEnable = true;

        switch (emitter.GetSpec().GetBlendMode())
        {
        case PartSysBlendMode.Add:
            blendState.srcBlend  = BlendOperand.SrcAlpha;
            blendState.destBlend = BlendOperand.One;
            break;

        case PartSysBlendMode.Subtract:
            blendState.srcBlend  = BlendOperand.Zero;
            blendState.destBlend = BlendOperand.InvSrcAlpha;
            break;

        case PartSysBlendMode.Blend:
            blendState.srcBlend  = BlendOperand.SrcAlpha;
            blendState.destBlend = BlendOperand.InvSrcAlpha;
            break;

        case PartSysBlendMode.Multiply:
            blendState.srcBlend  = BlendOperand.Zero;
            blendState.destBlend = BlendOperand.SrcColor;
            break;

        default:
            break;
        }

        // Particles respect the depth buffer, but do not modify it
        DepthStencilSpec depthStencilState = new DepthStencilSpec();

        depthStencilState.depthEnable = true;
        depthStencilState.depthWrite  = false;
        RasterizerSpec rasterizerState = new RasterizerSpec();

        rasterizerState.cullMode = CullMode.None;


        var samplers = new List <MaterialSamplerSpec>();

        var shaderName  = "diffuse_only_ps";
        var textureName = emitter.GetSpec().GetTextureName();

        if (textureName.Length > 0)
        {
            var samplerState = new SamplerSpec
            {
                addressU  = TextureAddress.Clamp,
                addressV  = TextureAddress.Clamp,
                minFilter = TextureFilterType.Linear,
                magFilter = TextureFilterType.Linear,
                mipFilter = TextureFilterType.Linear
            };
            var texture = device.GetTextures().Resolve(textureName, true);
            samplers.Add(new MaterialSamplerSpec(texture, samplerState));
            shaderName = "textured_simple_ps";
        }

        using var pixelShader = device.GetShaders().LoadPixelShader(shaderName);

        var vsName = pointSprites ? "particles_points_vs" : "particles_quads_vs";

        using var vertexShader = device.GetShaders().LoadVertexShader(vsName);

        return(device.CreateMaterial(blendState, depthStencilState, rasterizerState,
                                     samplers.ToArray(), vertexShader, pixelShader));
    }
示例#3
0
    public ShapeRenderer2d(RenderingDevice device)
    {
        _device   = device;
        _textures = device.GetTextures();

        untexturedMaterial = CreateMaterial(device, "diffuse_only_ps");
        texturedMaterial   = CreateMaterial(device, "textured_simple_ps");
        texturedWithoutBlendingMaterial = CreateMaterial(device, "textured_simple_ps", false, false);
        texturedWithMaskMaterial        = CreateMaterial(device, "textured_two_ps");
        lineMaterial      = CreateMaterial(device, "diffuse_only_ps", true);
        outlineMaterial   = CreateOutlineMaterial(device);
        pieFillMaterial   = CreatePieFillMaterial(device);
        bufferBinding     = new BufferBinding(device, texturedMaterial.VertexShader).Ref();
        mdfBufferBinding  = _device.CreateMdfBufferBinding();
        lineBufferBinding = new BufferBinding(device, lineMaterial.VertexShader).Ref();


        SamplerSpec samplerWrapSpec = new SamplerSpec();

        samplerWrapSpec.addressU  = TextureAddress.Wrap;
        samplerWrapSpec.addressV  = TextureAddress.Wrap;
        samplerWrapSpec.minFilter = TextureFilterType.Linear;
        samplerWrapSpec.magFilter = TextureFilterType.Linear;
        samplerWrapSpec.mipFilter = TextureFilterType.Linear;
        samplerWrapState          = _device.CreateSamplerState(samplerWrapSpec);

        SamplerSpec samplerClampPointSpec = new SamplerSpec();

        samplerClampPointSpec.addressU  = TextureAddress.Clamp;
        samplerClampPointSpec.addressV  = TextureAddress.Clamp;
        samplerClampPointSpec.minFilter = TextureFilterType.NearestNeighbor;
        samplerClampPointSpec.magFilter = TextureFilterType.NearestNeighbor;
        samplerClampPointSpec.mipFilter = TextureFilterType.NearestNeighbor;
        samplerClampPointState          = _device.CreateSamplerState(samplerClampPointSpec);

        SamplerSpec samplerClampSpec = new SamplerSpec();

        samplerClampSpec.addressU  = TextureAddress.Clamp;
        samplerClampSpec.addressV  = TextureAddress.Clamp;
        samplerClampSpec.minFilter = TextureFilterType.Linear;
        samplerClampSpec.magFilter = TextureFilterType.Linear;
        samplerClampSpec.mipFilter = TextureFilterType.Linear;
        samplerClampState          = _device.CreateSamplerState(samplerClampSpec);

        DepthStencilSpec noDepthSpec = new DepthStencilSpec();

        noDepthSpec.depthEnable = false;
        noDepthState            = _device.CreateDepthStencilState(noDepthSpec);

        vertexBuffer = _device.CreateEmptyVertexBuffer(Vertex2d.Size * 8, debugName: "ShapeRenderer2d");

        var indexData = new ushort[]
        {
            0, 1, 2,
            2, 3, 0
        };

        indexBuffer = _device.CreateIndexBuffer(indexData);

        bufferBinding.Resource.AddBuffer <Vertex2d>(vertexBuffer, 0)
        .AddElement(VertexElementType.Float4, VertexElementSemantic.Position)
        .AddElement(VertexElementType.Float4, VertexElementSemantic.Normal)
        .AddElement(VertexElementType.Color, VertexElementSemantic.Color)
        .AddElement(VertexElementType.Float2, VertexElementSemantic.TexCoord);

        mdfBufferBinding.AddBuffer <Vertex2d>(vertexBuffer, 0)
        .AddElement(VertexElementType.Float4, VertexElementSemantic.Position)
        .AddElement(VertexElementType.Float4, VertexElementSemantic.Normal)
        .AddElement(VertexElementType.Color, VertexElementSemantic.Color)
        .AddElement(VertexElementType.Float2, VertexElementSemantic.TexCoord);
    }