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