override public void GenerateHeightField(int currentFrame, ExtendedHeightField extendedHeightField)
    {
        Profiler.BeginSample("Clear Height Field");
        extendedHeightField.Clear();
        Profiler.EndSample();

        Profiler.BeginSample("Set Point Map");
        waveParticles.setPointMap(currentFrame, ref pointMap);
        Profiler.EndSample();

        Profiler.BeginSample("Convolve Wave Particles");
        convolveWaveParticles();
        extendedHeightField.UpdateTexture(convolvedTexture);
        Profiler.EndSample();
    }
Esempio n. 2
0
    public void setPointMap(int currentFrame, ref ExtendedHeightField pointMap)
    {
        Profiler.BeginSample("Set Point Map Internal");
        Profiler.BeginSample("Initialisation");
        ///
        /// Initialise _splatTexture if it hasn't been yet.
        ///
        if (_splatTexture == null)
        {
            if (antiAliased == AntiAlias.SCALE)
            {
                _splatTexture = new RenderTexture(pointMap.HoriRes * antiAliasFactor, pointMap.VertRes * antiAliasFactor, 24, RenderTextureFormat.ARGBFloat);
            }
            else
            {
                _splatTexture = new RenderTexture(pointMap.HoriRes, pointMap.VertRes, 24, RenderTextureFormat.ARGBFloat);
            }
            _splatTexture.enableRandomWrite = true;
        }

        if (!_splatTexture.IsCreated())
        {
            _splatTexture.Create();
        }
        Profiler.EndSample();

        Profiler.BeginSample("Intitialise Splatter");
        ///
        /// Initialise the SplatParticles GPU Compute Kernel to splat the particles to a texture
        ///
        _gpuSplatParticles.SetTexture(kernel_SplatParticles, SPLAT_TEXTURE, _splatTexture);
        _gpuSplatParticles.SetBuffer(kernel_SplatParticles, WAVE_PARTICLE_BUFFER, _waveParticlesBuffer);
        _gpuSplatParticles.SetFloat(FIXED_DELTA_TIME, Time.fixedDeltaTime);
        _gpuSplatParticles.SetInt(CURRENT_FRAME, currentFrame);
        _gpuSplatParticles.SetFloat(PARTICLE_SPEED, WaveParticle.PARTICLE_SPEED);
        _gpuSplatParticles.SetInt(HORI_RES, pointMap.HoriRes);
        _gpuSplatParticles.SetInt(VERT_RES, pointMap.VertRes);
        _gpuSplatParticles.SetFloat(PLANE_WIDTH, pointMap.Width);
        _gpuSplatParticles.SetFloat(PLANE_HEIGHT, pointMap.Height);
        _gpuSplatParticles.SetInt(ANTI_ALIASED, (int)antiAliased);
        _gpuSplatParticles.SetInt(ANTI_ALIAS_FACTOR, antiAliasFactor);

        ///
        /// Dispatch the kernel that splats wave particles to the _splatTexture
        ///
        _gpuSplatParticles.Dispatch(kernel_SplatParticles, ((int)_particleContainerSize) / THREAD_GROUPS_X, 1, 1);
        Profiler.EndSample();

        Profiler.BeginSample("Do the splatting");
        if (antiAliased == AntiAlias.SCALE)
        {
            ///
            /// As we have SCALE anti-aliasing, we need to downscale the texture to a new one.
            ///

            downsampleMaterial.SetInt(DOWNSAMPLE_ANTI_ALIAS_FACTOR_ID, antiAliasFactor);
            downsampleMaterial.SetInt(DOWNSAMPLE_TEXTURE_WIDTH_ID, pointMap.HoriRes);
            downsampleMaterial.SetInt(DOWNSAMPLE_TEXTURE_HEIGHT_ID, pointMap.VertRes);

            if (outputTexture == null || downscaleRenderTexture == null)
            {
                pointMap.InitialiseTexture(out outputTexture, name: "Downscale Output Texture");

                // Set the texture to the active one so that it's values can be read back out to the pointMapTexture
                downscaleRenderTexture = new RenderTexture(pointMap.HoriRes, pointMap.VertRes, 24, RenderTextureFormat.ARGBFloat);
            }

            if (!downscaleRenderTexture.IsCreated())
            {
                downscaleRenderTexture.Create();
            }

            RenderTexture.active = downscaleRenderTexture;

            GL.Clear(true, true, Color.black);

            // Blit what is stored in the _splatTexture to downscaleRenderTexture
            Graphics.Blit(_splatTexture, downscaleRenderTexture, downsampleMaterial);

            // Read back values from the render texture
            outputTexture.ReadPixels(new Rect(0, 0, outputTexture.width, outputTexture.height), 0, 0, false);
            outputTexture.Apply();


            RenderTexture.active = _splatTexture;
            downscaleRenderTexture.Release();

            GL.Clear(true, true, Color.black);

            RenderTexture.active = null;
            pointMap.UpdateTexture(outputTexture);
        }
        else
        {
            // Copy RenderTexture to a Texture2D
            RenderTexture.active = _splatTexture;

            if (outputTexture == null)
            {
                pointMap.InitialiseTexture(out outputTexture, name: "Point Map Texture");
            }

            // Read back values from the render texture
            outputTexture.ReadPixels(new Rect(0, 0, outputTexture.width, outputTexture.height), 0, 0, false);
            outputTexture.Apply();

            pointMap.UpdateTexture(outputTexture);

            GL.Clear(true, true, Color.black);
            RenderTexture.active = null;
        }
        Profiler.EndSample();
        Profiler.EndSample();
    }