void AddPathAlgebra(int i, MPath path) { if (path.Count > 3) { Debug.Log("Set Text " + i); var textObj = Instantiate(textPrefab); Text text = textObj.GetComponentInChildren <Text> (); var str = "f" + i; if (path.dotFrom.Equals(path.dotTo)) { str += "; "; var dictionary = windingNumbersForObstacles[i]; for (int j = 0; j < statics.Count; j++) { var obstacle = statics[j]; str += "Ind(x" + j + ") = " + dictionary[j] + ", "; } } text.text = str; textObj.transform.SetParent(canvas.transform); Vector3 normal = Vector3.zero; var k = path.Count / 2; Debug.Log(path.Count); Debug.Log(k); normal = Vector3.Cross(path.GetPosition(k) - path.GetPosition(k - 1), Vector3.forward); var position = Camera.main.WorldToScreenPoint(path.GetPosition(k) + Statics.lineThickness * 2f * normal.normalized); textObj.transform.position = position + Vector3.right * dotPrefab.transform.lossyScale.x; UpdateHomAlgebra(); } }
public IEnumerator AddParticleSystem(MPath path) { float timeSum = 0f; bool isOk = true; var nextPosition = 0; while (path != null && path.Count > 0 && isOk) { isOk = false; try { var m_currentParticleEffect = path.gameObject.GetComponent <ParticleSystem> (); var numParticles = m_currentParticleEffect.particleCount; ParticleSystem.Particle[] ParticleList = new ParticleSystem.Particle[numParticles]; m_currentParticleEffect.GetParticles(ParticleList); for (int i = 0; i < numParticles; ++i) { if (path != null) { timeSum += Time.deltaTime; ParticleList[i].position = path.GetPosition(nextPosition); nextPosition = nextPosition + 1 < path.Count ? nextPosition + 1 : 0; } } m_currentParticleEffect.SetParticles(ParticleList, numParticles); isOk = true; } catch (MissingReferenceException ex) { Debug.Log(ex); yield break; } yield return(null); } yield break; }
public void ConstructCanonicalPath(MPath path) { path.canonicalPath = new List <int> (); for (int j = 0; j < path.Count - 1; j++) { var point1 = path.GetPosition(j); var point2 = path.GetPosition(j + 1); var comparedXIndex = CompareXIndex(point1, point2); if (comparedXIndex != -1) { bool over = overObstacle(comparedXIndex, point1, point2); if (over) { path.canonicalPath.Add(comparedXIndex * 2 + 1); } else { path.canonicalPath.Add(comparedXIndex * 2); } } } var can = path.canonicalPath; int i = 0; while (i < can.Count - 1) { if (can[i] == can[i + 1]) { can.RemoveAt(i); can.RemoveAt(i); if (i > 0) { i = i - 1; } } else { i++; } } }
Vector3 OnMeshTriangle(int i, MPath path, out int triangleIndex) { RaycastHit hit; // Debug.Log ("At point " + i + " normals " + path.line.GetNormals ().Count + " path.line " + path.Count); var normal = i < path.Count ? path.GetNormal(i) : path.GetNormal(0); var position = i < path.Count ? path.GetPosition(i) : path.GetPosition(0); Ray ray = new Ray(position, -normal); if (manifold.gameObject.GetComponent <MeshCollider> ().Raycast(ray, out hit, 0.5f)) { triangleIndex = hit.triangleIndex; return(hit.point); } else { Debug.LogWarning("Warning in OnMesh: Raycast not working, returning closest Point instead"); triangleIndex = -1; return(GameObject.FindGameObjectWithTag("Sphere").GetComponent <MeshCollider> ().ClosestPoint(position)); } }
void AdjustPositionsRetraction(int j) { var start = j + 1; for (int i = start; i < path.Count - 1; i++) { var posBefore = path.GetPosition(i - 1); var position = path.GetPosition(i); float factor = (i - start) / (path.Count - start); var dist = Vector3.Distance(posBefore, position); if (dist >= Statics.meanDist && factor < 0.999f) { // var newPos = Vector3.Lerp (posBefore, position, 0.5f + 0.5f * Mathf.Clamp01 (factor)); var newPos = Vector3.Lerp(posBefore, position, Statics.meanDist / dist); Collider collider1; bool hasHit1 = Misc.HasHit(newPos, out collider1); if (hasHit1) { newPos = Misc.PushOutOfObstacle(collider1, newPos); } path.SetPosition(i, new Vector3(newPos.x, newPos.y, position.z)); } else { break; } } var beforeLast = path.GetPosition(path.Count - 2); var last = path.GetPosition(path.Count - 1); if (Vector3.Distance(beforeLast, last) > Statics.meanDist) { path.InsertPositionAt(path.Count - 1, Vector3.Lerp(beforeLast, last, Statics.meanDist / Vector3.Distance(beforeLast, last))); } }
public Homotopy newHomotopy(MPath path1, MPath midPath) { Homotopy hom = new Homotopy(path1, midPath, homotopyMaterial); Debug.Log(midPath.ToString()); Debug.Log(midPath.Count.ToString()); var color = path1.GetColor(); midPath.SetColor(color); color.a = color.a / 3f; path1.SetColor(color); // for (int i = 0; i < Statics.numHomotopyLines; i++) { // GameObject lineObj = new GameObject (); // var line = lineObj.AddComponent<LineRenderer> (); // line.positionCount = hom.counter; // line.startWidth = Statics.lineThickness / 3f; // line.endWidth = Statics.lineThickness / 3f; // line.numCornerVertices = 0; // line.GetComponent<Renderer> ().material = pathMaterial; // line.GetComponent<Renderer> ().material.SetColor ("_Color", Statics.homotopyColor); //// hom.homotopyLines.Add (line); // } for (int i = 0; i < path1.Count; i++) { midPath.SetPosition(i, path1.GetPosition(i)); if (path1.hasNormals) { midPath.SetNormal(i, path1.GetNormal(i)); } } midPath.SetMesh(); // int counter = 1; // float sum = 0f; // var length = midPath.Length (); // for (int i = 0; i < count - 1; i++) { // if (sum > counter * (length / (Statics.numHomotopyLines + 1))) { // var line = hom.homotopyLines [counter - 1]; // line.SetPosition (0, midpath.GetPosition (i)); // counter++; // } // sum += Vector3.Distance (midpath.GetPosition (i), midpath.GetPosition (i + 1)); // } return(hom); }
List <Vector3> getPoints(MPath path2) { var points = new List <Vector3> (); List <Vertex> vertices = new List <Vertex> (); for (int i = 0; i < path2.Count; i++) { var point = path2.GetPosition(i); points.Add(point); } for (int i = path1.Count - 2; i > 0; i--) { var point = path1.GetPosition(i); points.Add(point); } points = DouglasPeucker.DouglasPeuckerReduction(points, 0.01d); return(points); }
public void AddCurveToBundle(PathFactory pf, int draggingPosition) { MPath path; if (curveBundle.Count == 0) { path = path1; } else { path = curveBundle[curveBundle.Count - 1]; } if (Vector3.Distance(path.GetPosition(draggingPosition), midPath.GetPosition(draggingPosition)) > Statics.bundleDist) { MPath newPath = pf.newPath(path1.GetColor(), path1.dotFrom, path1.dotTo); newPath.SetPositions(midPath.GetPositions()); curveBundle.Add(newPath); } }
IEnumerator drawConnector(bool space) { Debug.Log("Start Checking Paths"); var connectorObject = new GameObject("ConnectionTest between " + path1.pathNumber + " and " + path2.pathNumber); connector = new MLine(connectorObject); connector.SetMaterial(level.pathMat); connector.sortingOrder = Statics.LineSortingOrder + 1; width = Statics.lineThickness / 2f; connector.width = width; connector.SetColor(Color.red); connector.SetLocalPosition(0, path1.GetPosition(0)); connector.SetLocalPosition(1, path2.GetPosition(0)); dot1 = Instantiate(level.dotPrefab); dot1.transform.position = path1.GetPosition(0); dot1.GetComponent <SpriteRenderer> ().color = connector.GetColor(); GameObject.Destroy(dot1.GetComponentInChildren <TrailRenderer> ()); dot1.GetComponent <SpriteRenderer> ().sortingOrder = connector.sortingOrder; dot1.transform.localScale = width * 2 * Vector3.one; dot2 = Instantiate(level.dotPrefab); dot2.transform.position = path2.GetPosition(0); dot2.GetComponent <SpriteRenderer> ().color = connector.GetColor(); GameObject.Destroy(dot2.GetComponentInChildren <TrailRenderer> ()); dot2.GetComponent <SpriteRenderer> ().sortingOrder = connector.sortingOrder; dot2.transform.localScale = width * 2 * Vector3.one; var maxDistance = 1f; var speed = 10f; var meanTime = 1f / speed; var timeForStep1 = meanTime; var timeForStep2 = meanTime; if (space == false) { if (path1.Count < path2.Count) { timeForStep2 = ((float)path1.Count / path2.Count) * timeForStep1; } else { timeForStep1 = ((float)path2.Count / path1.Count) * timeForStep2; } } Debug.Log(path1.Count * timeForStep1 + " vs. " + path2.Count * timeForStep2); Debug.Log(timeForStep1 + " vs. " + timeForStep2); var timeGone1 = 0f; var timeGone2 = 0f; var i = 1; var j = 1; while (i < path1.Count - 1 || j < path2.Count - 1) { if (i < path1.Count - 1 && connector.GetLocalPosition(0) == path1.GetPosition(i)) { i++; timeGone1 = timeGone1 - timeForStep1; } if (j < path2.Count - 1 && connector.GetLocalPosition(1) == path2.GetPosition(j)) { j++; timeGone2 = timeGone2 - timeForStep2; } timeGone1 += Time.deltaTime; timeGone2 += Time.deltaTime; // var t = GetPercentage (path1, i) - GetPercentage (path2, j); // timeForStep1 = 1f / (speed + Mathf.Lerp (-speed, speed, 0.5f - t / 2)); // timeForStep2 = 1f / (speed + Mathf.Lerp (-speed, speed, 0.5f + t / 2)); // Debug.Log ("t = " + t + ", factor= " + (0.5f - t / 2) + ", lerp = " + Mathf.Lerp (-speed, speed, 0.5f - t / 2) + ", timeForStep1-meantime = " + (timeForStep1 - meanTime)); connector.SetLocalPosition(0, Vector3.Lerp(path1.GetPosition(i - 1), path1.GetPosition(i), timeGone1 / timeForStep1)); connector.SetLocalPosition(1, Vector3.Lerp(path2.GetPosition(j - 1), path2.GetPosition(j), timeGone2 / timeForStep2)); connector.width = width * (1 - Vector3.Distance(connector.GetLocalPosition(0), connector.GetLocalPosition(1)) / (maxDistance * 1.1f)); connector.SetMesh(); dot1.transform.position = connector.GetLocalPosition(0); dot2.transform.position = connector.GetLocalPosition(1); if (Vector3.Distance(dot1.transform.position, dot2.transform.position) > maxDistance) { StartCoroutine(ShrinkConnector(3f)); yield return(new WaitForSeconds(5)); Destroy(connectorObject); Destroy(dot1); Destroy(dot2); yield break; } yield return(null); } if (isHomotopic) { level.ShowHintCanvas(); LevelData.showHint("The paths are homotopic!"); yield return(new WaitForSeconds(5)); level.HideHintCanvas(); } yield break; }