egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/effects/SkGradientShader.h" |
| 10 | #include "include/effects/SkPerlinNoiseShader.h" |
| 11 | #include "samplecode/Sample.h" |
| 12 | #include "src/utils/SkPatchUtils.h" |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 13 | #include "tools/skui/ModifierKey.h" |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 14 | |
| 15 | static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) { |
| 16 | //draw control points |
| 17 | SkPaint paint; |
| 18 | SkPoint bottom[SkPatchUtils::kNumPtsCubic]; |
Mike Reed | 4ebb43e | 2017-04-05 11:06:15 -0400 | [diff] [blame] | 19 | SkPatchUtils::GetBottomCubic(cubics, bottom); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 20 | SkPoint top[SkPatchUtils::kNumPtsCubic]; |
Mike Reed | 4ebb43e | 2017-04-05 11:06:15 -0400 | [diff] [blame] | 21 | SkPatchUtils::GetTopCubic(cubics, top); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 22 | SkPoint left[SkPatchUtils::kNumPtsCubic]; |
Mike Reed | 4ebb43e | 2017-04-05 11:06:15 -0400 | [diff] [blame] | 23 | SkPatchUtils::GetLeftCubic(cubics, left); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 24 | SkPoint right[SkPatchUtils::kNumPtsCubic]; |
Mike Reed | 4ebb43e | 2017-04-05 11:06:15 -0400 | [diff] [blame] | 25 | SkPatchUtils::GetRightCubic(cubics, right); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 26 | |
| 27 | paint.setColor(SK_ColorBLACK); |
| 28 | paint.setStrokeWidth(0.5f); |
| 29 | SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] }; |
| 30 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint); |
| 31 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint); |
| 32 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint); |
| 33 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint); |
| 34 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint); |
| 35 | |
| 36 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint); |
| 37 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint); |
| 38 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint); |
| 39 | |
| 40 | paint.setStrokeWidth(2); |
| 41 | |
| 42 | paint.setColor(SK_ColorRED); |
| 43 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint); |
| 44 | |
| 45 | paint.setColor(SK_ColorBLUE); |
| 46 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint); |
| 47 | |
| 48 | paint.setColor(SK_ColorCYAN); |
| 49 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint); |
| 50 | |
| 51 | paint.setColor(SK_ColorYELLOW); |
| 52 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint); |
| 53 | |
| 54 | paint.setColor(SK_ColorGREEN); |
| 55 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint); |
| 56 | } |
| 57 | |
| 58 | // These are actually half the total width and hieghts |
| 59 | const SkScalar TexWidth = 100.0f; |
| 60 | const SkScalar TexHeight = 100.0f; |
| 61 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 62 | class PerlinPatchView : public Sample { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 63 | sk_sp<SkShader> fShader0; |
| 64 | sk_sp<SkShader> fShader1; |
| 65 | sk_sp<SkShader> fShaderCompose; |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 66 | SkScalar fXFreq; |
| 67 | SkScalar fYFreq; |
| 68 | SkScalar fSeed; |
| 69 | SkPoint fPts[SkPatchUtils::kNumCtrlPts]; |
| 70 | SkScalar fTexX; |
| 71 | SkScalar fTexY; |
| 72 | SkScalar fTexScale; |
| 73 | SkMatrix fInvMatrix; |
reed | c0c6e1c | 2016-08-22 08:45:15 -0700 | [diff] [blame] | 74 | bool fShowGrid = false; |
| 75 | |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 76 | public: |
| 77 | PerlinPatchView() : fXFreq(0.025f), fYFreq(0.025f), fSeed(0.0f), |
| 78 | fTexX(100.0), fTexY(50.0), fTexScale(1.0f) { |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 79 | const SkScalar s = 2; |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 80 | // The order of the colors and points is clockwise starting at upper-left corner. |
| 81 | //top points |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 82 | fPts[0].set(100 * s, 100 * s); |
| 83 | fPts[1].set(150 * s, 50 * s); |
| 84 | fPts[2].set(250 * s, 150 * s); |
| 85 | fPts[3].set(300 * s, 100 * s); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 86 | //right points |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 87 | fPts[4].set(275 * s, 150 * s); |
| 88 | fPts[5].set(350 * s, 250 * s); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 89 | //bottom points |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 90 | fPts[6].set(300 * s, 300 * s); |
| 91 | fPts[7].set(250 * s, 250 * s); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 92 | //left points |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 93 | fPts[8].set(150 * s, 350 * s); |
| 94 | fPts[9].set(100 * s, 300 * s); |
| 95 | fPts[10].set(50 * s, 250 * s); |
| 96 | fPts[11].set(150 * s, 150 * s); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 97 | |
| 98 | const SkColor colors[SkPatchUtils::kNumCorners] = { |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 99 | 0xFF5555FF, 0xFF8888FF, 0xFFCCCCFF |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 100 | }; |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 101 | const SkPoint points[2] = { SkPoint::Make(0.0f, 0.0f), |
| 102 | SkPoint::Make(100.0f, 100.0f) }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 103 | fShader0 = SkGradientShader::MakeLinear(points, |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 104 | colors, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 105 | nullptr, |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 106 | 3, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 107 | SkTileMode::kMirror, |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 108 | 0, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 109 | nullptr); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 110 | } |
| 111 | |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 112 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 113 | SkString name() override { return SkString("PerlinPatch"); } |
| 114 | |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 115 | bool onChar(SkUnichar uni) override { |
reed | c0c6e1c | 2016-08-22 08:45:15 -0700 | [diff] [blame] | 116 | switch (uni) { |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 117 | case 'g': fShowGrid = !fShowGrid; return true; |
reed | c0c6e1c | 2016-08-22 08:45:15 -0700 | [diff] [blame] | 118 | default: break; |
| 119 | } |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 120 | return false; |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 123 | bool onAnimate(double nanos) override { |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 124 | fSeed += 0.005f; |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 128 | void onDrawContent(SkCanvas* canvas) override { |
| 129 | if (!canvas->getTotalMatrix().invert(&fInvMatrix)) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | SkPaint paint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 134 | |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 135 | SkScalar texWidth = fTexScale * TexWidth; |
| 136 | SkScalar texHeight = fTexScale * TexHeight; |
| 137 | const SkPoint texCoords[SkPatchUtils::kNumCorners] = { |
| 138 | { fTexX - texWidth, fTexY - texHeight}, |
| 139 | { fTexX + texWidth, fTexY - texHeight}, |
| 140 | { fTexX + texWidth, fTexY + texHeight}, |
| 141 | { fTexX - texWidth, fTexY + texHeight}} |
| 142 | ; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 143 | |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 144 | SkScalar scaleFreq = 2.0; |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 145 | fShader1 = SkPerlinNoiseShader::MakeImprovedNoise(fXFreq/scaleFreq, fYFreq/scaleFreq, 4, |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 146 | fSeed); |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 147 | fShaderCompose = SkShaders::Blend(SkBlendMode::kSrcOver, fShader0, fShader1); |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 148 | |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 149 | paint.setShader(fShaderCompose); |
reed | c0c6e1c | 2016-08-22 08:45:15 -0700 | [diff] [blame] | 150 | |
| 151 | const SkPoint* tex = texCoords; |
| 152 | if (fShowGrid) { |
| 153 | tex = nullptr; |
| 154 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 155 | canvas->drawPatch(fPts, nullptr, tex, SkBlendMode::kSrc, paint); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 156 | |
| 157 | draw_control_points(canvas, fPts); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | class PtClick : public Click { |
| 161 | public: |
| 162 | int fIndex; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 163 | PtClick(int index) : fIndex(index) {} |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) { |
| 167 | return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5); |
| 168 | } |
| 169 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 170 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
| 171 | modi &= ~skui::ModifierKey::kFirstPress; // ignore this |
| 172 | if (skui::ModifierKey::kShift == modi) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 173 | return new PtClick(-1); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 174 | } |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 175 | if (skui::ModifierKey::kControl == modi) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 176 | return new PtClick(-2); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 177 | } |
| 178 | SkPoint clickPoint = {x, y}; |
| 179 | fInvMatrix.mapPoints(&clickPoint, 1); |
| 180 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) { |
| 181 | if (hittest(fPts[i], clickPoint.fX, clickPoint.fY)) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 182 | return new PtClick((int)i); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 185 | return nullptr; |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | bool onClick(Click* click) override { |
| 189 | PtClick* ptClick = (PtClick*)click; |
| 190 | if (ptClick->fIndex >= 0) { |
| 191 | fPts[ptClick->fIndex].set(click->fCurr.fX , click->fCurr.fY ); |
| 192 | } else if (-1 == ptClick->fIndex) { |
| 193 | SkScalar xDiff = click->fPrev.fX - click->fCurr.fX; |
| 194 | SkScalar yDiff = click->fPrev.fY - click->fCurr.fY; |
| 195 | fTexX += xDiff * fTexScale; |
| 196 | fTexY += yDiff * fTexScale; |
| 197 | } else if (-2 == ptClick->fIndex) { |
| 198 | SkScalar yDiff = click->fCurr.fY - click->fPrev.fY; |
| 199 | fTexScale += yDiff / 10.0f; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 200 | fTexScale = std::max(0.1f, std::min(20.f, fTexScale)); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 201 | } |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | |
| 205 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 206 | using INHERITED = Sample; |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | DEF_SAMPLE( return new PerlinPatchView(); ) |