Esempio n. 1
0
    Mesh GenerateMesh()
    {
        var mc = new MeshConstructor();
        mc.mesh.name = name;
        mc.SetSubmeshCount(2);

        latitudes = latitudeSpans + 1;
        longitudes = longitudeSpans + 1;
        pictureLatitudeSpans = 2 * halfLatitudePictureSpans;
        pictureLongitudeSpans = 2 * halfLongitudePictureSpans;
        pictureLatitudes = pictureLatitudeSpans + 1;
        pictureLongitudes = pictureLongitudeSpans + 1;

        pictureSpans = new bool[latitudeSpans, longitudeSpans];
        baseMatrix = new MeshVertex[latitudes, longitudes];
        pictureMatrix = new MeshVertex[latitudes, longitudes];

        for (int latitudePictureSpanIndex = 0; latitudePictureSpanIndex < pictureLatitudeSpans; latitudePictureSpanIndex++) {
            for (int longitudePictureSpanIndex = 0; longitudePictureSpanIndex < pictureLongitudeSpans; longitudePictureSpanIndex++) {
                int latitudeSpanIndex = latitudes/2-halfLatitudePictureSpans+latitudePictureSpanIndex;
                int longitudeSpanIndex = Extensions.Modulo(-halfLongitudePictureSpans+longitudePictureSpanIndex, longitudeSpans);
                pictureSpans[latitudeSpanIndex, longitudeSpanIndex] = true;
            }
        }

        for (int latitudeIndex = 0; latitudeIndex <= latitudeSpans; latitudeIndex++) {
            for (int longitudeIndex = 0; longitudeIndex <= longitudeSpans; longitudeIndex++) {
                baseMatrix[latitudeIndex, longitudeIndex] = new MeshVertex(
                    position: getSpherePoint(latitudeIndex, longitudeIndex),
                    uv: new Vector2(1f * longitudeIndex / longitudeSpans, 1f * latitudeIndex / latitudeSpans),
                    normal: getSpherePoint(latitudeIndex, longitudeIndex).normalized
                );
            }
        }

        for (int pictureLatitudeIndex = 0; pictureLatitudeIndex <= 2 * halfLatitudePictureSpans; pictureLatitudeIndex++) {
            for (int pictureLongitudeIndex = 0; pictureLongitudeIndex <= 2 * halfLongitudePictureSpans; pictureLongitudeIndex++) {
                int latitudeIndex = latitudes/2 -halfLatitudePictureSpans + pictureLatitudeIndex;
                int longitudeIndex = Extensions.Modulo(-halfLongitudePictureSpans + pictureLongitudeIndex, longitudeSpans);

                //Debug.LogFormat("Adding picture vertex {0}, {1}", latitudeIndex, longitudeIndex);
                pictureMatrix[latitudeIndex, longitudeIndex] = new MeshVertex(
                    position: getSpherePoint(latitudeIndex, longitudeIndex),
                    uv: new Vector2(1f * pictureLongitudeIndex / pictureLongitudeSpans, 1f * pictureLatitudeIndex / pictureLatitudeSpans),
                    normal: getSpherePoint(latitudeIndex, longitudeIndex).normalized
                );

                if (longitudeIndex == 0) {
                    longitudeIndex = longitudes - 1;
                    //Debug.LogFormat("Adding picture vertex {0}, {1}", latitudeIndex, longitudeIndex);
                    pictureMatrix[latitudeIndex, longitudeIndex] = new MeshVertex(
                        position: getSpherePoint(latitudeIndex, longitudeIndex),
                        uv: new Vector2(1f * pictureLongitudeIndex / pictureLongitudeSpans, 1f * pictureLatitudeIndex / pictureLatitudeSpans),
                        normal: getSpherePoint(latitudeIndex, longitudeIndex).normalized
                    );
                }
            }
        }

        for (int latitudeSpanIndex = 0; latitudeSpanIndex < latitudeSpans; latitudeSpanIndex++) {
            for (int longitudeSpanIndex = 0; longitudeSpanIndex < longitudeSpans; longitudeSpanIndex++) {
                if (isPictureSpan(latitudeSpanIndex, longitudeSpanIndex)) {
                    AddSpan(mc, pictureMatrix, latitudeSpanIndex, longitudeSpanIndex, 1);
                }
                AddSpan(mc, baseMatrix, latitudeSpanIndex, longitudeSpanIndex);
            }
        }

        return mc.Done();
    }