public virtual void Reset(float lerpValue = 0f) { float time = 0f; float minTime1 = 0f, minTime2 = 0f; float maxTime1 = 0f, maxTime2 = 0f; switch (MyType) { case TimerType.CONST: CurrentTimeValue = Time1; break; case TimerType.LERP_TWO_CONSTANTS: CurrentTimeValue = Mathf.Lerp(Time1, Time2, lerpValue); break; case TimerType.RANDOM_TWO_CONSTANTS: CurrentTimeValue = Random.Range(Time1, Time2); break; case TimerType.LERP_RANDOM_FOUR_CONSTANTS: CurrentTimeValue = Random.Range(Mathf.Lerp(Time1, Time2, lerpValue), Mathf.Lerp(Time3, Time4, lerpValue)); break; case TimerType.LERP_CURVE: CurrentTimeValue = Curve1.Evaluate(Mathf.Lerp(CurveMaxTime(ref Curve1), CurveMinTime(ref Curve1), lerpValue)) * ValueMultiplier; break; case TimerType.RANDOM_CURVE: CurrentTimeValue = Curve1.Evaluate(RandomCurveTime(ref Curve1)) * ValueMultiplier; break; case TimerType.LERP_RANDOM_TWO_CURVES: minTime1 = CurveMinTime(ref Curve1); maxTime1 = CurveMaxTime(ref Curve2); minTime2 = CurveMinTime(ref Curve2); maxTime2 = CurveMaxTime(ref Curve2); time = Mathf.Clamp(Mathf.Lerp(minTime1, maxTime1, lerpValue), minTime2, maxTime2); CurrentTimeValue = Random.Range(Curve1.Evaluate(time), Curve2.Evaluate(time)) * ValueMultiplier; break; case TimerType.RANDOM_TWO_CURVES: time = RandomCurveTime(ref Curve1); minTime2 = CurveMinTime(ref Curve2); maxTime2 = CurveMaxTime(ref Curve2); time = Mathf.Clamp(time, minTime2, maxTime2); CurrentTimeValue = Random.Range(Curve1.Evaluate(time), Curve2.Evaluate(time)) * ValueMultiplier; break; default: CurrentTimeValue = Time1; break; } timer = 0f; }
public float LerpedLocalRotation(float time) { if (previous == null) { return(localRotation); } float t = (this.time.time - time) / this.time.deltaTime; if (t < 0 || t > 1) { UnityEngine.Debug.Log("t " + t); } return(Mathf.Lerp(((Transform)previous).localRotation, localRotation, t)); }
public static float RangeLerp(this Vector2 vector2AsRange, float value) { return(Mathf.Lerp(vector2AsRange.x, vector2AsRange.y, value)); }
public static bool GeneratePathedStairs(ref ChiselBrushContainer brushContainer, ref ChiselPathedStairsDefinition definition) { definition.Validate(); var shapeVertices = new List <Vector2>(); var shapeSegmentIndices = new List <int>(); GetPathVertices(definition.shape, definition.curveSegments, shapeVertices, shapeSegmentIndices); var totalSubMeshCount = 0; for (int i = 0; i < shapeVertices.Count; i++) { if (i == 0 && !definition.shape.closed) { continue; } var leftSide = (!definition.shape.closed && i == 1) ? definition.stairs.leftSide : StairsSideType.None; var rightSide = (!definition.shape.closed && i == shapeVertices.Count - 1) ? definition.stairs.rightSide : StairsSideType.None; totalSubMeshCount += BrushMeshFactory.GetLinearStairsSubMeshCount(definition.stairs, leftSide, rightSide); } if (totalSubMeshCount == 0) { return(false); } // var stairDirections = definition.shape.closed ? shapeVertices.Count : (shapeVertices.Count - 1); brushContainer.EnsureSize(totalSubMeshCount); var depth = definition.stairs.depth; var height = definition.stairs.height; var halfDepth = depth * 0.5f; var halfHeight = height * 0.5f; int subMeshIndex = 0; for (int vi0 = shapeVertices.Count - 3, vi1 = shapeVertices.Count - 2, vi2 = shapeVertices.Count - 1, vi3 = 0; vi3 < shapeVertices.Count; vi0 = vi1, vi1 = vi2, vi2 = vi3, vi3++) { if (vi2 == 0 && !definition.shape.closed) { continue; } // TODO: optimize this, we're probably redoing a lot of stuff for every iteration var v0 = shapeVertices[vi0]; var v1 = shapeVertices[vi1]; var v2 = shapeVertices[vi2]; var v3 = shapeVertices[vi3]; var m0 = (v0 + v1) * 0.5f; var m1 = (v1 + v2) * 0.5f; var m2 = (v2 + v3) * 0.5f; var d0 = (v1 - v0); var d1 = (v2 - v1); var d2 = (v3 - v2); var maxWidth0 = d0.magnitude; var maxWidth1 = d1.magnitude; var maxWidth2 = d2.magnitude; var halfWidth1 = d1 * 0.5f; d0 /= maxWidth0; d1 /= maxWidth1; d2 /= maxWidth2; var depthVector = new Vector3(d1.y, 0, -d1.x); var lineCenter = new Vector3(m1.x, halfHeight, m1.y) - (depthVector * halfDepth); var depthVector0 = new Vector2(d0.y, -d0.x) * depth; var depthVector1 = new Vector2(d1.y, -d1.x) * depth; var depthVector2 = new Vector2(d2.y, -d2.x) * depth; m0 -= depthVector0; m1 -= depthVector1; m2 -= depthVector2; Vector2 output; var leftShear = Intersect(m1, d1, m0, d0, out output) ? Vector2.Dot(d1, (output - (m1 - halfWidth1))) : 0; var rightShear = Intersect(m1, d1, m2, d2, out output) ? -Vector2.Dot(d1, (output - (m1 + halfWidth1))) : 0; var transform = Matrix4x4.TRS(lineCenter, // move to center of line Quaternion.LookRotation(depthVector, Vector3.up), // rotate to align with line Vector3.one); // set the width to the width of the line definition.stairs.width = maxWidth1; definition.stairs.nosingWidth = 0; var leftSide = (!definition.shape.closed && vi2 == 1) ? definition.stairs.leftSide : StairsSideType.None; var rightSide = (!definition.shape.closed && vi2 == shapeVertices.Count - 1) ? definition.stairs.rightSide : StairsSideType.None; var subMeshCount = BrushMeshFactory.GetLinearStairsSubMeshCount(definition.stairs, leftSide, rightSide); if (subMeshCount == 0) { continue; } if (!BrushMeshFactory.GenerateLinearStairsSubMeshes(ref brushContainer, definition.stairs, leftSide, rightSide, subMeshIndex)) { return(false); } var halfWidth = maxWidth1 * 0.5f; for (int m = 0; m < subMeshCount; m++) { var vertices = brushContainer.brushMeshes[subMeshIndex + m].vertices; for (int v = 0; v < vertices.Length; v++) { // TODO: is it possible to put all of this in a single matrix? // lerp the stairs to go from less wide to wider depending on the depth of the vertex var depthFactor = 1.0f - ((vertices[v].z / definition.stairs.depth) + 0.5f); var wideFactor = (vertices[v].x / halfWidth) + 0.5f; var scale = (vertices[v].x / halfWidth); // lerp the stairs width depending on if it's on the left or right side of the stairs vertices[v].x = Mathf.Lerp(scale * (halfWidth - (rightShear * depthFactor)), scale * (halfWidth - (leftShear * depthFactor)), wideFactor); vertices[v] = transform.MultiplyPoint(vertices[v]); } } subMeshIndex += subMeshCount; } return(false); }
internal static void Interpolate(JToken a, JToken b, float linearT, ref JToken mix, CubicBezier easing) { var easedT = easing.Sample(linearT); // compound types if (a.Type == JTokenType.Object) { JObject A = (JObject)a; JObject B = (JObject)b; JObject Mix = (JObject)mix; if (A.ContainsKey("x") && A.ContainsKey("y")) { if (A.ContainsKey("z")) { // quaternion if (A.ContainsKey("w")) { tempA.Set(A.ForceFloat("x"), A.ForceFloat("y"), A.ForceFloat("z"), A.ForceFloat("w")); tempB.Set(B.ForceFloat("x"), B.ForceFloat("y"), B.ForceFloat("z"), B.ForceFloat("w")); tempMix = Quaternion.Slerp(tempA, tempB, easedT); Mix.SetOrAdd("x", tempMix.x); Mix.SetOrAdd("y", tempMix.y); Mix.SetOrAdd("z", tempMix.z); Mix.SetOrAdd("w", tempMix.w); } // Vector3 else { Mix.SetOrAdd("x", UnityMath.Lerp(A.ForceFloat("x"), B.ForceFloat("x"), easedT)); Mix.SetOrAdd("y", UnityMath.Lerp(A.ForceFloat("y"), B.ForceFloat("y"), easedT)); Mix.SetOrAdd("z", UnityMath.Lerp(A.ForceFloat("z"), B.ForceFloat("z"), easedT)); } } // Vector2 else { Mix.SetOrAdd("x", UnityMath.Lerp(A.ForceFloat("x"), B.ForceFloat("x"), easedT)); Mix.SetOrAdd("y", UnityMath.Lerp(A.ForceFloat("y"), B.ForceFloat("y"), easedT)); } } // TODO: other compound types (color3, color4) } // simple types else { JValue A = (JValue)a; JValue B = (JValue)b; JValue Mix = (JValue)mix; // numeric types if (a.Type == JTokenType.Float || a.Type == JTokenType.Integer) { Mix.Value = UnityMath.Lerp(A.ForceFloat(), B.ForceFloat(), easedT); } // no interpolation available, just use A else { Mix.Value = A.Value; } } }