blob: d659914f2b49ad49dd25e38fe0705600772f5b03 [file] [log] [blame]
egdanielfca294b2015-11-05 13:09:08 -08001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Canaryb1f411a2019-08-29 10:39:22 -040013#include "tools/skui/ModifierKey.h"
egdanielfca294b2015-11-05 13:09:08 -080014
15static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
16 //draw control points
17 SkPaint paint;
18 SkPoint bottom[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040019 SkPatchUtils::GetBottomCubic(cubics, bottom);
egdanielfca294b2015-11-05 13:09:08 -080020 SkPoint top[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040021 SkPatchUtils::GetTopCubic(cubics, top);
egdanielfca294b2015-11-05 13:09:08 -080022 SkPoint left[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040023 SkPatchUtils::GetLeftCubic(cubics, left);
egdanielfca294b2015-11-05 13:09:08 -080024 SkPoint right[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040025 SkPatchUtils::GetRightCubic(cubics, right);
egdanielfca294b2015-11-05 13:09:08 -080026
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
59const SkScalar TexWidth = 100.0f;
60const SkScalar TexHeight = 100.0f;
61
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040062class PerlinPatchView : public Sample {
reed8a21c9f2016-03-08 18:50:00 -080063 sk_sp<SkShader> fShader0;
64 sk_sp<SkShader> fShader1;
65 sk_sp<SkShader> fShaderCompose;
egdanielfca294b2015-11-05 13:09:08 -080066 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;
reedc0c6e1c2016-08-22 08:45:15 -070074 bool fShowGrid = false;
75
egdanielfca294b2015-11-05 13:09:08 -080076public:
77 PerlinPatchView() : fXFreq(0.025f), fYFreq(0.025f), fSeed(0.0f),
78 fTexX(100.0), fTexY(50.0), fTexScale(1.0f) {
reedb2ce0b02015-11-09 13:10:30 -080079 const SkScalar s = 2;
egdanielfca294b2015-11-05 13:09:08 -080080 // The order of the colors and points is clockwise starting at upper-left corner.
81 //top points
reedb2ce0b02015-11-09 13:10:30 -080082 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);
egdanielfca294b2015-11-05 13:09:08 -080086 //right points
reedb2ce0b02015-11-09 13:10:30 -080087 fPts[4].set(275 * s, 150 * s);
88 fPts[5].set(350 * s, 250 * s);
egdanielfca294b2015-11-05 13:09:08 -080089 //bottom points
reedb2ce0b02015-11-09 13:10:30 -080090 fPts[6].set(300 * s, 300 * s);
91 fPts[7].set(250 * s, 250 * s);
egdanielfca294b2015-11-05 13:09:08 -080092 //left points
reedb2ce0b02015-11-09 13:10:30 -080093 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);
egdanielfca294b2015-11-05 13:09:08 -080097
98 const SkColor colors[SkPatchUtils::kNumCorners] = {
egdaniel478c04e2015-11-09 07:40:49 -080099 0xFF5555FF, 0xFF8888FF, 0xFFCCCCFF
egdanielfca294b2015-11-05 13:09:08 -0800100 };
egdaniel478c04e2015-11-09 07:40:49 -0800101 const SkPoint points[2] = { SkPoint::Make(0.0f, 0.0f),
102 SkPoint::Make(100.0f, 100.0f) };
reed8a21c9f2016-03-08 18:50:00 -0800103 fShader0 = SkGradientShader::MakeLinear(points,
egdanielfca294b2015-11-05 13:09:08 -0800104 colors,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400105 nullptr,
egdaniel478c04e2015-11-09 07:40:49 -0800106 3,
Mike Reedfae8fce2019-04-03 10:27:45 -0400107 SkTileMode::kMirror,
egdanielfca294b2015-11-05 13:09:08 -0800108 0,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400109 nullptr);
egdanielfca294b2015-11-05 13:09:08 -0800110 }
111
egdanielfca294b2015-11-05 13:09:08 -0800112protected:
Hal Canary8a027312019-07-03 10:55:44 -0400113 SkString name() override { return SkString("PerlinPatch"); }
114
Hal Canary6cc65e12019-07-03 15:53:04 -0400115 bool onChar(SkUnichar uni) override {
reedc0c6e1c2016-08-22 08:45:15 -0700116 switch (uni) {
Brian Osmanede860e2017-11-22 16:36:07 -0500117 case 'g': fShowGrid = !fShowGrid; return true;
reedc0c6e1c2016-08-22 08:45:15 -0700118 default: break;
119 }
Hal Canary6cc65e12019-07-03 15:53:04 -0400120 return false;
egdanielfca294b2015-11-05 13:09:08 -0800121 }
122
Hal Canary41248072019-07-11 16:32:53 -0400123 bool onAnimate(double nanos) override {
egdaniel478c04e2015-11-09 07:40:49 -0800124 fSeed += 0.005f;
egdanielfca294b2015-11-05 13:09:08 -0800125 return true;
126 }
127
egdanielfca294b2015-11-05 13:09:08 -0800128 void onDrawContent(SkCanvas* canvas) override {
129 if (!canvas->getTotalMatrix().invert(&fInvMatrix)) {
130 return;
131 }
132
133 SkPaint paint;
halcanary9d524f22016-03-29 09:03:52 -0700134
egdanielfca294b2015-11-05 13:09:08 -0800135 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 Wagner63fd7602017-10-09 15:45:33 -0400143
egdaniel478c04e2015-11-09 07:40:49 -0800144 SkScalar scaleFreq = 2.0;
Mike Reedf2ae2b22017-05-30 15:22:54 -0400145 fShader1 = SkPerlinNoiseShader::MakeImprovedNoise(fXFreq/scaleFreq, fYFreq/scaleFreq, 4,
egdaniel478c04e2015-11-09 07:40:49 -0800146 fSeed);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400147 fShaderCompose = SkShaders::Blend(SkBlendMode::kSrcOver, fShader0, fShader1);
egdaniel478c04e2015-11-09 07:40:49 -0800148
egdanielfca294b2015-11-05 13:09:08 -0800149 paint.setShader(fShaderCompose);
reedc0c6e1c2016-08-22 08:45:15 -0700150
151 const SkPoint* tex = texCoords;
152 if (fShowGrid) {
153 tex = nullptr;
154 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400155 canvas->drawPatch(fPts, nullptr, tex, SkBlendMode::kSrc, paint);
egdanielfca294b2015-11-05 13:09:08 -0800156
157 draw_control_points(canvas, fPts);
egdanielfca294b2015-11-05 13:09:08 -0800158 }
159
160 class PtClick : public Click {
161 public:
162 int fIndex;
Hal Canaryfcf63592019-07-12 11:32:43 -0400163 PtClick(int index) : fIndex(index) {}
egdanielfca294b2015-11-05 13:09:08 -0800164 };
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 Canaryb1f411a2019-08-29 10:39:22 -0400170 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 Canaryfcf63592019-07-12 11:32:43 -0400173 return new PtClick(-1);
egdanielfca294b2015-11-05 13:09:08 -0800174 }
Hal Canaryb1f411a2019-08-29 10:39:22 -0400175 if (skui::ModifierKey::kControl == modi) {
Hal Canaryfcf63592019-07-12 11:32:43 -0400176 return new PtClick(-2);
egdanielfca294b2015-11-05 13:09:08 -0800177 }
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 Canaryfcf63592019-07-12 11:32:43 -0400182 return new PtClick((int)i);
egdanielfca294b2015-11-05 13:09:08 -0800183 }
184 }
Hal Canaryfcf63592019-07-12 11:32:43 -0400185 return nullptr;
egdanielfca294b2015-11-05 13:09:08 -0800186 }
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 Osman788b9162020-02-07 10:36:46 -0500200 fTexScale = std::max(0.1f, std::min(20.f, fTexScale));
egdanielfca294b2015-11-05 13:09:08 -0800201 }
egdanielfca294b2015-11-05 13:09:08 -0800202 return true;
203 }
204
205private:
John Stiles7571f9e2020-09-02 22:42:33 -0400206 using INHERITED = Sample;
egdanielfca294b2015-11-05 13:09:08 -0800207};
208
209DEF_SAMPLE( return new PerlinPatchView(); )