private void UpdateTileColors() { if (!setTileColors) { return; } else { setTileColors = false; } Color32[] cols = MESH_TRACKFLOOR.sharedMesh.colors32; for (int i = 0; i < MESH_TRACKFLOOR.sharedMesh.triangles.Length; i += 3) { TrTile tile = TrackDataHelper.TileFromTriangleIndex(i / 3, E_TRACKMESH.FLOOR, TRACK_DATA); if (tile.TILE_TYPE == E_TILETYPE.BOOST) { Debug.Log("Found boost!"); cols[MESH_TRACKFLOOR.sharedMesh.triangles[i + 0]] = Color.blue; cols[MESH_TRACKFLOOR.sharedMesh.triangles[i + 1]] = Color.blue; cols[MESH_TRACKFLOOR.sharedMesh.triangles[i + 2]] = Color.blue; } } MESH_TRACKFLOOR.sharedMesh.colors32 = cols; }
private void UpdateShipLighting() { // get ship color from vertex colors of track mesh RaycastHit hit; Vector3 to = r.currentSection.SECTION_POSITION; to.y -= 5; if (Physics.Linecast(transform.position, to, out hit, 1 << LayerMask.NameToLayer("TrackFloor"))) { int tri = hit.triangleIndex; MeshCollider mc = hit.collider as MeshCollider; Mesh m = mc.sharedMesh; // get tile TrTile tile = TrackDataHelper.TileFromTriangleIndex(tri, E_TRACKMESH.FLOOR, RaceSettings.trackData.TRACK_DATA); if (tile.TILE_TYPE == E_TILETYPE.BOOST) { shipColor = Color.blue; } else if (tile.TILE_TYPE == E_TILETYPE.WEAPON) { shipColor = Color.red; } else { shipColor = Color.Lerp(shipColor, m.colors32[m.triangles[hit.triangleIndex * 3]], Time.deltaTime * 15); } } r.mesh.GetComponent <Renderer>().material.SetColor("_Color", shipColor); }
private void UpdateInitialSection() { // try to find track section using a raycast RaycastHit hit; if (Physics.Raycast(transform.position, -transform.up, out hit, 1000.0f, 1 << LayerMask.NameToLayer("TrackFloor"))) { int tri = hit.triangleIndex; TrTile tile = TrackDataHelper.TileFromTriangleIndex(hit.triangleIndex, E_TRACKMESH.FLOOR, RaceSettings.trackData.TRACK_DATA); TrSection section = tile.TILE_SECTION; bool canUpdate = true; if (section.SECTION_TYPE == E_SECTIONTYPE.JUMP_START) { onJump = true; expectedLandSection = section.SECTION_NEXT; } if (section.SECTION_TYPE == E_SECTIONTYPE.JUMP_END) { onJump = false; if (section != expectedLandSection) { canUpdate = false; } } if (canUpdate) { r.initialSection = section; iSectionIndex = section.SECTION_INDEX; r.currentSection = r.initialSection; cSectionIndex = section.SECTION_INDEX; } iSectionFound = true; } else { iSectionFound = false; } }
/// <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); }
void OnSceneGUI() { HandleUtility.Repaint(); // do not allow selection HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); // get target class TrFlagPainter thisTarget = (TrFlagPainter)target; // cycle tile types if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.KeypadPlus) { if (thisTarget.DATA_FE.PAINT_MODE == E_PAINTMODE.TILE) { switch (currentTileType) { case E_TILETYPE.FLOOR: currentTileType = E_TILETYPE.BOOST; break; case E_TILETYPE.BOOST: currentTileType = E_TILETYPE.WEAPON; break; case E_TILETYPE.WEAPON: currentTileType = E_TILETYPE.RECHARGE; break; case E_TILETYPE.RECHARGE: currentTileType = E_TILETYPE.SPAWN; break; case E_TILETYPE.SPAWN: currentTileType = E_TILETYPE.FLOOR; break; } } else if (thisTarget.DATA_FE.PAINT_MODE == E_PAINTMODE.SECTION) { switch (currentSectionType) { case E_SECTIONTYPE.NORMAL: currentSectionType = E_SECTIONTYPE.LOGIC_STARTLINE; break; case E_SECTIONTYPE.LOGIC_STARTLINE: currentSectionType = E_SECTIONTYPE.JUNCTION_START; break; case E_SECTIONTYPE.JUNCTION_START: currentSectionType = E_SECTIONTYPE.JUNCTION_END; break; case E_SECTIONTYPE.JUNCTION_END: currentSectionType = E_SECTIONTYPE.JUMP_START; break; case E_SECTIONTYPE.JUMP_START: currentSectionType = E_SECTIONTYPE.JUMP_END; break; case E_SECTIONTYPE.JUMP_END: currentSectionType = E_SECTIONTYPE.PITLANE_ENTRANCE; break; case E_SECTIONTYPE.PITLANE_ENTRANCE: currentSectionType = E_SECTIONTYPE.NORMAL; break; } } } // raycast from scene view mouse position RaycastHit hit; Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); Mesh m = thisTarget.DATA_FE.MESH_TRACKFLOOR.sharedMesh; GUIStyle label = new GUIStyle(); label.fontStyle = FontStyle.Bold; if (Physics.Raycast(ray, out hit, 1 << LayerMask.NameToLayer("TrackFloor"))) { TrTile tile = TrackDataHelper.TileFromTriangleIndex(hit.triangleIndex, E_TRACKMESH.FLOOR, thisTarget.TRACK_DATA); if (thisTarget.DATA_FE.PAINT_MODE == E_PAINTMODE.TILE) { // draw mesh Vector3[] verts = new Vector3[4]; int t = hit.triangleIndex * 3; verts[0] = hit.transform.TransformPoint(m.vertices[m.triangles[t + 0]]); verts[1] = hit.transform.TransformPoint(m.vertices[m.triangles[t + 1]]); verts[2] = hit.transform.TransformPoint(m.vertices[m.triangles[t + 2]]); verts[3] = hit.transform.TransformPoint(m.vertices[m.triangles[t + 0]]); // change mesh color depending on face type Color col = Color.grey; Color outline = col; switch (tile.TILE_TYPE) { case E_TILETYPE.BOOST: col = Color.blue; break; case E_TILETYPE.WEAPON: col = Color.red; break; case E_TILETYPE.SPAWN: col = Color.yellow; break; case E_TILETYPE.RECHARGE: col = Color.green; break; } switch (currentTileType) { case E_TILETYPE.BOOST: outline = Color.blue; break; case E_TILETYPE.WEAPON: outline = Color.red; break; case E_TILETYPE.SPAWN: outline = Color.yellow; break; case E_TILETYPE.RECHARGE: outline = Color.green; break; } outline *= 0.8f; col.a = 0.2f; Handles.DrawSolidRectangleWithOutline(verts, col, outline); // draw label string flip = tile.TILE_SECOND ? "Flipped" : "Normal"; Vector3 pos = tile.TILE_POSITION; col.a = 1.0f; label.normal.textColor = col; Handles.Label(pos, "Current = " + tile.TILE_TYPE.ToString() + "_" + flip, label); pos += tile.TILE_SECTION.SECTION_NORMAL * 0.4f; outline.a = 1.0f; label.normal.textColor = outline; Handles.Label(pos, "Paint = " + currentTileType.ToString() + "_" + flip, label); if (Event.current.type == EventType.mouseUp && Event.current.button == 0) { // apply change tile.TILE_TYPE = currentTileType; // cache change thisTarget.DATA_FE.CACHE_TILES[tile.TILE_INDEX] = currentTileType; } else if (Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.R) { // apply change tile.TILE_TYPE = E_TILETYPE.FLOOR; // cache change thisTarget.DATA_FE.CACHE_TILES[tile.TILE_INDEX] = currentTileType; } } else if (thisTarget.DATA_FE.PAINT_MODE == E_PAINTMODE.SECTION) { thisTarget.DATA_FE.highlightedSection = tile.TILE_SECTION; // change mesh color depending on section type Color col = Color.grey; Color outline = col; switch (tile.TILE_SECTION.SECTION_TYPE) { case E_SECTIONTYPE.JUMP_START: col = Color.green; break; case E_SECTIONTYPE.JUMP_END: col = Color.green; col *= 0.5f; break; case E_SECTIONTYPE.JUNCTION_START: col = Color.red; break; case E_SECTIONTYPE.JUNCTION_END: col = Color.red; col *= 0.5f; break; case E_SECTIONTYPE.LOGIC_STARTLINE: col = Color.blue; break; case E_SECTIONTYPE.PITLANE_ENTRANCE: col = Color.yellow; break; } switch (currentSectionType) { case E_SECTIONTYPE.JUMP_START: outline = Color.green; break; case E_SECTIONTYPE.JUMP_END: outline = Color.green; outline *= 0.5f; break; case E_SECTIONTYPE.JUNCTION_START: outline = Color.red; break; case E_SECTIONTYPE.JUNCTION_END: outline = Color.red; outline *= 0.5f; break; case E_SECTIONTYPE.LOGIC_STARTLINE: outline = Color.blue; break; case E_SECTIONTYPE.PITLANE_ENTRANCE: outline = Color.yellow; break; } outline *= 0.8f; outline.a = 1.0f; col.a = 1.0f; Vector3 pos = tile.TILE_SECTION.SECTION_POSITION; label.normal.textColor = col; Handles.Label(pos, "Current = " + tile.TILE_SECTION.SECTION_TYPE.ToString(), label); pos += tile.TILE_SECTION.SECTION_NORMAL * 0.4f; label.normal.textColor = outline; Handles.Label(pos, "Paint = " + currentSectionType.ToString(), label); if (Event.current.type == EventType.mouseUp && Event.current.button == 0) { // apply change tile.TILE_SECTION.SECTION_TYPE = currentSectionType; // cache change thisTarget.DATA_FE.CACHE_SECTIONS[tile.TILE_SECTION.SECTION_INDEX] = currentSectionType; } } } }
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); }