// Use this for initialization
    void Start()
    {
        //******** Create water mesh ********
        waterMeshFilter = GetComponent <MeshFilter>();
        GenerateWaterMesh.GenerateWater(waterMeshFilter, waterWidth, gridSpacing);
        //Need a box collider so the mouse can interact with the water
        BoxCollider boxCollider = GetComponent <BoxCollider>();

        boxCollider.size = new Vector3(waterWidth, 0.1f, waterWidth);

        //******** Precompute kernel ********
        storedKernelArray = new float[2 * P + 1, 2 * P + 1];
        PrecomputeKernelValues();

        //Get water mesh and its vertice
        waterMesh    = waterMeshFilter.mesh;
        meshVertices = new List <Vector3>();
        waterMesh.GetVertices(meshVertices);
        verticePerRow = (int)Mathf.Sqrt(meshVertices.Count);
        //Initialization
        source        = new float[verticePerRow, verticePerRow];
        obstruction   = new float[verticePerRow, verticePerRow];
        data          = new RenderTexture(verticePerRow, verticePerRow, 24);
        data.format   = RenderTextureFormat.ARGBFloat;
        kernel        = new RenderTexture(2 * P + 1, 2 * P + 1, 24);
        kernel.format = RenderTextureFormat.ARGBFloat;

        for (int c = 0, i = 0; c < verticePerRow; c++)
        {
            for (int r = 0; r < verticePerRow; r++, i++)
            {
                if (c == 0 || c == verticePerRow - 1 || r == 0 || r == verticePerRow - 1)
                {
                    obstruction[c, r] = 0f;
                }
                else
                {
                    obstruction[c, r] = 1f;
                }
            }
        }
        ApplyFloatsToRT(storedKernelArray, kernel);

        //Set shader parameters
        waterMat = GetComponent <Renderer>().material;
        waveUpdateMat.SetTexture("_kernel", kernel);
        waveUpdateMat.SetFloat("kernelSize", P);

        //Multiple render target
        mrts = new MRT[2];
        for (int i = 0, n = mrts.Length; i < n; i++)
        {
            mrts[i] = new MRT(verticePerRow, verticePerRow);
        }
    }
    // Use this for initialization
    void Start()
    {
        //Get the controllerScripts
        GameObject gameController = GameObject.FindGameObjectWithTag("GameController");

        worldScript = gameController.GetComponent <WorldController> ();

        //Get and update seabed mesh
        seabedMesh = this.GetComponent <MeshFilter>().mesh;
        GenerateWaterMesh.GenerateWater(this.GetComponent <MeshFilter>(), worldScript.width, 0.5f, 0.0f);
        seabedMesh = this.GetComponent <MeshFilter>().mesh;

        this.transform.position = new Vector3(0.0f, -worldScript.height, 0.0f);
    }
    void Start()
    {
        //Get the controllerScripts
        GameObject gameController = GameObject.FindGameObjectWithTag("GameController");

        waveScript  = gameController.GetComponent <WaveController>();
        worldScript = gameController.GetComponent <WorldController> ();

        //Get the water mesh
        waterMesh = this.GetComponent <MeshFilter>().mesh;
        GenerateWaterMesh.GenerateWater(this.GetComponent <MeshFilter>(), worldScript.width, 0.5f, 0.0f);
        waterMesh = this.GetComponent <MeshFilter>().mesh;

        originalVertices = waterMesh.vertices;
    }
    // Use this for initialization
    void Start()
    {
        //Create water mesh
        waterMeshFilter = this.GetComponent <MeshFilter>();
        GenerateWaterMesh.GenerateWater(waterMeshFilter, waterWidth, gridSpacing);
        List <Vector3[]> height_tmp = GenerateWaterMesh.vertices2dArray;

        waterMesh = waterMeshFilter.mesh;
        //Need a box collider so the mouse can interact with the water
        BoxCollider boxCollider = this.GetComponent <BoxCollider>();

        boxCollider.size = new Vector3(waterWidth, 0.1f, waterWidth);

        //Precompute the convolution kernel values
        storedKernelArray = new float[P * 2 + 1, P * 2 + 1];
        PrecomputeKernelValues();

        //Init the arrays we need
        //These are now filled with heights at 0
        height = height_tmp.ToArray();
        //Need to clone these
        previousHeight     = CloneList(height);
        verticalDerivative = CloneList(height);
        source             = CloneList(height);
        obstruction        = CloneList(height);
        heightDifference   = CloneList(height);
        //Create this once here, so we dont need to create it each update
        unfolded_verts = new Vector3[height.Length * height.Length];
        arrayLength    = height.Length;

        //Add obstruction when the wave hits the walls
        for (int j = 0; j < arrayLength; j++)
        {
            for (int i = 0; i < arrayLength; i++)
            {
                if (j == 0 || j == arrayLength - 1 || i == 0 || i == arrayLength - 1)
                {
                    obstruction[j][i].y = 0f;
                }
                else
                {
                    obstruction[j][i].y = 1f;
                }
            }
        }
    }
    void Start()
    {
        waterMeshFilter = this.GetComponent <MeshFilter>();

        List <Vector3[]> height_tmp = GenerateWaterMesh.GenerateWater(waterMeshFilter, waterWidth, gridSpacing);

        waterMesh = waterMeshFilter.mesh;

        BoxCollider boxCollider = this.GetComponent <BoxCollider>();

        boxCollider.center = new Vector3(waterWidth / 2f, 0f, waterWidth / 2f);
        boxCollider.size   = new Vector3(waterWidth, 0.1f, waterWidth);

        transform.position = new Vector3(-waterWidth / 2f, 0f, -waterWidth / 2f);

        storedKernelArray = new float[P * 2 + 1, P * 2 + 1];
        PrecomputeKernelValues();

        height = height_tmp.ToArray();
        //Need to clone these
        previousHeight     = CloneList(height);
        verticalDerivative = CloneList(height);
        source             = CloneList(height);
        obstruction        = CloneList(height);
        heightDifference   = CloneList(height);

        //Create this once here, so we dont need to create it each update
        unfolded_verts = new Vector3[height.Length * height.Length];
        arrayLength    = height.Length;

        //Add obstruction when the wave hits the walls
        for (int j = 0; j < arrayLength; j++)
        {
            for (int i = 0; i < arrayLength; i++)
            {
                if (j == 0 || j == arrayLength - 1 || i == 0 || i == arrayLength - 1)
                {
                    obstruction[j][i].y = 0f;
                }
                else
                {
                    obstruction[j][i].y = 1f;
                }
            }
        }
    }
