public void BuildVertices(List <TreeVertex> verts) { this.vertOffset = verts.Count; for (int i = 0; i <= segments; i++) { float a = (i * Mathf.PI * 2.0f) / segments; TreeVertex v = new TreeVertex(); float rad = radius; float uvx = 1.0f - ((float)(i) / segments); float uvy = baseOffset; float noiseX = uvx; float noiseY = uvy; if (i == segments) { noiseX = 1.0f; // Loop around } // Weld spreading float spreadFactor = Mathf.Cos(a); float spreadRad = 0.0f; if (spreadFactor > 0.0f) { spreadRad = Mathf.Pow(spreadFactor, 3.0f) * radius * spreadBot; } else { spreadRad = Mathf.Pow(Mathf.Abs(spreadFactor), 3.0f) * radius * spreadTop; } // Noise.. float perlinX = noiseX * noiseScaleU; float perlinY = noiseY * noiseScaleV; rad += (radius * (perlin.Noise(perlinX, perlinY) * noiseScale)); // Flare.. perlinX = noiseX * flareNoise; rad += flareRadius * Mathf.Abs(perlin.Noise(perlinX, 0.12932f)); v.pos = matrix.MultiplyPoint(new Vector3(Mathf.Sin(a) * (rad + (spreadRad * 0.25f)), 0.0f, Mathf.Cos(a) * (rad + spreadRad))); v.uv0 = new Vector2(uvx, uvy); v.SetAnimationProperties(animParams.x, animParams.y, animParams.z, animParams.w); verts.Add(v); } if (radius == 0.0f) { for (int i = 0; i <= segments; i++) { TreeVertex v = verts[i + vertOffset]; float a = (i * Mathf.PI * 2.0f) / segments; float b = a - Mathf.PI * 0.5f; Vector3 normal = Vector3.zero; normal.x = Mathf.Sin(a) * surfAngleCos; normal.y = surfAngleSin; normal.z = Mathf.Cos(a) * surfAngleCos; v.nor = Vector3.Normalize(matrix.MultiplyVector(normal)); Vector3 tangent = Vector3.zero; tangent.x = Mathf.Sin(b); tangent.y = 0.0f; tangent.z = Mathf.Cos(b); tangent = Vector3.Normalize(matrix.MultiplyVector(tangent)); v.tangent = new Vector4(tangent.x, tangent.y, tangent.z, -1.0f); } return; } // Rethink normals.. to take noise into account.. Matrix4x4 matrixInv = matrix.inverse; for (int i = 0; i <= segments; i++) { int a = i - 1; if (a < 0) { a = segments - 1; } int b = i + 1; if (b > segments) { b = 1; } TreeVertex va = verts[a + vertOffset]; TreeVertex vb = verts[b + vertOffset]; TreeVertex vi = verts[i + vertOffset]; // Use the previous and the next vertices in the loop to create the tangent. // It will be tangent to the perfect ring loop in case of no noise. Vector3 tangent = Vector3.Normalize(va.pos - vb.pos); Vector3 normal = matrixInv.MultiplyVector(va.pos - vb.pos); // rotate 90 degrees and normalize normal.y = normal.x; normal.x = normal.z; normal.z = -normal.y; normal.y = 0.0f; normal.Normalize(); // tilt accoring to surface angle normal.x = surfAngleCos * normal.x; normal.y = surfAngleSin; normal.z = surfAngleCos * normal.z; // set normal vi.nor = Vector3.Normalize(matrix.MultiplyVector(normal)); // set tangent vi.tangent.x = tangent.x; vi.tangent.y = tangent.y; vi.tangent.z = tangent.z; vi.tangent.w = -1.0f; } }
public void BuildVertices(List <TreeVertex> verts) { this.vertOffset = verts.Count; for (int i = 0; i <= this.segments; i++) { float f = ((i * 3.141593f) * 2f) / ((float)this.segments); TreeVertex item = new TreeVertex(); float radius = this.radius; float x = 1f - (((float)i) / ((float)this.segments)); float baseOffset = this.baseOffset; float num6 = x; float num7 = baseOffset; if (i == this.segments) { num6 = 1f; } float num8 = Mathf.Cos(f); float num9 = 0f; if (num8 > 0f) { num9 = (Mathf.Pow(num8, 3f) * this.radius) * this.spreadBot; } else { num9 = (Mathf.Pow(Mathf.Abs(num8), 3f) * this.radius) * this.spreadTop; } float num10 = num6 * this.noiseScaleU; float y = num7 * this.noiseScaleV; radius += this.radius * (perlin.Noise(num10, y) * this.noiseScale); num10 = num6 * this.flareNoise; radius += this.flareRadius * Mathf.Abs(perlin.Noise(num10, 0.12932f)); item.pos = this.matrix.MultiplyPoint(new Vector3(Mathf.Sin(f) * (radius + (num9 * 0.25f)), 0f, Mathf.Cos(f) * (radius + num9))); item.uv0 = new Vector2(x, baseOffset); item.SetAnimationProperties(this.animParams.x, this.animParams.y, this.animParams.z, this.animParams.w); verts.Add(item); } if (this.radius == 0f) { for (int j = 0; j <= this.segments; j++) { TreeVertex vertex2 = verts[j + this.vertOffset]; float num13 = ((j * 3.141593f) * 2f) / ((float)this.segments); float num14 = num13 - 1.570796f; Vector3 zero = Vector3.zero; zero.x = Mathf.Sin(num13) * this.surfAngleCos; zero.y = this.surfAngleSin; zero.z = Mathf.Cos(num13) * this.surfAngleCos; vertex2.nor = Vector3.Normalize(this.matrix.MultiplyVector(zero)); Vector3 v = Vector3.zero; v.x = Mathf.Sin(num14); v.y = 0f; v.z = Mathf.Cos(num14); v = Vector3.Normalize(this.matrix.MultiplyVector(v)); vertex2.tangent = new Vector4(v.x, v.y, v.z, -1f); } } else { Matrix4x4 inverse = this.matrix.inverse; for (int k = 0; k <= this.segments; k++) { int num16 = k - 1; if (num16 < 0) { num16 = this.segments - 1; } int num17 = k + 1; if (num17 > this.segments) { num17 = 1; } TreeVertex vertex3 = verts[num16 + this.vertOffset]; TreeVertex vertex4 = verts[num17 + this.vertOffset]; TreeVertex vertex5 = verts[k + this.vertOffset]; Vector3 vector3 = Vector3.Normalize(vertex3.pos - vertex4.pos); Vector3 vector4 = inverse.MultiplyVector(vertex3.pos - vertex4.pos); vector4.y = vector4.x; vector4.x = vector4.z; vector4.z = -vector4.y; vector4.y = 0f; vector4.Normalize(); vector4.x = this.surfAngleCos * vector4.x; vector4.y = this.surfAngleSin; vector4.z = this.surfAngleCos * vector4.z; vertex5.nor = Vector3.Normalize(this.matrix.MultiplyVector(vector4)); vertex5.tangent.x = vector3.x; vertex5.tangent.y = vector3.y; vertex5.tangent.z = vector3.z; vertex5.tangent.w = -1f; } } }