Ejemplo n.º 1
0
        public void FinaliseGeometry(Vector3 inputMin, Vector3 inputMax, float y)
        {
            if (subQuads.Count > 0)
            {
                a.FinaliseGeometry(inputMin, inputMax, y);
                b.FinaliseGeometry(inputMin, inputMax, y);
                c.FinaliseGeometry(inputMin, inputMax, y);
                d.FinaliseGeometry(inputMin, inputMax, y);
            }
            else
            {
                (Vector2 minV2, Vector2 maxV2) = NormaliseValuesZ(inputMin, inputMax);

                Vector2 minA = new Vector2(minX / 100, minY / 100);
                Vector2 maxB = new Vector2(maxX / 100, minY / 100);
                Vector2 maxC = new Vector2(minX / 100, maxY / 100);
                Vector2 maxD = new Vector2(maxX / 100, maxY / 100);

                Vector2 UVAB     = (minA + maxB) / 2;
                Vector2 UVAC     = (minA + maxC) / 2;
                Vector2 UVCD     = (maxC + maxD) / 2;
                Vector2 UVBD     = (maxB + maxD) / 2;
                Vector2 UVCENTER = (minA + maxB + maxC + maxD) / 4;

                //0
                Vector3 a = new Vector3(minV2.x, y, minV2.y);

                //1
                Vector3 b = new Vector3(maxV2.x, y, minV2.y);

                //2
                Vector3 c = new Vector3(minV2.x, y, maxV2.y);

                //3
                Vector3 d = new Vector3(maxV2.x, y, maxV2.y);

                //Top (4)
                Vector3 aToB = (a + b) / 2;

                //Right (5)
                Vector3 bToD = (b + d) / 2;

                //Bottom (6)
                Vector3 cToD = (c + d) / 2;

                //Left (7)
                Vector3 aToC = (a + c) / 2;

                //Center (8)
                Vector3 center = (a + b + c + d) / 4;

                //A = 0, A->B = 1, B = 2, B->D = 3, D = 4, D->C = 5, C = 6, C->A = 7, CENTER = 8

                List <Vector3> rangeAdd   = new List <Vector3>(new Vector3[] { a, aToB, b, bToD, d, cToD, c, aToC, center });
                List <Vector2> rangeAddUV = new List <Vector2>(new Vector2[] { minA, UVAB, maxB, UVBD, maxD, UVCD, maxC, UVAC, UVCENTER });

                for (int i = 0; i < rangeAdd.Count; i++)
                {
                    if (!instance.verts.Contains(instance.transform.InverseTransformPoint(rangeAdd[i])))
                    {
                        instance.verts.Add(instance.transform.InverseTransformPoint(rangeAdd[i]));
                        instance.UVs.Add(rangeAddUV[i]);
                        VertexReference reference = new VertexReference();
                        reference.vertex = instance.verts.Count - 1;
                        if (i != 8)
                        {
                            reference.references = 1;
                        }
                        reference.quads = new List <Quad>();
                        reference.quads.Add(this);

                        instance.vertIndexDictionary.Add(instance.transform.InverseTransformPoint(rangeAdd[i]), reference);
                    }
                    else
                    {
                        VertexReference reference = instance.vertIndexDictionary[instance.transform.InverseTransformPoint(rangeAdd[i])];
                        reference.quads.Add(this);
                        reference.references += 1;
                    }
                    //Won't work because sometimes it's not called!!
                    localVerts.Add(instance.vertIndexDictionary[instance.transform.InverseTransformPoint(rangeAdd[i])].vertex);
                }

                //A -> B (Index of local vert corresponding to global vertex index)
                Edge e1 = new Edge(0, 1, 2);

                //B to D
                Edge e2 = new Edge(2, 3, 4);

                //C to D
                Edge e3 = new Edge(4, 5, 6);

                //A -> C
                Edge e4 = new Edge(0, 7, 6);

                edges.AddRange(new Edge[] { e1, e2, e3, e4 });

                //If one of the verts get deleted a triangle get's made using the same ciclical pattern
                for (int i = 0; i < localVerts.Count; i++)
                {
                    if (i == localVerts.Count - 1 || i == 0)
                    {
                        continue;
                    }

                    int aT = i - 1;
                    int bT = i;
                    int cT = localVerts.Count - 1;

                    Triangle t = new Triangle();
                    t.a = aT;
                    t.b = bT;
                    t.c = cT;

                    //Temporary Add
                    instance.tris.AddRange(new int[] { localVerts[aT], localVerts[bT], localVerts[cT] });

                    triangles.Add(t);
                }

                instance.tris.AddRange(new int[] { localVerts[localVerts.Count - 2], localVerts[0], localVerts[localVerts.Count - 1] });
                Triangle t2 = new Triangle();
                t2.a = 7;
                t2.b = 0;
                t2.c = 8;

                triangles.Add(t2);
            }
        }
Ejemplo n.º 2
0
    private void Awake()
    {
        instance   = this;
        emptyMesh  = new Mesh();
        parentQuad = new Quad(0, 0, 100, 100);

        AutoSubdivide(parentQuad);

//        NeighbourSubdivide(parentQuad);
        parentQuad.EnsureMaxDepth();

        parentQuad.FinaliseGeometry(min, max, transform.position.y);

        parentQuad.DeleteUnrequired(min, max, transform.position.y);
        tris.Clear();
        parentQuad.RedrawGeometry(min, max, transform.position.y);


        Vector3 minT = transform.InverseTransformPoint(min);
        Vector3 maxT = transform.InverseTransformPoint(max);

        for (int i = 0; i < verts.Count; i++)
        {
            Vector3 localPos = verts[i];
            //float realXPos = trans.TransformPoint(localPos).x;
            //float realZPos = trans.TransformPoint(localPos).z;

            float realXPos = localPos.x;
            float realZPos = localPos.y;

            float percX = ((realXPos - minT.x) * 100) / (maxT.x - minT.x);
            float percY = ((realZPos - minT.y) * 100) / (maxT.y - minT.y);



            int pixelX = Mathf.Clamp(Mathf.RoundToInt(percX / 100 * testTexture.width), 0, testTexture.width - 1);
            int pixelY = Mathf.Clamp(Mathf.RoundToInt(percY / 100 * testTexture.height), 0, testTexture.height - 1);

            float r = testTexture.GetPixel(pixelX, pixelY).r;

            float val = r;
            float Fy  = (val * maxHeight);

            localPos = new Vector3(localPos.x, localPos.y, Fy);

            verts[i] = localPos;
        }

        emptyMesh.vertices  = verts.ToArray();
        emptyMesh.triangles = tris.ToArray();
        emptyMesh.uv        = UVs.ToArray();

        emptyMesh.RecalculateBounds();
        emptyMesh.RecalculateNormals();

        emptyMesh.RecalculateTangents();



        GetComponent <MeshFilter>().mesh         = emptyMesh;
        GetComponent <MeshCollider>().sharedMesh = emptyMesh;
        string path = "Assets/TerrainChunks/MeshData/";


        AssetDatabase.CreateAsset(emptyMesh, path + ID + "_MESH.mesh");
        AssetDatabase.SaveAssets();

        verts.Clear();
        indexFromVector.Clear();
        tris.Clear();
        UVs.Clear();
    }