blob: 86151c1a03ff53f0bc5b7d0c7d5527c85bc328f3 [file] [log] [blame]
Jim Van Verthe7705782017-05-04 14:00:59 -04001
2/*
3 * Copyright 2017 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkColorFilter.h"
10#include "include/core/SkPath.h"
11#include "include/core/SkPoint3.h"
12#include "include/effects/SkBlurMaskFilter.h"
13#include "include/pathops/SkPathOps.h"
14#include "include/utils/SkCamera.h"
15#include "include/utils/SkShadowUtils.h"
16#include "samplecode/Sample.h"
17#include "src/core/SkBlurMask.h"
18#include "src/utils/SkUTF.h"
19#include "tools/ToolUtils.h"
Jim Van Verthe7705782017-05-04 14:00:59 -040020
21////////////////////////////////////////////////////////////////////////////
22
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040023class ShadowUtilsView : public Sample {
Jim Van Verth22526362018-02-28 14:51:19 -050024 SkTArray<SkPath> fConvexPaths;
25 SkTArray<SkPath> fConcavePaths;
Jim Van Verthe7705782017-05-04 14:00:59 -040026 SkScalar fZDelta;
27
28 bool fShowAmbient;
29 bool fShowSpot;
30 bool fUseAlt;
31 bool fShowObject;
32 bool fIgnoreShadowAlpha;
33
34public:
35 ShadowUtilsView()
36 : fZDelta(0)
37 , fShowAmbient(true)
38 , fShowSpot(true)
39 , fUseAlt(false)
40 , fShowObject(false)
41 , fIgnoreShadowAlpha(false) {}
42
43protected:
44 void onOnceBeforeDraw() override {
Mike Reed4241f5e2019-09-14 19:13:23 +000045 fConvexPaths.push_back().addRoundRect(SkRect::MakeWH(50, 50), 10, 10);
Jim Van Verthe7705782017-05-04 14:00:59 -040046 SkRRect oddRRect;
47 oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16);
Jim Van Verth22526362018-02-28 14:51:19 -050048 fConvexPaths.push_back().addRRect(oddRRect);
49 fConvexPaths.push_back().addRect(SkRect::MakeWH(50, 50));
50 fConvexPaths.push_back().addCircle(25, 25, 25);
51 fConvexPaths.push_back().cubicTo(100, 50, 20, 100, 0, 0);
52 fConvexPaths.push_back().addOval(SkRect::MakeWH(20, 60));
53
54 // star
55 fConcavePaths.push_back().moveTo(0.0f, -33.3333f);
56 fConcavePaths.back().lineTo(9.62f, -16.6667f);
57 fConcavePaths.back().lineTo(28.867f, -16.6667f);
58 fConcavePaths.back().lineTo(19.24f, 0.0f);
59 fConcavePaths.back().lineTo(28.867f, 16.6667f);
60 fConcavePaths.back().lineTo(9.62f, 16.6667f);
61 fConcavePaths.back().lineTo(0.0f, 33.3333f);
62 fConcavePaths.back().lineTo(-9.62f, 16.6667f);
63 fConcavePaths.back().lineTo(-28.867f, 16.6667f);
64 fConcavePaths.back().lineTo(-19.24f, 0.0f);
65 fConcavePaths.back().lineTo(-28.867f, -16.6667f);
66 fConcavePaths.back().lineTo(-9.62f, -16.6667f);
67 fConcavePaths.back().close();
68
69 // dumbbell
70 fConcavePaths.push_back().moveTo(50, 0);
71 fConcavePaths.back().cubicTo(100, 25, 60, 50, 50, 0);
72 fConcavePaths.back().cubicTo(0, -25, 40, -50, 50, 0);
Jim Van Verthe7705782017-05-04 14:00:59 -040073 }
74
Hal Canary8a027312019-07-03 10:55:44 -040075 SkString name() override { return SkString("ShadowUtils"); }
Jim Van Verthe7705782017-05-04 14:00:59 -040076
Hal Canary6cc65e12019-07-03 15:53:04 -040077 bool onChar(SkUnichar uni) override {
Jim Van Verthe7705782017-05-04 14:00:59 -040078 bool handled = false;
79 switch (uni) {
80 case 'W':
81 fShowAmbient = !fShowAmbient;
82 handled = true;
83 break;
84 case 'S':
85 fShowSpot = !fShowSpot;
86 handled = true;
87 break;
88 case 'T':
89 fUseAlt = !fUseAlt;
90 handled = true;
91 break;
92 case 'O':
93 fShowObject = !fShowObject;
94 handled = true;
95 break;
96 case '>':
97 fZDelta += 0.5f;
98 handled = true;
99 break;
100 case '<':
101 fZDelta -= 0.5f;
102 handled = true;
103 break;
104 case '?':
105 fIgnoreShadowAlpha = !fIgnoreShadowAlpha;
106 handled = true;
107 break;
108 default:
109 break;
110 }
111 if (handled) {
Jim Van Verthe7705782017-05-04 14:00:59 -0400112 return true;
113 }
Hal Canary6cc65e12019-07-03 15:53:04 -0400114 return false;
Jim Van Verthe7705782017-05-04 14:00:59 -0400115 }
116
117 void drawBG(SkCanvas* canvas) {
Jim Van Verth8d1e0ac2017-05-05 15:53:23 -0400118 canvas->drawColor(0xFFFFFFFF);
Jim Van Verthe7705782017-05-04 14:00:59 -0400119 }
120
121 void drawShadowedPath(SkCanvas* canvas, const SkPath& path,
Jim Van Verthe308a122017-05-08 14:19:30 -0400122 const SkPoint3& zPlaneParams,
Jim Van Verthe7705782017-05-04 14:00:59 -0400123 const SkPaint& paint, SkScalar ambientAlpha,
124 const SkPoint3& lightPos, SkScalar lightWidth, SkScalar spotAlpha,
125 uint32_t flags) {
126 if (fIgnoreShadowAlpha) {
127 ambientAlpha = 255;
128 spotAlpha = 255;
129 }
130 if (!fShowAmbient) {
131 ambientAlpha = 0;
132 }
133 if (!fShowSpot) {
134 spotAlpha = 0;
135 }
136 if (fUseAlt) {
137 flags |= SkShadowFlags::kGeometricOnly_ShadowFlag;
138 }
Jim Van Vertha5566842018-02-22 10:58:34 -0500139
140 SkColor ambientColor = SkColorSetARGB(ambientAlpha * 255, 255, 0, 0);
141 SkColor spotColor = SkColorSetARGB(spotAlpha * 255, 0, 0, 255);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400142 SkShadowUtils::DrawShadow(canvas, path, zPlaneParams,
143 lightPos, lightWidth,
Jim Van Vertha5566842018-02-22 10:58:34 -0500144 ambientColor, spotColor, flags);
Jim Van Verthe7705782017-05-04 14:00:59 -0400145
146 if (fShowObject) {
147 canvas->drawPath(path, paint);
148 } else {
149 SkPaint strokePaint;
150
151 strokePaint.setColor(paint.getColor());
152 strokePaint.setStyle(SkPaint::kStroke_Style);
153
154 canvas->drawPath(path, strokePaint);
155 }
156 }
157
158 void onDrawContent(SkCanvas* canvas) override {
159 this->drawBG(canvas);
160
161 static constexpr int kW = 800;
162 static constexpr SkScalar kPad = 15.f;
Jim Van Verthe7705782017-05-04 14:00:59 -0400163 static constexpr SkScalar kLightR = 100.f;
164 static constexpr SkScalar kHeight = 50.f;
165 static constexpr SkScalar kAmbientAlpha = 0.5f;
166 static constexpr SkScalar kSpotAlpha = 0.5f;
Jim Van Verth8793e382017-05-22 15:52:21 -0400167 static constexpr SkPoint3 lightPos = { 250, 400, 500 };
Jim Van Verth9392f569c2017-05-16 15:01:43 -0400168
Jim Van Verthe7705782017-05-04 14:00:59 -0400169 canvas->translate(3 * kPad, 3 * kPad);
170 canvas->save();
171 SkScalar x = 0;
172 SkScalar dy = 0;
173 SkTDArray<SkMatrix> matrices;
174 matrices.push()->reset();
175 SkMatrix* m = matrices.push();
176 m->setRotate(33.f, 25.f, 25.f);
177 m->postScale(1.2f, 0.8f, 25.f, 25.f);
178 SkPaint paint;
179 paint.setColor(SK_ColorGREEN);
180 paint.setAntiAlias(true);
Jim Van Verthe308a122017-05-08 14:19:30 -0400181 SkPoint3 zPlaneParams = SkPoint3::Make(0, 0, SkTMax(1.0f, kHeight + fZDelta));
Jim Van Verth22526362018-02-28 14:51:19 -0500182
183 // convex paths
Jim Van Verthe7705782017-05-04 14:00:59 -0400184 for (auto& m : matrices) {
185 for (auto flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) {
Jim Van Verth22526362018-02-28 14:51:19 -0500186 for (const auto& path : fConvexPaths) {
Jim Van Verthe7705782017-05-04 14:00:59 -0400187 SkRect postMBounds = path.getBounds();
188 m.mapRect(&postMBounds);
189 SkScalar w = postMBounds.width() + kHeight;
190 SkScalar dx = w + kPad;
191 if (x + dx > kW - 3 * kPad) {
192 canvas->restore();
193 canvas->translate(0, dy);
194 canvas->save();
195 x = 0;
196 dy = 0;
197 }
198
199 canvas->save();
200 canvas->concat(m);
Jim Van Verth22526362018-02-28 14:51:19 -0500201 this->drawShadowedPath(canvas, path, zPlaneParams, paint, kAmbientAlpha,
202 lightPos, kLightR, kSpotAlpha, flags);
Jim Van Verthe7705782017-05-04 14:00:59 -0400203 canvas->restore();
204
205 canvas->translate(dx, 0);
206 x += dx;
207 dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
208 }
209 }
210 }
Jim Van Verth22526362018-02-28 14:51:19 -0500211
212 // concave paths
213 canvas->restore();
214 canvas->translate(kPad, dy);
215 canvas->save();
216 x = kPad;
217 dy = 0;
218 for (auto& m : matrices) {
219 for (const auto& path : fConcavePaths) {
220 SkRect postMBounds = path.getBounds();
221 m.mapRect(&postMBounds);
222 SkScalar w = postMBounds.width();
223 SkScalar dx = w + kPad;
224
225 canvas->save();
226 canvas->concat(m);
227 this->drawShadowedPath(canvas, path, zPlaneParams, paint, kAmbientAlpha, lightPos,
228 kLightR, kSpotAlpha, kNone_ShadowFlag);
229 canvas->restore();
230
231 canvas->translate(dx, 0);
232 x += dx;
233 dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
234 }
235 }
236
Jim Van Verthe7705782017-05-04 14:00:59 -0400237 // Show where the light is in x,y as a circle (specified in device space).
238 SkMatrix invCanvasM = canvas->getTotalMatrix();
239 if (invCanvasM.invert(&invCanvasM)) {
240 canvas->save();
241 canvas->concat(invCanvasM);
242 SkPaint paint;
243 paint.setColor(SK_ColorBLACK);
244 paint.setAntiAlias(true);
Jim Van Verth9392f569c2017-05-16 15:01:43 -0400245 canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint);
Jim Van Verthe7705782017-05-04 14:00:59 -0400246 canvas->restore();
247 }
248 }
249
250private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400251 typedef Sample INHERITED;
Jim Van Verthe7705782017-05-04 14:00:59 -0400252};
253
254//////////////////////////////////////////////////////////////////////////////
255
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400256DEF_SAMPLE( return new ShadowUtilsView(); )