Exemplo n.º 1
0
 public void SetTerrain(TerrainHolder terrain)
 {
     upperBoundry = terrain.upBoundry;
     lowerBoundry = terrain.downBoundry;
     rightBoundry = terrain.rightBoundry;
     leftBoundry = terrain.leftBoundry;
     isSet = true;
 }
    public static bool Initialise()
    {
        instance = (TerrainHolder)FindObjectOfType<TerrainHolder>();

        if (instance == null) {
            Debug.LogError("There isn't any TerrainHolder instance.");
            return false;
        }

        if (!instance.LoadDictionaryById())
            return false;
        if (!instance.LoadDictionaryByName())
            return false;

        return true;
    }
Exemplo n.º 3
0
    static public bool Initialise()
    {
        instance = (TerrainHolder)FindObjectOfType <TerrainHolder>();

        if (instance == null)
        {
            Debug.LogError("There isn't any TerrainHolder instance.");
            return(false);
        }

        if (!instance.LoadDictionaryById())
        {
            return(false);
        }
        if (!instance.LoadDictionaryByName())
        {
            return(false);
        }

        return(true);
    }
Exemplo n.º 4
0
        // Mesh
        public static Mesh GenerateMesh(List <Node> nodes, Vector3 position, out Material[] _materials)
        {
            //List<CombineInstance[]>
            List <int> materialsID = new List <int>();

            //CombineInstance[] combine = new CombineInstance[nodes.Count];
            Dictionary <int, List <CombineInstance> > combine = new Dictionary <int, List <CombineInstance> >();

            // Material
            for (int i = 0; i < nodes.Count; i++)
            {
                // Mesh
                Mesh nodeMesh = nodes[i].GenerateMesh();
                int  subMeshIndex;
                if (materialsID.Contains(nodes[i].terrainId))
                {
                    subMeshIndex = materialsID.IndexOf(nodes[i].terrainId);
                }
                else
                {
                    materialsID.Add(nodes[i].terrainId);
                    subMeshIndex = materialsID.IndexOf(nodes[i].terrainId);
                }
                List <CombineInstance> ciList;
                if (!combine.TryGetValue(nodes[i].terrainId, out ciList))
                {
                    ciList = new List <CombineInstance>();
                    combine.Add(nodes[i].terrainId, ciList);
                }
                CombineInstance ci = new CombineInstance();
                ci.mesh      = nodeMesh;
                ci.transform = Matrix4x4.TRS(position + nodes[i].position, Quaternion.identity, Vector3.one);
                ciList.Add(ci);

                /*
                 * combine[i].mesh = nodeMesh;
                 * combine[i].subMeshIndex = subMeshIndex;
                 * combine[i].transform = Matrix4x4.TRS(position + nodes[i].position, Quaternion.identity, Vector3.one);
                 */
            }

            // Prepare submeshes
            CombineInstance[] finalCombine = new CombineInstance[materialsID.Count];
            for (int i = 0; i < materialsID.Count; i++)
            {
                List <CombineInstance> ciList;
                if (combine.TryGetValue(materialsID[i], out ciList))
                {
                    Mesh subMesh = new Mesh();
                    subMesh.CombineMeshes(ciList.ToArray(), true);
                    finalCombine[i].mesh      = subMesh;
                    finalCombine[i].transform = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
                }
            }

            // Merge meshes
            Mesh output = new Mesh();

            output.subMeshCount = materialsID.Count;
            output.CombineMeshes(finalCombine, false);

            //output.CombineMeshes(combine.TryGetValue(, true);

            Material[] materials = new Material[materialsID.Count];
            for (int i = 0; i < materials.Length; i++)
            {
                materials[i] = TerrainHolder.GetTerrainFromId(materialsID[i]).material;
            }
            _materials = materials;
            return(output);
        }
