/// <summary> /// Get a tile from a vertex. /// </summary> /// <param name="index"></param> /// <returns></returns> public static TrTile TileFromTriangleIndex(int index, E_TRACKMESH meshType, TrGenData data) { if (meshType == E_TRACKMESH.FLOOR) { if (data.TILES_FLOOR_MAPPED[index * 3] != null) { return(data.TILES_FLOOR_MAPPED[index * 3]); } else { return(null); } } else if (meshType == E_TRACKMESH.WALL) { if (data.TILES_WALL_MAPPED[index * 3] != null) { return(data.TILES_WALL_MAPPED[index * 3]); } else { return(null); } } else { return(null); } }
/// <summary> /// Get a section by its index. /// </summary> /// <param name="index"></param> /// <returns></returns> public static TrSection SectionFromIndex(int index, TrGenData data) { for (int i = 0; i < data.SECTIONS.Count; i++) { if (data.SECTIONS[i].SECTION_INDEX == index) return data.SECTIONS[i]; } return null; }
/// <summary> /// Get a tile from a vertex. /// </summary> /// <param name="index"></param> /// <returns></returns> public static TrTile TileFromTriangleIndex(int index, E_TRACKMESH meshType, TrGenData data) { if (meshType == E_TRACKMESH.FLOOR) { if (data.TILES_FLOOR_MAPPED[index * 3] != null) return data.TILES_FLOOR_MAPPED[index * 3]; else return null; } else if (meshType == E_TRACKMESH.WALL) { if (data.TILES_WALL_MAPPED[index * 3] != null) return data.TILES_WALL_MAPPED[index * 3]; else return null; } else { return null; } }
/// <summary> /// Forces the track data to be regenerated. /// </summary> public void UpdateTrackData() { // if there is no track data or the meshes have changed then generate new track data if (TRACK_DATA == null || MESH_TRACKFLOOR_PREV != MESH_TRACKFLOOR || MESH_TRACKWALL != MESH_TRACKWALL_PREV || reloadTrackData) { reloadTrackData = false; // makes sure both track and wall meshes are present before updating if (MESH_TRACKFLOOR == null || MESH_TRACKWALL == null) return; // restore original meshes before regenerating track data if (!hasCachedVerts) { originalFloorMesh = MESH_TRACKFLOOR.sharedMesh; originalWallMesh = MESH_TRACKWALL.sharedMesh; hasCachedVerts = true; } else if (hasCachedVerts) { MESH_TRACKFLOOR.sharedMesh = originalFloorMesh; MESH_TRACKWALL.sharedMesh = originalWallMesh; } TRACK_DATA = TrGen.GenerateTrack(MESH_TRACKFLOOR.sharedMesh, MESH_TRACKWALL.sharedMesh, MESH_TRACKFLOOR.transform, MESH_TRACKWALL.transform); MESH_TRACKFLOOR_PREV = MESH_TRACKFLOOR; MESH_TRACKWALL_PREV = MESH_TRACKWALL; // fetch tile and section types from cache if (CACHE_TILES.Length > 0) { for (int i = 0; i < CACHE_TILES.Length; i++) { TRACK_DATA.TILES_FLOOR[i].TILE_TYPE = CACHE_TILES[i]; } } if (CACHE_SECTIONS.Length > 0) { for (int i = 0; i < CACHE_SECTIONS.Length; i++) { TRACK_DATA.SECTIONS[i].SECTION_TYPE = CACHE_SECTIONS[i]; } } // set cache array lengths if (CACHE_TILES.Length != TRACK_DATA.TILES_FLOOR.Count) CACHE_TILES = new E_TILETYPE[TRACK_DATA.TILES_FLOOR.Count]; if (CACHE_SECTIONS.Length != TRACK_DATA.SECTIONS.Count) CACHE_SECTIONS = new E_SECTIONTYPE[TRACK_DATA.SECTIONS.Count]; // load cached section positions if (CACHE_SECTIONPOSITIONS.Count > 0) { for (int i = 0; i < CACHE_SECTIONPOSITIONS.Count; i++) { TRACK_DATA.SECTIONS[i].SECTION_POSITION = CACHE_SECTIONPOSITIONS[i]; } } } }
public static TrGenData GenerateTrack(Mesh trackFloor, Mesh trackWall, Transform floorT, Transform floorW) { if (trackFloor == null || trackWall == null) { Debug.LogError("TRGEN: track floor and/or track wall meshes not present!"); return null; } TrGenData gen = new TrGenData(); // re-build floor mesh so all tris have unique verts Vector3[] newVertices = new Vector3[trackFloor.triangles.Length]; Vector2[] newUV = new Vector2[trackFloor.triangles.Length]; Vector3[] newNormals = new Vector3[trackFloor.triangles.Length]; int[] newTriangles = new int[trackFloor.triangles.Length]; int triLength = trackFloor.triangles.Length; for (int i = 0; i < triLength; i++) { newVertices[i] = trackFloor.vertices[trackFloor.triangles[i]]; newUV[i] = trackFloor.uv[trackFloor.triangles[i]]; newNormals[i] = trackFloor.normals[trackFloor.triangles[i]]; newTriangles[i] = i; } trackFloor.vertices = newVertices; trackFloor.uv = newUV; trackFloor.normals = newNormals; trackFloor.triangles = newTriangles; // re-build wall mesh newVertices = new Vector3[trackWall.triangles.Length]; newUV = new Vector2[trackWall.triangles.Length]; newNormals = new Vector3[trackWall.triangles.Length]; newTriangles = new int[trackWall.triangles.Length]; triLength = trackWall.triangles.Length; for (int i = 0; i < triLength; i++) { newVertices[i] = trackWall.vertices[trackWall.triangles[i]]; newUV[i] = trackWall.uv[trackWall.triangles[i]]; newNormals[i] = trackWall.normals[trackWall.triangles[i]]; newTriangles[i] = i; } trackWall.vertices = newVertices; trackWall.uv = newUV; trackWall.normals = newNormals; trackWall.triangles = newTriangles; #region TEMPS // verts Vector3[] verts = trackFloor.vertices; Vector3[] verts2 = trackWall.vertices; // section data TrSection newSection = new TrSection(); TrTile[] tiles = new TrTile[2]; // mesh triangles int[] tris = trackFloor.triangles; int[] tris2 = trackWall.triangles; // tiles to be mapped to indicies TrTile[] mappedFloor = new TrTile[tris.Length]; TrTile[] mappedWall = new TrTile[tris.Length]; // mesh normals Vector3[] normals = trackFloor.normals; Vector3[] normals2 = trackWall.normals; // vertex positions Vector3 p1 = Vector3.zero; Vector3 p2 = Vector3.zero; Vector3 pMid = Vector3.zero; // vertex normals Vector3 n1 = Vector3.zero; Vector3 n2 = Vector3.zero; Vector3 nMid = Vector3.zero; #endregion // create floor tiles int index = 0; for (int i = 0; i < tris.Length - 3; i += 6) { // create new tile TrTile newTile = new TrTile(); // add tile indicies newTile.TILE_INDICES.Add(tris[i + 0]); newTile.TILE_INDICES.Add(tris[i + 1]); newTile.TILE_INDICES.Add(tris[i + 2]); newTile.TILE_INDICES.Add(tris[i + 4]); // get mid position of all vertices p1 = floorT.TransformPoint(verts[tris[i]]); p2 = floorT.TransformPoint(verts[tris[i + 1]]); pMid = (p1 + p2) / 2; // set default tile settings newTile.TILE_TYPE = E_TILETYPE.FLOOR; newTile.TILE_COLOR = Color.white; newTile.TILE_POSITION = pMid; newTile.TILE_INDEX = index; // add tile to list gen.TILES_FLOOR.Add(newTile); mappedFloor[i + 0] = newTile; mappedFloor[i + 1] = newTile; mappedFloor[i + 2] = newTile; mappedFloor[i + 3] = newTile; index++; } /* // create wall tiles index = 0; for (int i = 0; i < tris2.Length - 3; i += 6) { // create new tile TrTile newTile = new TrTile(); // add tile indicies newTile.TILE_INDICES.Add(tris2[i + 0]); newTile.TILE_INDICES.Add(tris2[i + 1]); newTile.TILE_INDICES.Add(tris2[i + 2]); newTile.TILE_INDICES.Add(tris2[i + 4]); // get mid position of all vertices p1 = floorT.TransformPoint(verts2[tris2[i]]); p2 = floorT.TransformPoint(verts2[tris2[i + 1]]); pMid = (p1 + p2) / 2; // set default tile settings newTile.TILE_TYPE = E_TILETYPE.FLOOR; newTile.TILE_COLOR = Color.white; newTile.TILE_POSITION = pMid; newTile.TILE_INDEX = 0; // add tile to list gen.TILES_WALL.Add(newTile); mappedWall[i + 0] = newTile; mappedWall[i + 1] = newTile; mappedWall[i + 2] = newTile; mappedWall[i + 4] = newTile; index++; } */ // create sections index = 0; for (int i = 0; i < gen.TILES_FLOOR.Count - 1; i += 2) { // setup section and get tiles newSection = new TrSection(); tiles[0] = gen.TILES_FLOOR[i + 0]; tiles[1] = gen.TILES_FLOOR[i + 1]; // set section defaults newSection.SECTION_TYPE = E_SECTIONTYPE.NORMAL; newSection.SECTION_TILES = tiles; newSection.SECTION_INDEX = index; // set section position p1 = floorT.transform.TransformPoint(verts[tiles[0].TILE_INDICES[0]]); p2 = floorT.transform.TransformPoint(verts[tiles[1].TILE_INDICES[1]]); pMid = (p1 + p2) / 2; newSection.SECTION_POSITION = pMid; // set section normal n1 = floorT.transform.TransformDirection(normals[tiles[0].TILE_INDICES[0]]); n2 = floorT.transform.TransformDirection(normals[tiles[1].TILE_INDICES[1]]); nMid = (n1 + n2) / 2; newSection.SECTION_NORMAL = nMid; // add section gen.SECTIONS.Add(newSection); // add section to tiles tiles[0].TILE_SECTION = newSection; tiles[1].TILE_SECTION = newSection; tiles[1].TILE_SECOND = true; index++; } // set next sections for (int i = 0; i < gen.SECTIONS.Count; i++) { if (i == gen.SECTIONS.Count - 1) gen.SECTIONS[i].SECTION_NEXT = gen.SECTIONS[0]; else gen.SECTIONS[i].SECTION_NEXT = gen.SECTIONS[i + 1]; } // add mapped tiles to gendata for (int i = 0; i < mappedFloor.Length; i++) gen.TILES_FLOOR_MAPPED.Add(mappedFloor[i]); return gen; }
/// <summary> /// Forces the track data to be regenerated. /// </summary> public void UpdateTrackData() { // if there is no track data or the meshes have changed then generate new track data if (TRACK_DATA == null || MESH_TRACKFLOOR_PREV != MESH_TRACKFLOOR || MESH_TRACKWALL != MESH_TRACKWALL_PREV || reloadTrackData) { reloadTrackData = false; // makes sure both track and wall meshes are present before updating if (MESH_TRACKFLOOR == null || MESH_TRACKWALL == null) { return; } // restore original meshes before regenerating track data if (!hasCachedVerts) { originalFloorMesh = MESH_TRACKFLOOR.sharedMesh; originalWallMesh = MESH_TRACKWALL.sharedMesh; hasCachedVerts = true; } else if (hasCachedVerts) { MESH_TRACKFLOOR.sharedMesh = originalFloorMesh; MESH_TRACKWALL.sharedMesh = originalWallMesh; } TRACK_DATA = TrGen.GenerateTrack(MESH_TRACKFLOOR.sharedMesh, MESH_TRACKWALL.sharedMesh, MESH_TRACKFLOOR.transform, MESH_TRACKWALL.transform); MESH_TRACKFLOOR_PREV = MESH_TRACKFLOOR; MESH_TRACKWALL_PREV = MESH_TRACKWALL; // fetch tile and section types from cache if (CACHE_TILES.Length > 0) { for (int i = 0; i < CACHE_TILES.Length; i++) { TRACK_DATA.TILES_FLOOR[i].TILE_TYPE = CACHE_TILES[i]; } } if (CACHE_SECTIONS.Length > 0) { for (int i = 0; i < CACHE_SECTIONS.Length; i++) { TRACK_DATA.SECTIONS[i].SECTION_TYPE = CACHE_SECTIONS[i]; } } // set cache array lengths if (CACHE_TILES.Length != TRACK_DATA.TILES_FLOOR.Count) { CACHE_TILES = new E_TILETYPE[TRACK_DATA.TILES_FLOOR.Count]; } if (CACHE_SECTIONS.Length != TRACK_DATA.SECTIONS.Count) { CACHE_SECTIONS = new E_SECTIONTYPE[TRACK_DATA.SECTIONS.Count]; } // load cached section positions if (CACHE_SECTIONPOSITIONS.Count > 0) { for (int i = 0; i < CACHE_SECTIONPOSITIONS.Count; i++) { TRACK_DATA.SECTIONS[i].SECTION_POSITION = CACHE_SECTIONPOSITIONS[i]; } } } }
public static TrGenData GenerateTrack(Mesh trackFloor, Mesh trackWall, Transform floorT, Transform floorW) { if (trackFloor == null || trackWall == null) { Debug.LogError("TRGEN: track floor and/or track wall meshes not present!"); return(null); } TrGenData gen = new TrGenData(); // re-build floor mesh so all tris have unique verts Vector3[] newVertices = new Vector3[trackFloor.triangles.Length]; Vector2[] newUV = new Vector2[trackFloor.triangles.Length]; Vector3[] newNormals = new Vector3[trackFloor.triangles.Length]; int[] newTriangles = new int[trackFloor.triangles.Length]; for (int i = 0; i < trackFloor.triangles.Length; i++) { newVertices[i] = trackFloor.vertices[trackFloor.triangles[i]]; newUV[i] = trackFloor.uv[trackFloor.triangles[i]]; newNormals[i] = trackFloor.normals[trackFloor.triangles[i]]; newTriangles[i] = i; } trackFloor.vertices = newVertices; trackFloor.uv = newUV; trackFloor.normals = newNormals; trackFloor.triangles = newTriangles; // re-build wall mesh newVertices = new Vector3[trackWall.triangles.Length]; newUV = new Vector2[trackWall.triangles.Length]; newNormals = new Vector3[trackWall.triangles.Length]; newTriangles = new int[trackWall.triangles.Length]; for (int i = 0; i < trackWall.triangles.Length; i++) { newVertices[i] = trackWall.vertices[trackWall.triangles[i]]; newUV[i] = trackWall.uv[trackWall.triangles[i]]; newNormals[i] = trackWall.normals[trackWall.triangles[i]]; newTriangles[i] = i; } trackWall.vertices = newVertices; trackWall.uv = newUV; trackWall.normals = newNormals; trackWall.triangles = newTriangles; #region TEMPS // verts Vector3[] verts = trackFloor.vertices; Vector3[] verts2 = trackWall.vertices; // section data TrSection newSection = new TrSection(); TrTile[] tiles = new TrTile[2]; // mesh triangles int[] tris = trackFloor.triangles; int[] tris2 = trackWall.triangles; // tiles to be mapped to indicies TrTile[] mappedFloor = new TrTile[tris.Length]; TrTile[] mappedWall = new TrTile[tris.Length]; // mesh normals Vector3[] normals = trackFloor.normals; Vector3[] normals2 = trackWall.normals; // vertex positions Vector3 p1 = Vector3.zero; Vector3 p2 = Vector3.zero; Vector3 pMid = Vector3.zero; // vertex normals Vector3 n1 = Vector3.zero; Vector3 n2 = Vector3.zero; Vector3 nMid = Vector3.zero; #endregion // create floor tiles int index = 0; for (int i = 0; i < tris.Length - 3; i += 6) { // create new tile TrTile newTile = new TrTile(); // add tile indicies newTile.TILE_INDICES.Add(tris[i + 0]); newTile.TILE_INDICES.Add(tris[i + 1]); newTile.TILE_INDICES.Add(tris[i + 2]); newTile.TILE_INDICES.Add(tris[i + 4]); // get mid position of all vertices p1 = floorT.TransformPoint(verts[tris[i]]); p2 = floorT.TransformPoint(verts[tris[i + 1]]); pMid = (p1 + p2) / 2; // set default tile settings newTile.TILE_TYPE = E_TILETYPE.FLOOR; newTile.TILE_COLOR = Color.white; newTile.TILE_POSITION = pMid; newTile.TILE_INDEX = index; // add tile to list gen.TILES_FLOOR.Add(newTile); mappedFloor[i + 0] = newTile; mappedFloor[i + 1] = newTile; mappedFloor[i + 2] = newTile; mappedFloor[i + 3] = newTile; index++; } // create wall tiles index = 0; for (int i = 0; i < tris2.Length - 3; i += 6) { // create new tile TrTile newTile = new TrTile(); // add tile indicies newTile.TILE_INDICES.Add(tris2[i + 0]); newTile.TILE_INDICES.Add(tris2[i + 1]); newTile.TILE_INDICES.Add(tris2[i + 2]); newTile.TILE_INDICES.Add(tris2[i + 4]); // get mid position of all vertices p1 = floorT.TransformPoint(verts2[tris2[i]]); p2 = floorT.TransformPoint(verts2[tris2[i + 1]]); pMid = (p1 + p2) / 2; // set default tile settings newTile.TILE_TYPE = E_TILETYPE.FLOOR; newTile.TILE_COLOR = Color.white; newTile.TILE_POSITION = pMid; newTile.TILE_INDEX = 0; // add tile to list gen.TILES_WALL.Add(newTile); mappedWall[i + 0] = newTile; mappedWall[i + 1] = newTile; mappedWall[i + 2] = newTile; mappedWall[i + 4] = newTile; index++; } // create sections index = 0; for (int i = 0; i < gen.TILES_FLOOR.Count - 1; i += 2) { // setup section and get tiles newSection = new TrSection(); tiles[0] = gen.TILES_FLOOR[i + 0]; tiles[1] = gen.TILES_FLOOR[i + 1]; // set section defaults newSection.SECTION_TYPE = E_SECTIONTYPE.NORMAL; newSection.SECTION_TILES = tiles; newSection.SECTION_INDEX = index; // set section position p1 = floorT.transform.TransformPoint(verts[tiles[0].TILE_INDICES[0]]); p2 = floorT.transform.TransformPoint(verts[tiles[1].TILE_INDICES[1]]); pMid = (p1 + p2) / 2; newSection.SECTION_POSITION = pMid; // set section normal n1 = floorT.transform.TransformDirection(normals[tiles[0].TILE_INDICES[0]]); n2 = floorT.transform.TransformDirection(normals[tiles[1].TILE_INDICES[1]]); nMid = (n1 + n2) / 2; newSection.SECTION_NORMAL = nMid; // add section gen.SECTIONS.Add(newSection); // add section to tiles tiles[0].TILE_SECTION = newSection; tiles[1].TILE_SECTION = newSection; tiles[1].TILE_SECOND = true; index++; } // set next sections for (int i = 0; i < gen.SECTIONS.Count; i++) { if (i == gen.SECTIONS.Count - 1) { gen.SECTIONS[i].SECTION_NEXT = gen.SECTIONS[0]; } else { gen.SECTIONS[i].SECTION_NEXT = gen.SECTIONS[i + 1]; } } // add mapped tiles to gendata for (int i = 0; i < mappedFloor.Length; i++) { gen.TILES_FLOOR_MAPPED.Add(mappedFloor[i]); } return(gen); }