public void UpdateNoise() { ValidateParamaters(); CreateTexture(ref shapeTexture, shapeResolution, shapeNoiseName); CreateTexture(ref detailTexture, detailResolution, detailNoiseName); if (updateNoise && noiseCompute) { var timer = System.Diagnostics.Stopwatch.StartNew(); updateNoise = false; WorleyNoiseSettings activeSettings = ActiveSettings; if (activeSettings == null) { return; } buffersToRelease = new List <ComputeBuffer> (); int activeTextureResolution = ActiveTexture.width; // Set values: noiseCompute.SetFloat("persistence", activeSettings.persistence); noiseCompute.SetInt("resolution", activeTextureResolution); noiseCompute.SetVector("channelMask", ChannelMask); // Set noise gen kernel data: noiseCompute.SetTexture(0, "Result", ActiveTexture); var minMaxBuffer = CreateBuffer(new int[] { int.MaxValue, 0 }, sizeof(int), "minMax", 0); UpdateWorley(ActiveSettings); noiseCompute.SetTexture(0, "Result", ActiveTexture); //var noiseValuesBuffer = CreateBuffer (activeNoiseValues, sizeof (float) * 4, "values"); // Dispatch noise gen kernel int numThreadGroups = Mathf.CeilToInt(activeTextureResolution / (float)computeThreadGroupSize); noiseCompute.Dispatch(0, numThreadGroups, numThreadGroups, numThreadGroups); // Set normalization kernel data: noiseCompute.SetBuffer(1, "minMax", minMaxBuffer); noiseCompute.SetTexture(1, "Result", ActiveTexture); // Dispatch normalization kernel noiseCompute.Dispatch(1, numThreadGroups, numThreadGroups, numThreadGroups); if (logComputeTime) { // Get minmax data just to force main thread to wait until compute shaders are finished. // This allows us to measure the execution time. var minMax = new int[2]; minMaxBuffer.GetData(minMax); Debug.Log($"Noise Generation: {timer.ElapsedMilliseconds}ms"); } // Release buffers foreach (var buffer in buffersToRelease) { buffer.Release(); } } }
void UpdateWorley(WorleyNoiseSettings settings) { var prng = new System.Random(settings.seed); CreateWorleyPointsBuffer(prng, settings.numDivisionsA, "pointsA"); CreateWorleyPointsBuffer(prng, settings.numDivisionsB, "pointsB"); CreateWorleyPointsBuffer(prng, settings.numDivisionsC, "pointsC"); noiseCompute.SetInt("numCellsA", settings.numDivisionsA); noiseCompute.SetInt("numCellsB", settings.numDivisionsB); noiseCompute.SetInt("numCellsC", settings.numDivisionsC); noiseCompute.SetBool("invertNoise", settings.invert); noiseCompute.SetInt("tile", settings.tile); }