Vector3 GetPositionAlongPoints(float val, out float w) { // Reduce by tiny amount so we can still sample up! // Also it gives us less of a chance of hitting the points exactly float v = val * .99f * ((float)numPoints - 1); float up = Mathf.Ceil(v); float down = Mathf.Floor(v); float inVal = v - down; Vector3 fPos; if (inVal == 0 || up == down) { fPos = points[(int)down].position; w = points[(int)down].width; } else { BranchPoint p1 = points[(int)down]; BranchPoint p2 = points[(int)up]; fPos = cubicPoint(inVal, p1.position, p1.position + p1.normal / 3, p2.position - p2.normal / 3, p2.position); w = Mathf.Lerp(p1.width, p2.width, inVal); } return(fPos); }
public void GetBarkData(float x, float y, out Vector3 pos, out Vector3 centerPos, out Vector3 dir, out Vector3 nor, out Vector3 tang, out float life, out float width) { float v = x * .99f * ((float)numPoints - 1); float angle = y * 2 * Mathf.PI; float up = Mathf.Ceil(v); float down = Mathf.Floor(v); float inVal = v - down; Vector3 fPos; Vector3 fPos1; float fLife; float fWidth; if (inVal == 0 || up == down) { v += .0001f; up = Mathf.Ceil(v); down = Mathf.Floor(v); inVal = v - down; } BranchPoint p1 = points[(int)down]; BranchPoint p2 = points[(int)up]; fPos = cubicPoint(inVal, p1.position, p1.position + p1.normal / 3, p2.position - p2.normal / 3, p2.position); fPos1 = cubicPoint(inVal + .001f, p1.position, p1.position + p1.normal / 3, p2.position - p2.normal / 3, p2.position); fLife = Mathf.Lerp(p1.timeCreated, p2.timeCreated, inVal); fWidth = Mathf.Lerp(p1.width, p2.width, inVal); Vector3 fNor = Vector3.Lerp(p1.normal, p2.normal, inVal); Vector3 fTan = Vector3.Lerp(p1.tangent, p2.tangent, inVal); Vector3 fBi = Vector3.Lerp(p1.binormal, p2.binormal, inVal); Vector3 outVec = (fTan * Mathf.Sin(angle) - fBi * Mathf.Cos(angle)) * fWidth; centerPos = fPos; fPos += outVec; // radius;; pos = fPos; dir = (fPos1 - fPos).normalized; nor = outVec.normalized; tang = Vector3.Cross(nor, dir).normalized; life = fLife; width = fWidth; }
// Making points along each branch public void MakePoints() { Vector3 currPos = startPosition; tree.currentTotalPoints += numPoints; // place the points along the branch for (int i = 0; i < numPoints; i++) { float valInBranch = ((float)i / ((float)numPoints - 1)); float widthMultiplier = 1; if (iterationLevel == 0) { widthMultiplier = tree.trunkCurve.Evaluate(valInBranch); } else { widthMultiplier = tree.branchCurve.Evaluate(valInBranch); } if (i != 0) { float currNoiseSize = currVal(tree.noiseSize, tree.noiseSizeReducer); float currNoisePower = currVal(tree.noisePower, tree.noisePowerReducer); float currUp = currVal(tree.upDesire, tree.upDesireReducer); if (i != 1) { Vector3 dir = points[i - 1].position - points[i - 2].position; currPos += dir.normalized * length * ((float)1 / ((float)numPoints - 1)); } else { currPos += length * direction * ((float)1 / ((float)numPoints - 1)); } currPos += currUp * Vector3.up * .003f; Vector3 noiseDir = Perlin.CurlNoise(currPos * currNoiseSize + tree.noiseOffset); currPos += noiseDir * currNoisePower * .04f; } float fWidth = baseWidth * widthMultiplier; BranchPoint p = new BranchPoint(currPos, valInBranch, timeCreated + valInBranch, fWidth); points.Add(p); // TODO ADD NOISE } // Gets Tangents for each of the points for sake of // cubic beziers for (int i = 0; i < numPoints; i++) { BranchPoint p = points[i]; if (i == 0) { p.normal = (points[1].position - p.position); } else if (i == points.Count - 1) { p.normal = (p.position - points[points.Count - 2].position); } else { p.normal = -(points[i - 1].position - points[i + 1].position); } if (i == 0) { p.tangent = (Vector3.Cross(p.normal.normalized, Vector3.left)).normalized; p.binormal = (Vector3.Cross(p.normal, p.tangent)).normalized; } else { p.tangent = -(Vector3.Cross(p.normal.normalized, points[i - 1].binormal)).normalized; p.binormal = (Vector3.Cross(p.normal, p.tangent)).normalized; } points[i] = p; } }