| 1 |
|
| 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 |
*/ |
| 8 |
#include "include/core/SkCanvas.h" |
| 9 |
#include "include/core/SkColorFilter.h" |
| 10 |
#include "include/core/SkPath.h" |
| 11 |
#include "include/core/SkPathBuilder.h" |
| 12 |
#include "include/core/SkPoint3.h" |
| 13 |
#include "include/core/SkRRect.h" |
| 14 |
#include "include/utils/SkCamera.h" |
| 15 |
#include "include/utils/SkShadowUtils.h" |
| 16 |
#include "src/base/SkUTF.h" |
| 17 |
#include "src/core/SkBlurMask.h" |
| 18 |
#include "tools/ToolUtils.h" |
| 19 |
#include "tools/viewer/Slide.h" |
| 20 |
|
| 21 |
using namespace skia_private; |
| 22 |
|
| 23 |
//////////////////////////////////////////////////////////////////////////// |
| 24 |
|
| 25 |
class ShadowUtilsSlide : public Slide { |
| 26 |
TArray<SkPath> fConvexPaths; |
| 27 |
TArray<SkPath> fConcavePaths; |
| 28 |
SkScalar fZDelta; |
| 29 |
|
| 30 |
bool fShowAmbient; |
| 31 |
bool fShowSpot; |
| 32 |
bool fUseAlt; |
| 33 |
bool fShowObject; |
| 34 |
bool fIgnoreShadowAlpha; |
| 35 |
|
| 36 |
public: |
| 37 |
ShadowUtilsSlide() |
| 38 |
: fZDelta(0) |
| 39 |
, fShowAmbient(true) |
| 40 |
, fShowSpot(true) |
| 41 |
, fUseAlt(false) |
| 42 |
, fShowObject(false) |
| 43 |
, fIgnoreShadowAlpha(false) { |
| 44 |
fName = "ShadowUtils"; |
| 45 |
} |
| 46 |
|
| 47 |
void load(SkScalar w, SkScalar h) override { |
| 48 |
fConvexPaths.push_back() = SkPath::RRect(SkRect::MakeWH(50, 50), 10, 10); |
| 49 |
SkRRect oddRRect; |
| 50 |
oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16); |
| 51 |
fConvexPaths.push_back() = SkPath::RRect(oddRRect); |
| 52 |
fConvexPaths.push_back() = SkPath::Rect(SkRect::MakeWH(50, 50)); |
| 53 |
fConvexPaths.push_back() = SkPath::Circle(25, 25, 25); |
| 54 |
fConvexPaths.push_back() = SkPathBuilder() |
| 55 |
.cubicTo(100, 50, 20, 100, 0, 0) |
| 56 |
.detach(); |
| 57 |
fConvexPaths.push_back() = SkPath::Oval(SkRect::MakeWH(20, 60)); |
| 58 |
|
| 59 |
// star |
| 60 |
fConcavePaths.push_back() = SkPathBuilder() |
| 61 |
.moveTo(0.0f, -33.3333f) |
| 62 |
.lineTo(9.62f, -16.6667f) |
| 63 |
.lineTo(28.867f, -16.6667f) |
| 64 |
.lineTo(19.24f, 0.0f) |
| 65 |
.lineTo(28.867f, 16.6667f) |
| 66 |
.lineTo(9.62f, 16.6667f) |
| 67 |
.lineTo(0.0f, 33.3333f) |
| 68 |
.lineTo(-9.62f, 16.6667f) |
| 69 |
.lineTo(-28.867f, 16.6667f) |
| 70 |
.lineTo(-19.24f, 0.0f) |
| 71 |
.lineTo(-28.867f, -16.6667f) |
| 72 |
.lineTo(-9.62f, -16.6667f) |
| 73 |
.close() |
| 74 |
.detach(); |
| 75 |
|
| 76 |
// dumbbell |
| 77 |
fConcavePaths.push_back() = SkPathBuilder() |
| 78 |
.moveTo(50, 0) |
| 79 |
.cubicTo(100, 25, 60, 50, 50, 0) |
| 80 |
.cubicTo(0, -25, 40, -50, 50, 0) |
| 81 |
.detach(); |
| 82 |
} |
| 83 |
|
| 84 |
bool onChar(SkUnichar uni) override { |
| 85 |
bool handled = false; |
| 86 |
switch (uni) { |
| 87 |
case 'W': |
| 88 |
fShowAmbient = !fShowAmbient; |
| 89 |
handled = true; |
| 90 |
break; |
| 91 |
case 'S': |
| 92 |
fShowSpot = !fShowSpot; |
| 93 |
handled = true; |
| 94 |
break; |
| 95 |
case 'T': |
| 96 |
fUseAlt = !fUseAlt; |
| 97 |
handled = true; |
| 98 |
break; |
| 99 |
case 'O': |
| 100 |
fShowObject = !fShowObject; |
| 101 |
handled = true; |
| 102 |
break; |
| 103 |
case '>': |
| 104 |
fZDelta += 0.5f; |
| 105 |
handled = true; |
| 106 |
break; |
| 107 |
case '<': |
| 108 |
fZDelta -= 0.5f; |
| 109 |
handled = true; |
| 110 |
break; |
| 111 |
case '?': |
| 112 |
fIgnoreShadowAlpha = !fIgnoreShadowAlpha; |
| 113 |
handled = true; |
| 114 |
break; |
| 115 |
default: |
| 116 |
break; |
| 117 |
} |
| 118 |
if (handled) { |
| 119 |
return true; |
| 120 |
} |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
void draw(SkCanvas* canvas) override { |
| 125 |
this->drawBG(canvas); |
| 126 |
|
| 127 |
static constexpr int kW = 800; |
| 128 |
static constexpr SkScalar kPad = 15.f; |
| 129 |
static constexpr SkScalar kLightR = 100.f; |
| 130 |
static constexpr SkScalar kHeight = 50.f; |
| 131 |
static constexpr SkScalar kAmbientAlpha = 0.5f; |
| 132 |
static constexpr SkScalar kSpotAlpha = 0.5f; |
| 133 |
static constexpr SkPoint3 lightPos = { 250, 400, 500 }; |
| 134 |
|
| 135 |
canvas->translate(3 * kPad, 3 * kPad); |
| 136 |
canvas->save(); |
| 137 |
SkScalar x = 0; |
| 138 |
SkScalar dy = 0; |
| 139 |
SkTDArray<SkMatrix> matrices; |
| 140 |
matrices.append()->reset(); |
| 141 |
matrices.append()->setRotate(33.f, 25.f, 25.f).postScale(1.2f, 0.8f, 25.f, 25.f); |
| 142 |
SkPaint greenPaint; |
| 143 |
greenPaint.setColor(SK_ColorGREEN); |
| 144 |
greenPaint.setAntiAlias(true); |
| 145 |
SkPoint3 zPlaneParams = SkPoint3::Make(0, 0, std::max(1.0f, kHeight + fZDelta)); |
| 146 |
|
| 147 |
// convex paths |
| 148 |
for (auto& m : matrices) { |
| 149 |
for (auto flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) { |
| 150 |
for (const auto& path : fConvexPaths) { |
| 151 |
SkRect postMBounds = path.getBounds(); |
| 152 |
m.mapRect(&postMBounds); |
| 153 |
SkScalar w = postMBounds.width() + kHeight; |
| 154 |
SkScalar dx = w + kPad; |
| 155 |
if (x + dx > kW - 3 * kPad) { |
| 156 |
canvas->restore(); |
| 157 |
canvas->translate(0, dy); |
| 158 |
canvas->save(); |
| 159 |
x = 0; |
| 160 |
dy = 0; |
| 161 |
} |
| 162 |
|
| 163 |
canvas->save(); |
| 164 |
canvas->concat(m); |
| 165 |
this->drawShadowedPath(canvas, path, zPlaneParams, greenPaint, kAmbientAlpha, |
| 166 |
lightPos, kLightR, kSpotAlpha, flags); |
| 167 |
canvas->restore(); |
| 168 |
|
| 169 |
canvas->translate(dx, 0); |
| 170 |
x += dx; |
| 171 |
dy = std::max(dy, postMBounds.height() + kPad + kHeight); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// concave paths |
| 177 |
canvas->restore(); |
| 178 |
canvas->translate(kPad, dy); |
| 179 |
canvas->save(); |
| 180 |
x = kPad; |
| 181 |
dy = 0; |
| 182 |
for (auto& m : matrices) { |
| 183 |
for (const auto& path : fConcavePaths) { |
| 184 |
SkRect postMBounds = path.getBounds(); |
| 185 |
m.mapRect(&postMBounds); |
| 186 |
SkScalar w = postMBounds.width(); |
| 187 |
SkScalar dx = w + kPad; |
| 188 |
|
| 189 |
canvas->save(); |
| 190 |
canvas->concat(m); |
| 191 |
this->drawShadowedPath(canvas, path, zPlaneParams, greenPaint, kAmbientAlpha, |
| 192 |
lightPos, kLightR, kSpotAlpha, kNone_ShadowFlag); |
| 193 |
canvas->restore(); |
| 194 |
|
| 195 |
canvas->translate(dx, 0); |
| 196 |
x += dx; |
| 197 |
dy = std::max(dy, postMBounds.height() + kPad + kHeight); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
// Show where the light is in x,y as a circle (specified in device space). |
| 202 |
SkMatrix invCanvasM = canvas->getTotalMatrix(); |
| 203 |
if (invCanvasM.invert(&invCanvasM)) { |
| 204 |
canvas->save(); |
| 205 |
canvas->concat(invCanvasM); |
| 206 |
SkPaint blackPaint; |
| 207 |
blackPaint.setColor(SK_ColorBLACK); |
| 208 |
blackPaint.setAntiAlias(true); |
| 209 |
canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, blackPaint); |
| 210 |
canvas->restore(); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
private: |
| 215 |
void drawBG(SkCanvas* canvas) { |
| 216 |
canvas->drawColor(0xFFFFFFFF); |
| 217 |
} |
| 218 |
|
| 219 |
void drawShadowedPath(SkCanvas* canvas, const SkPath& path, |
| 220 |
const SkPoint3& zPlaneParams, |
| 221 |
const SkPaint& paint, SkScalar ambientAlpha, |
| 222 |
const SkPoint3& lightPos, SkScalar lightWidth, SkScalar spotAlpha, |
| 223 |
uint32_t flags) { |
| 224 |
if (fIgnoreShadowAlpha) { |
| 225 |
ambientAlpha = 255; |
| 226 |
spotAlpha = 255; |
| 227 |
} |
| 228 |
if (!fShowAmbient) { |
| 229 |
ambientAlpha = 0; |
| 230 |
} |
| 231 |
if (!fShowSpot) { |
| 232 |
spotAlpha = 0; |
| 233 |
} |
| 234 |
if (fUseAlt) { |
| 235 |
flags |= SkShadowFlags::kGeometricOnly_ShadowFlag; |
| 236 |
} |
| 237 |
|
| 238 |
SkColor ambientColor = SkColorSetARGB(ambientAlpha * 255, 255, 0, 0); |
| 239 |
SkColor spotColor = SkColorSetARGB(spotAlpha * 255, 0, 0, 255); |
| 240 |
SkShadowUtils::DrawShadow(canvas, path, zPlaneParams, |
| 241 |
lightPos, lightWidth, |
| 242 |
ambientColor, spotColor, flags); |
| 243 |
|
| 244 |
if (fShowObject) { |
| 245 |
canvas->drawPath(path, paint); |
| 246 |
} else { |
| 247 |
SkPaint strokePaint; |
| 248 |
|
| 249 |
strokePaint.setColor(paint.getColor()); |
| 250 |
strokePaint.setStyle(SkPaint::kStroke_Style); |
| 251 |
|
| 252 |
canvas->drawPath(path, strokePaint); |
| 253 |
} |
| 254 |
} |
| 255 |
}; |
| 256 |
|
| 257 |
////////////////////////////////////////////////////////////////////////////// |
| 258 |
|
| 259 |
DEF_SLIDE( return new ShadowUtilsSlide(); ) |