public virtual void applyTerrain(ref float[] existing_noise) { //default behaviour is to add new noise to old noise Debug.Log("default apply"); for (int i = 0; i < noise_store.getWidth(); i++) { for (int j = 0; j < noise_store.getHeight(); j++) { existing_noise[i + noise_store.getWidth() * j] += noise_store.get(i, j); } } }
private void constructMesh() { if (noise_store == null) { Debug.Log("noise store null"); return; } //asuming square and 2d for the moment int res = noise_store.getDims()[0]; verts = new Vector3[noise_store.storeLength()]; triangles = new int[(noise_store.getDims()[0] - 1) * (noise_store.getDims()[1] - 1) * 6]; int vert_index = 0; int tri_index = 0; for (int i = 0; i < noise_store.getDims()[0]; i++) { for (int j = 0; j < noise_store.getDims()[1]; j++) { vert_index = noise_store.getStoreIndex(new int[] { i, j }); verts[vert_index] = new Vector3(i / (noise_store.getDims()[0] - 1f), noise_store.get(new int[] { i, j }), j / (noise_store.getDims()[1] - 1f)); if (i != (noise_store.getDims()[0] - 1) && j != (noise_store.getDims()[1] - 1)) { triangles[tri_index] = vert_index; triangles[tri_index + 1] = vert_index + noise_store.getDims()[0]; triangles[tri_index + 2] = vert_index + noise_store.getDims()[0] + 1; triangles[tri_index + 3] = vert_index; triangles[tri_index + 4] = vert_index + noise_store.getDims()[0] + 1; triangles[tri_index + 5] = vert_index + 1; tri_index += 6; } } } this.mesh_filter.sharedMesh.Clear(); this.mesh_filter.sharedMesh.vertices = verts; this.mesh_filter.sharedMesh.triangles = triangles; this.mesh_filter.sharedMesh.RecalculateNormals(); }
private void constructMesh() { Debug.Log("construct mesh"); if (noise_store == null) { Debug.Log("noise store null"); return; } verts = new Vector3[noise_store.storeLength()]; triangles = new int[(noise_store.getDims()[0] - 1) * (noise_store.getDims()[1] - 1) * 6]; reverse_triangles = new int[(noise_store.getDims()[0] - 1) * (noise_store.getDims()[1] - 1) * 6]; //theta is always in the same plane. float theta; //phi rotates to form the shell of the donut float phi; Vector3 r; int vert_index; int tri_index = 0; for (int i = 0; i < noise_store.getDims()[0]; i++) { theta = length * i / (noise_store.getDims()[0] - 1f); //Debug.Log("theta : " + theta); r = new Vector3(hold_radius * Mathf.Sin(theta), 0f, hold_radius * Mathf.Cos(theta)); for (int j = 0; j < noise_store.getDims()[1]; j++) { vert_index = noise_store.getStoreIndex(new int[] { i, j }); phi = 2f * Mathf.PI * j / (noise_store.getDims()[1] - 1f); //verts[vert_index] = r + thick_radius * (new Vector3(- Mathf.Cos(phi) * Mathf.Sin(theta), Mathf.Cos(phi), -Mathf.Cos(phi) * Mathf.Cos(theta))); verts[vert_index] = new Vector3( (hold_radius + (thick_radius + noise_store.get(new int[] { i, j })) * Mathf.Cos(phi)) * Mathf.Sin(theta), (thick_radius + noise_store.get(new int[] { i, j })) * Mathf.Sin(phi) + y_max * i / (noise_store.getDims()[0] - 1f), (hold_radius + (thick_radius + noise_store.get(new int[] { i, j })) * Mathf.Cos(phi)) * Mathf.Cos(theta) ); if (i != (noise_store.getDims()[0] - 1) && j != (noise_store.getDims()[1] - 1)) { triangles[tri_index] = vert_index; triangles[tri_index + 1] = vert_index + noise_store.getDims()[0]; triangles[tri_index + 2] = vert_index + noise_store.getDims()[0] + 1; triangles[tri_index + 3] = vert_index; triangles[tri_index + 4] = vert_index + noise_store.getDims()[0] + 1; triangles[tri_index + 5] = vert_index + 1; //reverse triangles reverse_triangles[tri_index] = vert_index; reverse_triangles[tri_index + 1] = vert_index + noise_store.getDims()[0] + 1; reverse_triangles[tri_index + 2] = vert_index + noise_store.getDims()[0]; reverse_triangles[tri_index + 3] = vert_index; reverse_triangles[tri_index + 4] = vert_index + 1; reverse_triangles[tri_index + 5] = vert_index + noise_store.getDims()[0] + 1; tri_index += 6; } } } this.mesh_filter.sharedMesh.Clear(); this.mesh_filter.sharedMesh.vertices = verts; this.mesh_filter.sharedMesh.triangles = triangles; this.mesh_filter.sharedMesh.RecalculateNormals(); this.reverse_mesh_filter.sharedMesh.Clear(); this.reverse_mesh_filter.sharedMesh.vertices = verts; this.reverse_mesh_filter.sharedMesh.triangles = reverse_triangles; this.reverse_mesh_filter.sharedMesh.RecalculateNormals(); }
private void constructMesh() { //Debug.Log("construct mesh"); if (noise_store == null) { //Debug.Log("noise store null"); return; } verts = new Vector3[noise_store.storeLength()]; triangles = new int[(noise_store.getDims()[0] - 1) * (noise_store.getDims()[1] - 1) * 6]; reverse_triangles = new int[(noise_store.getDims()[0] - 1) * (noise_store.getDims()[1] - 1) * 6]; //theta is always in the same plane. float theta; //phi rotates to form the shell of the donut float phi; Vector3 r; int vert_index; int tri_index = 0; for (int i = 0; i < noise_store.getDims()[0]; i++) { theta = i / (noise_store.getDims()[0] - 1f); //Debug.Log("theta : " + theta); Vector3[] vs = curve.all(lerp(curve.max, curve.min, i / (noise_store.getDims()[0] - 1f))); r = vs[0]; for (int j = 0; j < noise_store.getDims()[1]; j++) { vert_index = noise_store.getStoreIndex(new int[] { i, j }); phi = 360 * j / (noise_store.getDims()[1] - 1f); verts[vert_index] = r + (Quaternion.AngleAxis(phi, vs[1]) * vs[2]) * (r_offset + noise_store.get(new int[] { i, j })); if (i != (noise_store.getDims()[0] - 1) && j != (noise_store.getDims()[1] - 1)) { triangles[tri_index] = vert_index; triangles[tri_index + 1] = vert_index + noise_store.getDims()[0]; triangles[tri_index + 2] = vert_index + noise_store.getDims()[0] + 1; triangles[tri_index + 3] = vert_index; triangles[tri_index + 4] = vert_index + noise_store.getDims()[0] + 1; triangles[tri_index + 5] = vert_index + 1; //reverse triangles reverse_triangles[tri_index] = vert_index; reverse_triangles[tri_index + 1] = vert_index + noise_store.getDims()[0] + 1; reverse_triangles[tri_index + 2] = vert_index + noise_store.getDims()[0]; reverse_triangles[tri_index + 3] = vert_index; reverse_triangles[tri_index + 4] = vert_index + 1; reverse_triangles[tri_index + 5] = vert_index + noise_store.getDims()[0] + 1; tri_index += 6; } } } this.mesh_filter.sharedMesh.Clear(); this.mesh_filter.sharedMesh.vertices = verts; this.mesh_filter.sharedMesh.triangles = triangles; this.mesh_filter.sharedMesh.RecalculateNormals(); this.reverse_mesh_filter.sharedMesh.Clear(); this.reverse_mesh_filter.sharedMesh.vertices = verts; this.reverse_mesh_filter.sharedMesh.triangles = reverse_triangles; this.reverse_mesh_filter.sharedMesh.RecalculateNormals(); }