public void OnSceneGUI(SceneView scene) { HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); RaycastHit info = new RaycastHit(); GameObject selected = null; Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); if (Physics.Raycast(ray, out info /*, LayerMask.NameToLayer("Cells Layer")*/)) //TODO Arreglar esto porque al parecer la distancia no funciona correctamente { if (info.collider.transform.IsChildOf(this.map.transform)) { selected = info.collider.gameObject; } } bool moveEntity = false; if (Event.current.isMouse && Event.current.button == 0 && Event.current.type == EventType.MouseDown) { moveEntity = true; } if (selected != null) { Cell cs = selected.GetComponent <Cell>(); if (cs != null) { FaceNoSC f = cs.getFaceByPoint(info.point); if (f != null) { if (moveEntity) { entity.Position = cs; } Vector3[] vertex = f.SharedVertex; int[] indexes = f.VertexIndex; Vector3[] puntos = new Vector3[4]; for (int i = 0; i < indexes.Length; i++) { puntos[i] = cs.transform.TransformPoint(vertex[indexes[i]]); } if (indexes.Length == 3) { puntos[3] = cs.transform.TransformPoint(vertex[indexes[2]]); } Handles.DrawSolidRectangleWithOutline(puntos, Color.yellow, Color.white); } } } }
private FaceNoSC selectFaceFor(FaceNoSC[] faces, List<FaceNoSC> tmpFaces) { FaceNoSC selected = null; if (faces != null) { if (faces.Length - 1 > tmpFaces.Count) { selected = faces[tmpFaces.Count]; } else if (tmpFaces.Count - 4 >= 0) { selected = tmpFaces[tmpFaces.Count - 4]; } } return selected; }
public CellFace[] getSameSurfaceAdjacentFaces(FaceNoSC face) { List <CellFace> adjacents = new List <CellFace>(); int index = getFaceIndex(face); Cell[] neighbors = this.Map.getNeightbours(this); if (index == this.properties.faces.Length - 1) { // Es el top foreach (var n in neighbors) { if (n != null) { if (n.Height == this.Height) { adjacents.Add(new CellFace(n, n.properties.faces[n.properties.faces.Length - 1])); } } } } else if (index != -1) { // The lower one if (index - 4 >= 0 && !isCoveredByOthers(this.properties.faces[index - 4], index - 4, neighbors)) { adjacents.Add(new CellFace(this, this.properties.faces[index - 4])); } // The upper one if (index + 4 < this.properties.faces.Length - 1 && !isCoveredByOthers(this.properties.faces[index + 4], index + 4, neighbors)) { adjacents.Add(new CellFace(this, this.properties.faces[index + 4])); } int direction = index % 4; Cell leftOne = neighbors[(direction + 1) % 4]; //The left one if (leftOne != null && index < leftOne.properties.faces.Length - 1 && !isCoveredByOthers(leftOne.properties.faces[index], index, leftOne.Map.getNeightbours(leftOne))) { adjacents.Add(new CellFace(leftOne, leftOne.properties.faces[index])); } Cell rightOne = neighbors[(direction + 3) % 4]; //The rightOne if (rightOne != null && index < rightOne.properties.faces.Length - 1 && !isCoveredByOthers(rightOne.properties.faces[index], index, rightOne.Map.getNeightbours(rightOne))) { adjacents.Add(new CellFace(rightOne, rightOne.properties.faces[index])); } } return(adjacents.ToArray()); }
private FaceNoSC createFace(int[] indexes, FaceNoSC copy, List<Vector3> vertexList, Vector3[] vertices) { FaceNoSC f = new FaceNoSC(); f.FinalVertexList = vertexList; f.SharedVertex = vertices; f.VertexIndex = indexes; f.regenerateTriangles(); if (copy != null) { //f.getAtribsFrom(copy); f.Texture = copy.Texture; f.TextureMapping = copy.TextureMapping; } return f; }
private FaceNoSC createFace(int[] indexes, FaceNoSC copy, List<Vector3> vertexList, Vector3[] vertices) { FaceNoSC f = new FaceNoSC(); f.FinalVertexList = vertexList; f.SharedVertex = vertices; f.VertexIndex = indexes; f.regenerateTriangles(); if (copy != null) { // f.getAtribsFrom(copy); f.Texture = copy.Texture; f.TextureMapping = copy.TextureMapping; } return f; }
private Vector3[] getRealPointsOf(FaceNoSC f) { Vector3[] vertex = f.SharedVertex; int[] indexes = f.VertexIndex; Vector3[] puntos = new Vector3[4]; for (int i = 0; i < indexes.Length; i++) { puntos[i] = cs.transform.TransformPoint(vertex[indexes[i]]); } if (indexes.Length == 3) { puntos[3] = cs.transform.TransformPoint(vertex[indexes[2]]); } return(puntos); }
private int getFaceIndex(FaceNoSC f) { if (f == properties.faces[properties.faces.Length - 1]) { return(properties.faces.Length - 1); } else { for (int i = 0; i < properties.faces.Length; i++) { if (properties.faces[i] == f) { return(i); } } } return(-1); }
// Deserialization moment void Awake() { #if UNITY_EDITOR if (Application.isEditor && !Application.isPlaying) { // Code to prevent losing information from old versions if (faces != null && faces.Length != 0) { // The first time i have to initialize those FaceNoSC[] nfaces = new FaceNoSC[faces.Length]; for (int i = 0; i < faces.Length; i++) { if (faces[i] == null) { // Debug.Log("Null face detected at " + this + " at map " + Map); break; } nfaces[i] = new FaceNoSC(); nfaces[i].Texture = faces[i].Texture; nfaces[i].TextureMapping = faces[i].TextureMapping; } this.properties = new CellProperties(height, topType, cellTopRotation, 1, faces != null ? nfaces : new FaceNoSC[0]); Debug.Log("No era null"); faces = null; } } #endif // This will prevent older versions to lose face information about textures. /*if (this.faces == null) { * faces = new Face[faceTextures.Length]; * for (int i = 0; i< faces.Length; i++) { * faces[i].Texture = faceTextures[i]; * faces[i].TextureMapping = faceMappings[i]; * } * faceTextures = null; * faceMappings = null; * } */ }
private static void extractFacesFromMesh(Mesh mesh, CellProperties properties) { int numLateralFaces = Mathf.CeilToInt(properties.height) * 4; //Numero de vertices de la ultima capa int[] topFacesNumVertex = (properties.top == CellTopType.flat) ? new int[1] { 4 } : new int[4] { 3, 4, 3, 4 }; int verticesForThisFace; int totalVertices = 0; for (int i = 0; i < numLateralFaces + topFacesNumVertex.Length; i++) { // Calculamos cuantos vertices tendra la cara. Si es lateral 4 y sino, los que esten preestablecidos. verticesForThisFace = (i < numLateralFaces)?4:topFacesNumVertex[i - numLateralFaces]; FaceNoSC.extractFaceInfoFromMesh(mesh, verticesForThisFace, totalVertices, properties.faces[i]); totalVertices += verticesForThisFace; } }
public bool isCoveredByOthers(FaceNoSC f, int pos, Cell[] neighbors) { bool isCovered = false; float height = ((pos / 4) + 1) * this.properties.width; int direction = pos % 4; switch (direction) { case 0: direction = 2; break; case 2: direction = 0; break; } if (neighbors[direction] != null) { if (neighbors[direction].Height >= height) { isCovered = true; } } return(isCovered); }
public CellFace(Cell c, FaceNoSC f) { this.c = c; this.f = f; }
public bool isCoveredByOthers(FaceNoSC f, int pos, Cell[] neighbors) { bool isCovered = false; float height = ((pos / 4) + 1) * this.properties.width; int direction = pos % 4; switch (direction) { case 0: direction = 2;break; case 2: direction = 0; break; } if (neighbors[direction] != null) if (neighbors[direction].Height >= height) isCovered = true; return isCovered; }
public void OnSceneGUI(SceneView scene) { HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); RaycastHit info = new RaycastHit(); GameObject selected = null; Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); if (Physics.Raycast(ray, out info /*, LayerMask.NameToLayer("Cells Layer")*/)) //TODO Arreglar esto porque al parecer la distancia no funciona correctamente { if (info.collider.transform.IsChildOf(this.map.transform)) { selected = info.collider.gameObject; } } /** * Mouse Events of painting mode */ bool decorateLater = false; if (Event.current.isMouse && Event.current.button == 0 && selected != null && Event.current.type == EventType.MouseUp) { decorateLater = true; } /** * Working zone */ if (selected != null) { Cell cs = selected.GetComponent <Cell>(); if (cs != null) { FaceNoSC f = (cs != null) ? cs.getFaceByPoint(info.point) : null; if (f != null) { Vector3 position = selected.transform.position; if (cs != null) { position.y = cs.Height; } Vector3[] vertex = f.SharedVertex; int[] indexes = f.VertexIndex; Vector3[] puntos = new Vector3[5]; for (int i = 0; i < indexes.Length; i++) { puntos[i] = cs.transform.TransformPoint(vertex[indexes[i]]); } if (indexes.Length == 3)//Triangular faces { puntos[3] = cs.transform.TransformPoint(vertex[indexes[2]]); } puntos[puntos.Length - 1] = puntos[0]; // Closes the line Handles.DrawPolyLine(puntos); Vector2 directions = new Vector2(puntos[1].x - puntos[0].x, puntos[2].y - puntos[1].y); int ang = 0; if (directions.x == 0f) { if (directions.y == 0f) { ang = 0; } else { ang = 2; } } else { ang = 1; } if (paintingIsoDecoration != null) { if (decorateLater) { GameObject tmpdec = cs.addDecoration(info.point, ang, parallelDecoration, (Event.current.shift) ? false : true, paintingIsoDecoration); if (autoanimate) { AutoAnimator tmpautoanim = tmpdec.AddComponent <AutoAnimator>() as AutoAnimator; tmpautoanim.FrameSecuence = this.FrameSecuence; tmpautoanim.FrameRate = this.FrameRate; } cs.refresh(); } else { map.ghostDecoration(cs, info.point, ang, parallelDecoration, (Event.current.shift) ? false : true, paintingIsoDecoration, 0.5f); } } } } } }
private FaceNoSC[] regenerateFaces(FaceNoSC[] faces, float height, CellTopType cellTop, float cellWidth, int CellTopRotation) { List<FaceNoSC> tmpFaces = new List<FaceNoSC>(); List<Vector3> finalVertexList = new List<Vector3>(); bool hasMediumTop = height != ((float)((int)height)); int numVert = ((int)height + 1) * 4 + ((hasMediumTop) ? 4 : 0); Vector3[] vertices = new Vector3[numVert + ((cellTop != CellTopType.flat) ? 2 : 0)]; float down = - height; switch (cellTop) { case CellTopType.plane: down -= 1f; break; case CellTopType.midPlane: down -= 0.5f; break; } // INDEXES FOR TOP FACE int vertTopLeft = vertices.Length - 4; int verTopRight = vertices.Length - 3; int vertBotLeft = vertices.Length - 1; int vertBotRight = vertices.Length - 2; //MAIN VARS FOR VERTICES float halfWidth = (cellWidth / 2.0f); Vector3 vectorHeight = new Vector3(0, cellWidth, 0); // BASE VERTICES vertices[0] = new Vector3(-halfWidth, 0, -halfWidth); vertices[1] = new Vector3(halfWidth, 0, -halfWidth); vertices[2] = new Vector3(halfWidth, 0, halfWidth); vertices[3] = new Vector3(-halfWidth, 0, halfWidth); if (height >= 1) vertices[4] = vertices[0] + vectorHeight; else if (hasMediumTop) vertices[4] = vertices[0] + vectorHeight * 0.5f; FaceNoSC last = null; //MAIN LATERAL FACE GENERATOR for (int i = 4; i < numVert; i++) { int cutted = (i % 4 == 3) ? 1 : 0; if (i + 1 < numVert) vertices[i + 1] = vertices[i - 3] + vectorHeight * ((hasMediumTop && (i + 1) >= numVert - 4) ? 0.5f : 1f); last = selectFaceFor(faces, tmpFaces); tmpFaces.Add(createFace(new int[4] { i - 4, i - 3 - (4 * cutted), i + 1 - (4 * cutted), i }, last, finalVertexList, vertices)); } //EXTRA FACES GENERATOR if (cellTop != CellTopType.flat) { float aumHeight = (cellTop == CellTopType.midPlane) ? 0.5f : 1f; int topBotLeft = numVert - (4 - (CellTopRotation + 0) % 4), topBotRight = numVert - (4 - (CellTopRotation + 1) % 4), topTopRight = numVert - (4 - (CellTopRotation + 2) % 4), topTopLeft = numVert - (4 - (CellTopRotation + 3) % 4); vertices[numVert] = vertices[topBotRight] + vectorHeight * aumHeight; vertices[numVert + 1] = vertices[topTopRight] + vectorHeight * aumHeight; //NEW TOP FACE int[] topFaceIndexes = new int[4] { topTopLeft, numVert + 1, numVert, topBotLeft }; vertBotLeft = topFaceIndexes[CellTopRotation]; vertTopLeft = topFaceIndexes[(3 + CellTopRotation) % 4]; verTopRight = topFaceIndexes[(2 + CellTopRotation) % 4]; vertBotRight = topFaceIndexes[(1 + CellTopRotation) % 4]; // Lado Derecho last = selectFaceFor(faces, tmpFaces); tmpFaces.Add(createFace(new int[3] { numVert, topBotLeft, topBotRight }, last, finalVertexList, vertices)); //Lado Izquierdo last = selectFaceFor(faces, tmpFaces); tmpFaces.Add(createFace(new int[3] { numVert + 1, topTopRight, topTopLeft }, last, finalVertexList, vertices)); //Parte de atras last = selectFaceFor(faces, tmpFaces); tmpFaces.Add(createFace(new int[4] { topBotRight, topTopRight, numVert + 1, numVert }, last, finalVertexList, vertices)); } //TOP FACE GENERATOR if (faces != null && faces.Length >= 1) last = faces[faces.Length - 1]; tmpFaces.Add(createFace(new int[4] { vertBotLeft, vertTopLeft, verTopRight, vertBotRight }, last, finalVertexList, vertices)); return tmpFaces.ToArray();//..ToArray(typeof(Face)) as Face[]; }
private int getFaceIndex(FaceNoSC f) { if (f == properties.faces[properties.faces.Length - 1]) { return properties.faces.Length - 1; } else { for (int i = 0; i < properties.faces.Length; i++) { if (properties.faces[i] == f) return i; } } return -1; }
public override void Prepare() { paintLater = false; backupTextures = false; RaycastHit info = new RaycastHit(); Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); GameObject go = null; if (Physics.Raycast(ray, out info /*, LayerMask.NameToLayer("Cells Layer")*/)) //TODO Arreglar esto porque al parecer la distancia no funciona correctamente { if (info.collider.transform.IsChildOf(paintModule.map.transform)) { go = info.collider.gameObject; } } if (Event.current.isMouse) { if (Event.current.button == 0) { if (Event.current.shift) { if (Event.current.type == EventType.mouseDown) { collectTexture = true; } if (collectTexture && Event.current.type != EventType.MouseUp) { backupTextures = true; } else { collectTexture = false; } } else if (Event.current.type == EventType.MouseDown) { painting = true; paintLater = true; } else if (Event.current.type == EventType.MouseUp) { painting = false; } else { if (painting) { paintLater = true; } } } } if (go != null) { cs = go.GetComponent <Cell>(); } else { cs = null; } if (cs != null) { f = cs.getFaceByPoint(info.point); } else { f = null; } }
private Vector3[] getRealPointsOf(FaceNoSC f) { Vector3[] vertex = f.SharedVertex; int[] indexes = f.VertexIndex; Vector3[] puntos = new Vector3[4]; for (int i = 0; i < indexes.Length; i++) puntos[i] = cs.transform.TransformPoint(vertex[indexes[i]]); if (indexes.Length == 3) puntos[3] = cs.transform.TransformPoint(vertex[indexes[2]]); return puntos; }
public override void Prepare() { paintLater = false; backupTextures = false; RaycastHit info = new RaycastHit(); Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); GameObject go = null; if (Physics.Raycast(ray, out info/*, LayerMask.NameToLayer("Cells Layer")*/)) //TODO Arreglar esto porque al parecer la distancia no funciona correctamente if (info.collider.transform.IsChildOf(paintModule.map.transform)) go = info.collider.gameObject; if (Event.current.isMouse) { if (Event.current.button == 0) { if (Event.current.shift) { if (Event.current.type == EventType.mouseDown) collectTexture = true; if (collectTexture && Event.current.type != EventType.MouseUp) backupTextures = true; else collectTexture = false; } else if (Event.current.type == EventType.MouseDown) { painting = true; paintLater = true; } else if (Event.current.type == EventType.MouseUp) { painting = false; } else { if (painting) paintLater = true; } } } if (go != null) cs = go.GetComponent<Cell>(); else cs = null; if (cs != null) f = cs.getFaceByPoint(info.point); else f = null; }
// Deserialization moment void Awake() { #if UNITY_EDITOR if (Application.isEditor && !Application.isPlaying) { // Code to prevent losing information from old versions if (faces != null && faces.Length != 0) { // The first time i have to initialize those FaceNoSC[] nfaces = new FaceNoSC[faces.Length]; for (int i = 0; i < faces.Length; i++) { if (faces[i] == null) { // Debug.Log("Null face detected at " + this + " at map " + Map); break; } nfaces[i] = new FaceNoSC(); nfaces[i].Texture = faces[i].Texture; nfaces[i].TextureMapping = faces[i].TextureMapping; } this.properties = new CellProperties(height, topType, cellTopRotation, 1, faces != null ? nfaces : new FaceNoSC[0]); Debug.Log("No era null"); faces = null; } } #endif // This will prevent older versions to lose face information about textures. /*if (this.faces == null) { faces = new Face[faceTextures.Length]; for (int i = 0; i< faces.Length; i++) { faces[i].Texture = faceTextures[i]; faces[i].TextureMapping = faceMappings[i]; } faceTextures = null; faceMappings = null; } */ }
public void OnSceneGUI(SceneView scene) { HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); RaycastHit info = new RaycastHit(); Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); GameObject selected = null; if (Physics.Raycast(ray, out info /*, LayerMask.NameToLayer("Cells Layer")*/)) //TODO Arreglar esto porque al parecer la distancia no funciona correctamente { if (info.collider.transform.IsChildOf(this.map.transform)) { selected = info.collider.gameObject; } } bool paintLater = false; bool backupTextures = false; /* * Mouse Events of painting mode */ if (Event.current.isMouse) { if (Event.current.button == 0) { if (Event.current.shift) { if (Event.current.type == EventType.mouseDown) { collectTexture = true; } if (collectTexture && Event.current.type != EventType.MouseUp) { backupTextures = true; } else { collectTexture = false; } } else if (Event.current.type == EventType.MouseDown) { painting = true; paintLater = true; } else if (Event.current.type == EventType.MouseUp) { painting = false; } else { if (painting) { paintLater = true; } } } } /** * Working zone */ if (selected != null) { Cell cs = selected.GetComponent <Cell>(); if (cs != null) { FaceNoSC f = cs.getFaceByPoint(info.point); if (f != null) { if (paintLater && paintingTexture != null) { f.Texture = paintingTexture; f.TextureMapping = paintingIsoTexture; cs.forceRefresh(); } if (backupTextures) { this.paintingTexture = f.Texture; this.paintingIsoTexture = f.TextureMapping; repaint = true; } Vector3[] vertex = f.SharedVertex; int[] indexes = f.VertexIndex; Vector3[] puntos = new Vector3[4]; for (int i = 0; i < indexes.Length; i++) { puntos[i] = cs.transform.TransformPoint(vertex[indexes[i]]); } if (indexes.Length == 3) { puntos[3] = cs.transform.TransformPoint(vertex[indexes[2]]); } Handles.DrawSolidRectangleWithOutline(puntos, (Event.current.shift)? Color.blue : Color.yellow, Color.white); } } } }
private Texture2D getTextureAndGenerateUVs(FaceNoSC[] faces) { // BASE TEXTURE ATLAS Texture2D TextureAtlas = new Texture2D(1, 1); TextureAtlas.anisoLevel = 0; TextureAtlas.filterMode = FilterMode.Point; //RECOPILATING TEXTURES Texture2D[] AllCubeTextures = new Texture2D[faces.Length]; for (int i = 0; i < faces.Length; i++) AllCubeTextures[i] = (faces[i] as FaceNoSC).Texture; Rect[] posTexturas = TextureAtlas.PackTextures(AllCubeTextures, 0); for (int i = 0; i < faces.Length; i++) faces[i].regenerateUVs(posTexturas[i]); int partialHash = 0; Color[] pixeles = TextureAtlas.GetPixels(); int incr = (pixeles.Length > 1000)? (int) pixeles.Length / 1000 : 1; for(int i = 0; i<pixeles.Length; i = i+incr){ partialHash += pixeles[i].GetHashCode(); } TextureHash = partialHash + ""; return TextureAtlas; }
public CellFace[] getSameSurfaceAdjacentFaces(FaceNoSC face) { List<CellFace> adjacents = new List<CellFace>(); int index = getFaceIndex(face); Cell[] neighbors = this.Map.getNeightbours(this); if (index == this.properties.faces.Length - 1) { // Es el top foreach(var n in neighbors) if(n !=null) if(n.Height == this.Height) adjacents.Add(new CellFace(n, n.properties.faces[n.properties.faces.Length-1])); } else if(index != -1) { // The lower one if (index - 4 >= 0 && !isCoveredByOthers(this.properties.faces[index - 4], index - 4, neighbors)) adjacents.Add(new CellFace(this, this.properties.faces[index - 4])); // The upper one if (index + 4 < this.properties.faces.Length-1 && !isCoveredByOthers(this.properties.faces[index + 4], index + 4, neighbors)) adjacents.Add(new CellFace(this, this.properties.faces[index + 4])); int direction = index % 4; Cell leftOne = neighbors[(direction+1) % 4]; //The left one if (leftOne != null && index < leftOne.properties.faces.Length - 1 && !isCoveredByOthers(leftOne.properties.faces[index], index, leftOne.Map.getNeightbours(leftOne))) adjacents.Add(new CellFace(leftOne, leftOne.properties.faces[index])); Cell rightOne = neighbors[(direction + 3) % 4]; //The rightOne if (rightOne != null && index < rightOne.properties.faces.Length - 1 && !isCoveredByOthers(rightOne.properties.faces[index], index, rightOne.Map.getNeightbours(rightOne))) adjacents.Add(new CellFace(rightOne, rightOne.properties.faces[index])); } return adjacents.ToArray(); }
private FaceNoSC[] regenerateFaces(FaceNoSC[] faces, float height, CellTopType cellTop, float cellWidth, int CellTopRotation) { List<FaceNoSC> tmpFaces = new List<FaceNoSC>(); List<Vector3> finalVertexList = new List<Vector3>(); bool hasMediumTop = height != ((float)((int)height)); int numVert = ((int)height + 1) * 4 + ((hasMediumTop) ? 4 : 0); Vector3[] vertices = new Vector3[numVert + ((cellTop != CellTopType.flat) ? 2 : 0)]; float down = -height; switch (cellTop) { case CellTopType.plane: down -= 1f; break; case CellTopType.midPlane: down -= 0.5f; break; } // INDEXES FOR TOP FACE int vertTopLeft = vertices.Length - 4; int verTopRight = vertices.Length - 3; int vertBotLeft = vertices.Length - 1; int vertBotRight = vertices.Length - 2; //MAIN VARS FOR VERTICES float halfWidth = (cellWidth / 2.0f); Vector3 vectorHeight = new Vector3(0, cellWidth, 0); // BASE VERTICES vertices[0] = new Vector3(-halfWidth, 0, -halfWidth); vertices[1] = new Vector3(halfWidth, 0, -halfWidth); vertices[2] = new Vector3(halfWidth, 0, halfWidth); vertices[3] = new Vector3(-halfWidth, 0, halfWidth); if (height >= 1) vertices[4] = vertices[0] + vectorHeight; else if (hasMediumTop) vertices[4] = vertices[0] + vectorHeight * 0.5f; FaceNoSC last = null; //MAIN LATERAL FACE GENERATOR for (int i = 4; i < numVert; i++) { int cutted = (i % 4 == 3) ? 1 : 0; if (i + 1 < numVert) vertices[i + 1] = vertices[i - 3] + vectorHeight * ((hasMediumTop && (i + 1) >= numVert - 4) ? 0.5f : 1f); last = selectFaceFor(faces, tmpFaces); tmpFaces.Add(createFace(new int[4] { i - 4, i - 3 - (4 * cutted), i + 1 - (4 * cutted), i }, last, finalVertexList, vertices)); } //EXTRA FACES GENERATOR if (cellTop != CellTopType.flat) { float aumHeight = (cellTop == CellTopType.midPlane) ? 0.5f : 1f; int topBotLeft = numVert - (4 - (CellTopRotation + 0) % 4), topBotRight = numVert - (4 - (CellTopRotation + 1) % 4), topTopRight = numVert - (4 - (CellTopRotation + 2) % 4), topTopLeft = numVert - (4 - (CellTopRotation + 3) % 4); vertices[numVert] = vertices[topBotRight] + vectorHeight * aumHeight; vertices[numVert + 1] = vertices[topTopRight] + vectorHeight * aumHeight; //NEW TOP FACE int[] topFaceIndexes = new int[4] { topTopLeft, numVert + 1, numVert, topBotLeft }; vertBotLeft = topFaceIndexes[CellTopRotation]; vertTopLeft = topFaceIndexes[(3 + CellTopRotation) % 4]; verTopRight = topFaceIndexes[(2 + CellTopRotation) % 4]; vertBotRight = topFaceIndexes[(1 + CellTopRotation) % 4]; // Lado Derecho last = selectFaceFor(faces, tmpFaces); tmpFaces.Add(createFace(new int[3] { numVert, topBotLeft, topBotRight }, last, finalVertexList, vertices)); //Lado Izquierdo last = selectFaceFor(faces, tmpFaces); tmpFaces.Add(createFace(new int[3] { numVert + 1, topTopRight, topTopLeft }, last, finalVertexList, vertices)); //Parte de atras last = selectFaceFor(faces, tmpFaces); tmpFaces.Add(createFace(new int[4] { topBotRight, topTopRight, numVert + 1, numVert }, last, finalVertexList, vertices)); } //TOP FACE GENERATOR if (faces != null && faces.Length >= 1) last = faces[faces.Length - 1]; tmpFaces.Add(createFace(new int[4] { vertBotLeft, vertTopLeft, verTopRight, vertBotRight }, last, finalVertexList, vertices)); return tmpFaces.ToArray();//..ToArray(typeof(Face)) as Face[]; }
private Mesh generateMeshFromFaces(FaceNoSC[] faces) { ArrayList triangles = new ArrayList(); ArrayList uvs = new ArrayList(); ArrayList finalVertices = new ArrayList(); long partialHash = 0; foreach (FaceNoSC f in faces) { triangles.AddRange(f.Triangles); foreach (int vertex in f.VertexIndex) finalVertices.Add(f.SharedVertex[vertex]); foreach (Vector2 uv in f.Uvs) { uvs.Add(uv); partialHash += 1000000L * Mathf.RoundToInt(1000000f * uv.x) + Mathf.RoundToInt(1000000f * uv.y); } // uvs.AddRange(f.Uvs); } UVHash = partialHash + ""; Mesh auxMesh = new Mesh(); auxMesh.vertices = finalVertices.ToArray(typeof(Vector3)) as Vector3[]; auxMesh.triangles = triangles.ToArray(typeof(int)) as int[]; auxMesh.RecalculateNormals(); auxMesh.uv = uvs.ToArray(typeof(Vector2)) as Vector2[]; auxMesh.name = "Dynamic Cell"; auxMesh.RecalculateBounds(); return auxMesh; }