public ComputeShaderSkinningAdapter(SkinningBlend method, ComputeShader computeShader, RenderChunk chunk, Transform[] bones, Material material, bool tension = false)
        {
            this.method = method;

            perVertexBuffer = new ComputeBuffer(chunk.vertexCount, Marshal.SizeOf(typeof(DataPerVertex)));
            perVertexBuffer.SetData(chunk.dataPerVertex);

            perVertexSkinBuffer = new ComputeBuffer(chunk.vertexCount, Marshal.SizeOf(typeof(SkinPerVertex)));
            perVertexSkinBuffer.SetData(chunk.skinPerVertex);

            perVertexStream = new ComputeBuffer(chunk.vertexCount, Marshal.SizeOf(typeof(DataPerVertex)));
            perVertexStream.SetData(chunk.dataPerVertex);

            indexBuffer = new ComputeBuffer(chunk.indices.Length, sizeof(int));
            indexBuffer.SetData(chunk.indices);

            indexCountBuffer = new ComputeBuffer(chunk.indexCounts.Length, sizeof(int));
            indexCountBuffer.SetData(chunk.indexCounts);

            sourceDispatcher = new DataToDataDispatcher(
                computeShader,
                () => perVertexBuffer,
                () => perVertexStream
                );
            dispatchcer = DispatcherFactory.CreateComputeBy(
                method,
                computeShader,
                chunk,
                bones,
                () => perVertexBuffer,
                () => perVertexSkinBuffer,
                () => perVertexStream
                );

            if (!tension)
            {
                renderer = new ComputeShaderRenderer(
                    chunk,
                    material,
                    () => perVertexStream,
                    () => indexBuffer,
                    () => indexCountBuffer
                    );
            }
            else
            {
                edgeLengthBuffer = new ComputeBuffer(chunk.edges.Length, sizeof(float));
                edgeLengthBuffer.SetData(chunk.edges);

                tensionBuffer = new ComputeBuffer(chunk.vertexCount, sizeof(float));
                tensionBuffer.SetData(new float[chunk.vertexCount]);

                tensionDispatcher = new TensionDispatcher(
                    computeShader,
                    chunk,
                    () => perVertexStream,
                    () => indexBuffer,
                    () => edgeLengthBuffer,
                    () => tensionBuffer
                    );
                renderer = new TensionRenderer(chunk, material, () => tensionBuffer, () => perVertexStream, () => indexBuffer, () => indexCountBuffer);
            }
        }
Beispiel #2
0
        public ComputeShaderSkinningAdapter(SkinningBlend method, ComputeShader computeShader, RenderChunk chunk, Transform[] bones, Material material, bool tension = false)
        {
            this.method = method;

            perVertexBuffer = new ComputeBuffer(chunk.vertexCount, Marshal.SizeOf(typeof(DataPerVertex)));
            perVertexBuffer.SetData(chunk.dataPerVertex);

            perVertexSkinBuffer = new ComputeBuffer(chunk.vertexCount, Marshal.SizeOf(typeof(SkinPerVertex)));
            perVertexSkinBuffer.SetData(chunk.skinPerVertex);

            perVertexStream = new ComputeBuffer(chunk.vertexCount, Marshal.SizeOf(typeof(DataPerVertex)));
            perVertexStream.SetData(chunk.dataPerVertex);

            indexBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Index, chunk.indices.Length, sizeof(int));
            indexBuffer.SetData(chunk.indices);

            indexCountBuffer = new ComputeBuffer(chunk.indexCounts.Length, sizeof(int));
            indexCountBuffer.SetData(chunk.indexCounts);

            minmaxBoundsBuffer = new ComputeBuffer(1, Marshal.SizeOf <Vector3>() * 2, ComputeBufferType.Structured);

            sourceDispatcher = new DataToDataDispatcher(
                computeShader,
                () => perVertexBuffer,
                () => perVertexStream,
                () => minmaxBoundsBuffer
                );
            dispatchcer = DispatcherFactory.CreateComputeBy(
                method,
                computeShader,
                chunk,
                bones,
                () => perVertexBuffer,
                () => perVertexSkinBuffer,
                () => perVertexStream,
                () => minmaxBoundsBuffer
                );

            if (!tension)
            {
                renderer =
                    new ComputeShaderRenderer(
                        chunk, material, () => perVertexStream, () => indexBuffer,
                        () =>
                {
                    Bounds b = new Bounds();
                    // TODO:: REMOVE GC ALLOCATION, fixed or stack allocation as c# Array
                    Vector3[] p = new Vector3[2];
                    minmaxBoundsBuffer.GetData(p);
                    b.center  = (p[0] + p[1]) / 2.0f;
                    b.extents = (p[1] - p[0]) / 2.0f;

                    return(b);
                }
                        );
            }
            else
            {
                edgeLengthBuffer = new ComputeBuffer(chunk.edges.Length, sizeof(float));
                edgeLengthBuffer.SetData(chunk.edges);

                tensionBuffer = new ComputeBuffer(chunk.vertexCount, sizeof(float));
                tensionBuffer.SetData(new float[chunk.vertexCount]);

                tensionDispatcher =
                    new TensionDispatcher(
                        computeShader, chunk,
                        () => perVertexStream, () => indexBuffer, () => edgeLengthBuffer, () => tensionBuffer
                        );
                renderer = new TensionRenderer(
                    chunk, material, () => tensionBuffer, () => perVertexStream, () => indexBuffer,
                    () =>
                {
                    Bounds b = new Bounds();
                    // TODO:: REMOVE GC ALLOCATION, fixed or stack allocation as c# Array
                    Vector3[] p = new Vector3[2];
                    minmaxBoundsBuffer.GetData(p);
                    b.center  = (p[0] + p[1]) / 2.0f;
                    b.extents = (p[1] - p[0]) / 2.0f;

                    return(b);
                }
                    );
            }
        }