public static Keyframe[] ToKeyframeArray(SerializableKeyframe[] sks) { Keyframe[] keys = new Keyframe[sks.Length]; for(int i = 0; i < keys.Length; i++) keys[i] = sks[i].ToKeyframe(); return keys; }
public static SerializableKeyframe[] FromKeyframeArray(Keyframe[] ks) { SerializableKeyframe[] keys = new SerializableKeyframe[ks.Length]; for(int i = 0; i < keys.Length; i++) keys[i] = new SerializableKeyframe(ks[i]); return keys; }
public static SerializableKeyframe[] FromKeyframeArray(Keyframe[] ks) { SerializableKeyframe[] keys = new SerializableKeyframe[ks.Length]; for (int i = 0; i < keys.Length; i++) { keys[i] = new SerializableKeyframe(ks[i]); } return(keys); }
public OperatorNode(int x, int y) : base("Operator", new SerializableRect(x, y, 200, 150)) { type = lastType = OperatorNode.OPERATORTYPE.ABS; SetInputs(); curve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f); if (keyframes != null) { curve.keys = SerializableKeyframe.ToKeyframeArray(keyframes); } }
public static float Evaluate(float time, SerializableKeyframe[] keyframes) { if (keyframes == null) { return(-1f); } SerializableKeyframe key1 = keyframes[0]; SerializableKeyframe key2 = keyframes[keyframes.Length - 1]; float closestDistance = Mathf.Infinity; int closestIndex = keyframes.Length; for (int i = 0; i < keyframes.Length; i++) { if (time >= keyframes[i].time) { float distance = time - keyframes[i].time; if (distance <= closestDistance) { closestDistance = distance; key1 = keyframes[i]; closestIndex = i; } } else { break; } } if (closestIndex + 1 >= keyframes.Length) { return(key2.value); } else { key2 = keyframes[closestIndex + 1]; } float dt = (time - key1.time) / (key2.time - key1.time); //float tanLength = Mathf.Abs(key1.time - key2.time) * 0.3333f; //float p1 = key1.value; //float p2 = key2.value; //float t1 = p1 + tanLength * key1.outTangent; //float t2 = p2 - tanLength * key2.inTangent; //float value = CubicSpline(dt, p1, t1, t2, p2); float value = Bezier(dt, key1, key2); return(value); }
public static float Bezier(float t, SerializableKeyframe key1, SerializableKeyframe key2) { float dt = key2.time - key1.time; float m0 = key1.outTangent * dt; float m1 = key2.inTangent * dt; float t2 = t * t; float t3 = t2 * t; float a = 2 * t3 - 3 * t2 + 1; float b = t3 - 2 * t2 + t; float c = t3 - t2; float d = -2 * t3 + 3 * t2; return(a * key1.value + b * m0 + c * m1 + d * key2.value); }
public static float Bezier(float t, SerializableKeyframe key1, SerializableKeyframe key2) { float dt = key2.time - key1.time; float m0 = key1.outTangent * dt; float m1 = key2.inTangent * dt; float t2 = t * t; float t3 = t2 * t; float a = 2 * t3 - 3 * t2 + 1; float b = t3 - 2 * t2 + t; float c = t3 - t2; float d = -2 * t3 + 3 * t2; return a * key1.value + b * m0 + c * m1 + d * key2.value; }
public static float Evaluate(float time, SerializableKeyframe[] keyframes) { if(keyframes == null) return -1f; SerializableKeyframe key1 = keyframes[0]; SerializableKeyframe key2 = keyframes[keyframes.Length-1]; float closestDistance = Mathf.Infinity; int closestIndex = keyframes.Length; for(int i = 0; i < keyframes.Length; i++) { if(time >= keyframes[i].time) { float distance = time - keyframes[i].time; if(distance <= closestDistance) { closestDistance = distance; key1 = keyframes[i]; closestIndex = i; } } else break; } if(closestIndex+1 >= keyframes.Length) return key2.value; else { key2 = keyframes[closestIndex+1]; } float dt = (time - key1.time) / (key2.time - key1.time); //float tanLength = Mathf.Abs(key1.time - key2.time) * 0.3333f; //float p1 = key1.value; //float p2 = key2.value; //float t1 = p1 + tanLength * key1.outTangent; //float t2 = p2 - tanLength * key2.inTangent; //float value = CubicSpline(dt, p1, t1, t2, p2); float value = Bezier(dt, key1, key2); return value; }
override public ModuleBase GetModule() { // check that has inputs for (int i = 0; i < inputs.Length; i++) { if (inputs[i] == null) { return(null); } } // get module switch (type) { case OPERATORTYPE.ABS: module = new Abs(inputs[0].GetModule()); break; case OPERATORTYPE.ADD: module = new Add(inputs[0].GetModule(), inputs[1].GetModule()); break; case OPERATORTYPE.BLEND: module = new Blend(inputs[0].GetModule(), inputs[1].GetModule(), inputs[2].GetModule()); break; case OPERATORTYPE.CLAMP: module = new Clamp(min, max, inputs[0].GetModule()); break; case OPERATORTYPE.EXPONENT: module = new Exponent(exponent, inputs[0].GetModule()); break; case OPERATORTYPE.INVERT: module = new Invert(inputs[0].GetModule()); break; case OPERATORTYPE.MAX: module = new Max(inputs[0].GetModule(), inputs[1].GetModule()); break; case OPERATORTYPE.MIN: module = new Min(inputs[0].GetModule(), inputs[1].GetModule()); break; case OPERATORTYPE.MULTIPLY: module = new Multiply(inputs[0].GetModule(), inputs[1].GetModule()); break; case OPERATORTYPE.POWER: module = new Power(inputs[0].GetModule(), inputs[1].GetModule()); break; case OPERATORTYPE.SUBTRACT: module = new Subtract(inputs[0].GetModule(), inputs[1].GetModule()); break; case OPERATORTYPE.TERRACE: module = new Terrace(min, max, power, inputs[0].GetModule()); break; case OPERATORTYPE.TRANSLATE: module = new Translate(x, y, z, inputs[0].GetModule()); break; case OPERATORTYPE.DIVIDE: module = new Divide(inputs[0].GetModule(), inputs[1].GetModule()); break; case OPERATORTYPE.CURVE: if (curve == null) { curve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f); if (keyframes != null) { curve.keys = SerializableKeyframe.ToKeyframeArray(keyframes); } } if (keyframes == null) { keyframes = SerializableKeyframe.FromKeyframeArray(curve.keys); } module = new Curve(keyframes, inputs[0].GetModule()); break; case OPERATORTYPE.WEIGHT: module = new Weight(inputs[0].GetModule(), min, max); break; case OPERATORTYPE.WARP: module = new Warp(power, inputs[0].GetModule(), inputs[1].GetModule()); break; case OPERATORTYPE.SELECT: module = new Select(inputs[0].GetModule(), min, max); break; } SetOutputOptions(); return(this.module); }
public Curve(SerializableKeyframe[] keyframes, ModuleBase m1) { this.keyframes = keyframes; module1 = m1; }
private void OperatorGUI() { // operator type selection operatorNode.type = (OperatorNode.OPERATORTYPE)EditorGUILayout.EnumPopup(operatorNode.type); if (operatorNode.type != operatorNode.lastType) { operatorNode.SetInputs(); UpdatePreview(); operatorNode.lastType = operatorNode.type; node.title = operatorNode.type.ToString(); } node.rect.height += controlHeight * 1.5f; // parameters showParameters = EditorGUILayout.Foldout(showParameters, "Parameters"); if (showParameters) { // min max if (operatorNode.type == OperatorNode.OPERATORTYPE.CLAMP) { GUILayout.Label("Min:"); operatorNode.min = EditorGUILayout.Slider(operatorNode.min, -1.0f, 1.0f); GUILayout.Label("Max:"); operatorNode.max = EditorGUILayout.Slider(operatorNode.max, -1.0f, 1.0f); node.rect.height += controlHeight * 2.5f; } if (operatorNode.type == OperatorNode.OPERATORTYPE.TERRACE) { GUILayout.Label("Min:"); operatorNode.min = EditorGUILayout.Slider(operatorNode.min, -1.0f, 1.0f); GUILayout.Label("Max:"); operatorNode.max = EditorGUILayout.Slider(operatorNode.max, -1.0f, 1.0f); GUILayout.Label("Power:"); operatorNode.power = EditorGUILayout.Slider(operatorNode.power, 0f, 1.0f); node.rect.height += controlHeight * 4f; } // exponent if (operatorNode.type == OperatorNode.OPERATORTYPE.EXPONENT) { GUILayout.Label("Exponent:"); operatorNode.exponent = EditorGUILayout.Slider(operatorNode.exponent, -10.0f, 10f); node.rect.height += controlHeight; } // x, y, z if (operatorNode.type == OperatorNode.OPERATORTYPE.TRANSLATE) { GUILayout.Label("X:"); operatorNode.x = EditorGUILayout.Slider(operatorNode.x, -10.0f, 10.0f); GUILayout.Label("Y:"); operatorNode.y = EditorGUILayout.Slider(operatorNode.y, -10.0f, 10.0f); GUILayout.Label("Z:"); operatorNode.z = EditorGUILayout.Slider(operatorNode.z, -10.0f, 10.0f); node.rect.height += controlHeight * 4f; } // curve if (operatorNode.type == OperatorNode.OPERATORTYPE.CURVE) { if (operatorNode.curve == null) { if (operatorNode.keyframes != null) { operatorNode.curve = new AnimationCurve(SerializableKeyframe.ToKeyframeArray(operatorNode.keyframes)); } else { operatorNode.curve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f); } } try { operatorNode.curve = EditorGUILayout.CurveField(operatorNode.curve, GUILayout.Width(40), GUILayout.Height(30)); } catch {} operatorNode.keyframes = SerializableKeyframe.FromKeyframeArray(operatorNode.curve.keys); node.rect.height += controlHeight; } // weight if (operatorNode.type == OperatorNode.OPERATORTYPE.WEIGHT) { GUILayout.Label("Weight:"); operatorNode.min = EditorGUILayout.Slider(operatorNode.min, 0f, 1f); GUILayout.Label("Target:"); operatorNode.max = EditorGUILayout.Slider(operatorNode.max, -1f, 1f); node.rect.height += controlHeight * 2.5f; } // exponent if (operatorNode.type == OperatorNode.OPERATORTYPE.WARP) { GUILayout.Label("Power:"); operatorNode.power = EditorGUILayout.Slider(operatorNode.power, -.5f, .5f); node.rect.height += controlHeight; } if (operatorNode.type == OperatorNode.OPERATORTYPE.SELECT) { GUILayout.Label("Target:"); operatorNode.min = EditorGUILayout.Slider(operatorNode.min, -2.0f, 2.0f); GUILayout.Label("Range:"); operatorNode.max = EditorGUILayout.Slider(operatorNode.max, 0f, 2.0f); node.rect.height += controlHeight * 2.5f; } } }