public void Init() { track = gameObject.AddComponent <TrackBuildRTrack>(); track.InitTextures(); track.baseTransform = transform; TrackBuildRPoint p0 = gameObject.AddComponent <TrackBuildRPoint>(); // ScriptableObject.CreateInstance<TrackBuildRPoint>(); TrackBuildRPoint p1 = gameObject.AddComponent <TrackBuildRPoint>(); //ScriptableObject.CreateInstance<TrackBuildRPoint>(); TrackBuildRPoint p2 = gameObject.AddComponent <TrackBuildRPoint>(); //ScriptableObject.CreateInstance<TrackBuildRPoint>(); TrackBuildRPoint p3 = gameObject.AddComponent <TrackBuildRPoint>(); //ScriptableObject.CreateInstance<TrackBuildRPoint>(); p0.baseTransform = transform; p1.baseTransform = transform; p2.baseTransform = transform; p3.baseTransform = transform; p0.position = new Vector3(-20, 0, -20); p1.position = new Vector3(20, 0, -20); p2.position = new Vector3(20, 0, 20); p3.position = new Vector3(-20, 0, 20); p0.forwardControlPoint = new Vector3(0, 0, -20); p1.forwardControlPoint = new Vector3(40, 0, -20); p2.forwardControlPoint = new Vector3(0, 0, 20); p3.forwardControlPoint = new Vector3(-40, 0, 20); p0.leftForwardControlPoint = new Vector3(-15, 0, -20); p1.leftForwardControlPoint = new Vector3(25, 0, -20); p2.leftForwardControlPoint = new Vector3(5, 0, 20); p3.leftForwardControlPoint = new Vector3(-35, 0, 20); p0.rightForwardControlPoint = new Vector3(15, 0, -20); p1.rightForwardControlPoint = new Vector3(55, 0, -20); p2.rightForwardControlPoint = new Vector3(-5, 0, 20); p3.rightForwardControlPoint = new Vector3(-45, 0, 20); track.AddPoint(p0); track.AddPoint(p1); track.AddPoint(p2); track.AddPoint(p3); generator = gameObject.AddComponent <TrackBuildRGenerator>(); ForceFullRecalculation(); track.diagramMesh = new Mesh(); track.diagramMesh.vertices = new [] { new Vector3(-1, 0, -1), new Vector3(1, 0, -1), new Vector3(-1, 0, 1), new Vector3(1, 0, 1) }; track.diagramMesh.uv = new [] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1) }; track.diagramMesh.triangles = new [] { 1, 0, 2, 1, 2, 3 }; track.diagramGO = new GameObject("Diagram"); track.diagramGO.transform.parent = transform; track.diagramGO.transform.localPosition = Vector3.zero; track.diagramGO.AddComponent <MeshFilter>().mesh = track.diagramMesh; track.diagramMaterial = new Material(Shader.Find("Unlit/Texture")); track.diagramGO.AddComponent <MeshRenderer>().material = track.diagramMaterial; track.diagramGO.AddComponent <MeshCollider>().sharedMesh = track.diagramMesh; }
/// <summary> /// This function renders and controls the layout track function in the menu /// Users can click place track points into the scene for a quick and easy creation of a track /// </summary> private void DrawTrack() { SceneView.focusedWindow.wantsMouseMove = true; _handleSize = HandleUtility.GetHandleSize(_trackBuildR.transform.position) * 0.1f; Plane buildingPlane = new Plane(Vector3.up, _trackBuildR.transform.forward); float distance; Vector3 mousePlanePoint = Vector3.zero; Ray mouseRay = Camera.current.ScreenPointToRay(new Vector3(Event.current.mousePosition.x, Screen.height - Event.current.mousePosition.y - 30, 0)); if (buildingPlane.Raycast(mouseRay, out distance)) { mousePlanePoint = mouseRay.GetPoint(distance); } int numberOfPoints = _track.realNumberOfPoints; for (int i = 0; i < numberOfPoints; i++) { TrackBuildRPoint thisPoint = _track[i]; Vector3 thisPosition = thisPoint.worldPosition; Vector3 lastPosition = (i > 0) ? _track[i - 1].worldPosition : thisPosition; Vector3 nextPosition = (i < numberOfPoints - 1) ? _track[i + 1].worldPosition : thisPosition; Vector3 backwardTrack = thisPosition - lastPosition; Vector3 forwardTrack = nextPosition - thisPosition; Vector3 controlDirection = (backwardTrack + forwardTrack).normalized; float controlMagnitude = (backwardTrack.magnitude + forwardTrack.magnitude) * 0.333f; thisPoint.forwardControlPoint = (controlDirection * controlMagnitude) + thisPosition; } //draw track outline int numberOfCurves = _track.numberOfCurves; Vector3 position = _trackBuildR.transform.position; for (int i = 0; i < numberOfCurves; i++) { TrackBuildRPoint curve = _track[i]; int curvePoints = curve.storedPointSize; bool lastPoint = i == numberOfCurves - 1; Handles.color = (lastPoint && _track.loop) ? TrackBuildRColours.RED : TrackBuildRColours.GREEN; for (int p = 1; p < curvePoints; p++) { int indexA = p - 1; int indexB = p; Handles.DrawLine(curve.sampledPoints[indexA] + position, curve.sampledPoints[indexB] + position); Vector3 trackCrossWidth = curve.sampledTrackCrosses[indexA] * (curve.sampledWidths[indexA] * 0.5f); Handles.DrawLine(curve.sampledPoints[indexA] + trackCrossWidth + position, curve.sampledPoints[indexB] + trackCrossWidth + position); Handles.DrawLine(curve.sampledPoints[indexA] - trackCrossWidth + position, curve.sampledPoints[indexB] - trackCrossWidth + position); } } Handles.color = Color.green; if (Handles.Button(mousePlanePoint, Quaternion.identity, _handleSize, _handleSize, Handles.DotCap)) { TrackBuildRPoint newTrackPoint = _track.gameObject.AddComponent <TrackBuildRPoint>();//CreateInstance<TrackBuildRPoint>(); newTrackPoint.baseTransform = _trackBuildR.transform; newTrackPoint.position = mousePlanePoint; _track.loop = true; _track.AddPoint(newTrackPoint); } if (_track.realNumberOfPoints > 0) { TrackBuildRPoint pointOne = _track[0]; Handles.Label(pointOne.worldPosition, "Loop Track"); if (Handles.Button(pointOne.worldPosition, Quaternion.identity, _handleSize, _handleSize, Handles.DotCap)) { _track.drawMode = false; _trackBuildR.UpdateRender(); } } if (GUI.changed) { UpdateGui(); } }
public void Init() { track = gameObject.AddComponent<TrackBuildRTrack>(); track.InitTextures(); track.baseTransform = transform; TrackBuildRPoint p0 = gameObject.AddComponent<TrackBuildRPoint>();// ScriptableObject.CreateInstance<TrackBuildRPoint>(); TrackBuildRPoint p1 = gameObject.AddComponent<TrackBuildRPoint>();//ScriptableObject.CreateInstance<TrackBuildRPoint>(); TrackBuildRPoint p2 = gameObject.AddComponent<TrackBuildRPoint>();//ScriptableObject.CreateInstance<TrackBuildRPoint>(); TrackBuildRPoint p3 = gameObject.AddComponent<TrackBuildRPoint>();//ScriptableObject.CreateInstance<TrackBuildRPoint>(); p0.baseTransform = transform; p1.baseTransform = transform; p2.baseTransform = transform; p3.baseTransform = transform; p0.position = new Vector3(-20, 0, -20); p1.position = new Vector3(20, 0, -20); p2.position = new Vector3(20, 0, 20); p3.position = new Vector3(-20, 0, 20); p0.forwardControlPoint = new Vector3(0, 0, -20); p1.forwardControlPoint = new Vector3(40, 0, -20); p2.forwardControlPoint = new Vector3(0, 0, 20); p3.forwardControlPoint = new Vector3(-40, 0, 20); p0.leftForwardControlPoint = new Vector3(-15, 0, -20); p1.leftForwardControlPoint = new Vector3(25, 0, -20); p2.leftForwardControlPoint = new Vector3(5, 0, 20); p3.leftForwardControlPoint = new Vector3(-35, 0, 20); p0.rightForwardControlPoint = new Vector3(15, 0, -20); p1.rightForwardControlPoint = new Vector3(55, 0, -20); p2.rightForwardControlPoint = new Vector3(-5, 0, 20); p3.rightForwardControlPoint = new Vector3(-45, 0, 20); track.AddPoint(p0); track.AddPoint(p1); track.AddPoint(p2); track.AddPoint(p3); generator = gameObject.AddComponent<TrackBuildRGenerator>(); ForceFullRecalculation(); track.diagramMesh = new Mesh(); track.diagramMesh.vertices = new [] { new Vector3(-1, 0, -1), new Vector3(1, 0, -1), new Vector3(-1, 0, 1), new Vector3(1, 0, 1)}; track.diagramMesh.uv = new [] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(0,1), new Vector2(1,1)}; track.diagramMesh.triangles = new []{1,0,2,1,2,3}; track.diagramGO = new GameObject("Diagram"); track.diagramGO.transform.parent = transform; track.diagramGO.transform.localPosition = Vector3.zero; track.diagramGO.AddComponent<MeshFilter>().mesh = track.diagramMesh; track.diagramMaterial = new Material(Shader.Find("Unlit/Texture")); track.diagramGO.AddComponent<MeshRenderer>().material = track.diagramMaterial; track.diagramGO.AddComponent<MeshCollider>().sharedMesh = track.diagramMesh; }