private void Start()
    {
        binaryVals   = new int[size + 1, size + 1];
        VertexValues = new float[size + 1, size + 1];

        for (int x1 = 0; x1 <= size; x1++)
        {
            for (int y1 = 0; y1 <= size; y1++)
            {
                binaryVals[x1, y1]   = noise((float)x1, (float)y1) > (scale / 2) ? 1 : 0;
                VertexValues[x1, y1] = noise((float)x1, (float)y1);
            }
        }
        float noise(float x, float y) => Mathf.PerlinNoise(x / scale, y / scale) * scale;

        for (int x = 0; x < size; x++)
        {
            for (int y = 0; y < size; y++)
            {
                int g = binaryVals[x, y + 1] * 8 + binaryVals[x + 1, y + 1] * 4 + binaryVals[x + 1, y] * 2 + binaryVals[x, y];

                for (int i = 1; i <= 4; i++)
                {
                    Vector3 a = TriangulationTable[g, i * 3 - 3] != -1 ? point(x, y, TriangulationTable[g, i * 3 - 3] - 1) : Vector3.zero;
                    Vector3 b = TriangulationTable[g, i * 3 - 2] != -1 ? point(x, y, TriangulationTable[g, i * 3 - 2] - 1) : Vector3.zero;
                    Vector3 c = TriangulationTable[g, i * 3 - 1] != -1 ? point(x, y, TriangulationTable[g, i * 3 - 1] - 1) : Vector3.zero;

                    if (a != Vector3.zero || b != Vector3.zero || c != Vector3.zero)
                    {
                        tris.AddRange(new List <int>()
                        {
                            Verts.Count, Verts.Count + 1, Verts.Count + 2
                        });
                        Verts.AddRange(new List <Vector3>()
                        {
                            a, b, c
                        });
                    }
                }
            }
        }

        GetComponent <MeshFilter>().mesh = new Mesh()
        {
            vertices  = Verts.ToArray(),
            triangles = tris.ToArray()
        };

        var welder = new MeshWelder(GetComponent <MeshFilter>().mesh);

        welder.Weld();
    }
Exemplo n.º 2
0
    void GenerateMapFromNavMesh(ref DiffuseNode[] array)
    {
        navMesh  = NavMesh.CalculateTriangulation();
        vertices = navMesh.vertices;
        polygons = navMesh.indices;

        Mesh m = new Mesh();

        m.vertices  = vertices;
        m.triangles = polygons;

        MeshWelder welder = new MeshWelder(m);

        welder.Weld();

        vertices = m.vertices;
        polygons = m.triangles;

        size         = polygons.Length;
        array        = new DiffuseNode[size];
        arrayColSize = 20;
        arrayRowSize = 20;

        arrayInfo.ARRAY_MAX        = size;
        arrayInfo.ARRAY_MIN        = 0;
        arrayInfo.ARRAY_HALF_WIDTH = arrayInfo.ARRAY_MAX / 2;

        DiffuseNode t = new DiffuseNode();

        for (int i = arrayInfo.ARRAY_MIN; i < arrayInfo.ARRAY_MAX; i++)
        {
            t.wall         = false;
            t.lambda       = 1.0f;
            t.p            = 1.0f;
            t.position     = vertices[polygons[i]];
            t.polygonIndex = polygons[i];
            t.arrayIndex   = i;
            array[i]       = t;
            array[i].skip  = false;
        }
    }
