public void Regenerate() { float halfSize = size / 2f; float origin = -halfSize; int triNum = size * size * 6; Vector3[] verts = new Vector3[triNum]; Color32[] colors = new Color32[triNum]; int[] triangles = new int[triNum]; int sideVertsNum = size + 1; int vertsNum = sideVertsNum * sideVertsNum; Vector3[] smoothVerts = new Vector3[vertsNum]; Surfacer surfacer = Surfacer.GetInstance(); int vertexIndex = 0; for (int z = 0; z < sideVertsNum; z++) { for (int x = 0; x < sideVertsNum; x++) { float height = surfacer.GetHeight(centerX + origin + x, centerZ + origin + z); smoothVerts[vertexIndex] = new Vector3(origin + x, height, origin + z); if (x < size && z < size) { AddTriangle(triangles, vertexIndex, vertexIndex + sideVertsNum, vertexIndex + sideVertsNum + 1); AddTriangle(triangles, vertexIndex, vertexIndex + sideVertsNum + 1, vertexIndex + 1); } vertexIndex++; } } for (int i = 0; i < triangles.Length; i++) { verts[i] = smoothVerts[triangles[i]]; triangles[i] = i; } for (int i = 0; i < verts.Length; i += 3) { float averageTriHeight = (verts[i].y + verts[i + 1].y + verts[i + 2].y) / 3f; Color triColor = SurfDecorator.GetTriColor(averageTriHeight); AddColor(colors, i, triColor); } InitMesh(verts, triangles, colors); }
void Start() { int patchSize = size / 2; float halfSize = patchSize / 2.0f; Surfacer surfacer = Surfacer.GetInstance(); surfacer.InitSurface(size, maxHeight, fieldMaxHeight); surfacer.InitAllFBM(mainScaleFBM, fieldScaleFBM, offsetMainFBM, offsetFieldFBM); SurfDecorator.shaderColorFill = shaderColorFill; SurfDecorator.MaxHeight = maxHeight; SurfDecorator.WaterHeightPerc = waterHeightPerc; SurfDecorator.CoastHeightPerc = coastHeightPerc; SurfDecorator.GrassHeightPerc = grassHeightPerc; SurfDecorator.RockHeightPerc = rockHeightPerc; SurfDecorator.InitMaterial(); patchArr = new TerrainPatch[numPatches]; patchArr[0] = new TerrainPatch(gameObject.transform, -halfSize, -halfSize, patchSize); patchArr[1] = new TerrainPatch(gameObject.transform, -halfSize, halfSize, patchSize); patchArr[2] = new TerrainPatch(gameObject.transform, halfSize, -halfSize, patchSize); patchArr[3] = new TerrainPatch(gameObject.transform, halfSize, halfSize, patchSize); }