void Update() { if (!firstRun) { return; } firstRun = false; float startTime = Time.realtimeSinceStartup; //record start time at beginning of function shapes = this.GetComponent <MeshShapes>(); populatePipes(); Debug.Log("Time to generate pipes: " + (Time.realtimeSinceStartup - startTime).ToString()); }
void Update() { if (!firstRun) { return; } firstRun = false; float startTime = Time.realtimeSinceStartup; //record start time at beginning of function shapes = this.GetComponent <MeshShapes>(); populateFlowers(3, 3); //restrict stems to 3 sides only Debug.Log("Time to generate flowers: " + (Time.realtimeSinceStartup - startTime).ToString()); }
//generate numerous flowers with properties between the input ranges void populateFlowers(int minStemSides = 3, int maxStemSides = 12, float minStemHeight = .12f, float maxStemHeight = .24f, float minStemWidth = .01f, float maxStemWidth = .03f, float minTilt = 0, float maxTilt = 90f, int minPetals = 8, int maxPetals = 18, float minPetalLength = .04f, float maxPetalLength = .14f, float minPetalWidth = .015f, float maxPetalWidth = .035f, int minPetalSegs = 2, int maxPetalSegs = 6, float minPetalSegRot = 5f, float maxPetalSegRot = 14f) { GameObject flowerParent = new GameObject(); flowerParent.name = "flower container"; //generate a 50x50 array of flowers on the x,z axes for (int i = 0; i < 50; ++i) { for (int r = 0; r < 50; ++r) { //calculate random position and properties within input range Vector3 curPos = new Vector3(1.5f + Random.Range(0, 49f), .5f, .25f + Random.Range(0, 49f)); int stemSides = Random.Range(minStemSides, maxStemSides); float stemHeight = Random.Range(minStemHeight, maxStemHeight); float stemWidth = Random.Range(minStemWidth, maxStemWidth); float petalTilt = Random.Range(minTilt, maxTilt); int numPetals = Random.Range(minPetals, maxPetals); float petalLength = Random.Range(minPetalLength, maxPetalLength); float petalWidth = Random.Range(minPetalWidth, maxPetalWidth); int petalSegs = Random.Range(minPetalSegs, maxPetalSegs); float petalSegRot = Random.Range(minPetalSegRot, maxPetalSegRot); GameObject go = new GameObject(); go.name = "flower"; go.transform.position = curPos; go.transform.rotation = meshGenerator.rotateQuaternion(go.transform.rotation, Vector3.up, Random.Range(0, 359f)); GenerateMesh newGenerator = go.AddComponent <GenerateMesh>(); MeshShapes shapes = go.AddComponent <MeshShapes>(); shapes.generateFlower(null, meshGenerator.rotateQuaternion(new Quaternion(0, 0, 0, 1), Vector3.left, -90f), stemSides, stemHeight, stemWidth, petalTilt, numPetals, petalLength, petalWidth, petalSegs, petalSegRot); newGenerator.finalizeMesh(); go.GetComponent <MeshRenderer>().material = this.GetComponent <GenerateMesh>().material; go.GetComponent <MeshRenderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f); go.GetComponent <MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; go.transform.SetParent(flowerParent.transform, true); //move flower to the ground RaycastHit hit; if (Physics.Raycast(go.transform.position, Vector3.down, out hit)) { go.transform.position = new Vector3(go.transform.position.x, go.transform.position.y - hit.distance + stemHeight, go.transform.position.z); } } } }
public void MakeQuadMeshData(float extents) { thisShape = MeshShapes.quad; this.QuadExtents = extents; verticies = new Vector3[] { new Vector3(0, 0, 0), new Vector3(extents, 0, 0), new Vector3(0, 0, extents), new Vector3(extents, 0, extents), //bottom face new Vector3(0, 0, 0), new Vector3(0, extents, 0), new Vector3(extents, 0, 0), new Vector3(extents, extents, 0), //front face new Vector3(0, extents, 0), new Vector3(0, extents, extents), new Vector3(extents, extents, 0), new Vector3(extents, extents, extents), //top face new Vector3(0, 0, extents), new Vector3(extents, 0, extents), new Vector3(0, extents, extents), new Vector3(extents, extents, extents), //back face new Vector3(0, 0, 0), new Vector3(0, 0, extents), new Vector3(0, extents, 0), new Vector3(0, extents, extents), //left face new Vector3(extents, 0, 0), new Vector3(extents, extents, 0), new Vector3(extents, 0, extents), new Vector3(extents, extents, extents) //right face }; triangles = new int[] { 0, 1, 2, 2, 1, 3, 4, 5, 6, 6, 5, 7, 8, 9, 10, 10, 9, 11, 12, 13, 14, 14, 13, 15, 16, 17, 18, 18, 17, 19, 20, 21, 22, 22, 21, 23 }; }
public void MakeLoopMeshData() { thisShape = MeshShapes.loop; List <Vector3> NewVerticies = new List <Vector3>(); List <int> newTriangles = new List <int>(); for (int i = 1; i < 6; i++) { NewVerticies.Add(new Vector3(i, i, i)); NewVerticies.Add(new Vector3(i, i + 1, i + 2)); NewVerticies.Add(new Vector3(i + 2, i + 1, i)); newTriangles.Add(3 * (i - 1)); newTriangles.Add(3 * (i - 1) + 1); newTriangles.Add(3 * (i - 1) + 2); } verticies = NewVerticies.ToArray(); triangles = newTriangles.ToArray(); }
//generate numerous bricks with properties between the input ranges void populateBricks(float length = .3f, float width = .6f, float height = .4f, float maxShiftOut = .15f) { GameObject brickParent = new GameObject(); brickParent.name = "brick container"; //first generate filling between the bricks, as a single wall GameObject go = new GameObject(); go.name = "brick seams"; GenerateMesh newGenerator = go.AddComponent <GenerateMesh>(); MeshShapes shapes = go.AddComponent <MeshShapes>(); shapes.generateBox(50 * (length + .02f) - .12f, 50 * (width + .02f) - .12f, height - maxShiftOut - .1f, new Vector3(.05f, 49 * (length + .02f) - .05f, -.05f), meshGenerator.rotateQuaternion(new Quaternion(0, 0, 0, 1), Vector3.left, -90f)); newGenerator.finalizeMesh(); go.GetComponent <MeshRenderer>().material = this.GetComponent <GenerateMesh>().material2; go.transform.SetParent(brickParent.transform, true); //generate 50x50 array of bricks on the x,y axes for (int i = 0; i < 50; ++i) { for (int r = 0; r < 50; ++r) { float shiftOut = Random.Range(0, maxShiftOut); Vector3 curPos = new Vector3(i * (width + .02f), r * (length + .02f), shiftOut); go = new GameObject(); go.name = "brick"; go.transform.position = curPos; newGenerator = go.AddComponent <GenerateMesh>(); shapes = go.AddComponent <MeshShapes>(); shapes.generateBox(length, width, height, null, meshGenerator.rotateQuaternion(new Quaternion(0, 0, 0, 1), Vector3.left, -90f)); newGenerator.finalizeMesh(); go.GetComponent <MeshRenderer>().material = this.GetComponent <GenerateMesh>().material; go.transform.SetParent(brickParent.transform, true); } } }
//generate numerous pipes with properties between the input ranges void populatePipes(int pipeSegs = 16, float circumference = 3, float minExtents = .8f, float maxExtents = 3f) { GameObject pipeParent = new GameObject(); pipeParent.name = "pipe container"; //setup initial rotation and position Vector3 curPos = new Vector3(0, 0, 0); Quaternion curRot = new Quaternion(0, 0, 0, 1); Vector3 prevForwardDir = Vector3.zero; Vector3 forwardDir = Vector3.zero; GameObject prevPipe = null; GameObject go = null; int rotAxis = 0; //generate 2000 pipe segments, each with a random color for (int i = 0; i < 2000; ++i) { float extents = Random.Range(minExtents, maxExtents); prevForwardDir = forwardDir; prevPipe = go; forwardDir = curRot * Vector3.forward; go = new GameObject(); go.name = "pipe segment"; if (i != 0 && rotAxis != 0) //move all but the first pipe so that they do not intersect, unless current pipe rotation was a spin { curPos = curPos + (prevForwardDir.normalized * (circumference / 2f / Mathf.PI)); curPos = curPos + (forwardDir.normalized * (circumference / 2f / Mathf.PI)); } go.transform.position = curPos; GenerateMesh newGenerator = go.AddComponent <GenerateMesh>(); shapes = go.AddComponent <MeshShapes>(); shapes.generateCylinder(extents, circumference, pipeSegs, false, "centerCap", null, curRot); newGenerator.finalizeMesh(); go.GetComponent <MeshRenderer>().material = this.GetComponent <GenerateMesh>().material; go.GetComponent <MeshRenderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f); go.transform.SetParent(pipeParent.transform, true); if (i != 0 && rotAxis != 0) //if the current pipe rotation was not a spin, add an elbow joint { GameObject elbow = new GameObject(); elbow.name = "elbow"; elbow.transform.position = curPos; GenerateMesh elbowGenerator = elbow.AddComponent <GenerateMesh>(); //generate triangles between all end verts of prevPipe, and all start verts of go GenerateMesh prevGenerator = prevPipe.GetComponent <GenerateMesh>(); GenerateMesh curGenerator = go.GetComponent <GenerateMesh>(); for (int r = 0; r < pipeSegs; ++r) { Vector3 vert0 = prevGenerator.vertices[6 + r * 8] + (prevPipe.transform.position - curPos); //start front vert index is 6, each subsequent vert is 8 more (4 outside, 4 inside per quad) Vector3 vert1 = prevGenerator.vertices[6 + (r + 1) % pipeSegs * 8] + (prevPipe.transform.position - curPos); Vector3 vert2 = curGenerator.vertices[5 + r * 8]; //start back vert index is 5, each subsequent vert is 8 more (4 outside, 4 inside per quad) Vector3 vert3 = curGenerator.vertices[5 + (r + 1) % pipeSegs * 8]; elbowGenerator.generateQuad(vert0, vert1, vert2, vert3); //generate outside quad elbowGenerator.generateQuad(vert0, vert2, vert1, vert3); //generate inside (flipped) quad } elbowGenerator.finalizeMesh(); elbow.GetComponent <MeshRenderer>().material = this.GetComponent <GenerateMesh>().material; elbow.GetComponent <MeshRenderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f); elbow.transform.SetParent(pipeParent.transform, true); } //update position to center of end cap curPos = curPos + (forwardDir.normalized * extents); rotAxis = Random.Range(0, 2); int rotOrientation = Random.Range(0, 1); curRot = meshGenerator.rotateQuaternion(curRot, rotAxis == 0 ? Vector3.forward : rotAxis == 1 ? Vector3.up : Vector3.left, 90 * (rotOrientation == 0 ? 1 : -1)); } }
public void MakeSquareMeshData(float xextents) { thisShape = MeshShapes.square; verticies = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 0, xextents), new Vector3(xextents, 0, 0), new Vector3(xextents, 0, xextents) }; triangles = new int[] { 0, 1, 2, 2, 1, 3 }; }
public void MakeTriangleMeshData() { thisShape = MeshShapes.triangle; verticies = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 0, 1), new Vector3(1, 0, 0) }; triangles = new int[] { 0, 1, 2 }; }