Ejemplo n.º 1
0
    // Meth Construct Mesh
    public void ConstructMesh()
    {
        // Create an array of vertices (square of res)
        Vector3[] vertices = new Vector3[resolution * resolution];
        // create an array of triangles ((square of res - 1 on each side) * (2 triangles on each face + 3 vertices that compose a triangle))
        int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        // index
        int triIndex = 0;

        // Loop throught the resolution square
        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                // the place we are on the loop
                int i = x + y * resolution;
                // percentage of completion
                Vector2 percent = new Vector2(x, y) / (resolution - 1);
                // set the coor of the point
                Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                // now trick it into a sphere
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                // set the vercite i the value of pointOnUnitCube
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

                // create triangles
                if (x != resolution - 1 && y != resolution - 1) // dont create triangle on the last x and y ligne
                {
                    // Create a square with 2 triangles

                    // first tri
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;
                    // sec tri
                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;

                    // increment triIndex by 6
                    triIndex += 6;
                }
            }
        }
        // Unassign the stuff in mesh
        mesh.Clear();
        // assign to mesh the vertices and triangles
        mesh.vertices  = vertices;
        mesh.normals   = vertices; // vert are alredy normalized
        mesh.triangles = triangles;
        // Recalc all
        //mesh.RecalculateNormals();
    }
Ejemplo n.º 2
0
    public void ConstructMesh(PhysicMaterial material)
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];

        int triIndex = 0;

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int     i                 = x + y * resolution;
                Vector2 percent           = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube   = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);
                //Debug.Log(shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere)+"x"+pointOnUnitSphere);

                if (x != resolution - 1 && y != resolution - 1)
                {
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;
                    triIndex += 6;
                }
            }
        }

        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
    }
Ejemplo n.º 3
0
    public void ConstructMesh()
    {
        int resMin1 = resolution - 1;

        Vector3[] vertices = new Vector3[resolution * resolution];
        int[]     tris     = new int[resMin1 * resMin1 * 6];
        Vector3[] norms    = new Vector3[resolution * resolution];

        int vCount = 0;

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                Vector2 percent = new Vector2(x, y) / resMin1;

                Vector3 pointOnSphere;

                if (true)
                {
                    if (percent.x == 0.5 && percent.y == 0.5)
                    {
                        pointOnSphere = normal;
                    }
                    else if (percent.y == 0.5f)
                    {
                        pointOnSphere = Quaternion.AngleAxis((percent.x - 0.5f) * 90.0f, tangent) * normal;
                    }
                    else if (percent.x == 0.5f)
                    {
                        pointOnSphere = Quaternion.AngleAxis((percent.y - 0.5f) * 90.0f, bitangent) * normal;
                    }
                    else
                    {
                        Vector3 rotOnTan   = Quaternion.AngleAxis((percent.x - 0.5f) * 90.0f, tangent) * normal;
                        Vector3 planeANorm = Vector3.Cross(rotOnTan, tangent).normalized;
                        Vector3 rotOnBitan = Quaternion.AngleAxis((percent.y - 0.5f) * 90.0f, bitangent) * normal;
                        Vector3 planeBNorm = Vector3.Cross(rotOnBitan, bitangent).normalized;

                        if (planeANorm.x == 0)
                        {
                            if (planeBNorm.x == 0)
                            {
                                Debug.LogError("No x is non-zero.");
                                return;
                            }
                            else
                            {
                                Vector3 temp = planeBNorm;
                                planeBNorm = planeANorm;
                                planeANorm = temp;
                            }
                        }
                        float hDenom = planeBNorm.y * planeANorm.x - planeBNorm.x * planeANorm.y;
                        if (hDenom == 0)
                        {
                            Debug.LogError("H denominator borked.");
                            return;
                        }

                        float hValue = (planeANorm.z * planeBNorm.x - planeANorm.x * planeBNorm.z) / hDenom;
                        float gValue = ((-planeANorm.y) * hValue - planeANorm.z) / planeANorm.x;

                        float kDenom = gValue * gValue + hValue * hValue + 1;
                        if (kDenom == 0)
                        {
                            Debug.LogError("Denominator of k has encountered a problem.");
                            return;
                        }
                        float kValue = Mathf.Sqrt(1.0f / kDenom);

                        Vector3 test1 = new Vector3(gValue * kValue, hValue * kValue, kValue);
                        if (Vector3.Dot(test1, normal) > 0)
                        {
                            pointOnSphere = test1;
                        }
                        else
                        {
                            pointOnSphere = new Vector3(-gValue * kValue, -hValue * kValue, -kValue);
                        }

                        pointOnSphere = pointOnSphere.normalized;
                    }
                }
                else
                {
                    Vector3 pointOnCube = normal + (percent.x - 0.5f) * 2.0f * tangent + (percent.y - 0.5f) * 2.0f * bitangent;

                    pointOnSphere = pointOnCube.normalized;
                }
                vertices[vCount] = shapeGenerator.CalculatePointOnPlanet(pointOnSphere);
                norms[vCount]    = pointOnSphere;

                if (!(y == resolution - 1 || x == resolution - 1))
                {
                    int triCount = (vCount - y) * 6;
                    tris[triCount]     = vCount;
                    tris[triCount + 1] = vCount + resolution + 1;
                    tris[triCount + 2] = vCount + resolution;

                    tris[triCount + 3] = vCount;
                    tris[triCount + 4] = vCount + 1;
                    tris[triCount + 5] = vCount + resolution + 1;
                }

                vCount++;
            }
        }

        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = tris;
        //mesh.normals = norms;
        mesh.RecalculateNormals();
    }