public float[,] createHeightMapPerlinNoiseCS( int x, int y, float scale, float startx, float starty, float shiftx, float shifty) { if (GameObject.FindGameObjectWithTag("debugtoggle").GetComponent <UnityEngine.UI.Toggle>().isOn) { print("START COMPUTE SHADER"); } PerlinInfo[] data = new PerlinInfo[x * y]; float[] output = new float[x * y]; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { data[i * y + j] = new PerlinInfo(x, y, scale, startx, starty, shiftx + i, shifty + j); } } ComputeBuffer buffer = new ComputeBuffer(data.Length, 28); buffer.SetData(data); ComputeBuffer outputBuffer = new ComputeBuffer(output.Length, 4); outputBuffer.SetData(output); int kernel = shader.FindKernel("computeHeightMap"); shader.SetBuffer(kernel, "dataBuffer", buffer); shader.SetBuffer(kernel, "output", outputBuffer); shader.Dispatch(kernel, data.Length / 32, 1, 1); outputBuffer.GetData(output); buffer.Dispose(); outputBuffer.Dispose(); float[,] heightmap = new float[x, y]; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { heightmap[i, j] = output[i * y + j]; } } if (GameObject.FindGameObjectWithTag("debugtoggle").GetComponent <UnityEngine.UI.Toggle>().isOn) { print("END COMPUTE SHADER"); } return(heightmap); }
public static float[,] createHeightMapPerlinNoiseJobs( int x, int y, float scale, float startx, float starty, float shiftx, float shifty) { var perlinInfoArray = new NativeArray <PerlinInfo>(x * y, Allocator.Persistent); var output = new NativeArray <float>(x * y, Allocator.Persistent); for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { perlinInfoArray[i * y + j] = new PerlinInfo(x, y, scale, startx, starty, shiftx + i, shifty + j); } } var job = new generatePerlinJob { perlinInfoArray = perlinInfoArray, perlinOutput = output }; var jobHandle = job.Schedule(x * y, 1); jobHandle.Complete(); float[,] heightmap = new float[x, y]; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { heightmap[i, j] = output[i * y + j]; } } perlinInfoArray.Dispose(); output.Dispose(); return(heightmap); }