public static RandomWalk3D createWalk(int numPts, int maxAcutePercent, int maxClusterSize, float percAreNodes = 1.0f) { int redraws = 0; RandomWalk3D line = new RandomWalk3D(); if (maxAcutePercent < 0) { maxAcutePercent = 100; } if (maxClusterSize < 0) { maxClusterSize = numPts; } Vector3 last = Vector3.zero; Vector3 randPt; int maxAcute = (numPts * maxAcutePercent) / 100; int numAcuteX, numAcuteY, numAcuteZ; int consecAcuteX, consecAcuteY, consecAcuteZ; bool foundCluster = true; bool isAcute = true; //Random rand; //long seed = 45; while (foundCluster || isAcute) { last = Vector3.zero; foundCluster = false; isAcute = false; numAcuteX = 0; numAcuteY = 0; numAcuteZ = 0; consecAcuteX = 0; consecAcuteY = 0; consecAcuteZ = 0; line.clear(); Vector3 tmpPt; // Loop through the algorithm the specified number of times. for (int i = 0; i < numPts; i++) { //randPt = Point.getRandomPoint(rand); randPt = getRandomPoint(); // add random Point last += randPt; if (Random.value > percAreNodes) { continue; } tmpPt = new Vector3(last.x, last.y, last.z); // add the new point to the list line.addPoint(tmpPt); if (line.size() > 2) { if (line.isAcuteXAngle()) { numAcuteX++; consecAcuteX++; } else { consecAcuteX = 0; } if (line.isAcuteYAngle()) { numAcuteY++; consecAcuteY++; } else { consecAcuteY = 0; } if (line.isAcuteZAngle()) { numAcuteZ++; consecAcuteZ++; } else { consecAcuteZ = 0; } } if (consecAcuteX > maxClusterSize || consecAcuteY > maxClusterSize || consecAcuteZ > maxClusterSize) { redraws++; i += numPts; foundCluster = true; break; } } if (numAcuteX > maxAcute || numAcuteY > maxAcute || numAcuteZ > maxAcute) { if (!foundCluster) { redraws++; } isAcute = true; } } Debug.Log("Num Redraws: " + redraws); line.scale(); return(line); }
void init() { RandomWalk3D flight = RandomWalk3D.createWalk(numPoints, 100, 100); Vector3 min = new Vector3(-3.0f, 0.0f, -2.2f); Vector3 max = new Vector3(0.3f, 4.0f, 2.2f); flight.scale(min, max); List <Vector3> ptList = flight.getPoints(); Vector3[] pts = ptList.ToArray(); if (useParticles) { ParticleSystem.MainModule main = system.main; main.maxParticles = pts.Length; main.loop = false; main.playOnAwake = false; main.simulationSpace = ParticleSystemSimulationSpace.World; ParticleSystem.EmissionModule emMod = system.emission; emMod.enabled = false; ParticleSystem.ShapeModule shMod = system.shape; shMod.enabled = false; ParticleSystem.Particle[] particles = new ParticleSystem.Particle[pts.Length]; for (int i = 0; i < pts.Length; i++) { particles[i] = new ParticleSystem.Particle(); particles[i].position = pts[i]; particles[i].velocity = Vector3.zero; particles[i].angularVelocity = 0.0f; particles[i].rotation = 0.0f; particles[i].startSize = 0.1f; particles[i].startLifetime = 10000.0f; particles[i].remainingLifetime = 10000.0f; particles[i].randomSeed = 0; particles[i].startColor = Color.white; } system.SetParticles(particles, pts.Length); } else { Vector3 cylTrans = new Vector3(0.0f, 0.1f, 0.0f); Vector3 sphereScale = new Vector3(nodeScale, nodeScale, nodeScale); Vector3 cylScale = new Vector3(edgeScale, 0.0f, edgeScale); for (int i = 0; i < pts.Length; i++) { GameObject sphere = getNewPoint(); sphere.transform.position = pts[i]; sphere.transform.localScale = sphereScale; GameObject sphere2 = getNewPoint(); sphere2.transform.position = pts[i]; sphere2.transform.localScale = sphereScale; sphere2.transform.localRotation = Quaternion.Euler(90.0f, 0.0f, 0.0f); GameObject sphere3 = getNewPoint(); sphere3.transform.position = pts[i]; sphere3.transform.localScale = sphereScale; sphere3.transform.localRotation = Quaternion.Euler(0.0f, 90.0f, 0.0f); if (includeEdges) { if (i == 0) { continue; } GameObject cylinder = (GameObject)Instantiate(cylinderPrefab); Vector3 dir = pts[i] - pts[i - 1]; float len = dir.magnitude; dir = Vector3.Normalize(dir); cylinder.transform.up = dir; cylinder.transform.position = pts[i - 1] + dir * len * 0.5f; Vector3 currScale = cylScale; currScale.y = len * 0.5f; cylinder.transform.localScale = currScale; } } } }