public void init(Vector3 posi) { dad = new branca (); dad.rad = minRad; dad.lenght = minLen; dad.ringShape = new float[NumberOfSides + 1]; dad.quaternion = new Quaternion (); dad.pos = posi; dad.texCoordV = 0f; for (var n = 0; n < NumberOfSides; n++) { dad.ringShape[n] = Random.Range(1-regular, 1+regular); } dad.ringShape[NumberOfSides] = dad.ringShape[0]; dad.nsons = 0; dad.level = 1; dad.leaf = false; }
private branca initSon(branca dadd) { branca aux = new branca (); aux.rad = minRad; aux.lenght = minLen; aux.ringShape = new float[NumberOfSides + 1]; Quaternion qaux = dadd.quaternion; qaux.eulerAngles += new Vector3 (Random.Range(-rota,rota), 0, Random.Range(-rota,rota)); aux.quaternion = qaux; aux.texCoordV = 0f; for (var n = 0; n < NumberOfSides; n++) { aux.ringShape[n] = Random.Range(1-regular, 1+regular); } aux.ringShape[NumberOfSides] = aux.ringShape[0]; aux.nsons = 0; aux.level = dadd.level + 1; aux.leaf = false; return aux; }
private void recGrow(branca node, float value, int lastRingVertexIndex, bool beat) { //Carguem els vertex de l'anell del node var offset = Vector3.zero; var texCoord = new Vector2(0f, node.texCoordV); var textureStepU = 1f / NumberOfSides; var angInc = 2f * Mathf.PI * textureStepU; var ang = 0f; node.rad += Mathf.Min(((value*speed))/node.level, 0.2f); //creixer (rad) node.lenght += Mathf.Min(0.2f,((value * speed*ratio))/node.level); //creixer (lenght) for (var n = 0; n <= NumberOfSides; n++, ang += angInc) //afegir vertex anell node { var r = node.ringShape[n] * node.rad; offset.x = r * Mathf.Cos(ang); // Get X, Z vertex offsets offset.z = r * Mathf.Sin(ang); vertexList.Add(node.pos + node.quaternion * offset); // Add Vertex position uvList.Add(texCoord); // Add UV coord texCoord.x += textureStepU; } //Conectem vertex amb nodes anteriors, si no era el primer node if (lastRingVertexIndex >= 0) { // After first base ring is added ... // Create new branch segment quads, between last two vertex rings for (var currentRingVertexIndex = vertexList.Count - NumberOfSides - 1; currentRingVertexIndex < vertexList.Count - 1; currentRingVertexIndex++, lastRingVertexIndex++) { triangleList.Add (lastRingVertexIndex + 1); // Triangle A triangleList.Add (lastRingVertexIndex); triangleList.Add (currentRingVertexIndex); triangleList.Add (currentRingVertexIndex); // Triangle B triangleList.Add (currentRingVertexIndex + 1); triangleList.Add (lastRingVertexIndex + 1); } } //si la ultima branca no te fills, per la seguent crida (no recursiva) if(node.nsons == 0 ){ //Acabar en punta // Create a cap for ending the branch vertexList.Add(node.pos + node.quaternion * new Vector3(0f,node.lenght,0f)); // Add central vertex uvList.Add(texCoord + Vector2.one); // Twist UVs to get rings effect for (var n = vertexList.Count - NumberOfSides - 2; n < vertexList.Count - 2; n++) // Add cap { triangleList.Add(n); triangleList.Add(vertexList.Count - 1); triangleList.Add(n + 1); } if(/*fill < maxfill*/beat){ //treure n branquetes node.nsons = (int) Random.Range(0, maxSon); node.sons = new branca[node.nsons]; ///set random sons for(int i = 0; i < node.nsons; ++i){ node.sons[i] = initSon(node); ++fill; } } else node.leaf = true; } else { //crides recursives als fills del node node.leaf = false; node.texCoordV += 0.0625f * (node.lenght + node.lenght / node.rad); lastRingVertexIndex = vertexList.Count - NumberOfSides - 1; for (int i = 0; i<node.nsons; ++i) { node.sons [i].texCoordV = node.texCoordV; node.sons[i].pos = node.pos + node.quaternion * new Vector3(0f, node.lenght, 0f); recGrow (node.sons [i], value, lastRingVertexIndex, beat); } } }
private void initLeaf(branca node) { if (node.leaf) { Instantiate(copa, node.pos + node.quaternion * new Vector3(0f,node.lenght,0f), Quaternion.identity); } else { for(int i = 0; i<node.nsons; ++i){ initLeaf (node.sons[i]); } } }