public static Color[] creatKernel(int kernelHeight, int kernelWidth, ExtendedHeightField.HeightFieldInfo heightFieldInfo)
    {
        Color[] kernel = new Color[kernelWidth * kernelHeight];

        // Create the kernel that is used in convolution.
        for (int y = 0; y < kernelHeight; y++)
        {
            for (int x = 0; x < kernelWidth; x++)
            {
                int   index = (y * kernelWidth) + x;
                float abs_diff;

                float x_component = ((kernelWidth / 2) - x) * heightFieldInfo.UnitX;
                float y_component = ((kernelHeight / 2) - y) * heightFieldInfo.UnitY;
                {
                    abs_diff = Mathf.Sqrt((y_component * y_component) + (x_component * x_component));
                }
                if (abs_diff > WaveParticle.RADIUS)
                {
                    // Don't need to do rest of calculation, as these pixel fall outside of wave particles's radii.
                    kernel[index] = new Vector4(0, 0, 0, 1);
                }
                else
                {
                    float   relativePixelDistance = (Mathf.PI * abs_diff) / WaveParticle.RADIUS;
                    float   y_displacement_factor = 0.5f * (Mathf.Cos(relativePixelDistance) + 1);
                    Vector2 long_component        = -Mathf.Sqrt(2) * y_displacement_factor * Mathf.Sin(relativePixelDistance) * new Vector2(x_component, y_component);
                    kernel[index] = new Color(long_component.x, y_displacement_factor, long_component.y, 1);
                }
            }
        }
        return(kernel);
    }
    override public void Initialise(ExtendedHeightField.HeightFieldInfo heightFieldInfo, ParticleContainer waveParticles)
    {
        this.heightFieldInfo = heightFieldInfo;
        this.waveParticles   = waveParticles;

        sectionWidth  = Mathf.CeilToInt((WaveParticle.RADIUS / heightFieldInfo.Width) * heightFieldInfo.HoriRes);
        sectionHeight = Mathf.CeilToInt((WaveParticle.RADIUS / heightFieldInfo.Height) * heightFieldInfo.VertRes);
    }
    override public void Initialise(ExtendedHeightField.HeightFieldInfo hf, ParticleContainer wp)
    {
        heightFieldInfo = hf;
        pointMap        = new ExtendedHeightField(hf.Width, hf.Height, hf.HoriRes, hf.VertRes);
        pointMap.textureHeightMap.name = "Point Map Texture";
        waveParticles = wp;

        kernelWidth  = Mathf.CeilToInt((WaveParticle.RADIUS / heightFieldInfo.Width) * heightFieldInfo.HoriRes);
        kernelHeight = Mathf.CeilToInt((WaveParticle.RADIUS / heightFieldInfo.Height) * heightFieldInfo.VertRes);
        kernel       = creatKernel(kernelWidth, kernelHeight, heightFieldInfo);
    }
    override public void Initialise(ExtendedHeightField.HeightFieldInfo hf, ParticleContainer wp)
    {
        heightFieldInfo = hf;
        pointMap        = new ExtendedHeightField(hf.Width, hf.Height, hf.HoriRes, hf.VertRes);

        convolvedTexture            = new Texture2D(heightFieldInfo.HoriRes, heightFieldInfo.VertRes, TextureFormat.RGBAFloat, false);
        convolvedTexture.anisoLevel = 1;
        convolvedTexture.filterMode = FilterMode.Point;
        convolvedTexture.wrapMode   = TextureWrapMode.Clamp;
        convolvedTexture.name       = "Convolved Texture";

        shaderTexture = new RenderTexture(heightFieldInfo.HoriRes, heightFieldInfo.VertRes, 24, RenderTextureFormat.ARGBFloat);
        shaderTexture.antiAliasing     = 1;
        shaderTexture.anisoLevel       = 0;
        shaderTexture.autoGenerateMips = false;
        shaderTexture.wrapMode         = TextureWrapMode.Clamp;
        shaderTexture.filterMode       = FilterMode.Point;


        int kernelWidth  = Mathf.CeilToInt((WaveParticle.RADIUS / heightFieldInfo.Width) * heightFieldInfo.HoriRes);
        int kernelHeight = Mathf.CeilToInt((WaveParticle.RADIUS / heightFieldInfo.Height) * heightFieldInfo.VertRes);

        Color[] kernelArray = Convolution2DFastHeightFieldGenerator.creatKernel(kernelHeight, kernelWidth, heightFieldInfo);

        kernel = new Texture2D(kernelWidth, kernelHeight, TextureFormat.RGBAFloat, false);
        kernel.SetPixels(kernelArray);
        kernel.Apply();

        convolutionMaterial = new Material(Shader.Find("Unlit/2DFunction"));
        convolutionMaterial.SetTexture(Shader.PropertyToID("_KernelTex"), kernel);
        convolutionMaterial.SetFloat(Shader.PropertyToID("_Width"), heightFieldInfo.Width);
        convolutionMaterial.SetFloat(Shader.PropertyToID("_Height"), heightFieldInfo.Height);
        convolutionMaterial.SetInt(Shader.PropertyToID("_HoriRes"), heightFieldInfo.HoriRes);
        convolutionMaterial.SetInt(Shader.PropertyToID("_VertRes"), heightFieldInfo.VertRes);
        convolutionMaterial.SetFloat(Shader.PropertyToID("_ParticleRadii"), WaveParticle.RADIUS);
        convolutionMaterial.SetFloat(Shader.PropertyToID("_KernelWidth"), kernelWidth);
        convolutionMaterial.SetFloat(Shader.PropertyToID("_KernelHeight"), kernelHeight);

        waveParticles = wp;
    }
    public static HeightFieldGenerator CreateAndInitialise(Choice hfge, ExtendedHeightField.HeightFieldInfo heightFieldInfo, ParticleContainer waveParticles)
    {
        HeightFieldGenerator heightFieldGenerator;

        switch (hfge)
        {
        case Choice.GPU_CONVOLUTION_2D:
        {
            heightFieldGenerator = new GPUConvolution2DFastHeightFieldGenerator();
        }
        break;

        case Choice.CPU_CONVOLUTION_2D:
        {
            heightFieldGenerator = new Convolution2DFastHeightFieldGenerator();
        }
        break;

        case Choice.PRECISE:
        {
            heightFieldGenerator = new PreciseHeightFieldGenerator();
        }
        break;

        case Choice.NONE:
        {
            heightFieldGenerator = new EmptyHeightFieldGenerator();
        }
        break;

        default:
        {
            // TODO: throw an appropriate exception!
            throw new System.Exception();
        }
        }
        heightFieldGenerator.Initialise(heightFieldInfo, waveParticles);
        return(heightFieldGenerator);
    }
 public abstract void Initialise(ExtendedHeightField.HeightFieldInfo heightFieldInfo, ParticleContainer waveParticles);
Ejemplo n.º 7
0
 public override void Initialise(ExtendedHeightField.HeightFieldInfo heightFieldInfo, ParticleContainer waveParticles)
 {
     return;
 }