2015-11-05 21:09:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/effects/SkPerlinNoiseShader.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkPatchUtils.h"
|
2019-08-29 14:39:22 +00:00
|
|
|
#include "tools/skui/ModifierKey.h"
|
2015-11-05 21:09:08 +00:00
|
|
|
|
|
|
|
static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
|
|
|
|
//draw control points
|
|
|
|
SkPaint paint;
|
|
|
|
SkPoint bottom[SkPatchUtils::kNumPtsCubic];
|
2017-04-05 15:06:15 +00:00
|
|
|
SkPatchUtils::GetBottomCubic(cubics, bottom);
|
2015-11-05 21:09:08 +00:00
|
|
|
SkPoint top[SkPatchUtils::kNumPtsCubic];
|
2017-04-05 15:06:15 +00:00
|
|
|
SkPatchUtils::GetTopCubic(cubics, top);
|
2015-11-05 21:09:08 +00:00
|
|
|
SkPoint left[SkPatchUtils::kNumPtsCubic];
|
2017-04-05 15:06:15 +00:00
|
|
|
SkPatchUtils::GetLeftCubic(cubics, left);
|
2015-11-05 21:09:08 +00:00
|
|
|
SkPoint right[SkPatchUtils::kNumPtsCubic];
|
2017-04-05 15:06:15 +00:00
|
|
|
SkPatchUtils::GetRightCubic(cubics, right);
|
2015-11-05 21:09:08 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setStrokeWidth(0.5f);
|
|
|
|
SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint);
|
|
|
|
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint);
|
|
|
|
|
|
|
|
paint.setStrokeWidth(2);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorCYAN);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorYELLOW);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are actually half the total width and hieghts
|
|
|
|
const SkScalar TexWidth = 100.0f;
|
|
|
|
const SkScalar TexHeight = 100.0f;
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class PerlinPatchView : public Sample {
|
2016-03-09 02:50:00 +00:00
|
|
|
sk_sp<SkShader> fShader0;
|
|
|
|
sk_sp<SkShader> fShader1;
|
|
|
|
sk_sp<SkShader> fShaderCompose;
|
2015-11-05 21:09:08 +00:00
|
|
|
SkScalar fXFreq;
|
|
|
|
SkScalar fYFreq;
|
|
|
|
SkScalar fSeed;
|
|
|
|
SkPoint fPts[SkPatchUtils::kNumCtrlPts];
|
|
|
|
SkScalar fTexX;
|
|
|
|
SkScalar fTexY;
|
|
|
|
SkScalar fTexScale;
|
|
|
|
SkMatrix fInvMatrix;
|
2016-08-22 15:45:15 +00:00
|
|
|
bool fShowGrid = false;
|
|
|
|
|
2015-11-05 21:09:08 +00:00
|
|
|
public:
|
|
|
|
PerlinPatchView() : fXFreq(0.025f), fYFreq(0.025f), fSeed(0.0f),
|
|
|
|
fTexX(100.0), fTexY(50.0), fTexScale(1.0f) {
|
2015-11-09 21:10:30 +00:00
|
|
|
const SkScalar s = 2;
|
2015-11-05 21:09:08 +00:00
|
|
|
// The order of the colors and points is clockwise starting at upper-left corner.
|
|
|
|
//top points
|
2015-11-09 21:10:30 +00:00
|
|
|
fPts[0].set(100 * s, 100 * s);
|
|
|
|
fPts[1].set(150 * s, 50 * s);
|
|
|
|
fPts[2].set(250 * s, 150 * s);
|
|
|
|
fPts[3].set(300 * s, 100 * s);
|
2015-11-05 21:09:08 +00:00
|
|
|
//right points
|
2015-11-09 21:10:30 +00:00
|
|
|
fPts[4].set(275 * s, 150 * s);
|
|
|
|
fPts[5].set(350 * s, 250 * s);
|
2015-11-05 21:09:08 +00:00
|
|
|
//bottom points
|
2015-11-09 21:10:30 +00:00
|
|
|
fPts[6].set(300 * s, 300 * s);
|
|
|
|
fPts[7].set(250 * s, 250 * s);
|
2015-11-05 21:09:08 +00:00
|
|
|
//left points
|
2015-11-09 21:10:30 +00:00
|
|
|
fPts[8].set(150 * s, 350 * s);
|
|
|
|
fPts[9].set(100 * s, 300 * s);
|
|
|
|
fPts[10].set(50 * s, 250 * s);
|
|
|
|
fPts[11].set(150 * s, 150 * s);
|
2015-11-05 21:09:08 +00:00
|
|
|
|
|
|
|
const SkColor colors[SkPatchUtils::kNumCorners] = {
|
2015-11-09 15:40:49 +00:00
|
|
|
0xFF5555FF, 0xFF8888FF, 0xFFCCCCFF
|
2015-11-05 21:09:08 +00:00
|
|
|
};
|
2015-11-09 15:40:49 +00:00
|
|
|
const SkPoint points[2] = { SkPoint::Make(0.0f, 0.0f),
|
|
|
|
SkPoint::Make(100.0f, 100.0f) };
|
2016-03-09 02:50:00 +00:00
|
|
|
fShader0 = SkGradientShader::MakeLinear(points,
|
2015-11-05 21:09:08 +00:00
|
|
|
colors,
|
2017-08-28 14:34:05 +00:00
|
|
|
nullptr,
|
2015-11-09 15:40:49 +00:00
|
|
|
3,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kMirror,
|
2015-11-05 21:09:08 +00:00
|
|
|
0,
|
2017-08-28 14:34:05 +00:00
|
|
|
nullptr);
|
2015-11-05 21:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("PerlinPatch"); }
|
|
|
|
|
2019-07-03 19:53:04 +00:00
|
|
|
bool onChar(SkUnichar uni) override {
|
2016-08-22 15:45:15 +00:00
|
|
|
switch (uni) {
|
2017-11-22 21:36:07 +00:00
|
|
|
case 'g': fShowGrid = !fShowGrid; return true;
|
2016-08-22 15:45:15 +00:00
|
|
|
default: break;
|
|
|
|
}
|
2019-07-03 19:53:04 +00:00
|
|
|
return false;
|
2015-11-05 21:09:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
2015-11-09 15:40:49 +00:00
|
|
|
fSeed += 0.005f;
|
2015-11-05 21:09:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
if (!canvas->getTotalMatrix().invert(&fInvMatrix)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-11-05 21:09:08 +00:00
|
|
|
SkScalar texWidth = fTexScale * TexWidth;
|
|
|
|
SkScalar texHeight = fTexScale * TexHeight;
|
|
|
|
const SkPoint texCoords[SkPatchUtils::kNumCorners] = {
|
|
|
|
{ fTexX - texWidth, fTexY - texHeight},
|
|
|
|
{ fTexX + texWidth, fTexY - texHeight},
|
|
|
|
{ fTexX + texWidth, fTexY + texHeight},
|
|
|
|
{ fTexX - texWidth, fTexY + texHeight}}
|
|
|
|
;
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2015-11-09 15:40:49 +00:00
|
|
|
SkScalar scaleFreq = 2.0;
|
2017-05-30 19:22:54 +00:00
|
|
|
fShader1 = SkPerlinNoiseShader::MakeImprovedNoise(fXFreq/scaleFreq, fYFreq/scaleFreq, 4,
|
2015-11-09 15:40:49 +00:00
|
|
|
fSeed);
|
2019-04-09 17:55:36 +00:00
|
|
|
fShaderCompose = SkShaders::Blend(SkBlendMode::kSrcOver, fShader0, fShader1);
|
2015-11-09 15:40:49 +00:00
|
|
|
|
2015-11-05 21:09:08 +00:00
|
|
|
paint.setShader(fShaderCompose);
|
2016-08-22 15:45:15 +00:00
|
|
|
|
|
|
|
const SkPoint* tex = texCoords;
|
|
|
|
if (fShowGrid) {
|
|
|
|
tex = nullptr;
|
|
|
|
}
|
2016-10-28 19:42:34 +00:00
|
|
|
canvas->drawPatch(fPts, nullptr, tex, SkBlendMode::kSrc, paint);
|
2015-11-05 21:09:08 +00:00
|
|
|
|
|
|
|
draw_control_points(canvas, fPts);
|
|
|
|
}
|
|
|
|
|
|
|
|
class PtClick : public Click {
|
|
|
|
public:
|
|
|
|
int fIndex;
|
2019-07-12 15:32:43 +00:00
|
|
|
PtClick(int index) : fIndex(index) {}
|
2015-11-05 21:09:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) {
|
|
|
|
return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5);
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
|
|
|
|
modi &= ~skui::ModifierKey::kFirstPress; // ignore this
|
|
|
|
if (skui::ModifierKey::kShift == modi) {
|
2019-07-12 15:32:43 +00:00
|
|
|
return new PtClick(-1);
|
2015-11-05 21:09:08 +00:00
|
|
|
}
|
2019-08-29 14:39:22 +00:00
|
|
|
if (skui::ModifierKey::kControl == modi) {
|
2019-07-12 15:32:43 +00:00
|
|
|
return new PtClick(-2);
|
2015-11-05 21:09:08 +00:00
|
|
|
}
|
|
|
|
SkPoint clickPoint = {x, y};
|
|
|
|
fInvMatrix.mapPoints(&clickPoint, 1);
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
|
|
|
|
if (hittest(fPts[i], clickPoint.fX, clickPoint.fY)) {
|
2019-07-12 15:32:43 +00:00
|
|
|
return new PtClick((int)i);
|
2015-11-05 21:09:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-12 15:32:43 +00:00
|
|
|
return nullptr;
|
2015-11-05 21:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool onClick(Click* click) override {
|
|
|
|
PtClick* ptClick = (PtClick*)click;
|
|
|
|
if (ptClick->fIndex >= 0) {
|
|
|
|
fPts[ptClick->fIndex].set(click->fCurr.fX , click->fCurr.fY );
|
|
|
|
} else if (-1 == ptClick->fIndex) {
|
|
|
|
SkScalar xDiff = click->fPrev.fX - click->fCurr.fX;
|
|
|
|
SkScalar yDiff = click->fPrev.fY - click->fCurr.fY;
|
|
|
|
fTexX += xDiff * fTexScale;
|
|
|
|
fTexY += yDiff * fTexScale;
|
|
|
|
} else if (-2 == ptClick->fIndex) {
|
|
|
|
SkScalar yDiff = click->fCurr.fY - click->fPrev.fY;
|
|
|
|
fTexScale += yDiff / 10.0f;
|
2020-02-07 15:36:46 +00:00
|
|
|
fTexScale = std::max(0.1f, std::min(20.f, fTexScale));
|
2015-11-05 21:09:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2015-11-05 21:09:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_SAMPLE( return new PerlinPatchView(); )
|