예제 #1
0
        public async Task ModifyVertices(SQT.Core.Context context, SQT.Core.Node node, CancellationTokenSource cancellation)
        {
            if (positionBuffer == null || positionBuffer.count != node.positions.Length)
            {
                if (positionBuffer != null)
                {
                    positionBuffer.Release();
                }
                positionBuffer = new ComputeBuffer(node.positions.Length, 3 * 4);
            }
            if (normalBuffer == null || normalBuffer.count != node.normals.Length)
            {
                if (normalBuffer != null)
                {
                    normalBuffer.Release();
                }
                normalBuffer = new ComputeBuffer(node.normals.Length, 3 * 4);
            }

            positionBuffer.SetData(node.positions);
            normalBuffer.SetData(node.normals);

            computeShader.SetBuffer(computeKernel, "positionBuffer", positionBuffer);
            computeShader.SetBuffer(computeKernel, "normalBuffer", normalBuffer);
            computeShader.SetTexture(computeKernel, "_Gradients2D", gradientsTexture);
            computeShader.SetTexture(computeKernel, "_Permutation2D", permutationTexture);
            computeShader.SetFloat("_Strength", strength);
            computeShader.SetFloat("_Frequency", frequency);
            computeShader.SetFloat("_Lacunarity", lacunarity);
            computeShader.SetFloat("_Persistence", persistence);
            computeShader.SetInt("_Octaves", octaves);

            uint x, y, z;

            computeShader.GetKernelThreadGroupSizes(computeKernel, out x, out y, out z);
            float total = x * y * z;

            computeShader.Dispatch(computeKernel, Mathf.CeilToInt(node.positions.Length / total), 1, 1);

            Task positionsTask = new Task(() => { });
            Task normalsTask   = new Task(() => { });
            Action <AsyncGPUReadbackRequest> positionsAction = new Action <AsyncGPUReadbackRequest>((request) =>
            {
                node.positions = request.GetData <Vector3>().ToArray();
                positionsTask.Start();
            });
            Action <AsyncGPUReadbackRequest> normalsAction = new Action <AsyncGPUReadbackRequest>((request) =>
            {
                node.normals = request.GetData <Vector3>().ToArray();
                normalsTask.Start();
            });

            AsyncGPUReadback.Request(positionBuffer, positionsAction);
            AsyncGPUReadback.Request(normalBuffer, normalsAction);

            await positionsTask;
            await normalsTask;
        }
예제 #2
0
 public Task ModifyVertices(SQT.Core.Context context, SQT.Core.Node node, CancellationTokenSource cancellation)
 {
     return(Task.Factory.StartNew(() =>
     {
         for (int i = 0; i < node.positions.Length; i++)
         {
             Perlin.PerlinSample sample = GetSample(node.positions[i]);
             node.positions[i] += node.normals[i] * sample.value;
             node.normals[i] = (node.normals[i] - sample.derivative).normalized;
         }
     }));
 }
예제 #3
0
 public void ModifyMarkedSet(SQT.Core.Context context, HashSet <SQT.Core.Node> marked, SQT.Core.Node leaf)
 {
     SQT.Core.Node n1 = SQT.Core.Reconciler.EnsureNeighbor(context, leaf, 0);
     SQT.Core.Node n2 = SQT.Core.Reconciler.EnsureNeighbor(context, leaf, 1);
     SQT.Core.Node n3 = SQT.Core.Reconciler.EnsureNeighbor(context, leaf, 2);
     SQT.Core.Node n4 = SQT.Core.Reconciler.EnsureNeighbor(context, leaf, 3);
     SQT.Core.Node n5 = SQT.Core.Reconciler.EnsureNeighbor(context, n1, 3);
     SQT.Core.Node n6 = SQT.Core.Reconciler.EnsureNeighbor(context, n2, 2);
     SQT.Core.Node n7 = SQT.Core.Reconciler.EnsureNeighbor(context, n3, 0);
     SQT.Core.Node n8 = SQT.Core.Reconciler.EnsureNeighbor(context, n4, 1);
     marked.Add(n1);
     marked.Add(n2);
     marked.Add(n3);
     marked.Add(n4);
     marked.Add(n5);
     marked.Add(n6);
     marked.Add(n7);
     marked.Add(n8);
 }