/// <summary> /// Get the section a tile belongs to. /// </summary> /// <param name="tile"></param> /// <returns></returns> public static TrSection TileGetSection(TrTile tile) { return tile.TILE_SECTION; }
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; }
private void UpdateShipLighting() { // get ship color from vertex colors of track mesh RaycastHit hit; Vector3 to = r.currentSection.SECTION_POSITION; to.y -= 5; r.recharging = false; overWetSurface = false; if (Physics.Linecast(transform.position, to, out hit, 1 << LayerMask.NameToLayer("TrackFloor"))) { // get mesh information int tri = hit.triangleIndex; MeshCollider mc = hit.collider as MeshCollider; m = mc.sharedMesh; // if mesh changes then cache needed information (optimization - prevents the need for garbage collection) if (prevMesh != m) { trackColors = m.colors32; trackTriangles = m.triangles; prevMesh = m; } // get tile tile = TrackDataHelper.TileFromTriangleIndex(tri, E_TRACKMESH.FLOOR, RaceSettings.trackData.TRACK_DATA); // get color from track mesh (AI uses cached colors for performance, player ship grabs current color) if (r.isAI) shipColor = trackColors[trackTriangles[hit.triangleIndex * 3]]; else shipColor = m.colors32[trackTriangles[hit.triangleIndex * 3]]; if (tile.TILE_TYPE == E_TILETYPE.RECHARGE) r.recharging = true; if (tile.TILE_ISWET && r.sim.isShipGrounded) { overWetSurface = true; sprayPosition = hit.point; sprayPosition.x = transform.position.x; sprayPosition.z = transform.position.z; } if (tile.TILE_TYPE == E_TILETYPE.WEAPON) r.PickupItem(); // boost pad management if (tile.TILE_TYPE == E_TILETYPE.BOOST) { TrSection section = TrackDataHelper.TileGetSection(tile); Quaternion padRot = TrackDataHelper.SectionGetRotation(section); Vector3 padDir = padRot * Vector3.forward; r.body.AddForce(padDir * 35, ForceMode.Acceleration); if (tile != r.lastBoost) { r.HitSpeedPad(); r.lastBoost = tile; } } else { r.lastBoost = null; } } // dead color if (r.isDead) { shipDeadMult = Mathf.Lerp(shipDeadMult, 0.15f, Time.deltaTime * 3); shipColor *= shipDeadMult; } shipMat.SetColor("_Color", shipColor); if (r.recharging) rechargeSFX.volume = 1.0f; else rechargeSFX.volume = 0.0f; }