示例#1
0
        public void FillBeziers(int count, Position <float>[] controls, Color <float>[] colors, Matrix4x4[] transforms)
        {
            Utility.Assert(controls.Length >= count * 3 && colors.Length >= count * 3);
            Utility.Assert(Mode == RenderMode.Fill);

            if (count == 0)
            {
                return;
            }

            TrianglePoints[] trianglePointsCanvas = new TrianglePoints[count];
            TrianglePoints[] trianglePoints       = new TrianglePoints[count];
            TriangleColors[] triangleColors       = new TriangleColors[count];

            var size = new Vector4(mCanvasSize.Width * 0.5f, mCanvasSize.Height * 0.5f, 0, 0);

            //create triangle points struct
            for (int index = 0; index < count; index++)
            {
                //get transform matrix
                var transformMatrix = (transforms == null ? Matrix4x4.Identity : transforms[index]) * Transform * mProjection;

                //get control points for bezier_i
                var points = new Position <float>[]
                {
                    controls[index * 3 + 0],
                    controls[index * 3 + 1],
                    controls[index * 3 + 2]
                };

                Vector4 FromColor(Color <float> color) => new Vector4(color.Red, color.Green, color.Blue, color.Alpha);

                //create data for bezier_i in shader
                TrianglePoints subTrianglePoints = new TrianglePoints()
                {
                    Position0 = Vector4.Transform(new Vector3(points[0].X, points[0].Y, 0), transformMatrix),
                    Position1 = Vector4.Transform(new Vector3(points[1].X, points[1].Y, 0), transformMatrix),
                    Position2 = Vector4.Transform(new Vector3(points[2].X, points[2].Y, 0), transformMatrix),
                };

                TriangleColors subTriangleColors = new TriangleColors()
                {
                    Color0 = FromColor(colors[index * 3 + 0]),
                    Color1 = FromColor(colors[index * 3 + 1]),
                    Color2 = FromColor(colors[index * 3 + 2])
                };

                TrianglePoints subTrianglePointsCanvas = new TrianglePoints()
                {
                    Position0 = (subTrianglePoints.Position0 + new Vector4(1)) * size,
                    Position1 = (subTrianglePoints.Position1 + new Vector4(1)) * size,
                    Position2 = (subTrianglePoints.Position2 + new Vector4(1)) * size
                };

                subTrianglePointsCanvas.Position0.Y = mCanvasSize.Height - subTrianglePointsCanvas.Position0.Y;
                subTrianglePointsCanvas.Position1.Y = mCanvasSize.Height - subTrianglePointsCanvas.Position1.Y;
                subTrianglePointsCanvas.Position2.Y = mCanvasSize.Height - subTrianglePointsCanvas.Position2.Y;

                trianglePointsCanvas[index] = subTrianglePointsCanvas;
                trianglePoints[index]       = subTrianglePoints;
                triangleColors[index]       = subTriangleColors;
            }

            if (mTrianglePointsBufferArray == null || mTrianglePointsBufferArray.ElementCount != count)
            {
                Utility.Dispose(ref mTrianglePointsBufferArray);
                Utility.Dispose(ref mTriangleColorsBufferArray);
                Utility.Dispose(ref mTrianglePointsCanvasBufferArray);

                Utility.Dispose(ref mTrianglePointsBufferArrayUsage);
                Utility.Dispose(ref mTriangleColorsBufferArrayUsage);
                Utility.Dispose(ref mTrianglePointsCanvasBufferArrayUsage);

                mTrianglePointsBufferArray = new GpuBufferArray(
                    Utility.SizeOf <TrianglePoints>(),
                    count,
                    mDevice,
                    GpuResourceInfo.BufferArray());

                mTriangleColorsBufferArray = new GpuBufferArray(
                    Utility.SizeOf <TrianglePoints>(),
                    count,
                    mDevice,
                    GpuResourceInfo.BufferArray());

                mTrianglePointsCanvasBufferArray = new GpuBufferArray(
                    Utility.SizeOf <TrianglePoints>(),
                    count,
                    mDevice,
                    GpuResourceInfo.BufferArray());

                mTrianglePointsBufferArrayUsage       = new GpuResourceUsage(mDevice, mTrianglePointsBufferArray);
                mTriangleColorsBufferArrayUsage       = new GpuResourceUsage(mDevice, mTriangleColorsBufferArray);
                mTrianglePointsCanvasBufferArrayUsage = new GpuResourceUsage(mDevice, mTrianglePointsCanvasBufferArray);
            }

            mTrianglePointsBufferArray.Update(trianglePoints);
            mTriangleColorsBufferArray.Update(triangleColors);
            mTrianglePointsCanvasBufferArray.Update(trianglePointsCanvas);

            //change shader we use
            mDevice.SetVertexShader(mFillBeziersVertexShader);
            mDevice.SetPixelShader(mFillBeziersPixelShader);

            //set vertex buffer
            mDevice.SetVertexBuffer(mFillVertexBuffer);
            mDevice.SetIndexBuffer(mFillIndexBuffer);

            //set constant buffer and resource
            mDevice.SetResourceUsage(mTrianglePointsBufferArrayUsage, 0);
            mDevice.SetResourceUsage(mTriangleColorsBufferArrayUsage, 1);
            mDevice.SetResourceUsage(mTrianglePointsCanvasBufferArrayUsage, 2);

            //drwa indexed instanced
            mDevice.DrawIndexedInstanced(3, count);

            //reset the shader
            mDevice.SetVertexShader(mFillBezierVertexShader);
            mDevice.SetPixelShader(mFillBezierPixelShader);
        }