예제 #1
0
        public bool Init(LayerTerrainColor color, LayerTerrainGroups.Chunk data, TerrainFile file, Material mat)
        {
            chunkData   = data;
            filedata    = file;
            ChunkColumn = (file.terrainWidth / data.ChunkWidth);
            //2 *2 or 3*3 4*4
            ChunkRow   = (file.terrainLength / data.ChunkLenght);
            ChunkCount = ChunkColumn * ChunkRow;
            //create Chunk
            Chunks.Capacity = ChunkCount;
            float WorldChunkSize = file.worldSize.x / ChunkColumn;



            for (int idy = 0; idy < ChunkRow; ++idy)
            {
                for (int idx = 0; idx < ChunkColumn; ++idx)
                {
                    GameObject Chunk = new GameObject(this.gameObject.name + "Chunk[" + idy.ToString() + ":" + idx.ToString() + "]");
                    Chunk.transform.parent = transform;
                    Chunk.tag   = "Terrain";
                    Chunk.layer = 8;
                    MeshFilter meshfilter = Chunk.AddComponent <MeshFilter>();
                    meshfilter.hideFlags = HideFlags.HideAndDontSave;
                    MeshRenderer render = Chunk.AddComponent <MeshRenderer>();
                    render.hideFlags = HideFlags.DontSave;

                    TerrainChunk terrainChunkItem = Chunk.AddComponent <TerrainChunk>();
                    terrainChunkItem.hideFlags = HideFlags.HideAndDontSave;

                    Vector2Int index = new Vector2Int();
                    index.x = idx;
                    index.y = idy;

                    Vector3 offset = new Vector3();
                    offset.x = WorldChunkSize * idx;
                    offset.z = WorldChunkSize * idy;
                    offset.y = 0.0f;

                    terrainChunkItem.Init(new Vector2Int(idx, idy), offset,
                                          new Vector2(WorldChunkSize, WorldChunkSize),
                                          file, color, data, mat);
                    Chunks.Add(terrainChunkItem);
                }
            }
            return(true);
        }
예제 #2
0
        private void BuildTriangles(LayerTerrainColor colorConfig, LayerTerrainGroups.Chunk chunkConfig)
        {
            float cellx = worldSize.x / boxSize.x;
            float celly = worldSize.y / boxSize.y;
            //triangles.Capacity = boxSize.x * boxSize.y * 2;
            int heightDataIndex = nChunkId.y * (groupInfo.terrainWidth * boxSize.y) + nChunkId.x * boxSize.x;
            //int valueTest = 0;

            ///  V1-----V2
            ///  |      |
            ///  V0-----V3
            ///

            Vector3 v0 = new Vector3();
            Vector3 v1 = new Vector3();
            Vector3 v2 = new Vector3();
            Vector3 v3 = new Vector3();

            Vector3 vCellx        = new Vector3(cellx, 0, 0);
            Vector3 vCelly        = new Vector3(0, 0, celly);
            int     triangleIndex = 0;
            int     boxIndex      = 0;

            for (int idy = 0; idy < boxSize.y; ++idy)
            {
                for (int idx = 0; idx < boxSize.x; ++idx)
                {
                    v0.x = idx * cellx;
                    v0.z = idy * celly;
                    v1   = v0 + vCelly;
                    v2   = v0 + vCellx + vCelly;
                    v3   = v0 + vCellx;
                    int BoxIndex = heightDataIndex + (idy * groupInfo.terrainWidth) + idx;
                    //Triangle 1
                    TerrainTriangle triangle1 = new TerrainTriangle();
                    triangle1.groupInfo   = groupInfo;
                    triangle1.colorConfig = colorConfig;
                    triangle1.chunkConfig = chunkConfig;

                    triangle1.chunkIndex       = new TerrainTriangle.HeightDataIndex(BoxIndex, BoxIndex + groupInfo.terrainWidth, BoxIndex + groupInfo.terrainWidth + 1);
                    triangle1._v0              = v0;
                    triangle1._v1              = v1;
                    triangle1._v2              = v2;
                    triangles[triangleIndex++] = triangle1;

                    //Triangle 2
                    TerrainTriangle triangle2 = new TerrainTriangle();
                    triangle2.groupInfo   = groupInfo;
                    triangle2.colorConfig = colorConfig;
                    triangle2.chunkConfig = chunkConfig;

                    triangle2.chunkIndex       = new TerrainTriangle.HeightDataIndex(BoxIndex, BoxIndex + groupInfo.terrainWidth + 1, BoxIndex + 1);
                    triangle2._v0              = v0;
                    triangle2._v1              = v2;
                    triangle2._v2              = v3;
                    triangles[triangleIndex++] = triangle2;

                    TerrainBox box = new TerrainBox(groupInfo, colorConfig, chunkConfig);
                    box.ChunkOffSet = offset;
                    box.v0Index     = BoxIndex;
                    box.v1Index     = BoxIndex + groupInfo.terrainWidth;
                    box.v2Index     = BoxIndex + groupInfo.terrainWidth + 1;
                    box.v3Index     = BoxIndex + 1;
                    box._v0         = v0;
                    box._v1         = v1;
                    box._v2         = v2;
                    box._v3         = v3;

                    box.groupInfo   = groupInfo;
                    box.colorConfig = colorConfig;
                    box.chunkConfig = chunkConfig;

                    boxs[boxIndex++] = box;
                }
            }
        }
예제 #3
0
        public bool Init(Vector2Int ChunkId, Vector3 vOffset, Vector2 vRealSize, TerrainFile _groupInfo, LayerTerrainColor colorConfig, LayerTerrainGroups.Chunk chunkConfig, Material mat)
        {
            _boxRect = new Rect(new Vector2(vOffset.x, vOffset.z), vRealSize);

            // Material matrial = Resources.Load<Material>("Terrain/TerainShader");
            //matrial.
            //LayerTerrain.terrainMatrial;
            meshFilter = this.gameObject.GetComponent <MeshFilter>();
            if (meshFilter.sharedMesh == null)
            {
                meshFilter.mesh = new Mesh();
                meshFilter.sharedMesh.MarkDynamic();
            }

            meshRender = gameObject.GetComponent <MeshRenderer>();

#if UNITY_ANDROID
            meshRender.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
#endif
            meshRender.receiveShadows = true;
            meshRender.sharedMaterial = mat;


            meshCollider                = gameObject.AddComponent <MeshCollider>();
            meshCollider.hideFlags      = HideFlags.HideAndDontSave;
            meshCollider.cookingOptions = MeshColliderCookingOptions.CookForFasterSimulation | MeshColliderCookingOptions.EnableMeshCleaning | MeshColliderCookingOptions.InflateConvexMesh | MeshColliderCookingOptions.WeldColocatedVertices;
            meshCollider.sharedMesh     = meshFilter.sharedMesh;


            nChunkId  = ChunkId;
            offset    = vOffset;
            boxSize   = new Vector2Int(chunkConfig.ChunkWidth, chunkConfig.ChunkLenght);
            worldSize = vRealSize;
            groupInfo = _groupInfo;

            //build triangles
            BuildTriangles(colorConfig, chunkConfig);
            BuildChunk();
            BrushChunkEnd();
            transform.localPosition = offset;
            return(true);
        }
예제 #4
0
 public TerrainBox(TerrainFile file, LayerTerrainColor _colorConfig, LayerTerrainGroups.Chunk chunkdata)
 {
     groupInfo   = file;
     colorConfig = _colorConfig;
     chunkConfig = chunkdata;
 }