Esempio n. 1
0
 void Start()
 {
     Nplus1          = N + 1;
     m_fourier       = new FourierCPU(N);
     m_windDirection = new Vector2(m_windSpeed.x, m_windSpeed.y);
     m_windDirection.Normalize();
     m_dispersionTable = new float[Nplus1 * Nplus1];
     for (int m_prime = 0; m_prime < Nplus1; m_prime++)
     {
         for (int n_prime = 0; n_prime < Nplus1; n_prime++)
         {
             int index = m_prime * Nplus1 + n_prime;
             m_dispersionTable[index] = Dispersion(n_prime, m_prime);
         }
     }
     m_heightBuffer       = new Vector2[2, N *N];
     m_slopeBuffer        = new Vector4[2, N *N];
     m_displacementBuffer = new Vector4[2, N *N];
     m_spectrum           = new Vector2[Nplus1 * Nplus1];
     m_spectrum_conj      = new Vector2[Nplus1 * Nplus1];
     m_position           = new Vector3[Nplus1 * Nplus1];
     m_vertices           = new Vector3[Nplus1 * Nplus1];
     m_normals            = new Vector3[Nplus1 * Nplus1];
     m_mesh = MakeMesh(Nplus1);
     m_mesh.Optimize();
     m_oceanGrid = new GameObject[m_numGridsX * m_numGridsZ];
     for (int x = 0; x < m_numGridsX; x++)
     {
         for (int z = 0; z < m_numGridsZ; z++)
         {
             int idx = x + z * m_numGridsX;
             m_oceanGrid[idx] = new GameObject("Ocean grid " + idx.ToString());
             m_oceanGrid[idx].AddComponent <MeshFilter>();
             m_oceanGrid[idx].AddComponent <MeshRenderer>();
             m_oceanGrid[idx].GetComponent <Renderer>().material = m_mat;
             m_oceanGrid[idx].GetComponent <MeshFilter>().mesh   = m_mesh;
             m_oceanGrid[idx].transform.Translate(new Vector3(x * m_length - m_numGridsX * m_length / 2, 0.0f, z * m_length - m_numGridsZ * m_length / 2));
             m_oceanGrid[idx].transform.parent = this.transform;
         }
     }
     UnityEngine.Random.seed = 0;
     Vector3[] vertices = m_mesh.vertices;
     for (int m_prime = 0; m_prime < Nplus1; m_prime++)
     {
         for (int n_prime = 0; n_prime < Nplus1; n_prime++)
         {
             int index = m_prime * Nplus1 + n_prime;
             m_spectrum[index]         = GetSpectrum(n_prime, m_prime);
             m_spectrum_conj[index]    = GetSpectrum(-n_prime, -m_prime);
             m_spectrum_conj[index].y *= -1.0f;
             m_position[index].x       = vertices[index].x = n_prime * m_length / N;
             m_position[index].y       = vertices[index].y = 0.0f;
             m_position[index].z       = vertices[index].z = m_prime * m_length / N;
         }
     }
     m_mesh.vertices = vertices;
     m_mesh.RecalculateBounds();
     CreateFresnelLookUp();
 }
Esempio n. 2
0
        void Start()
        {
            waterTiles.Clear ();
            Nplus1 = N+1;

            waterTilesMax = (m_numGridsX * m_numGridsZ);

            m_fourier = new FourierCPU(N);

            m_windDirection = new Vector2(m_windSpeed.x, m_windSpeed.y);
            m_windDirection.Normalize();

            m_dispersionTable = new float[Nplus1*Nplus1];

            for (int m_prime = 0; m_prime < Nplus1; m_prime++)
            {
                for (int n_prime = 0; n_prime < Nplus1; n_prime++)
                {
                    int index = m_prime * Nplus1 + n_prime;
                    m_dispersionTable[index] = Dispersion(n_prime,m_prime);
                }
            }

            m_heightBuffer = new Vector2[2,N*N];
            m_slopeBuffer = new Vector4[2,N*N];
            m_displacementBuffer = new Vector4[2,N*N];

            m_spectrum = new Vector2[Nplus1*Nplus1];
            m_spectrum_conj = new Vector2[Nplus1*Nplus1];

            m_position = new Vector3[Nplus1*Nplus1];
            m_vertices = new Vector3[Nplus1*Nplus1];
            m_normals = new Vector3[Nplus1*Nplus1];

            m_mesh = MakeMesh(Nplus1);
            //m_mesh.Optimize();

            m_oceanGrid = new GameObject[m_numGridsX*m_numGridsZ];

            if (waterTiles.Count < (m_numGridsX * m_numGridsZ)) {
                for (int x = 0; x < m_numGridsX; x++) {
                    for (int z = 0; z < m_numGridsZ; z++) {
                        int idx = x + z * m_numGridsX;

                        m_oceanGrid [idx] = new GameObject ("Ocean grid " + idx.ToString ());
                        m_oceanGrid [idx].AddComponent<MeshFilter> ();
                        m_oceanGrid [idx].AddComponent<MeshRenderer> ();
                        m_oceanGrid [idx].GetComponent<Renderer> ().material = m_mat;
                        m_oceanGrid [idx].GetComponent<MeshFilter> ().mesh = m_mesh;
                        m_oceanGrid [idx].transform.Translate (new Vector3 (x * m_length - m_numGridsX * m_length / 2, 0.0f, z * m_length - m_numGridsZ * m_length / 2));
                        m_oceanGrid [idx].transform.parent = this.transform;
                        waterTiles.Add (m_oceanGrid [idx]);
                        m_oceanGrid [idx].transform.position = new Vector3 (m_oceanGrid [idx].transform.position.x + m_oceanGrid [idx].transform.parent.position.x, m_oceanGrid [idx].transform.position.y + m_oceanGrid [idx].transform.parent.position.y, m_oceanGrid [idx].transform.position.z + m_oceanGrid [idx].transform.parent.position.z);
                        m_oceanGrid [idx].AddComponent<MeshCollider>();
                    }
                }
            }

            UnityEngine.Random.seed = 0;

            Vector3[] vertices = m_mesh.vertices;

            for (int m_prime = 0; m_prime < Nplus1; m_prime++)
            {
                for (int n_prime = 0; n_prime < Nplus1; n_prime++)
                {
                    int index = m_prime * Nplus1 + n_prime;

                    m_spectrum[index] = GetSpectrum( n_prime,  m_prime);

                    m_spectrum_conj[index] = GetSpectrum(-n_prime, -m_prime);
                    m_spectrum_conj[index].y *= -1.0f;

                    m_position[index].x = vertices[index].x =  n_prime * m_length/N;
                    m_position[index].y = vertices[index].y =  0.0f;
                    m_position[index].z = vertices[index].z =  m_prime * m_length/N;

                }
            }

            m_mesh.vertices = vertices;
            m_mesh.RecalculateBounds();

            CreateFresnelLookUp();
        }