2016-08-12 17:40:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#include "SampleCode.h"
|
2017-03-10 19:34:51 +00:00
|
|
|
#include "SkAnimTimer.h"
|
2016-08-12 17:40:38 +00:00
|
|
|
#include "SkBlurMask.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
2017-02-06 19:36:01 +00:00
|
|
|
#include "SkColorFilter.h"
|
2017-03-10 19:34:51 +00:00
|
|
|
#include "SkCamera.h"
|
2016-08-12 17:40:38 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPath.h"
|
2017-03-10 19:34:51 +00:00
|
|
|
#include "SkPathOps.h"
|
2016-08-12 17:40:38 +00:00
|
|
|
#include "SkPoint3.h"
|
2017-01-13 19:37:37 +00:00
|
|
|
#include "SkShadowUtils.h"
|
2016-08-12 17:40:38 +00:00
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "sk_tool_utils.h"
|
|
|
|
|
2016-08-17 14:59:41 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-08-12 17:40:38 +00:00
|
|
|
class ShadowsView : public SampleView {
|
|
|
|
SkPath fRectPath;
|
|
|
|
SkPath fRRPath;
|
|
|
|
SkPath fCirclePath;
|
2017-01-25 14:39:46 +00:00
|
|
|
SkPath fFunkyRRPath;
|
|
|
|
SkPath fCubicPath;
|
2017-03-10 19:34:51 +00:00
|
|
|
SkPath fSquareRRectPath;
|
|
|
|
SkPath fWideRectPath;
|
|
|
|
SkPath fWideOvalPath;
|
2016-08-12 17:40:38 +00:00
|
|
|
SkPoint3 fLightPos;
|
2017-02-06 19:36:01 +00:00
|
|
|
SkScalar fZDelta;
|
2017-03-10 19:34:51 +00:00
|
|
|
SkScalar fAnimTranslate;
|
2017-04-11 19:29:14 +00:00
|
|
|
SkScalar fAnimAngle;
|
2017-12-19 18:02:38 +00:00
|
|
|
SkScalar fAnimAlpha;
|
2016-08-12 17:40:38 +00:00
|
|
|
|
|
|
|
bool fShowAmbient;
|
|
|
|
bool fShowSpot;
|
2016-08-17 17:06:18 +00:00
|
|
|
bool fUseAlt;
|
2016-08-12 17:40:38 +00:00
|
|
|
bool fShowObject;
|
2016-10-07 18:44:07 +00:00
|
|
|
bool fIgnoreShadowAlpha;
|
2017-12-19 18:02:38 +00:00
|
|
|
bool fDoAlphaAnimation;
|
2016-08-12 17:40:38 +00:00
|
|
|
|
|
|
|
public:
|
2017-02-06 19:36:01 +00:00
|
|
|
ShadowsView()
|
2017-03-10 19:34:51 +00:00
|
|
|
: fZDelta(0)
|
|
|
|
, fAnimTranslate(0)
|
2017-04-11 19:29:14 +00:00
|
|
|
, fAnimAngle(0)
|
2017-12-19 18:02:38 +00:00
|
|
|
, fAnimAlpha(1)
|
2017-02-06 19:36:01 +00:00
|
|
|
, fShowAmbient(true)
|
2016-08-12 17:40:38 +00:00
|
|
|
, fShowSpot(true)
|
2017-04-28 15:00:35 +00:00
|
|
|
, fUseAlt(false)
|
2016-10-07 18:44:07 +00:00
|
|
|
, fShowObject(true)
|
2017-12-19 18:02:38 +00:00
|
|
|
, fIgnoreShadowAlpha(false)
|
|
|
|
, fDoAlphaAnimation(false) {}
|
2016-08-12 17:40:38 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fCirclePath.addCircle(0, 0, 50);
|
|
|
|
fRectPath.addRect(SkRect::MakeXYWH(-100, -50, 200, 100));
|
|
|
|
fRRPath.addRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-100, -50, 200, 100), 4, 4));
|
2017-01-25 14:39:46 +00:00
|
|
|
fFunkyRRPath.addRoundRect(SkRect::MakeXYWH(-50, -50, SK_Scalar1 * 100, SK_Scalar1 * 100),
|
|
|
|
40 * SK_Scalar1, 20 * SK_Scalar1,
|
|
|
|
SkPath::kCW_Direction);
|
|
|
|
fCubicPath.cubicTo(100 * SK_Scalar1, 50 * SK_Scalar1,
|
|
|
|
20 * SK_Scalar1, 100 * SK_Scalar1,
|
|
|
|
0 * SK_Scalar1, 0 * SK_Scalar1);
|
2017-03-10 19:34:51 +00:00
|
|
|
fSquareRRectPath.addRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-50, -50, 100, 100),
|
|
|
|
10, 10));
|
|
|
|
fWideRectPath.addRect(SkRect::MakeXYWH(0, 0, 630, 70));
|
|
|
|
fWideOvalPath.addOval(SkRect::MakeXYWH(0, 0, 630, 70));
|
2017-01-25 14:39:46 +00:00
|
|
|
|
2017-04-28 15:00:35 +00:00
|
|
|
fLightPos = SkPoint3::Make(350, 0, 600);
|
2016-08-12 17:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// overrides from SkEventSink
|
|
|
|
bool onQuery(SkEvent* evt) override {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "AndroidShadows");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkUnichar uni;
|
|
|
|
if (SampleCode::CharQ(*evt, &uni)) {
|
2017-02-14 20:16:46 +00:00
|
|
|
bool handled = false;
|
2016-08-12 17:40:38 +00:00
|
|
|
switch (uni) {
|
2017-02-06 19:36:01 +00:00
|
|
|
case 'W':
|
2016-08-12 17:40:38 +00:00
|
|
|
fShowAmbient = !fShowAmbient;
|
2017-02-14 20:16:46 +00:00
|
|
|
handled = true;
|
2016-08-12 17:40:38 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
fShowSpot = !fShowSpot;
|
2017-02-14 20:16:46 +00:00
|
|
|
handled = true;
|
2016-08-12 17:40:38 +00:00
|
|
|
break;
|
2016-08-17 17:06:18 +00:00
|
|
|
case 'T':
|
|
|
|
fUseAlt = !fUseAlt;
|
2017-02-14 20:16:46 +00:00
|
|
|
handled = true;
|
2016-08-17 17:06:18 +00:00
|
|
|
break;
|
2016-08-12 17:40:38 +00:00
|
|
|
case 'O':
|
|
|
|
fShowObject = !fShowObject;
|
2017-02-14 20:16:46 +00:00
|
|
|
handled = true;
|
2016-08-12 17:40:38 +00:00
|
|
|
break;
|
2017-12-19 18:02:38 +00:00
|
|
|
case 'N':
|
|
|
|
fDoAlphaAnimation = !fDoAlphaAnimation;
|
|
|
|
if (!fDoAlphaAnimation) {
|
|
|
|
fAnimAlpha = 1;
|
|
|
|
}
|
|
|
|
handled = true;
|
|
|
|
break;
|
2016-08-12 17:40:38 +00:00
|
|
|
case '>':
|
2017-02-06 19:36:01 +00:00
|
|
|
fZDelta += 0.5f;
|
2017-02-14 20:16:46 +00:00
|
|
|
handled = true;
|
2016-08-12 17:40:38 +00:00
|
|
|
break;
|
|
|
|
case '<':
|
2017-02-06 19:36:01 +00:00
|
|
|
fZDelta -= 0.5f;
|
2017-02-14 20:16:46 +00:00
|
|
|
handled = true;
|
2016-08-12 17:40:38 +00:00
|
|
|
break;
|
2016-10-07 18:44:07 +00:00
|
|
|
case '?':
|
|
|
|
fIgnoreShadowAlpha = !fIgnoreShadowAlpha;
|
2017-02-14 20:16:46 +00:00
|
|
|
handled = true;
|
2016-10-07 18:44:07 +00:00
|
|
|
break;
|
2016-08-12 17:40:38 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-02-14 20:16:46 +00:00
|
|
|
if (handled) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-12 17:40:38 +00:00
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawBG(SkCanvas* canvas) {
|
|
|
|
canvas->drawColor(0xFFDDDDDD);
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:29:14 +00:00
|
|
|
void drawShadowedPath(SkCanvas* canvas, const SkPath& path,
|
2017-05-08 18:19:30 +00:00
|
|
|
const SkPoint3& zPlaneParams,
|
2016-08-17 17:06:18 +00:00
|
|
|
const SkPaint& paint, SkScalar ambientAlpha,
|
|
|
|
const SkPoint3& lightPos, SkScalar lightWidth, SkScalar spotAlpha) {
|
2017-05-04 18:00:59 +00:00
|
|
|
if (fIgnoreShadowAlpha) {
|
|
|
|
ambientAlpha = 255;
|
|
|
|
spotAlpha = 255;
|
|
|
|
}
|
2017-05-04 13:58:17 +00:00
|
|
|
if (!fShowAmbient) {
|
|
|
|
ambientAlpha = 0;
|
2016-10-31 20:06:51 +00:00
|
|
|
}
|
2017-05-04 13:58:17 +00:00
|
|
|
if (!fShowSpot) {
|
|
|
|
spotAlpha = 0;
|
2016-08-12 17:40:38 +00:00
|
|
|
}
|
2017-05-04 13:58:17 +00:00
|
|
|
uint32_t flags = 0;
|
|
|
|
if (fUseAlt) {
|
|
|
|
flags |= SkShadowFlags::kGeometricOnly_ShadowFlag;
|
2016-08-12 17:40:38 +00:00
|
|
|
}
|
2017-06-09 15:09:03 +00:00
|
|
|
|
2018-02-22 15:58:34 +00:00
|
|
|
SkColor ambientColor = SkColorSetARGB(ambientAlpha * 255, 0, 0, 0);
|
|
|
|
SkColor spotColor = SkColorSetARGB(spotAlpha * 255, 0, 0, 0);
|
|
|
|
SkShadowUtils::DrawShadow(canvas, path, zPlaneParams, lightPos, lightWidth,
|
|
|
|
ambientColor, spotColor, flags);
|
2016-10-31 20:06:51 +00:00
|
|
|
|
2016-08-12 17:40:38 +00:00
|
|
|
if (fShowObject) {
|
|
|
|
canvas->drawPath(path, paint);
|
2016-10-07 18:44:07 +00:00
|
|
|
} else {
|
|
|
|
SkPaint strokePaint;
|
|
|
|
|
|
|
|
strokePaint.setColor(paint.getColor());
|
|
|
|
strokePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
canvas->drawPath(path, strokePaint);
|
2016-08-12 17:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
this->drawBG(canvas);
|
2017-04-28 15:00:35 +00:00
|
|
|
const SkScalar kLightWidth = 800;
|
|
|
|
const SkScalar kAmbientAlpha = 0.1f;
|
2016-08-17 17:06:18 +00:00
|
|
|
const SkScalar kSpotAlpha = 0.25f;
|
2016-08-12 17:40:38 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2016-08-17 17:06:18 +00:00
|
|
|
SkPoint3 lightPos = fLightPos;
|
2017-05-08 18:19:30 +00:00
|
|
|
SkPoint3 zPlaneParams = SkPoint3::Make(0, 0, 0);
|
2016-08-17 17:06:18 +00:00
|
|
|
|
2016-08-12 17:40:38 +00:00
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
canvas->translate(200, 90);
|
2017-05-08 18:19:30 +00:00
|
|
|
zPlaneParams.fZ = SkTMax(1.0f, 2 + fZDelta);
|
2017-12-19 18:02:38 +00:00
|
|
|
this->drawShadowedPath(canvas, fRRPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
|
|
|
|
lightPos, kLightWidth, fAnimAlpha*kSpotAlpha);
|
2016-08-12 17:40:38 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->translate(250, 0);
|
2017-05-08 18:19:30 +00:00
|
|
|
zPlaneParams.fZ = SkTMax(1.0f, 8 + fZDelta);
|
2017-12-19 18:02:38 +00:00
|
|
|
this->drawShadowedPath(canvas, fRectPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
|
|
|
|
lightPos, kLightWidth, fAnimAlpha*kSpotAlpha);
|
2016-08-12 17:40:38 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
canvas->translate(-250, 110);
|
2017-05-08 18:19:30 +00:00
|
|
|
zPlaneParams.fZ = SkTMax(1.0f, 12 + fZDelta);
|
2017-12-19 18:02:38 +00:00
|
|
|
this->drawShadowedPath(canvas, fCirclePath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
|
|
|
|
lightPos, kLightWidth, fAnimAlpha*0.5f);
|
2016-08-17 14:59:41 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->translate(250, 0);
|
2017-05-08 18:19:30 +00:00
|
|
|
zPlaneParams.fZ = SkTMax(1.0f, 64 + fZDelta);
|
2017-12-19 18:02:38 +00:00
|
|
|
this->drawShadowedPath(canvas, fRRPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
|
|
|
|
lightPos, kLightWidth, fAnimAlpha*kSpotAlpha);
|
2017-01-25 14:39:46 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorYELLOW);
|
|
|
|
canvas->translate(-250, 110);
|
2017-05-08 18:19:30 +00:00
|
|
|
zPlaneParams.fZ = SkTMax(1.0f, 8 + fZDelta);
|
2017-12-19 18:02:38 +00:00
|
|
|
this->drawShadowedPath(canvas, fFunkyRRPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
|
|
|
|
lightPos, kLightWidth, fAnimAlpha*kSpotAlpha);
|
2017-01-25 14:39:46 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorCYAN);
|
|
|
|
canvas->translate(250, 0);
|
2017-05-08 18:19:30 +00:00
|
|
|
zPlaneParams.fZ = SkTMax(1.0f, 16 + fZDelta);
|
2017-12-19 18:02:38 +00:00
|
|
|
this->drawShadowedPath(canvas, fCubicPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
|
|
|
|
lightPos, kLightWidth, fAnimAlpha*kSpotAlpha);
|
2017-03-10 19:34:51 +00:00
|
|
|
|
|
|
|
// circular reveal
|
|
|
|
SkPath tmpPath;
|
|
|
|
SkPath tmpClipPath;
|
|
|
|
tmpClipPath.addCircle(fAnimTranslate, 0, 60);
|
|
|
|
Op(fSquareRRectPath, tmpClipPath, kIntersect_SkPathOp, &tmpPath);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorMAGENTA);
|
|
|
|
canvas->translate(-125, 60);
|
2017-05-08 18:19:30 +00:00
|
|
|
zPlaneParams.fZ = SkTMax(1.0f, 32 + fZDelta);
|
|
|
|
this->drawShadowedPath(canvas, tmpPath, zPlaneParams, paint, .1f,
|
2017-03-10 19:34:51 +00:00
|
|
|
lightPos, kLightWidth, .5f);
|
|
|
|
|
|
|
|
// perspective paths
|
|
|
|
SkPoint pivot = SkPoint::Make(fWideRectPath.getBounds().width()/2,
|
|
|
|
fWideRectPath.getBounds().height()/2);
|
2017-04-11 19:29:14 +00:00
|
|
|
SkPoint translate = SkPoint::Make(100, 450);
|
2017-03-10 19:34:51 +00:00
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
Sk3DView view;
|
|
|
|
view.save();
|
2017-04-11 19:29:14 +00:00
|
|
|
view.rotateX(fAnimAngle);
|
2017-03-10 19:34:51 +00:00
|
|
|
SkMatrix persp;
|
|
|
|
view.getMatrix(&persp);
|
|
|
|
persp.preTranslate(-pivot.fX, -pivot.fY);
|
|
|
|
persp.postTranslate(pivot.fX + translate.fX, pivot.fY + translate.fY);
|
|
|
|
canvas->setMatrix(persp);
|
2017-04-11 19:29:14 +00:00
|
|
|
SkScalar radians = SkDegreesToRadians(fAnimAngle);
|
2017-05-08 18:19:30 +00:00
|
|
|
zPlaneParams = SkPoint3::Make(0,
|
|
|
|
SkScalarSin(-radians),
|
|
|
|
SkTMax(1.0f, 16 + fZDelta) - SkScalarSin(-radians)*pivot.fY);
|
|
|
|
this->drawShadowedPath(canvas, fWideRectPath, zPlaneParams, paint, .1f,
|
2017-03-10 19:34:51 +00:00
|
|
|
lightPos, kLightWidth, .5f);
|
|
|
|
|
|
|
|
pivot = SkPoint::Make(fWideOvalPath.getBounds().width() / 2,
|
|
|
|
fWideOvalPath.getBounds().height() / 2);
|
2017-04-11 19:29:14 +00:00
|
|
|
translate = SkPoint::Make(100, 600);
|
2017-03-10 19:34:51 +00:00
|
|
|
view.restore();
|
2017-04-11 19:29:14 +00:00
|
|
|
view.rotateY(fAnimAngle);
|
2017-03-10 19:34:51 +00:00
|
|
|
view.getMatrix(&persp);
|
|
|
|
persp.preTranslate(-pivot.fX, -pivot.fY);
|
|
|
|
persp.postTranslate(pivot.fX + translate.fX, pivot.fY + translate.fY);
|
|
|
|
canvas->setMatrix(persp);
|
2017-05-08 18:19:30 +00:00
|
|
|
zPlaneParams = SkPoint3::Make(-SkScalarSin(radians),
|
|
|
|
0,
|
|
|
|
SkTMax(1.0f, 32 + fZDelta) + SkScalarSin(radians)*pivot.fX);
|
|
|
|
this->drawShadowedPath(canvas, fWideOvalPath, zPlaneParams, paint, .1f,
|
2017-03-10 19:34:51 +00:00
|
|
|
lightPos, kLightWidth, .5f);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onAnimate(const SkAnimTimer& timer) override {
|
Revert "Revert "Create new inset algorithm for spot shadows""
This reverts commit e7c85c45c4c0a97adc6711bb12ecacc36af4ba11.
Reason for revert: <INSERT REASONING HERE>
Original change's description:
> Revert "Create new inset algorithm for spot shadows"
>
> This reverts commit e5f5bf5175e426ebb6aa234f4387831c898f20ad.
>
> Reason for revert: Breaking a bunch of bots. e.g:
>
> https://luci-milo.appspot.com/swarming/task/3519cae0a03c7b10/steps/dm/0/stdout
>
> Original change's description:
> > Create new inset algorithm for spot shadows
> >
> > BUG=skia:
> >
> > Change-Id: If7c67c2a5b9beea28f86d13362a5156b46394d0e
> > Reviewed-on: https://skia-review.googlesource.com/9875
> > Commit-Queue: Ravi Mistry <rmistry@google.com>
> > Reviewed-by: Brian Salomon <bsalomon@google.com>
> > Reviewed-by: Robert Phillips <robertphillips@google.com>
> >
>
> TBR=jvanverth@google.com,bsalomon@google.com,rmistry@google.com,robertphillips@google.com,msarett@google.com,reviews@skia.org
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:
>
> Change-Id: I3d119ff631dbb1a41f873b9c8753d542ec91254e
> Reviewed-on: https://skia-review.googlesource.com/10112
> Reviewed-by: Brian Salomon <bsalomon@google.com>
> Commit-Queue: Brian Salomon <bsalomon@google.com>
>
TBR=jvanverth@google.com,bsalomon@google.com,rmistry@google.com,msarett@google.com,robertphillips@google.com,reviews@skia.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:
Change-Id: Ib3998300606d3a2e2fb3a14b2088cfad48363501
Reviewed-on: https://skia-review.googlesource.com/10113
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
2017-03-24 16:07:20 +00:00
|
|
|
fAnimTranslate = timer.pingPong(30, 0, 200, -200);
|
2017-04-11 19:29:14 +00:00
|
|
|
fAnimAngle = timer.pingPong(15, 0, 0, 20);
|
2017-12-19 18:02:38 +00:00
|
|
|
if (fDoAlphaAnimation) {
|
|
|
|
fAnimAlpha = timer.pingPong(5, 0, 1, 0);
|
|
|
|
}
|
2017-03-10 19:34:51 +00:00
|
|
|
return true;
|
2016-08-12 17:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-02-14 20:16:46 +00:00
|
|
|
typedef SampleView INHERITED;
|
2016-08-12 17:40:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new ShadowsView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|