Esempio n. 1
0
    /// <summary>
    /// Executed by Unity on every first frame <see cref="https://docs.unity3d.com/Manual/ExecutionOrder.html"/>
    /// </summary>
    private void Update()
    {
        CubeGrid cubegrid = new CubeGrid((float)0.05);

        cubegrid.genTriangle();

        List <Vector3> vertices  = new List <Vector3>();
        List <Vector3> norms     = new List <Vector3>();
        List <int>     triangles = new List <int>();

        // What is going to happen if we don't split the vertices? Check it out by yourself by passing
        // sourceVertices and sourceTriangles to the mesh.
        for (int i = 0; i < cubegrid.sourceTriangles.Count; i++)
        {
            triangles.Add(vertices.Count);
            Vector3 vertexPos = cubegrid.sourceVertices[cubegrid.sourceTriangles[i]];
            Vector3 vnorm     = cubegrid.sourceNorms[cubegrid.sourceTriangles[i]];

            //Uncomment for some animation:
            //vertexPos += new Vector3
            //(
            //    Mathf.Sin(Time.time + vertexPos.z),
            //    Mathf.Sin(Time.time + vertexPos.y),
            //    Mathf.Sin(Time.time + vertexPos.x)
            //);

            vertices.Add(2 * (vertexPos - new Vector3((float)0.5, (float)0.5, (float)0.5)));
            norms.Add(vnorm);
        }

        // Here unity automatically assumes that vertices are points and hence will be represented as (x, y, z, 1) in homogenous coordinates
        _mesh.SetVertices(vertices);
        _mesh.SetTriangles(triangles, 0);
        _mesh.SetNormals(norms);
        //_mesh.RecalculateNormals();

        // Upload mesh data to the GPU
        _mesh.UploadMeshData(false);
    }