Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkCurve_DEFINED |
| 9 | #define SkCurve_DEFINED |
| 10 | |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 11 | #include "SkColor.h" |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 12 | #include "SkParticleData.h" |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 13 | #include "SkScalar.h" |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 14 | #include "SkTArray.h" |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 15 | |
| 16 | class SkFieldVisitor; |
| 17 | class SkRandom; |
| 18 | |
Brian Osman | e12e499 | 2019-02-19 15:39:18 -0500 | [diff] [blame] | 19 | /** |
| 20 | * SkCurve implements a keyframed 1D function, useful for animating values over time. This pattern |
| 21 | * is common in digital content creation tools. An SkCurve might represent rotation, scale, opacity, |
| 22 | * or any other scalar quantity. |
| 23 | * |
| 24 | * An SkCurve has a logical domain of [0, 1], and is made of one or more SkCurveSegments. |
| 25 | * Each segment describes the behavior of the curve in some sub-domain. For an SkCurve with N |
| 26 | * segments, there are (N - 1) intermediate x-values that subdivide the domain. The first and last |
| 27 | * x-values are implicitly 0 and 1: |
| 28 | * |
| 29 | * 0 ... x[0] ... x[1] ... ... 1 |
| 30 | * Segment_0 Segment_1 ... Segment_N-1 |
| 31 | * |
| 32 | * Each segment describes a function over [0, 1] - x-values are re-normalized to the segment's |
| 33 | * domain when being evaluated. The segments are cubic polynomials, defined by four values (fMin). |
| 34 | * These are the values at x=0 and x=1, as well as control points at x=1/3 and x=2/3. |
| 35 | * |
| 36 | * For segments with fConstant == true, only the first value is used (fMin[0]). |
| 37 | * |
| 38 | * Each segment has two additional features for creating interesting (and varied) animation: |
| 39 | * - A segment can be ranged. Ranged segments have two sets of coefficients, and a random value |
| 40 | * taken from the SkRandom will be used to lerp betwen them. Typically, the SkRandom passed to |
| 41 | * eval will be in the same state at each call, so this value will be stable. That causes a |
| 42 | * ranged SkCurve to produce a single smooth cubic function somewhere within the range defined |
| 43 | * by fMin and fMax. |
| 44 | * - A segment can be bidirectional. In that case, after a value is computed, it will be negated |
| 45 | * 50% of the time. |
| 46 | */ |
| 47 | |
Brian Osman | 34d1331 | 2019-02-27 11:19:19 -0500 | [diff] [blame] | 48 | enum SkCurveSegmentType { |
| 49 | kConstant_SegmentType, |
| 50 | kLinear_SegmentType, |
| 51 | kCubic_SegmentType, |
| 52 | }; |
| 53 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 54 | struct SkCurveSegment { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 55 | SkScalar eval(SkScalar x, SkScalar t, bool negate) const; |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 56 | void visitFields(SkFieldVisitor* v); |
| 57 | |
| 58 | void setConstant(SkScalar c) { |
Brian Osman | 34d1331 | 2019-02-27 11:19:19 -0500 | [diff] [blame] | 59 | fType = kConstant_SegmentType; |
| 60 | fRanged = false; |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 61 | fMin[0] = c; |
| 62 | } |
| 63 | |
| 64 | SkScalar fMin[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 65 | SkScalar fMax[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 66 | |
Brian Osman | 34d1331 | 2019-02-27 11:19:19 -0500 | [diff] [blame] | 67 | int fType = kConstant_SegmentType; |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 68 | bool fRanged = false; |
| 69 | bool fBidirectional = false; |
| 70 | }; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 71 | |
| 72 | struct SkCurve { |
| 73 | SkCurve(SkScalar c = 0.0f) { |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 74 | fSegments.push_back().setConstant(c); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 77 | SkScalar eval(const SkParticleUpdateParams& params, SkParticleState& ps) const; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 78 | void visitFields(SkFieldVisitor* v); |
Brian Osman | e12e499 | 2019-02-19 15:39:18 -0500 | [diff] [blame] | 79 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 80 | // Parameters that determine our x-value during evaluation |
| 81 | SkParticleValue fInput; |
| 82 | |
Brian Osman | e12e499 | 2019-02-19 15:39:18 -0500 | [diff] [blame] | 83 | // It should always be true that (fXValues.count() + 1) == fSegments.count() |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 84 | SkTArray<SkScalar, true> fXValues; |
| 85 | SkTArray<SkCurveSegment, true> fSegments; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 86 | }; |
| 87 | |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 88 | /** |
| 89 | * SkColorCurve is similar to SkCurve, but keyframes 4D values - specifically colors. Because |
| 90 | * negative colors rarely make sense, SkColorCurves do not support bidirectional segments, but |
| 91 | * support all other features (including cubic interpolation). |
| 92 | */ |
| 93 | |
| 94 | struct SkColorCurveSegment { |
| 95 | SkColorCurveSegment() { |
| 96 | for (int i = 0; i < 4; ++i) { |
| 97 | fMin[i] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 98 | fMax[i] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 99 | } |
| 100 | } |
| 101 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 102 | SkColor4f eval(SkScalar x, SkScalar t) const; |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 103 | void visitFields(SkFieldVisitor* v); |
| 104 | |
| 105 | void setConstant(SkColor4f c) { |
Brian Osman | 34d1331 | 2019-02-27 11:19:19 -0500 | [diff] [blame] | 106 | fType = kConstant_SegmentType; |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 107 | fRanged = false; |
| 108 | fMin[0] = c; |
| 109 | } |
| 110 | |
| 111 | SkColor4f fMin[4]; |
| 112 | SkColor4f fMax[4]; |
| 113 | |
Brian Osman | 34d1331 | 2019-02-27 11:19:19 -0500 | [diff] [blame] | 114 | int fType = kConstant_SegmentType; |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 115 | bool fRanged = false; |
| 116 | }; |
| 117 | |
| 118 | struct SkColorCurve { |
| 119 | SkColorCurve(SkColor4f c = { 1.0f, 1.0f, 1.0f, 1.0f }) { |
| 120 | fSegments.push_back().setConstant(c); |
| 121 | } |
| 122 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 123 | SkColor4f eval(const SkParticleUpdateParams& params, SkParticleState& ps) const; |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 124 | void visitFields(SkFieldVisitor* v); |
| 125 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 126 | SkParticleValue fInput; |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 127 | SkTArray<SkScalar, true> fXValues; |
| 128 | SkTArray<SkColorCurveSegment, true> fSegments; |
| 129 | }; |
| 130 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 131 | #endif // SkCurve_DEFINED |