public Block(Vector3Int position, float tileX = 12, float tileY = 3) { HasUV2 = false; UVSetter = new UVSetter(12, 3); UVSetter2 = null; this.Position = position; var x = SetUVs.GetStandardMeshFilter(); this.Mesh = new Mesh { vertices = x.vertices, triangles = x.triangles, name = this.Position.ToString() }; }
public static void CombineForAll(GameObject currentChunk, bool remove = false) { Vector3 pos = currentChunk.transform.position; IChunk chunk = currentChunk.GetComponent <IChunk>(); currentChunk.transform.position = Vector3.zero; currentChunk.SetActive(false); if (uvs == null) { uvs = SetUVs.GetStandardUVs(); } Block[] blocks = chunk.GetBlocks(); CombineInstance[] combine = new CombineInstance[blocks.Length]; currentChunk.GetComponents <MeshCollider>().ToList().ForEach(GameObject.Destroy); for (int i = 0; i < blocks.Length; i++) { combine[i].mesh = blocks[i].Mesh; combine[i].transform = Matrix4x4.TRS(blocks[i].Position, Quaternion.identity, Vector3.one); } MeshFilter refMesh = currentChunk.GetComponent <MeshFilter>(); refMesh.mesh = new Mesh(); refMesh.mesh.CombineMeshes(combine, true); List <Vector2> newMeshUVs = new List <Vector2>(); for (int i = 0; i < blocks.Length; i++) { //add new UVs based on individual block settings UVSetter suv = blocks[i].UVSetter; float tilePerc = 1 / UVSetter.pixelSize; float umin = tilePerc * suv.TileX; float umax = tilePerc * (suv.TileX + 1); float vmin = tilePerc * suv.TileY; float vmax = tilePerc * (suv.TileY + 1); for (int j = 0; j < 24; j++) { float x = Mathf.Approximately(uvs[j].x, 0f) ? umin : umax; float y = Mathf.Approximately(uvs[j].y, 0f) ? vmin : vmax; newMeshUVs.Add(new Vector2(x, y)); } } refMesh.mesh.uv = newMeshUVs.ToArray(); refMesh.mesh.RecalculateBounds(); refMesh.mesh.RecalculateNormals(); currentChunk.transform.position = pos; currentChunk.AddComponent <MeshCollider>(); currentChunk.gameObject.SetActive(true); }
void Combine(GameObject block) { MeshFilter[] meshFilters = GetComponentsInChildren <MeshFilter>(); CombineInstance[] combine = new CombineInstance[meshFilters.Length]; Destroy(this.gameObject.GetComponent <MeshCollider>()); Vector2[] oldMeshUVs = transform.GetComponent <MeshFilter>().mesh.uv; int i = 0; while (i < meshFilters.Length) { combine[i].mesh = meshFilters[i].sharedMesh; combine[i].transform = meshFilters[i].transform.localToWorldMatrix; meshFilters[i].gameObject.SetActive(false); i++; } transform.GetComponent <MeshFilter>().mesh = new Mesh(); transform.GetComponent <MeshFilter>().mesh.CombineMeshes(combine, true); //make new UV array Vector2[] newMeshUVs = new Vector2[oldMeshUVs.Length + 24]; //copy over all UVs for (i = 0; i < oldMeshUVs.Length; i++) { newMeshUVs[i] = oldMeshUVs[i]; } //add new UVs based on individual block settings SetUVs suv = block.GetComponent <SetUVs>(); float tilePerc = 1 / suv.pixelSize; float umin = tilePerc * suv.tileX; float umax = tilePerc * (suv.tileX + 1); float vmin = tilePerc * suv.tileY; float vmax = tilePerc * (suv.tileY + 1); newMeshUVs[newMeshUVs.Length - 24] = new Vector2(umin, vmin); newMeshUVs[newMeshUVs.Length - 23] = new Vector2(umax, vmin); newMeshUVs[newMeshUVs.Length - 22] = new Vector2(umin, vmax); newMeshUVs[newMeshUVs.Length - 21] = new Vector2(umax, vmax); newMeshUVs[newMeshUVs.Length - 20] = new Vector2(umin, vmax); newMeshUVs[newMeshUVs.Length - 19] = new Vector2(umax, vmax); newMeshUVs[newMeshUVs.Length - 18] = new Vector2(umin, vmax); newMeshUVs[newMeshUVs.Length - 17] = new Vector2(umax, vmax); newMeshUVs[newMeshUVs.Length - 16] = new Vector2(umin, vmin); newMeshUVs[newMeshUVs.Length - 15] = new Vector2(umax, vmin); newMeshUVs[newMeshUVs.Length - 14] = new Vector2(umin, vmin); newMeshUVs[newMeshUVs.Length - 13] = new Vector2(umax, vmin); newMeshUVs[newMeshUVs.Length - 12] = new Vector2(umin, vmin); newMeshUVs[newMeshUVs.Length - 11] = new Vector2(umin, vmax); newMeshUVs[newMeshUVs.Length - 10] = new Vector2(umax, vmax); newMeshUVs[newMeshUVs.Length - 9] = new Vector2(umax, vmin); newMeshUVs[newMeshUVs.Length - 8] = new Vector2(umin, vmin); newMeshUVs[newMeshUVs.Length - 7] = new Vector2(umin, vmax); newMeshUVs[newMeshUVs.Length - 6] = new Vector2(umax, vmax); newMeshUVs[newMeshUVs.Length - 5] = new Vector2(umax, vmin); newMeshUVs[newMeshUVs.Length - 4] = new Vector2(umin, vmin); newMeshUVs[newMeshUVs.Length - 3] = new Vector2(umin, vmax); newMeshUVs[newMeshUVs.Length - 2] = new Vector2(umax, vmax); newMeshUVs[newMeshUVs.Length - 1] = new Vector2(umax, vmin); transform.GetComponent <MeshFilter>().mesh.uv = newMeshUVs; transform.GetComponent <MeshFilter>().mesh.RecalculateBounds(); transform.GetComponent <MeshFilter>().mesh.RecalculateNormals(); transform.GetComponent <MeshFilter>().mesh.Optimize(); this.gameObject.AddComponent <MeshCollider>(); transform.gameObject.SetActive(true); Destroy(block); }
public static void Combine(Block blockToAdd, GameObject currentChunk) { Vector3 pos = currentChunk.transform.position; currentChunk.transform.position = Vector3.zero; MeshFilter meshFilter = currentChunk.GetComponent <MeshFilter>(); CombineInstance[] combine = new CombineInstance[2]; currentChunk.GetComponents <MeshCollider>().ToList().ForEach(GameObject.Destroy); Vector2[] oldMeshUVs = currentChunk.GetComponent <MeshFilter>().mesh.uv; combine[0].mesh = meshFilter.sharedMesh; combine[0].transform = meshFilter.transform.localToWorldMatrix; meshFilter.gameObject.SetActive(false); combine[1].mesh = blockToAdd.Mesh; // TODO: Hier möglicherweise zu mesh wechseln Matrix4x4 m = Matrix4x4.TRS(blockToAdd.Position, Quaternion.identity, Vector3.one); combine[1].transform = m; MeshFilter refMesh = currentChunk.GetComponent <MeshFilter>(); refMesh.mesh = new Mesh(); refMesh.mesh.CombineMeshes(combine, true); //make new UV array Vector2[] newMeshUVs = new Vector2[oldMeshUVs.Length + 24]; // //copy over all UVs for (int j = 0; j < oldMeshUVs.Length; j++) { newMeshUVs[j] = oldMeshUVs[j]; } UVSetter suv = blockToAdd.UVSetter; float tilePerc = 1 / UVSetter.pixelSize; float umin = tilePerc * suv.TileX; float umax = tilePerc * (suv.TileX + 1); float vmin = tilePerc * suv.TileY; float vmax = tilePerc * (suv.TileY + 1); if (uvs == null) { uvs = SetUVs.GetStandardUVs(); } int k = 0; for (int j = newMeshUVs.Length - 24; j < newMeshUVs.Length; j++) { float x = Mathf.Approximately(uvs[k].x, 0f) ? umin : umax; float y = Mathf.Approximately(uvs[k].y, 0f) ? vmin : vmax; newMeshUVs[j] = new Vector2(x, y); k++; } refMesh.mesh.uv = newMeshUVs; refMesh.mesh.RecalculateBounds(); refMesh.mesh.RecalculateNormals(); currentChunk.transform.position = pos; currentChunk.AddComponent <MeshCollider>(); currentChunk.gameObject.SetActive(true); }