void Initialize() { if (mMeshFiliters == null) { mMeshFiliters = new MeshFilter[6]; } if (mTerrainface == null) { mTerrainface = new TerrainFace[6]; } Vector3[] dirs = new Vector3[] { Vector3.up, Vector3.down, Vector3.right, Vector3.forward, Vector3.back, Vector3.left, }; for (int i = 0; i < dirs.Length; ++i) { if (mMeshFiliters[i] == null) { GameObject go = new GameObject(dirs[i].ToString()); go.transform.SetParent(this.transform); go.transform.localPosition = Vector3.zero; mMeshFiliters[i] = go.AddComponent <MeshFilter>(); go.AddComponent <MeshRenderer>(); } if (mTerrainface[i] == null) { mTerrainface[i] = new TerrainFace(); } mTerrainface[i].Resolution = resoultion; mTerrainface[i].Up = dirs[i]; mMeshFiliters[i].sharedMesh = mTerrainface[i].Mesh; } if (mShapeGenerator == null) { mShapeGenerator = new PlanetShapeGenerator(); } mShapeGenerator.Initialize(ShapeSetting); if (mColorGenerator == null) { mColorGenerator = new PlanetColorGenerator(); } mColorGenerator.Initialize(ColorSetting); }
public void Construct(PlanetShapeGenerator shapeGenerator, PlanetColorGenerator colorGenerator) { List <Vector3> vertices = new List <Vector3>(mResolution * mResolution); List <Vector2> uvs = new List <Vector2>(mResolution * mResolution); List <int> indices = new List <int>(mResolution * mResolution * 6); Vector3 right = new Vector3(mUp.y, mUp.z, mUp.x); Vector3 forward = Vector3.Cross(right, mUp); for (int y = 0; y < mResolution; ++y) { for (int x = 0; x < mResolution; ++x) { Vector2 percent = new Vector2((float)x / (mResolution - 1), (float)y / (mResolution - 1)); Vector3 pointOnCube = mUp + (percent.y - 0.5f) * 2f * forward + (percent.x - 0.5f) * 2f * right; Vector3 pointOnCircle = pointOnCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnCircle); vertices.Add(pointOnCircle * shapeGenerator.GetScaledElevation(unscaledElevation)); uvs.Add(new Vector2(colorGenerator.CalculateBiomeOnPlanet(pointOnCircle), unscaledElevation)); } } for (int y = 0; y < mResolution - 1; ++y) { for (int x = 0; x < mResolution - 1; ++x) { int index = x + y * (mResolution); indices.Add(index); indices.Add(index + mResolution); indices.Add(index + 1); indices.Add(index + mResolution + 1); indices.Add(index + 1); indices.Add(index + mResolution); } } mMesh.Clear(); mMesh.SetVertices(vertices); mMesh.SetUVs(0, uvs); mMesh.SetTriangles(indices, 0); mMesh.RecalculateNormals(); mMesh.RecalculateTangents(); mMesh.UploadMeshData(false); colorGenerator.SetMinMaxValue(shapeGenerator.VecMinMax); }
public void UpdateUVs(PlanetColorGenerator colorGenerator) { // Vector2[] uv = new Vector2[resolution * resolution]; Vector2[] uv = mesh.uv; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + y * resolution; Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * xAxis + (percent.y - 0.5f) * 2 * zAxis; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; // uv[i] = new Vector2(colorGenerator.BiomePercentFromPoint(pointOnUnitSphere), 0); uv[i].x = colorGenerator.BiomePercentFromPoint(pointOnUnitSphere); } } mesh.uv = uv; }