jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2016 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 | */ |
| 8 | #include "SampleCode.h" |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 10 | #include "SkBlurMask.h" |
| 11 | #include "SkBlurMaskFilter.h" |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 12 | #include "SkColorFilter.h" |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 13 | #include "SkCamera.h" |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
| 15 | #include "SkPath.h" |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 16 | #include "SkPathOps.h" |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 17 | #include "SkPoint3.h" |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 18 | #include "SkShadowUtils.h" |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 19 | #include "SkUtils.h" |
| 20 | #include "SkView.h" |
| 21 | #include "sk_tool_utils.h" |
| 22 | |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 23 | //////////////////////////////////////////////////////////////////////////// |
| 24 | |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 25 | class ShadowsView : public SampleView { |
| 26 | SkPath fRectPath; |
| 27 | SkPath fRRPath; |
| 28 | SkPath fCirclePath; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 29 | SkPath fFunkyRRPath; |
| 30 | SkPath fCubicPath; |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 31 | SkPath fStarPath; |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 32 | SkPath fSquareRRectPath; |
| 33 | SkPath fWideRectPath; |
| 34 | SkPath fWideOvalPath; |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 35 | SkPath fNotchPath; |
| 36 | SkPath fTabPath; |
| 37 | |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 38 | SkPoint3 fLightPos; |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 39 | SkScalar fZDelta; |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 40 | SkScalar fAnimTranslate; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 41 | SkScalar fAnimAngle; |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 42 | SkScalar fAnimAlpha; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 43 | |
| 44 | bool fShowAmbient; |
| 45 | bool fShowSpot; |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 46 | bool fUseAlt; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 47 | bool fShowObject; |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 48 | bool fIgnoreShadowAlpha; |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 49 | bool fDoAlphaAnimation; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 50 | |
| 51 | public: |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 52 | ShadowsView() |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 53 | : fZDelta(0) |
| 54 | , fAnimTranslate(0) |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 55 | , fAnimAngle(0) |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 56 | , fAnimAlpha(1) |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 57 | , fShowAmbient(true) |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 58 | , fShowSpot(true) |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 59 | , fUseAlt(false) |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 60 | , fShowObject(true) |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 61 | , fIgnoreShadowAlpha(false) |
| 62 | , fDoAlphaAnimation(false) {} |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 63 | |
| 64 | protected: |
| 65 | void onOnceBeforeDraw() override { |
| 66 | fCirclePath.addCircle(0, 0, 50); |
| 67 | fRectPath.addRect(SkRect::MakeXYWH(-100, -50, 200, 100)); |
| 68 | fRRPath.addRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-100, -50, 200, 100), 4, 4)); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 69 | fFunkyRRPath.addRoundRect(SkRect::MakeXYWH(-50, -50, SK_Scalar1 * 100, SK_Scalar1 * 100), |
| 70 | 40 * SK_Scalar1, 20 * SK_Scalar1, |
| 71 | SkPath::kCW_Direction); |
| 72 | fCubicPath.cubicTo(100 * SK_Scalar1, 50 * SK_Scalar1, |
| 73 | 20 * SK_Scalar1, 100 * SK_Scalar1, |
| 74 | 0 * SK_Scalar1, 0 * SK_Scalar1); |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 75 | fStarPath.moveTo(0.0f, -50.0f); |
| 76 | fStarPath.lineTo(14.43f, -25.0f); |
| 77 | fStarPath.lineTo(43.30f, -25.0f); |
| 78 | fStarPath.lineTo(28.86f, 0.0f); |
| 79 | fStarPath.lineTo(43.30f, 25.0f); |
| 80 | fStarPath.lineTo(14.43f, 25.0f); |
| 81 | fStarPath.lineTo(0.0f, 50.0f); |
| 82 | fStarPath.lineTo(-14.43f, 25.0f); |
| 83 | fStarPath.lineTo(-43.30f, 25.0f); |
| 84 | fStarPath.lineTo(-28.86f, 0.0f); |
| 85 | fStarPath.lineTo(-43.30f, -25.0f); |
| 86 | fStarPath.lineTo(-14.43f, -25.0f); |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 87 | fSquareRRectPath.addRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-50, -50, 100, 100), |
| 88 | 10, 10)); |
| 89 | fWideRectPath.addRect(SkRect::MakeXYWH(0, 0, 630, 70)); |
| 90 | fWideOvalPath.addOval(SkRect::MakeXYWH(0, 0, 630, 70)); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 91 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 92 | fNotchPath.moveTo(0, 80); |
| 93 | fNotchPath.arcTo(SkRect::MakeLTRB(-20, 80, 20, 120), -90, -90, false); |
| 94 | fNotchPath.lineTo(-75, 100); |
| 95 | fNotchPath.lineTo(-75, -100); |
| 96 | fNotchPath.lineTo(75, -100); |
| 97 | fNotchPath.lineTo(75, 100); |
| 98 | fNotchPath.arcTo(SkRect::MakeLTRB(-20, 80, 20, 120), 0, -90, false); |
| 99 | |
| 100 | fTabPath.moveTo(-75, -100); |
| 101 | fTabPath.lineTo(75, -100); |
| 102 | fTabPath.lineTo(75, 100); |
| 103 | fTabPath.arcTo(SkRect::MakeLTRB(-20, 80, 20, 120), 0, 90, false); |
| 104 | fTabPath.arcTo(SkRect::MakeLTRB(-20, 80, 20, 120), 90, 90, false); |
| 105 | fTabPath.lineTo(-75, 100); |
| 106 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 107 | fLightPos = SkPoint3::Make(350, 0, 600); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // overrides from SkEventSink |
| 111 | bool onQuery(SkEvent* evt) override { |
| 112 | if (SampleCode::TitleQ(*evt)) { |
| 113 | SampleCode::TitleR(evt, "AndroidShadows"); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | SkUnichar uni; |
| 118 | if (SampleCode::CharQ(*evt, &uni)) { |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 119 | bool handled = false; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 120 | switch (uni) { |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 121 | case 'W': |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 122 | fShowAmbient = !fShowAmbient; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 123 | handled = true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 124 | break; |
| 125 | case 'S': |
| 126 | fShowSpot = !fShowSpot; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 127 | handled = true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 128 | break; |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 129 | case 'T': |
| 130 | fUseAlt = !fUseAlt; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 131 | handled = true; |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 132 | break; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 133 | case 'O': |
| 134 | fShowObject = !fShowObject; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 135 | handled = true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 136 | break; |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 137 | case 'N': |
| 138 | fDoAlphaAnimation = !fDoAlphaAnimation; |
| 139 | if (!fDoAlphaAnimation) { |
| 140 | fAnimAlpha = 1; |
| 141 | } |
| 142 | handled = true; |
| 143 | break; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 144 | case '>': |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 145 | fZDelta += 0.5f; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 146 | handled = true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 147 | break; |
| 148 | case '<': |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 149 | fZDelta -= 0.5f; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 150 | handled = true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 151 | break; |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 152 | case '?': |
| 153 | fIgnoreShadowAlpha = !fIgnoreShadowAlpha; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 154 | handled = true; |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 155 | break; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 156 | default: |
| 157 | break; |
| 158 | } |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 159 | if (handled) { |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 160 | return true; |
| 161 | } |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 162 | } |
| 163 | return this->INHERITED::onQuery(evt); |
| 164 | } |
| 165 | |
| 166 | void drawBG(SkCanvas* canvas) { |
| 167 | canvas->drawColor(0xFFDDDDDD); |
| 168 | } |
| 169 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 170 | void drawShadowedPath(SkCanvas* canvas, const SkPath& path, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 171 | const SkPoint3& zPlaneParams, |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 172 | const SkPaint& paint, SkScalar ambientAlpha, |
| 173 | const SkPoint3& lightPos, SkScalar lightWidth, SkScalar spotAlpha) { |
Jim Van Verth | e770578 | 2017-05-04 14:00:59 -0400 | [diff] [blame] | 174 | if (fIgnoreShadowAlpha) { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 175 | ambientAlpha = 1; |
| 176 | spotAlpha = 1; |
Jim Van Verth | e770578 | 2017-05-04 14:00:59 -0400 | [diff] [blame] | 177 | } |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 178 | if (!fShowAmbient) { |
| 179 | ambientAlpha = 0; |
| 180 | } |
| 181 | if (!fShowSpot) { |
| 182 | spotAlpha = 0; |
| 183 | } |
| 184 | uint32_t flags = 0; |
Jim Van Verth | 3c1b7db | 2016-10-31 16:06:51 -0400 | [diff] [blame] | 185 | if (fUseAlt) { |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 186 | flags |= SkShadowFlags::kGeometricOnly_ShadowFlag; |
Jim Van Verth | 3c1b7db | 2016-10-31 16:06:51 -0400 | [diff] [blame] | 187 | } |
Jim Van Verth | 34d6e4b | 2017-06-09 11:09:03 -0400 | [diff] [blame] | 188 | |
Jim Van Verth | a556684 | 2018-02-22 10:58:34 -0500 | [diff] [blame] | 189 | SkColor ambientColor = SkColorSetARGB(ambientAlpha * 255, 0, 0, 0); |
| 190 | SkColor spotColor = SkColorSetARGB(spotAlpha * 255, 0, 0, 0); |
| 191 | SkShadowUtils::DrawShadow(canvas, path, zPlaneParams, lightPos, lightWidth, |
| 192 | ambientColor, spotColor, flags); |
Jim Van Verth | 3c1b7db | 2016-10-31 16:06:51 -0400 | [diff] [blame] | 193 | |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 194 | if (fShowObject) { |
| 195 | canvas->drawPath(path, paint); |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 196 | } else { |
| 197 | SkPaint strokePaint; |
| 198 | |
| 199 | strokePaint.setColor(paint.getColor()); |
| 200 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 201 | |
| 202 | canvas->drawPath(path, strokePaint); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | void onDrawContent(SkCanvas* canvas) override { |
| 207 | this->drawBG(canvas); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 208 | const SkScalar kLightWidth = 800; |
Jim Van Verth | 4c8c1e8 | 2018-04-23 17:14:48 -0400 | [diff] [blame] | 209 | const SkScalar kAmbientAlpha = 0.039f; |
| 210 | const SkScalar kSpotAlpha = 0.19f; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 211 | |
| 212 | SkPaint paint; |
| 213 | paint.setAntiAlias(true); |
| 214 | |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 215 | SkPoint3 lightPos = fLightPos; |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 216 | SkPoint3 zPlaneParams = SkPoint3::Make(0, 0, 0); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 217 | |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 218 | paint.setColor(SK_ColorWHITE); |
| 219 | canvas->translate(200, 90); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 220 | zPlaneParams.fZ = SkTMax(1.0f, 2 + fZDelta); |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 221 | this->drawShadowedPath(canvas, fRRPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha, |
| 222 | lightPos, kLightWidth, fAnimAlpha*kSpotAlpha); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 223 | |
| 224 | paint.setColor(SK_ColorRED); |
| 225 | canvas->translate(250, 0); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 226 | zPlaneParams.fZ = SkTMax(1.0f, 8 + fZDelta); |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 227 | this->drawShadowedPath(canvas, fRectPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha, |
| 228 | lightPos, kLightWidth, fAnimAlpha*kSpotAlpha); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 229 | |
| 230 | paint.setColor(SK_ColorBLUE); |
| 231 | canvas->translate(-250, 110); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 232 | zPlaneParams.fZ = SkTMax(1.0f, 12 + fZDelta); |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 233 | this->drawShadowedPath(canvas, fCirclePath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha, |
| 234 | lightPos, kLightWidth, fAnimAlpha*0.5f); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 235 | |
| 236 | paint.setColor(SK_ColorGREEN); |
| 237 | canvas->translate(250, 0); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 238 | zPlaneParams.fZ = SkTMax(1.0f, 64 + fZDelta); |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 239 | this->drawShadowedPath(canvas, fRRPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha, |
| 240 | lightPos, kLightWidth, fAnimAlpha*kSpotAlpha); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 241 | |
| 242 | paint.setColor(SK_ColorYELLOW); |
| 243 | canvas->translate(-250, 110); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 244 | zPlaneParams.fZ = SkTMax(1.0f, 8 + fZDelta); |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 245 | this->drawShadowedPath(canvas, fFunkyRRPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha, |
| 246 | lightPos, kLightWidth, fAnimAlpha*kSpotAlpha); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 247 | |
| 248 | paint.setColor(SK_ColorCYAN); |
| 249 | canvas->translate(250, 0); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 250 | zPlaneParams.fZ = SkTMax(1.0f, 16 + fZDelta); |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 251 | this->drawShadowedPath(canvas, fCubicPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha, |
| 252 | lightPos, kLightWidth, fAnimAlpha*kSpotAlpha); |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 253 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 254 | paint.setColor(SK_ColorWHITE); |
| 255 | canvas->translate(250, -180); |
| 256 | zPlaneParams.fZ = SkTMax(1.0f, 8 + fZDelta); |
| 257 | this->drawShadowedPath(canvas, fStarPath, zPlaneParams, paint, |
| 258 | kAmbientAlpha, lightPos, kLightWidth, kSpotAlpha); |
| 259 | |
| 260 | paint.setColor(SK_ColorWHITE); |
| 261 | canvas->translate(150, 0); |
| 262 | zPlaneParams.fZ = SkTMax(1.0f, 2 + fZDelta); |
| 263 | this->drawShadowedPath(canvas, fNotchPath, zPlaneParams, paint, |
| 264 | kAmbientAlpha, lightPos, kLightWidth, kSpotAlpha); |
| 265 | |
| 266 | paint.setColor(SK_ColorWHITE); |
| 267 | canvas->translate(200, 0); |
| 268 | zPlaneParams.fZ = SkTMax(1.0f, 16 + fZDelta); |
| 269 | this->drawShadowedPath(canvas, fTabPath, zPlaneParams, paint, |
| 270 | kAmbientAlpha, lightPos, kLightWidth, kSpotAlpha); |
| 271 | |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 272 | // circular reveal |
| 273 | SkPath tmpPath; |
| 274 | SkPath tmpClipPath; |
| 275 | tmpClipPath.addCircle(fAnimTranslate, 0, 60); |
| 276 | Op(fSquareRRectPath, tmpClipPath, kIntersect_SkPathOp, &tmpPath); |
| 277 | |
| 278 | paint.setColor(SK_ColorMAGENTA); |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 279 | canvas->translate(-725, 240); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 280 | zPlaneParams.fZ = SkTMax(1.0f, 32 + fZDelta); |
| 281 | this->drawShadowedPath(canvas, tmpPath, zPlaneParams, paint, .1f, |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 282 | lightPos, kLightWidth, .5f); |
| 283 | |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 284 | // path ops bug |
| 285 | SkPath tmpClipPathBug; |
| 286 | tmpClipPathBug.addCircle(88.0344925f, 0, 60); |
| 287 | Op(fSquareRRectPath, tmpClipPathBug, kIntersect_SkPathOp, &tmpPath); |
| 288 | |
| 289 | canvas->translate(250, 0); |
| 290 | zPlaneParams.fZ = SkTMax(1.0f, 32 + fZDelta); |
| 291 | this->drawShadowedPath(canvas, tmpPath, zPlaneParams, paint, .1f, |
| 292 | lightPos, kLightWidth, .5f); |
| 293 | |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 294 | // perspective paths |
| 295 | SkPoint pivot = SkPoint::Make(fWideRectPath.getBounds().width()/2, |
| 296 | fWideRectPath.getBounds().height()/2); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 297 | SkPoint translate = SkPoint::Make(100, 450); |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 298 | paint.setColor(SK_ColorWHITE); |
| 299 | Sk3DView view; |
| 300 | view.save(); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 301 | view.rotateX(fAnimAngle); |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 302 | SkMatrix persp; |
| 303 | view.getMatrix(&persp); |
| 304 | persp.preTranslate(-pivot.fX, -pivot.fY); |
| 305 | persp.postTranslate(pivot.fX + translate.fX, pivot.fY + translate.fY); |
| 306 | canvas->setMatrix(persp); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 307 | SkScalar radians = SkDegreesToRadians(fAnimAngle); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 308 | zPlaneParams = SkPoint3::Make(0, |
| 309 | SkScalarSin(-radians), |
| 310 | SkTMax(1.0f, 16 + fZDelta) - SkScalarSin(-radians)*pivot.fY); |
| 311 | this->drawShadowedPath(canvas, fWideRectPath, zPlaneParams, paint, .1f, |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 312 | lightPos, kLightWidth, .5f); |
| 313 | |
| 314 | pivot = SkPoint::Make(fWideOvalPath.getBounds().width() / 2, |
| 315 | fWideOvalPath.getBounds().height() / 2); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 316 | translate = SkPoint::Make(100, 600); |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 317 | view.restore(); |
Jim Van Verth | e39bc9f | 2018-06-14 14:43:00 -0400 | [diff] [blame] | 318 | view.save(); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 319 | view.rotateY(fAnimAngle); |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 320 | view.getMatrix(&persp); |
| 321 | persp.preTranslate(-pivot.fX, -pivot.fY); |
| 322 | persp.postTranslate(pivot.fX + translate.fX, pivot.fY + translate.fY); |
| 323 | canvas->setMatrix(persp); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 324 | zPlaneParams = SkPoint3::Make(-SkScalarSin(radians), |
| 325 | 0, |
| 326 | SkTMax(1.0f, 32 + fZDelta) + SkScalarSin(radians)*pivot.fX); |
| 327 | this->drawShadowedPath(canvas, fWideOvalPath, zPlaneParams, paint, .1f, |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 328 | lightPos, kLightWidth, .5f); |
Jim Van Verth | e39bc9f | 2018-06-14 14:43:00 -0400 | [diff] [blame] | 329 | |
| 330 | pivot = SkPoint::Make(fStarPath.getBounds().width() / 2, |
| 331 | fStarPath.getBounds().height() / 2); |
| 332 | translate = SkPoint::Make(700, 250); |
| 333 | view.restore(); |
| 334 | view.rotateY(fAnimAngle); |
| 335 | view.getMatrix(&persp); |
| 336 | persp.preTranslate(-pivot.fX, -pivot.fY); |
| 337 | persp.postTranslate(pivot.fX + translate.fX, pivot.fY + translate.fY); |
| 338 | canvas->setMatrix(persp); |
| 339 | zPlaneParams = SkPoint3::Make(-SkScalarSin(radians), |
| 340 | 0, |
| 341 | SkTMax(1.0f, 8 + fZDelta) + SkScalarSin(radians)*pivot.fX); |
| 342 | this->drawShadowedPath(canvas, fStarPath, zPlaneParams, paint, .1f, |
| 343 | lightPos, kLightWidth, .5f); |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | bool onAnimate(const SkAnimTimer& timer) override { |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 347 | fAnimTranslate = timer.pingPong(30, 0, 125, -125); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 348 | fAnimAngle = timer.pingPong(15, 0, 0, 20); |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 349 | if (fDoAlphaAnimation) { |
| 350 | fAnimAlpha = timer.pingPong(5, 0, 1, 0); |
| 351 | } |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 352 | return true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 353 | } |
| 354 | |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 355 | private: |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 356 | typedef SampleView INHERITED; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | ////////////////////////////////////////////////////////////////////////////// |
| 360 | |
| 361 | static SkView* MyFactory() { return new ShadowsView; } |
| 362 | static SkViewRegister reg(MyFactory); |