Exemple #6
0
    void Start()
    {
        waterMeshFilter = this.GetComponent <MeshFilter>();
        List <Vector3[]> height_tmp = GenerateWaterMesh.GenerateWater(waterMeshFilter, waterWidth, gridSpacing);

        waterMesh = waterMeshFilter.mesh;

        BoxCollider boxCollider = this.GetComponent <BoxCollider>();

        boxCollider.center = new Vector3(waterWidth / 2.0f, 0.0f, waterWidth / 2.0f);
        boxCollider.size   = new Vector3(waterWidth, 0.1f, waterWidth);

        transform.position = new Vector3(-waterWidth / 2.0f, 0f, -waterWidth / 2.0f);

        //part 2
        storedKernelArray = new float[P * 2 + 1, P * 2 + 1];
        PrecomputeKernelValues();

        //part 2.2
        height             = height_tmp.ToArray();
        previousHeight     = CloneList(height);
        verticalDerivative = CloneList(height);
        source             = CloneList(height);
        obstruction        = CloneList(height);
        heightDifference   = CloneList(height);

        unfolded_verts = new Vector3[height.Length * height.Length];
        arrayLength    = height.Length;

        //add obstructions
        for (int x = 0; x < arrayLength; x++)
        {
            for (int y = 0; y < arrayLength; y++)
            {
                if (x == 0 || x == arrayLength - 1 || y == 0 || y == arrayLength - 1)
                {
                    obstruction[x][y].y = 0f;
                }
                else
                {
                    obstruction[x][y].y = 1f;
                }
            }
        }
    }
    // Use this for initialization
    void Start()
    {
        //Need reference to the meshfilter so we can add the water
        storedKernelArray = new float[P * 2 + 1, P * 2 + 1];

        PrecomputeKernelValues();
        waterMeshFilter = this.GetComponent <MeshFilter>();

        //Create the water mesh
        //Don't forget to write "using System.Collections.Generic;" at the top
        List <Vector3[]> height_tmp = GenerateWaterMesh.GenerateWater(waterMeshFilter, waterWidth, gridSpacing);

        height = height_tmp.ToArray();
        //Need to clone these
        previousHeight     = CloneList(height);
        verticalDerivative = CloneList(height);
        source             = CloneList(height);
        obstruction        = CloneList(height);
        heightDifference   = CloneList(height);

        //Create this once here, so we dont need to create it each update
        unfolded_verts = new Vector3[height.Length * height.Length];

        arrayLength = height.Length;

        //Get a reference to the watermesh
        waterMesh = waterMeshFilter.mesh;

        //Resize box collider
        //Need a box collider so the mouse can interact with the water
        BoxCollider boxCollider = this.GetComponent <BoxCollider>();

        boxCollider.center = new Vector3(waterWidth / 2f, 0f, waterWidth / 2f);
        boxCollider.size   = new Vector3(waterWidth, 0.1f, waterWidth);

        //Center the mesh to make it easier to know where it is
        transform.position = new Vector3(-waterWidth / 2f, 0f, -waterWidth / 2f);
    }
    void Start()
    {
        //Need reference to the meshfilter so we can add the water
        waterMeshFilter = this.GetComponent <MeshFilter>();

        waterWidth = this.transform.localScale.x * 10;

        //Create the water mesh
        //Don't forget to write "using System.Collections.Generic;" at the top
        List <Vector3[]> height_tmp = GenerateWaterMesh.GenerateWater(waterMeshFilter, waterWidth, gridSpacing);

        //Get a reference to the watermesh
        waterMesh = waterMeshFilter.mesh;

        //Resize box collider
        //Need a box collider so the mouse can interact with the water
        BoxCollider boxCollider = this.GetComponent <BoxCollider>();

        boxCollider.center = new Vector3(waterWidth / 2f, 0f, waterWidth / 2f);
        boxCollider.size   = new Vector3(waterWidth, 0.1f, waterWidth);

        //Center the mesh to make it easier to know where it isW
        this.transform.position = new Vector3(-waterWidth / 2f, this.transform.position.y, -waterWidth / 2f);

        //Precompute the convolution kernel values
        storedKernelArray = new float[P * 2 + 1, P * 2 + 1];

        PrecomputeKernelValues();

        //Init the arrays we need
        //These are now filled with heights at 0
        height = height_tmp.ToArray();
        //Need to clone these
        previousHeight     = CloneList(height);
        verticalDerivative = CloneList(height);
        source             = CloneList(height);
        obstruction        = CloneList(height);
        heightDifference   = CloneList(height);

        //Create this once here, so we dont need to create it each update
        unfolded_verts = new Vector3[height.Length * height.Length];

        arrayLength = height.Length;



        //Add obstruction when the wave hits the walls
        for (int j = 0; j < arrayLength; j++)
        {
            for (int i = 0; i < arrayLength; i++)
            {
                if (j == 0 || j == arrayLength - 1 || i == 0 || i == arrayLength - 1)
                {
                    obstruction[j][i].y = 0f;
                }
                else
                {
                    obstruction[j][i].y = 1f;
                }
            }
        }
    }
    void Start()
    {
        surfaceRender = this.GetComponent <Renderer>();

        //initialization wave parameters
        direction  = new Vector4[waves.Length];
        depth      = new float[waves.Length];
        amplitude  = new float[waves.Length];
        velocity   = new float[waves.Length];
        wavelength = new float[waves.Length];
        WVT        = new float[waves.Length];
        steepness  = new float[waves.Length];
        SVT        = new float[waves.Length];

        //genereate water mesh and save
        waterMeshFilter = GetComponent <MeshFilter>();
        GenerateWaterMesh.GenerateWater(waterMeshFilter, waterWidth, gridSpacing);
        waterMesh = waterMeshFilter.mesh;
        AssetDatabase.CreateAsset(waterMesh, "Assets/demo/ProceduralWater/demoMeshes/" + "demoOceanPlane" + ".asset");

        //tesselation and oceanfloor setup
        surfaceRender.sharedMaterial.DisableKeyword("OCEAN_FLOOR_ON");
        surfaceRender.sharedMaterial.DisableKeyword("TESS_ON");
        if (enableTess)
        {
            surfaceRender.sharedMaterial.EnableKeyword("TESS_ON");
        }
        if (enableOceanFloor)
        {
            List <Vector3> meshVertices = new List <Vector3>();
            waterMesh.GetVertices(meshVertices);
            float[] terrainHeight = new float[meshVertices.Count];
            for (int i = 0; i < meshVertices.Count; i++)
            {
                Vector3 world_v = transform.localToWorldMatrix.MultiplyPoint3x4(meshVertices[i]);
                terrainHeight[i] = Terrain.activeTerrain.SampleHeight(world_v);
            }
            int verticePerRow = (int)Mathf.Sqrt(meshVertices.Count);

            oceanFloor        = new RenderTexture(verticePerRow, verticePerRow, 24);
            oceanFloor.format = RenderTextureFormat.ARGBFloat;
            Texture2D tempTexture = new Texture2D(verticePerRow, verticePerRow, TextureFormat.RGBAFloat, false);
            RenderTexture.active = oceanFloor;
            tempTexture.ReadPixels(new Rect(0, 0, oceanFloor.width, oceanFloor.height), 0, 0, false);
            for (int y = 0, i = 0; y < verticePerRow; y++)
            {
                for (int x = 0; x < verticePerRow; x++, i++)
                {
                    tempTexture.SetPixel(x, y, new Color(terrainHeight[i], 0, 0, 0));
                }
            }
            tempTexture.Apply();
            RenderTexture.active = null;
            Graphics.Blit(tempTexture, oceanFloor);
            surfaceRender.sharedMaterial.SetTexture("_OceanFloor", oceanFloor);
            surfaceRender.sharedMaterial.EnableKeyword("OCEAN_FLOOR_ON");
        }

        // reflection cubemap
        GameObject CubeCam = new GameObject("CubeCam");

        CubeCam.AddComponent <Camera>();
        CubeCam.transform.position = new Vector3(0, 10, 0);
        CubeCam.transform.rotation = Quaternion.identity;
        CubeCam.GetComponent <Camera>().RenderToCubemap(reflCubemap);
        DestroyImmediate(CubeCam);
    }
    void Start()
    {
        surfaceRender = this.GetComponent <Renderer>();

        //initialize shallow wave parameters
        direction_SW  = new Vector4[shallowWaves.Length];
        depth_SW      = new float[shallowWaves.Length];
        amplitude_SW  = new float[shallowWaves.Length];
        velocity_SW   = new float[shallowWaves.Length];
        wavelength_SW = new float[shallowWaves.Length];
        WVT_SW        = new float[shallowWaves.Length];
        steepness_SW  = new float[shallowWaves.Length];
        SVT_SW        = new float[shallowWaves.Length];

        //initialize gerstner wave parameters
        direction_GW  = new Vector4[gerstnerWaves.Length];
        steepness_GW  = new float[gerstnerWaves.Length];
        amplitude_GW  = new float[gerstnerWaves.Length];
        velocity_GW   = new float[gerstnerWaves.Length];
        wavelength_GW = new float[gerstnerWaves.Length];

        //genereate water mesh and save
        waterMeshFilter = GetComponent <MeshFilter>();
        GenerateWaterMesh.GenerateWater(waterMeshFilter, waterWidth, gridSpacing);
        waterMesh = waterMeshFilter.mesh;
        #if UNITY_EDITOR
        AssetDatabase.CreateAsset(waterMesh, "Assets/demo/IntegratedWater/" + "IntegratedWaterOceanPlane" + ".asset");
        #endif

        //Need a box collider so object can interact with the water
        BoxCollider boxCollider = GetComponent <BoxCollider>();
        boxCollider.size = new Vector3(waterWidth, 0.1f, waterWidth);

        //oceanfloor setup
        surfaceRender.sharedMaterial.DisableKeyword("OCEAN_FLOOR_ON");
        if (enableOceanFloor)
        {
            List <Vector3> meshVertices = new List <Vector3>();
            waterMesh.GetVertices(meshVertices);
            Vector3[] terrainPosition = new Vector3[meshVertices.Count];
            for (int i = 0; i < meshVertices.Count; i++)
            {
                Vector3 world_v = transform.localToWorldMatrix.MultiplyPoint3x4(meshVertices[i]);
                terrainPosition[i].y = Terrain.activeTerrain.SampleHeight(world_v);
                terrainPosition[i].x = meshVertices[i].x;
                terrainPosition[i].z = meshVertices[i].z;
            }
            int verticePerRow = (int)Mathf.Sqrt(meshVertices.Count);

            oceanFloor        = new RenderTexture(verticePerRow, verticePerRow, 24);
            oceanFloor.format = RenderTextureFormat.ARGBFloat;
            Texture2D tempTexture = new Texture2D(verticePerRow, verticePerRow, TextureFormat.RGBAFloat, false);
            RenderTexture.active = oceanFloor;
            tempTexture.ReadPixels(new Rect(0, 0, oceanFloor.width, oceanFloor.height), 0, 0, false);
            for (int y = 0, i = 0; y < verticePerRow; y++)
            {
                for (int x = 0; x < verticePerRow; x++, i++)
                {
                    tempTexture.SetPixel(x, y, new Color(terrainPosition[i].x, terrainPosition[i].y, terrainPosition[i].z, 0));
                }
            }
            tempTexture.Apply();
            RenderTexture.active = null;
            Graphics.Blit(tempTexture, oceanFloor);
            surfaceRender.sharedMaterial.SetTexture("_OceanFloor", oceanFloor);
            surfaceRender.sharedMaterial.EnableKeyword("OCEAN_FLOOR_ON");
        }

        // reflection cubemap
        GameObject CubeCam = new GameObject("CubeCam");
        CubeCam.AddComponent <Camera>();
        CubeCam.transform.position = new Vector3(0, 10, 0);
        CubeCam.transform.rotation = Quaternion.identity;
        CubeCam.GetComponent <Camera>().RenderToCubemap(reflCubemap);
        DestroyImmediate(CubeCam);
    }