private void initDefaultValues() { TrackName = "unnamed track"; Description = ""; waypoints = new List <Waypoint>(); logEntries = new List <LogEntry>(); directionMarkers = new List <GameObject>(); SamplingFactor = 1; LineColor = Color.green; LineWidth = 0.2f; NumDirectionMarkers = 0; ConeRadiusToLineWidthFactor = 30; ReplayColliders = false; //this.renderCoords = new List<Vector3>(); //init mesh and directionmarkes directionMarkerMesh = MeshFactory.createCone(1, 2, 12); EndAction = EndActions.STOP; LoopClosureTime = 0f; for (int i = 0; i < 20; ++i) { GameObject marker = MeshFactory.makeMeshGameObject(ref directionMarkerMesh, "cone"); marker.GetComponent <Renderer>().material = new Material(Shader.Find("KSP/Emissive/Diffuse")); marker.GetComponent <Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; marker.GetComponent <Renderer>().receiveShadows = false; marker.GetComponent <Renderer>().enabled = false; directionMarkers.Add(marker); } }
public ReplayWindow(Track track) : base("Replay Track: " + track.TrackName) { bool loadCraft = true; if (loadCraft) { try { ghost = CraftLoader.assembleCraft(Utilities.CraftPath + track.VesselName + ".crf", track.ReplayColliders); // --- add the craft file listed in the path, or selected from a menu --- } catch { //Debug.Log("ERROR LOADING CRF, FALLING BACK TO SPHERE"); loadCraft = false; } } if (!loadCraft) { Mesh sphere = MeshFactory.createSphere(); ghost = MeshFactory.makeMeshGameObject(ref sphere, "Track playback sphere"); ghost.transform.localScale = new Vector3(track.ConeRadiusToLineWidthFactor * track.LineWidth, track.ConeRadiusToLineWidthFactor * track.LineWidth, track.ConeRadiusToLineWidthFactor * track.LineWidth); //ghost.collider.enabled = false; ghost.GetComponent <Renderer>().material = new Material(Shader.Find("KSP/Emissive/Diffuse")); ghost.GetComponent <Renderer>().material.SetColor("_EmissiveColor", track.LineColor); } behaviour = ghost.AddComponent <ReplayBehaviour>(); behaviour.initialize(track, ghost); behaviour.enabled = true; this.windowPos = new Rect(600f, 50f, 300f, 100f); }
//assumes checks for actual movement have already been done private void updateForTire(ModuleTireTracker tire) { //Debug.Log("TireRecorder.updateForTire() " + tire.part.name); //if (!tire.part.GroundContact) //doesnt work // return; //Debug.Log("Part has ground contact!"); GameObject tireObject; if (!tires.TryGetValue(tire, out tireObject)) { //create mesh Debug.Log("Creating new Mesh for tire"); Mesh m = new Mesh(); m.triangles = new int[0]; m.vertices = new Vector3[0]; tireObject = MeshFactory.makeMeshGameObject(ref m, "TireObject"); //TODO texture tireObject.GetComponent <Renderer>().material = new Material(Shader.Find("KSP/Emissive/Diffuse")); tireObject.GetComponent <Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; tireObject.GetComponent <Renderer>().receiveShadows = false; tireObject.GetComponent <Renderer>().material.SetColor("_EmissiveColor", Color.red); tires.Add(tire, tireObject); //.renderer.material.te } Mesh mesh = tireObject.GetComponent <MeshFilter>().mesh; float width = 2; Vector3 groundContactCenter = tire.part.transform.position; //apply offset groundContactCenter.z -= tire.tireWidth; Vector3 tireAxis = tire.part.transform.right; int oldVertexCount = mesh.vertexCount; Vector3 newLeft = groundContactCenter - width / 2 * tireAxis; Vector3 newRight = groundContactCenter + width / 2 * tireAxis; //have we moved at all? if (oldVertexCount > 0 && (newLeft - mesh.vertices[mesh.vertexCount - 2]).sqrMagnitude < 0.1f && (newRight - mesh.vertices[mesh.vertexCount - 1]).sqrMagnitude < 0.1f ) { return; } //Debug.Log("updating tiretrack-mesh"); //add new mesh-vertices Vector3[] newVertices = new Vector3[oldVertexCount + 2]; Vector3[] newNormals = new Vector3[oldVertexCount + 2]; //copy old vertices for (int i = 0; i < mesh.vertexCount; ++i) { newVertices[i] = mesh.vertices[i]; newNormals[i] = mesh.normals[i]; } //Debug.Log("copied vertices and normals"); //put 2 new vertices and triangles newVertices[oldVertexCount] = newLeft; newVertices[oldVertexCount + 1] = newRight; //Debug.Log("created vertices"); int[] newTriangles = {}; if (oldVertexCount > 2) //first update creates vertices only { Debug.Log("oldVertexCount = " + oldVertexCount + ", newTriangleIndex-ArraySize = " + 3 * (oldVertexCount + 2)); newTriangles = new int[3 * (oldVertexCount + 2)]; for (int i = 0; i < mesh.triangles.Length; ++i) { newTriangles[i] = mesh.triangles[i]; } newTriangles[3 * oldVertexCount] = oldVertexCount; //new left upper newTriangles[3 * oldVertexCount + 1] = oldVertexCount - 2; //old last left; newTriangles[3 * oldVertexCount + 2] = oldVertexCount - 1; //old last right newTriangles[3 * oldVertexCount + 3] = oldVertexCount + 1; //new right newTriangles[3 * oldVertexCount + 4] = oldVertexCount; //new left newTriangles[3 * oldVertexCount + 5] = oldVertexCount - 1; //old last right string indexString = "Triangle indices:"; for (int i = 0; i < newTriangles.Length; ++i) { indexString += "[" + i + "] " + newTriangles[i] + ";"; } Debug.Log(indexString); } mesh.Clear(); mesh.vertices = newVertices; mesh.normals = newNormals; if (oldVertexCount > 2) { mesh.triangles = newTriangles; } }