/// <summary> /// 检查是否有需要添加的Chunk /// </summary> /// <param name="hero_pos"></param> private void UpdateAdd(Vector3 hero_pos) { foreach (var temp in chunk_map) { var ck = temp.Value; if (hero_pos.x >= ck.box.xMin && hero_pos.x <= ck.box.xMax && hero_pos.z >= ck.box.yMin && hero_pos.z <= ck.box.yMax) { bool isLeft = hero_pos.x < ck.box_add.xMin; bool isRight = hero_pos.x > ck.box_add.xMax; bool isUp = hero_pos.z > ck.box_add.yMax; bool isDown = hero_pos.z < ck.box_add.yMin; if (isLeft && ck.Left == null) { AddChunk(ck.id_x - 1, ck.id_y); } if (isRight && ck.Right == null) { AddChunk(ck.id_x + 1, ck.id_y); } if (isUp && ck.Up == null) { AddChunk(ck.id_x, ck.id_y + 1); } if (isDown && ck.Down == null) { AddChunk(ck.id_x, ck.id_y - 1); } if (isLeft && isUp && ck.LeftUp == null) { AddChunk(ck.id_x - 1, ck.id_y + 1); } if (isLeft && isDown && ck.LeftDown == null) { AddChunk(ck.id_x - 1, ck.id_y - 1); } if (isRight && isUp && ck.RightUp == null) { AddChunk(ck.id_x + 1, ck.id_y + 1); } if (isRight && isDown && ck.RightDown == null) { AddChunk(ck.id_x + 1, ck.id_y - 1); } } } while (add_queue.Count > 0) //添加也不可以放到foreach循环中,否则会出错 { var ck = add_queue.Dequeue(); HeightMapConfig lcfg = config; lcfg.seed = config.seed + ck.id_x + ck.id_y; //让粗糙度渐变,这个只是测试用,具体可根据实际情况确定 lcfg.H = config.H + ((Mathf.Sin(ck.id_x)) / 8) + ((Mathf.Cos(ck.id_y)) / 8); Debug.Log("Current H:" + lcfg.H); ck.GeneralData(lcfg, m_TotalMeshSegmentNum, m_MeshSegmentSize, MeshMat, MeshPieceSegment, MeshPieceNum); //此步需要访问相邻Chunk,当多个Chunk需要添加时,应当保证执行过此步的Chunk都已经存放到容器中了 m_thread_work.AddWork(ck); } }
/// <summary> /// 生成高度图和预计算网格信息 /// </summary> /// <param name="config"></param> /// <param name="mesh_segment"></param> /// <param name="mesh_segment_size"></param> /// <param name="mat"></param> /// <param name="mesh_piece_segment"></param> /// <param name="mesh_piece_num"></param> public void GeneralData(HeightMapConfig config, int mesh_segment, Vector2 mesh_segment_size, Material mat, int mesh_piece_segment, int mesh_piece_num) { m_heightmap_len = 1 << config.Iterations; m_cfg = config; mask = SurfaceMaskCreator.CreateMask(m_heightmap_len); m_chunk_mat = new Material(mat); m_chunk_mat.SetTexture("_Mask", mask); m_mesh_segment = mesh_segment; m_mesh_segment_size = mesh_segment_size; m_mesh_piece_segment = mesh_piece_segment; m_mesh_piece_num = mesh_piece_num; }
public SquareRegion(int seed, RegionGenConfig regionGenConfig, HeightMapConfig heightMapConfig, FastPerlinNoiseConfig noiseConfig ) : base(seed) { // ErosionConfig erosionConfig this.regionGenConfig = regionGenConfig; // compute required array dimensions this.gridRadius = computeGridRadius(); noiseConfig.resolution = (int)(this.gridRadius * heightMapConfig.resolutionScale) + 1; this.heightMap = new HeightMap(seed, heightMapConfig, noiseConfig); //erosionConfig); this.tileSize = regionGenConfig.tileSize; computeTileCenterCoords(); computeElevationParameters(); Debug.Log("Generated square region."); }