2017-05-04 18:00:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2017 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/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPoint3.h"
|
|
|
|
#include "include/pathops/SkPathOps.h"
|
|
|
|
#include "include/utils/SkCamera.h"
|
|
|
|
#include "include/utils/SkShadowUtils.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/core/SkBlurMask.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2017-05-04 18:00:59 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class ShadowUtilsView : public Sample {
|
2018-02-28 19:51:19 +00:00
|
|
|
SkTArray<SkPath> fConvexPaths;
|
|
|
|
SkTArray<SkPath> fConcavePaths;
|
2017-05-04 18:00:59 +00:00
|
|
|
SkScalar fZDelta;
|
|
|
|
|
|
|
|
bool fShowAmbient;
|
|
|
|
bool fShowSpot;
|
|
|
|
bool fUseAlt;
|
|
|
|
bool fShowObject;
|
|
|
|
bool fIgnoreShadowAlpha;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ShadowUtilsView()
|
|
|
|
: fZDelta(0)
|
|
|
|
, fShowAmbient(true)
|
|
|
|
, fShowSpot(true)
|
|
|
|
, fUseAlt(false)
|
|
|
|
, fShowObject(false)
|
|
|
|
, fIgnoreShadowAlpha(false) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
2019-09-14 19:13:23 +00:00
|
|
|
fConvexPaths.push_back().addRoundRect(SkRect::MakeWH(50, 50), 10, 10);
|
2017-05-04 18:00:59 +00:00
|
|
|
SkRRect oddRRect;
|
|
|
|
oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16);
|
2018-02-28 19:51:19 +00:00
|
|
|
fConvexPaths.push_back().addRRect(oddRRect);
|
|
|
|
fConvexPaths.push_back().addRect(SkRect::MakeWH(50, 50));
|
|
|
|
fConvexPaths.push_back().addCircle(25, 25, 25);
|
|
|
|
fConvexPaths.push_back().cubicTo(100, 50, 20, 100, 0, 0);
|
|
|
|
fConvexPaths.push_back().addOval(SkRect::MakeWH(20, 60));
|
|
|
|
|
|
|
|
// star
|
|
|
|
fConcavePaths.push_back().moveTo(0.0f, -33.3333f);
|
|
|
|
fConcavePaths.back().lineTo(9.62f, -16.6667f);
|
|
|
|
fConcavePaths.back().lineTo(28.867f, -16.6667f);
|
|
|
|
fConcavePaths.back().lineTo(19.24f, 0.0f);
|
|
|
|
fConcavePaths.back().lineTo(28.867f, 16.6667f);
|
|
|
|
fConcavePaths.back().lineTo(9.62f, 16.6667f);
|
|
|
|
fConcavePaths.back().lineTo(0.0f, 33.3333f);
|
|
|
|
fConcavePaths.back().lineTo(-9.62f, 16.6667f);
|
|
|
|
fConcavePaths.back().lineTo(-28.867f, 16.6667f);
|
|
|
|
fConcavePaths.back().lineTo(-19.24f, 0.0f);
|
|
|
|
fConcavePaths.back().lineTo(-28.867f, -16.6667f);
|
|
|
|
fConcavePaths.back().lineTo(-9.62f, -16.6667f);
|
|
|
|
fConcavePaths.back().close();
|
|
|
|
|
|
|
|
// dumbbell
|
|
|
|
fConcavePaths.push_back().moveTo(50, 0);
|
|
|
|
fConcavePaths.back().cubicTo(100, 25, 60, 50, 50, 0);
|
|
|
|
fConcavePaths.back().cubicTo(0, -25, 40, -50, 50, 0);
|
2017-05-04 18:00:59 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("ShadowUtils"); }
|
2017-05-04 18:00:59 +00:00
|
|
|
|
2019-07-03 19:53:04 +00:00
|
|
|
bool onChar(SkUnichar uni) override {
|
2017-05-04 18:00:59 +00:00
|
|
|
bool handled = false;
|
|
|
|
switch (uni) {
|
|
|
|
case 'W':
|
|
|
|
fShowAmbient = !fShowAmbient;
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
fShowSpot = !fShowSpot;
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
fUseAlt = !fUseAlt;
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
fShowObject = !fShowObject;
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
fZDelta += 0.5f;
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
fZDelta -= 0.5f;
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
fIgnoreShadowAlpha = !fIgnoreShadowAlpha;
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (handled) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-03 19:53:04 +00:00
|
|
|
return false;
|
2017-05-04 18:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawBG(SkCanvas* canvas) {
|
2017-05-05 19:53:23 +00:00
|
|
|
canvas->drawColor(0xFFFFFFFF);
|
2017-05-04 18:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawShadowedPath(SkCanvas* canvas, const SkPath& path,
|
2017-05-08 18:19:30 +00:00
|
|
|
const SkPoint3& zPlaneParams,
|
2017-05-04 18:00:59 +00:00
|
|
|
const SkPaint& paint, SkScalar ambientAlpha,
|
|
|
|
const SkPoint3& lightPos, SkScalar lightWidth, SkScalar spotAlpha,
|
|
|
|
uint32_t flags) {
|
|
|
|
if (fIgnoreShadowAlpha) {
|
|
|
|
ambientAlpha = 255;
|
|
|
|
spotAlpha = 255;
|
|
|
|
}
|
|
|
|
if (!fShowAmbient) {
|
|
|
|
ambientAlpha = 0;
|
|
|
|
}
|
|
|
|
if (!fShowSpot) {
|
|
|
|
spotAlpha = 0;
|
|
|
|
}
|
|
|
|
if (fUseAlt) {
|
|
|
|
flags |= SkShadowFlags::kGeometricOnly_ShadowFlag;
|
|
|
|
}
|
2018-02-22 15:58:34 +00:00
|
|
|
|
|
|
|
SkColor ambientColor = SkColorSetARGB(ambientAlpha * 255, 255, 0, 0);
|
|
|
|
SkColor spotColor = SkColorSetARGB(spotAlpha * 255, 0, 0, 255);
|
2017-05-10 18:13:24 +00:00
|
|
|
SkShadowUtils::DrawShadow(canvas, path, zPlaneParams,
|
|
|
|
lightPos, lightWidth,
|
2018-02-22 15:58:34 +00:00
|
|
|
ambientColor, spotColor, flags);
|
2017-05-04 18:00:59 +00:00
|
|
|
|
|
|
|
if (fShowObject) {
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
} else {
|
|
|
|
SkPaint strokePaint;
|
|
|
|
|
|
|
|
strokePaint.setColor(paint.getColor());
|
|
|
|
strokePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
canvas->drawPath(path, strokePaint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
this->drawBG(canvas);
|
|
|
|
|
|
|
|
static constexpr int kW = 800;
|
|
|
|
static constexpr SkScalar kPad = 15.f;
|
|
|
|
static constexpr SkScalar kLightR = 100.f;
|
|
|
|
static constexpr SkScalar kHeight = 50.f;
|
|
|
|
static constexpr SkScalar kAmbientAlpha = 0.5f;
|
|
|
|
static constexpr SkScalar kSpotAlpha = 0.5f;
|
2017-05-22 19:52:21 +00:00
|
|
|
static constexpr SkPoint3 lightPos = { 250, 400, 500 };
|
2017-05-16 19:01:43 +00:00
|
|
|
|
2017-05-04 18:00:59 +00:00
|
|
|
canvas->translate(3 * kPad, 3 * kPad);
|
|
|
|
canvas->save();
|
|
|
|
SkScalar x = 0;
|
|
|
|
SkScalar dy = 0;
|
|
|
|
SkTDArray<SkMatrix> matrices;
|
|
|
|
matrices.push()->reset();
|
|
|
|
SkMatrix* m = matrices.push();
|
|
|
|
m->setRotate(33.f, 25.f, 25.f);
|
|
|
|
m->postScale(1.2f, 0.8f, 25.f, 25.f);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
paint.setAntiAlias(true);
|
2017-05-08 18:19:30 +00:00
|
|
|
SkPoint3 zPlaneParams = SkPoint3::Make(0, 0, SkTMax(1.0f, kHeight + fZDelta));
|
2018-02-28 19:51:19 +00:00
|
|
|
|
|
|
|
// convex paths
|
2017-05-04 18:00:59 +00:00
|
|
|
for (auto& m : matrices) {
|
|
|
|
for (auto flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) {
|
2018-02-28 19:51:19 +00:00
|
|
|
for (const auto& path : fConvexPaths) {
|
2017-05-04 18:00:59 +00:00
|
|
|
SkRect postMBounds = path.getBounds();
|
|
|
|
m.mapRect(&postMBounds);
|
|
|
|
SkScalar w = postMBounds.width() + kHeight;
|
|
|
|
SkScalar dx = w + kPad;
|
|
|
|
if (x + dx > kW - 3 * kPad) {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, dy);
|
|
|
|
canvas->save();
|
|
|
|
x = 0;
|
|
|
|
dy = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(m);
|
2018-02-28 19:51:19 +00:00
|
|
|
this->drawShadowedPath(canvas, path, zPlaneParams, paint, kAmbientAlpha,
|
|
|
|
lightPos, kLightR, kSpotAlpha, flags);
|
2017-05-04 18:00:59 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
x += dx;
|
|
|
|
dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-28 19:51:19 +00:00
|
|
|
|
|
|
|
// concave paths
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(kPad, dy);
|
|
|
|
canvas->save();
|
|
|
|
x = kPad;
|
|
|
|
dy = 0;
|
|
|
|
for (auto& m : matrices) {
|
|
|
|
for (const auto& path : fConcavePaths) {
|
|
|
|
SkRect postMBounds = path.getBounds();
|
|
|
|
m.mapRect(&postMBounds);
|
|
|
|
SkScalar w = postMBounds.width();
|
|
|
|
SkScalar dx = w + kPad;
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(m);
|
|
|
|
this->drawShadowedPath(canvas, path, zPlaneParams, paint, kAmbientAlpha, lightPos,
|
|
|
|
kLightR, kSpotAlpha, kNone_ShadowFlag);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
x += dx;
|
|
|
|
dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 18:00:59 +00:00
|
|
|
// Show where the light is in x,y as a circle (specified in device space).
|
|
|
|
SkMatrix invCanvasM = canvas->getTotalMatrix();
|
|
|
|
if (invCanvasM.invert(&invCanvasM)) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(invCanvasM);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setAntiAlias(true);
|
2017-05-16 19:01:43 +00:00
|
|
|
canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint);
|
2017-05-04 18:00:59 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2017-05-04 18:00:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new ShadowUtilsView(); )
|