Пример #1
0
        public void Update(SCNNode node)
        {
            foreach (var cloth in this.clothData)
            {
                var wind = new SCNVector3(1.8f, 0f, 0f);

                // The multiplier is to rescale ball to flag model space.
                // The correct value should be passed in.
                var simData = new SimulationData(wind);
                this.Deform(cloth.MeshData, simData);
            }
        }
Пример #2
0
        public void Deform(ClothSimMetalNode mesh, SimulationData simData)
        {
            var w = this.pipelineStateClothSim.ThreadExecutionWidth;
            var threadsPerThreadgroup = new MTLSize((nint)w, 1, 1);

            var threadgroupsPerGrid = new MTLSize((mesh.VertexCount + (int)w - 1) / (int)w, 1, 1);

            var clothSimCommandBuffer  = this.commandQueue.CommandBuffer();
            var clothSimCommandEncoder = clothSimCommandBuffer?.ComputeCommandEncoder;

            if (clothSimCommandEncoder != null)
            {
                clothSimCommandEncoder.SetComputePipelineState(pipelineStateClothSim);

                clothSimCommandEncoder.SetBuffer(mesh.Vb1, 0, 0);
                clothSimCommandEncoder.SetBuffer(mesh.Vb2, 0, 1);
                clothSimCommandEncoder.SetBuffer(mesh.VelocityBuffers[mesh.CurrentBufferIndex], 0, 2);

                mesh.CurrentBufferIndex = (mesh.CurrentBufferIndex + 1) % 2;
                clothSimCommandEncoder.SetBuffer(mesh.VelocityBuffers[mesh.CurrentBufferIndex], 0, 3);

                //var pointer = System.Runtime.InteropServices.Marshal.GetComInterfaceForObject(simData, typeof(SimulationData));
                //clothSimCommandEncoder?.SetBytes(pointer, (nuint)System.Runtime.InteropServices.Marshal.SizeOf<SimulationData>(), 4);

                clothSimCommandEncoder.DispatchThreadgroups(threadgroupsPerGrid, threadsPerThreadgroup: threadsPerThreadgroup);

                clothSimCommandEncoder.EndEncoding();
            }

            clothSimCommandBuffer?.Commit();

            //

            var normalComputeCommandBuffer  = this.commandQueue.CommandBuffer();
            var normalComputeCommandEncoder = normalComputeCommandBuffer?.ComputeCommandEncoder;

            if (normalComputeCommandEncoder != null)
            {
                normalComputeCommandEncoder.SetComputePipelineState(pipelineStateNormalUpdate);
                normalComputeCommandEncoder.SetBuffer(mesh.Vb2, 0, 0);
                normalComputeCommandEncoder.SetBuffer(mesh.Vb1, 0, 1);
                normalComputeCommandEncoder.SetBuffer(mesh.NormalWorkBuffer, 0, 2);
                normalComputeCommandEncoder.DispatchThreadgroups(threadgroupsPerGrid, threadsPerThreadgroup);

                normalComputeCommandEncoder.EndEncoding();
            }

            normalComputeCommandBuffer?.Commit();

            //

            var normalSmoothComputeCommandBuffer  = this.commandQueue.CommandBuffer();
            var normalSmoothComputeCommandEncoder = normalSmoothComputeCommandBuffer?.ComputeCommandEncoder;

            if (normalSmoothComputeCommandEncoder != null)
            {
                normalSmoothComputeCommandEncoder.SetComputePipelineState(pipelineStateNormalSmooth);
                normalSmoothComputeCommandEncoder.SetBuffer(mesh.NormalWorkBuffer, 0, 0);
                normalSmoothComputeCommandEncoder.SetBuffer(mesh.NormalBuffer, 0, 1);
                normalSmoothComputeCommandEncoder.DispatchThreadgroups(threadgroupsPerGrid, threadsPerThreadgroup);

                normalSmoothComputeCommandEncoder.EndEncoding();
            }

            normalSmoothComputeCommandBuffer?.Commit();
        }