void MakeCurvesSimple (ConnectionNode startNode, ConnectionNode endNode, out ConnectionCurve startCurve, out ConnectionCurve endCurve) { Vector3 nodeDiff = endNode.Position - startNode.Position; float ndLenght2 = nodeDiff.sqrMagnitude; Vector3 startCentre = FindBestCentre(startNode, endNode.Position, ndLenght2); Vector3 endCentre = FindBestCentre(endNode, startNode.Position, ndLenght2); startCurve = new ConnectionCurve(startCentre, (startNode.Position - startCentre).NoY().normalized, startNode.Facing); endCurve = new ConnectionCurve(endCentre, (endNode.Position - endCentre).NoY().normalized, endNode.Facing); }
void MakeCurves(ConnectionNode startNode, ConnectionNode endNode, out ConnectionCurve startCurve, out ConnectionCurve endCurve) { Vector3 nodeDiff = endNode.Position - startNode.Position; float ndLenght2 = nodeDiff.sqrMagnitude; float startToEndDirL2 = ((endNode.Position + endNode.Facing) - startNode.Position).sqrMagnitude; float endToStartDirL2 = ((startNode.Position + startNode.Facing) - endNode.Position).sqrMagnitude; Vector3 startCentre = FindBestCentre(startNode, (endNode.Position + endNode.Facing), startToEndDirL2); Vector3 endCentre = FindBestCentre(endNode, (startNode.Position + startNode.Facing), endToStartDirL2); startCurve = new ConnectionCurve(startCentre, (startNode.Position - startCentre).NoY().normalized, startNode.Facing); endCurve = new ConnectionCurve(endCentre, (endNode.Position - endCentre).NoY().normalized, endNode.Facing); }
public ConnectionScenario(ConnectionNode sNode, ConnectionNode eNode, ConnectionCurve sCurve, ConnectionCurve eCurve) { StartCurve = sCurve; EndCurve = eCurve; StartNode = sNode; EndNode = eNode; if (sCurve.RotationDirection == eCurve.RotationDirection) { Type = eScenarioType.crossed; } else { Type = eScenarioType.perpendicular; } EvaluateScenario(); }
IEnumerator VisualiseContinuous() { Visualising = true; Line.startWidth = 0.4f; Line.endWidth = 0.05f; // --- Node creation --- ConnectionNode startNode = MakeConnectionNode(); DrawFacingLine(startNode, LineStartDir, StartMacVis); bool placementFinished = false; bool stopTracking = false; Camera camera = Camera.main; Vector3 endFacing = Vector3.forward; int currentScenario = 0; bool useDynamicScenario = true; while (!placementFinished) { yield return(null); if (Input.GetMouseButtonDown(0)) { placementFinished = true; } if (Input.GetMouseButtonDown(1)) { placementFinished = !placementFinished; placementFinished = !placementFinished; } if (Input.GetKeyDown(KeyCode.Tab)) { currentScenario = (currentScenario + 1) % Scenarios.Length; } if (Input.GetKeyDown(KeyCode.LeftControl)) { useDynamicScenario = !useDynamicScenario; } if (Input.GetKeyDown(KeyCode.P)) { stopTracking = !stopTracking; } if (stopTracking) { continue; } if (Input.mouseScrollDelta.y != 0f) { endFacing = Quaternion.Euler(0f, Input.mouseScrollDelta.y * 5f, 0f) * endFacing; } var raycasts = Physics.RaycastAll(camera.ScreenPointToRay(Input.mousePosition), 100); Vector3 rayHitPos = startNode.Position; foreach (var r in raycasts) { if (r.collider.name.Equals("Terrain")) { rayHitPos = r.point; break; } } if ((rayHitPos - startNode.Position).sqrMagnitude <= 4f * 4f) { continue; } ConnectionNode endNode = new ConnectionNode(rayHitPos, endFacing); DrawFacingLine(endNode, LineEndDir, EndMacVis); // --- Scenarios --- Scenarios[0] = new ConnectionScenario(startNode, endNode, startNode.MakeConnectionCurve(ConnectionCurve.eRotationDirection.anticlockwise), endNode.MakeConnectionCurve(ConnectionCurve.eRotationDirection.anticlockwise)); Scenarios[1] = new ConnectionScenario(startNode, endNode, startNode.MakeConnectionCurve(ConnectionCurve.eRotationDirection.anticlockwise), endNode.MakeConnectionCurve(ConnectionCurve.eRotationDirection.clockwise)); Scenarios[2] = new ConnectionScenario(startNode, endNode, startNode.MakeConnectionCurve(ConnectionCurve.eRotationDirection.clockwise), endNode.MakeConnectionCurve(ConnectionCurve.eRotationDirection.anticlockwise)); Scenarios[3] = new ConnectionScenario(startNode, endNode, startNode.MakeConnectionCurve(ConnectionCurve.eRotationDirection.clockwise), endNode.MakeConnectionCurve(ConnectionCurve.eRotationDirection.clockwise)); string scenarioString = ""; int doubleCount = 0; int shortestLengthI = 0; float minDist = float.MaxValue; for (int i = 0; i < Scenarios.Length; ++i) { float newDist = Mathf.Min(minDist, Scenarios[i].Length()); if (newDist < minDist) { minDist = newDist; shortestLengthI = i; } scenarioString += $"S{i}: {Scenarios[i].StartCurve.UsingFirstChoice} {Scenarios[i].EndCurve.UsingFirstChoice}. "; } Debug.Log(scenarioString); Debug.Log($"Double trues: {doubleCount}. Active index: {shortestLengthI}."); // --- Line making --- List <Vector3> points = new List <Vector3>(); int scenarioI = useDynamicScenario ? shortestLengthI : currentScenario; ConnectionCurve startCurve = Scenarios[scenarioI].StartCurve; ConnectionCurve endCurve = Scenarios[scenarioI].EndCurve; StartCircleVis.localPosition = startCurve.Centre; EndCircleVis.localPosition = endCurve.Centre; int curvePoints = 1 + Mathf.CeilToInt(Mathf.Abs(startCurve.EndAngle) / MaxDegreesPerLine); float curveRotationIncrements = startCurve.EndAngle / (curvePoints - 1); points.Add(startCurve.Centre + startCurve.StartPos); for (int i = 1; i < curvePoints; ++i) { Quaternion rot = Quaternion.Euler(0f, curveRotationIncrements * i * startCurve.RotationDirection, 0f); points.Add(startCurve.Centre + rot * startCurve.StartPos); } curvePoints = 1 + Mathf.CeilToInt(Mathf.Abs(endCurve.EndAngle) / MaxDegreesPerLine); curveRotationIncrements = endCurve.EndAngle / (curvePoints - 1); points.Add(endCurve.Centre + (curvePoints == 1 ? endCurve.StartPos : endCurve.EndPos)); for (int i = 1; i < curvePoints; ++i) { Quaternion rot = Quaternion.Euler(0f, curveRotationIncrements * (curvePoints - i - 1) * endCurve.RotationDirection, 0f); points.Add(endCurve.Centre + rot * endCurve.StartPos); } // --- Line value setting --- Line.positionCount = points.Count; Line.SetPositions(points.ToArray()); } yield return(null); Visualising = false; }