Exemplo n.º 3
0
    public GameObject CreateMergedCourse(CombineInstance[] wInstance, CombineInstance[] fInstance)
    {
        var defMats = GameObject.FindObjectOfType <DefaultMats>();
        var holeObj = new GameObject("Hole")
        {
            tag = "Hole", layer = 10
        };
        var wallObj = new GameObject("Walls")
        {
            tag = "HoleWalls", layer = 10
        };
        var floorObj = new GameObject("Floor")
        {
            tag = "HoleFloor", layer = 10
        };
        List <GameObject> objs = new List <GameObject>()
        {
            wallObj, floorObj
        };

        foreach (GameObject obj in objs)
        {
            //Parent to the hole object
            obj.transform.parent = holeObj.transform;

            //Add Components
            //Mesh Filter
            MeshFilter mFilter = obj.AddComponent(typeof(MeshFilter)) as MeshFilter;
            mFilter.mesh = new Mesh();
            switch (obj.name)
            {
            case "Walls":
                mFilter.mesh.CombineMeshes(wInstance);
                break;

            case "Floor":
                mFilter.mesh.CombineMeshes(fInstance);
                break;
            }

            //Mesh Renderer
            MeshRenderer mRenderer = obj.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
            mRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided;

            //Rigidbody
            Rigidbody rBody = obj.AddComponent(typeof(Rigidbody)) as Rigidbody;
            rBody.isKinematic = true;
            rBody.useGravity  = false;

            //Collider(s)
            MeshCollider collider = obj.AddComponent(typeof(MeshCollider)) as MeshCollider;
            MeshCollider trigger  = obj.AddComponent(typeof(MeshCollider)) as MeshCollider;
            trigger.convex    = true;
            trigger.isTrigger = true;

            //Collision Checks
            obj.AddComponent(typeof(EnvironmentCollisionCheck));

            //Set the relevant materials
            switch (obj.name)
            {
            case "Walls":
                mRenderer.material = defMats.defaultWalls;
                collider.material  = defMats.defaultPhysWalls;
                break;

            case "Floor":
                mRenderer.material = defMats.defaultFloors;
                collider.material  = defMats.defaultPhysFloors;
                break;
            }

            //Double vertice welding
            var welder = new MeshWelder(mFilter.mesh);
            welder.Weld();
            mFilter.mesh.RecalculateNormals();
        }
        return(holeObj);
    }
Exemplo n.º 4
0
    private void Start()
    {
        // Slow

        Vector3 pos = transform.position;

        for (int i = 0; i < HeightSegments; i++)
        {
            float x = 1 - BiasFunction((float)i / (float)HeightSegments, .2f);
            AddSquarePoints(x, Random.Range(-25, 25), new Vector3(pos.x, i * height + pos.y, pos.z), i);
        }

        float BiasFunction(float x, float Bias) => x *Mathf.Pow(1 - Bias, 3) / (x * Mathf.Pow(1 - Bias, 3) - x + 1);

        // Fast

        for (int i = 0; i < HeightSegments - 1; i++)
        {
            addquad(verts[i * 4], verts[i * 4 + 1], verts[(i + 1) * 4 + 1], verts[(i + 1) * 4]);
            addquad(verts[i * 4 + 1], verts[i * 4 + 2], verts[(i + 1) * 4 + 2], verts[(i + 1) * 4 + 1]);
            addquad(verts[i * 4 + 2], verts[i * 4 + 3], verts[(i + 1) * 4 + 3], verts[(i + 1) * 4 + 2]);
            addquad(verts[i * 4 + 3], verts[i * 4], verts[(i + 1) * 4], verts[(i + 1) * 4 + 3]);
        }

        // Slow

        int s = (HeightSegments - 1) * 4;

        addquad(verts[s], verts[s + 1], verts[s + 2], verts[s + 3]);

        // Fast

        float d = verts[(int)(verts.Count / 2) + (int)(verts.Count / 4)].y;

        AddTreeSquare(3, Random.Range(-5, 5), new Vector3(pos.x, pos.y + LeavesHeight + (d - (d / 2)), pos.z), true);
        AddTreeSquare(2, Random.Range(-5, 5), new Vector3(pos.x, pos.y + LeavesHeight + (d + (height) * 3), pos.z), false);

        // Slow

        int b = verts.Count - 8;

        // Fast

        addquad(verts[b], verts[b + 1], verts[b + 5], verts[b + 4]);
        addquad(verts[b + 1], verts[b + 2], verts[b + 6], verts[b + 5]);
        addquad(verts[b + 2], verts[b + 3], verts[b + 7], verts[b + 6]);
        addquad(verts[b], verts[b + 4], verts[b + 7], verts[b + 3]);

        // Slow

        GetComponent <MeshFilter>().mesh = new Mesh()
        {
            vertices  = verts.ToArray(),
            triangles = Triangles.ToArray()
        };

        // For End

        var welder = new MeshWelder(GetComponent <MeshFilter>().mesh);

        welder.Weld();

        // Slow

        GetComponent <MeshFilter>().mesh.RecalculateNormals();

        Colors = new Color[GetComponent <MeshFilter>().mesh.vertices.Length];

        for (int i = 0; i < GetComponent <MeshFilter>().mesh.vertices.Length; i++)
        {
            if (i < GetComponent <MeshFilter>().mesh.vertices.Length - 8)
            {
                Colors[i] = bark;
            }
            else
            {
                Colors[i] = Leaves;
            }
        }

        GetComponent <MeshFilter>().mesh.colors = Colors;
    }