Exemplo n.º 5
0
            public Mesh GenerateMesh()
            {
                if (terrainId == 0)
                {
                    return(null);
                }

                // Retrieve terrain
                TerrainHolder.Terrain terrain = TerrainHolder.GetTerrainFromId(terrainId);
                if (terrain == null)
                {
                    return(null);
                }

                List <CombineInstance> meshCombiner = new List <CombineInstance>();
                CombineInstance        tempCombiner;

                // Top
                if (configuration[1, 1])
                {
                    // Bottom left
                    #region Bottom left

                    Matrix4x4 transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 180, 0), Vector3.one);

                    // Angle
                    tempCombiner           = new CombineInstance();
                    tempCombiner.mesh      = terrain.angleConvexUnder;
                    tempCombiner.transform = transformMatrix;
                    meshCombiner.Add(tempCombiner);
                    #endregion

                    // Bottom right
                    #region Bottom right

                    transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 90, 0), Vector3.one);

                    // Angle
                    tempCombiner           = new CombineInstance();
                    tempCombiner.mesh      = terrain.angleConvexUnder;
                    tempCombiner.transform = transformMatrix;
                    meshCombiner.Add(tempCombiner);
                    #endregion

                    // Top left
                    #region Top left

                    transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, -90, 0), Vector3.one);

                    // Angle
                    tempCombiner           = new CombineInstance();
                    tempCombiner.mesh      = terrain.angleConvexUnder;
                    tempCombiner.transform = transformMatrix;
                    meshCombiner.Add(tempCombiner);
                    #endregion

                    // Top right
                    #region Top left

                    transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 0, 0), Vector3.one);

                    // Angle
                    tempCombiner           = new CombineInstance();
                    tempCombiner.mesh      = terrain.angleConvexUnder;
                    tempCombiner.transform = transformMatrix;
                    meshCombiner.Add(tempCombiner);
                    #endregion
                }
                else
                {
                    // Bottom left
                    #region Bottom left

                    Matrix4x4 transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 180, 0), Vector3.one);

                    if (configuration[0, 1])
                    {
                        if (configuration[1, 0])
                        {
                            if (configuration[0, 0])
                            {
                                // Fill
                                tempCombiner           = new CombineInstance();
                                tempCombiner.mesh      = terrain.fill;
                                tempCombiner.transform = transformMatrix;
                                meshCombiner.Add(tempCombiner);
                            }
                            else
                            {
                                // Angle concave
                                tempCombiner           = new CombineInstance();
                                tempCombiner.mesh      = terrain.angleConcave;
                                tempCombiner.transform = transformMatrix;
                                meshCombiner.Add(tempCombiner);
                            }
                        }
                        else
                        {
                            // Line B
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.lineB;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                    }
                    else
                    {
                        if (configuration[1, 0])
                        {
                            // Line A
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.lineA;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                        else
                        {
                            // Angle convex
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.angleConvex;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                    }
                    #endregion

                    // Bottom right
                    #region Bottom right

                    transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 90, 0), Vector3.one);

                    if (configuration[1, 0])
                    {
                        if (configuration[2, 1])
                        {
                            if (configuration[2, 0])
                            {
                                // Fill
                                tempCombiner           = new CombineInstance();
                                tempCombiner.mesh      = terrain.fill;
                                tempCombiner.transform = transformMatrix;
                                meshCombiner.Add(tempCombiner);
                            }
                            else
                            {
                                // Angle concave
                                tempCombiner           = new CombineInstance();
                                tempCombiner.mesh      = terrain.angleConcave;
                                tempCombiner.transform = transformMatrix;
                                meshCombiner.Add(tempCombiner);
                            }
                        }
                        else
                        {
                            // Line B
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.lineB;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                    }
                    else
                    {
                        if (configuration[2, 1])
                        {
                            // Line A
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.lineA;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                        else
                        {
                            // Angle convex
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.angleConvex;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                    }
                    #endregion

                    // Top left
                    #region Top left

                    transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, -90, 0), Vector3.one);

                    if (configuration[1, 2])
                    {
                        if (configuration[0, 1])
                        {
                            if (configuration[0, 2])
                            {
                                // Fill
                                tempCombiner           = new CombineInstance();
                                tempCombiner.mesh      = terrain.fill;
                                tempCombiner.transform = transformMatrix;
                                meshCombiner.Add(tempCombiner);
                            }
                            else
                            {
                                // Angle concave
                                tempCombiner           = new CombineInstance();
                                tempCombiner.mesh      = terrain.angleConcave;
                                tempCombiner.transform = transformMatrix;
                                meshCombiner.Add(tempCombiner);
                            }
                        }
                        else
                        {
                            // Line B
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.lineB;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                    }
                    else
                    {
                        if (configuration[0, 1])
                        {
                            // Line A
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.lineA;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                        else
                        {
                            // Angle convex
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.angleConvex;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                    }
                    #endregion

                    // Top right
                    #region Top left

                    transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 0, 0), Vector3.one);

                    if (configuration[2, 1])
                    {
                        if (configuration[1, 2])
                        {
                            if (configuration[2, 2])
                            {
                                // Fill
                                tempCombiner           = new CombineInstance();
                                tempCombiner.mesh      = terrain.fill;
                                tempCombiner.transform = transformMatrix;
                                meshCombiner.Add(tempCombiner);
                            }
                            else
                            {
                                // Angle concave
                                tempCombiner           = new CombineInstance();
                                tempCombiner.mesh      = terrain.angleConcave;
                                tempCombiner.transform = transformMatrix;
                                meshCombiner.Add(tempCombiner);
                            }
                        }
                        else
                        {
                            // Line B
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.lineB;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                    }
                    else
                    {
                        if (configuration[1, 2])
                        {
                            // Line A
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.lineA;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                        else
                        {
                            // Angle convex
                            tempCombiner           = new CombineInstance();
                            tempCombiner.mesh      = terrain.angleConvex;
                            tempCombiner.transform = transformMatrix;
                            meshCombiner.Add(tempCombiner);
                        }
                    }
                    #endregion
                }

                Mesh output = new Mesh();
                output.CombineMeshes(meshCombiner.ToArray());
                return(output);
            }
    public void SetUpTerrain(TerrainHolder terrain)
    {
        this.terrain = terrain;

        IList<ITerrainRequester> terrainYearners = InterfaceHelper.FindObjects<ITerrainRequester>();
        for (int i=0; i<terrainYearners.Count; ++i) {
            terrainYearners[i].SetTerrain(this.terrain);
        }
    }
Exemplo n.º 7
0
    bool SliceObject(ref Plane slicePlane, GameObject obj, List <Transform> positiveObjects, List <Transform> negativeObjects)
    {
        var mesh = obj.GetComponent <MeshFilter>().mesh;

        if (!meshCutter.SliceMesh(mesh, ref slicePlane))
        {
            // Put object in the respective list
            if (slicePlane.GetDistanceToPoint(meshCutter.GetFirstVertex()) >= 0)
            {
                positiveObjects.Add(obj.transform);
            }
            else
            {
                negativeObjects.Add(obj.transform);
            }

            return(false);
        }

        // TODO: Update center of mass

        // Silly condition that labels which mesh is bigger to keep the bigger mesh in the original gameobject
        bool posBigger = meshCutter.PositiveMesh.surfacearea > meshCutter.NegativeMesh.surfacearea;

        if (posBigger)
        {
            biggerMesh  = meshCutter.PositiveMesh;
            smallerMesh = meshCutter.NegativeMesh;
        }
        else
        {
            biggerMesh  = meshCutter.NegativeMesh;
            smallerMesh = meshCutter.PositiveMesh;
        }

        // Create new Sliced object with the other mesh
        GameObject newObject = Instantiate(obj, ObjectContainer);

        newObject.transform.SetPositionAndRotation(obj.transform.position, obj.transform.rotation);
        var newObjMesh = newObject.GetComponent <MeshFilter>().mesh;

        // Put the bigger mesh in the original object
        // TODO: Enable collider generation (either the exact mesh or compute smallest enclosing sphere)
        ReplaceMesh(mesh, biggerMesh);
        ReplaceMesh(newObjMesh, smallerMesh);

        // Update Meshes in MeshColliders if exists:
        UpdateMeshCollider(mesh, obj.GetComponent <MeshCollider>());
        UpdateMeshCollider(newObjMesh, newObject.GetComponent <MeshCollider>());

        // Set "IsKinematic" to true in Rigidbodies:
        UpdateRigidbody(obj.GetComponent <Rigidbody>());
        UpdateRigidbody(newObject.GetComponent <Rigidbody>());

        // Set "IsKinematic" to false in Rigidbody where its GameObjects collide with TerrainHolder:
        TerrainHolder.HoldTerrains();

        (posBigger ? positiveObjects : negativeObjects).Add(obj.transform);
        (posBigger ? negativeObjects : positiveObjects).Add(newObject.transform);

        return(true);
    }