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 | |
| 8 | #include "SampleCode.h" |
| 9 | #include "SkAnimTimer.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkPatchUtils.h" |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 13 | #include "SkPerlinNoiseShader2/SkPerlinNoiseShader2.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]; |
| 19 | SkPatchUtils::getBottomCubic(cubics, bottom); |
| 20 | SkPoint top[SkPatchUtils::kNumPtsCubic]; |
| 21 | SkPatchUtils::getTopCubic(cubics, top); |
| 22 | SkPoint left[SkPatchUtils::kNumPtsCubic]; |
| 23 | SkPatchUtils::getLeftCubic(cubics, left); |
| 24 | SkPoint right[SkPatchUtils::kNumPtsCubic]; |
| 25 | SkPatchUtils::getRightCubic(cubics, right); |
| 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 | |
| 62 | class PerlinPatchView : public SampleView { |
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; |
| 74 | public: |
| 75 | PerlinPatchView() : fXFreq(0.025f), fYFreq(0.025f), fSeed(0.0f), |
| 76 | fTexX(100.0), fTexY(50.0), fTexScale(1.0f) { |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 77 | const SkScalar s = 2; |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 78 | // The order of the colors and points is clockwise starting at upper-left corner. |
| 79 | //top points |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 80 | fPts[0].set(100 * s, 100 * s); |
| 81 | fPts[1].set(150 * s, 50 * s); |
| 82 | fPts[2].set(250 * s, 150 * s); |
| 83 | fPts[3].set(300 * s, 100 * s); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 84 | //right points |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 85 | fPts[4].set(275 * s, 150 * s); |
| 86 | fPts[5].set(350 * s, 250 * s); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 87 | //bottom points |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 88 | fPts[6].set(300 * s, 300 * s); |
| 89 | fPts[7].set(250 * s, 250 * s); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 90 | //left points |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 91 | fPts[8].set(150 * s, 350 * s); |
| 92 | fPts[9].set(100 * s, 300 * s); |
| 93 | fPts[10].set(50 * s, 250 * s); |
| 94 | fPts[11].set(150 * s, 150 * s); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 95 | |
| 96 | const SkColor colors[SkPatchUtils::kNumCorners] = { |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 97 | 0xFF5555FF, 0xFF8888FF, 0xFFCCCCFF |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 98 | }; |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 99 | const SkPoint points[2] = { SkPoint::Make(0.0f, 0.0f), |
| 100 | SkPoint::Make(100.0f, 100.0f) }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 101 | fShader0 = SkGradientShader::MakeLinear(points, |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 102 | colors, |
| 103 | NULL, |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 104 | 3, |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 105 | SkShader::kMirror_TileMode, |
| 106 | 0, |
| 107 | NULL); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 108 | } |
| 109 | |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 110 | protected: |
| 111 | // overrides from SkEventSink |
| 112 | bool onQuery(SkEvent* evt) override { |
| 113 | if (SampleCode::TitleQ(*evt)) { |
| 114 | SampleCode::TitleR(evt, "PerlinPatch"); |
| 115 | return true; |
| 116 | } |
| 117 | return this->INHERITED::onQuery(evt); |
| 118 | } |
| 119 | |
| 120 | bool onAnimate(const SkAnimTimer& timer) override { |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 121 | fSeed += 0.005f; |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | void onDrawContent(SkCanvas* canvas) override { |
| 127 | if (!canvas->getTotalMatrix().invert(&fInvMatrix)) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | SkPaint paint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 132 | |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 133 | SkScalar texWidth = fTexScale * TexWidth; |
| 134 | SkScalar texHeight = fTexScale * TexHeight; |
| 135 | const SkPoint texCoords[SkPatchUtils::kNumCorners] = { |
| 136 | { fTexX - texWidth, fTexY - texHeight}, |
| 137 | { fTexX + texWidth, fTexY - texHeight}, |
| 138 | { fTexX + texWidth, fTexY + texHeight}, |
| 139 | { fTexX - texWidth, fTexY + texHeight}} |
| 140 | ; |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 141 | |
| 142 | sk_sp<SkXfermode> xfer(SkXfermode::Make(SkXfermode::kSrc_Mode)); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 143 | |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 144 | SkScalar scaleFreq = 2.0; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 145 | fShader1 = SkPerlinNoiseShader2::MakeImprovedNoise(fXFreq/scaleFreq, fYFreq/scaleFreq, 4, |
egdaniel | 478c04e | 2015-11-09 07:40:49 -0800 | [diff] [blame] | 146 | fSeed); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 147 | fShaderCompose = SkShader::MakeComposeShader(fShader0, fShader1, nullptr); |
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); |
| 150 | canvas->drawPatch(fPts, nullptr, texCoords, xfer, paint); |
| 151 | |
| 152 | draw_control_points(canvas, fPts); |
egdaniel | fca294b | 2015-11-05 13:09:08 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | class PtClick : public Click { |
| 156 | public: |
| 157 | int fIndex; |
| 158 | PtClick(SkView* view, int index) : Click(view), fIndex(index) {} |
| 159 | }; |
| 160 | |
| 161 | static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) { |
| 162 | return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5); |
| 163 | } |
| 164 | |
| 165 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
| 166 | // holding down shift |
| 167 | if (1 == modi) { |
| 168 | return new PtClick(this, -1); |
| 169 | } |
| 170 | // holding down ctrl |
| 171 | if (2 == modi) { |
| 172 | return new PtClick(this, -2); |
| 173 | } |
| 174 | SkPoint clickPoint = {x, y}; |
| 175 | fInvMatrix.mapPoints(&clickPoint, 1); |
| 176 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) { |
| 177 | if (hittest(fPts[i], clickPoint.fX, clickPoint.fY)) { |
| 178 | return new PtClick(this, (int)i); |
| 179 | } |
| 180 | } |
| 181 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 182 | } |
| 183 | |
| 184 | bool onClick(Click* click) override { |
| 185 | PtClick* ptClick = (PtClick*)click; |
| 186 | if (ptClick->fIndex >= 0) { |
| 187 | fPts[ptClick->fIndex].set(click->fCurr.fX , click->fCurr.fY ); |
| 188 | } else if (-1 == ptClick->fIndex) { |
| 189 | SkScalar xDiff = click->fPrev.fX - click->fCurr.fX; |
| 190 | SkScalar yDiff = click->fPrev.fY - click->fCurr.fY; |
| 191 | fTexX += xDiff * fTexScale; |
| 192 | fTexY += yDiff * fTexScale; |
| 193 | } else if (-2 == ptClick->fIndex) { |
| 194 | SkScalar yDiff = click->fCurr.fY - click->fPrev.fY; |
| 195 | fTexScale += yDiff / 10.0f; |
| 196 | fTexScale = SkTMax(0.1f, SkTMin(20.f, fTexScale)); |
| 197 | } |
| 198 | this->inval(nullptr); |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | private: |
| 203 | typedef SampleView INHERITED; |
| 204 | }; |
| 205 | |
| 206 | DEF_SAMPLE( return new PerlinPatchView(); ) |