OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SampleCode.h" |
| 9 #include "SkPictureRecorder.h" |
| 10 #include "SkShadowPaintFilterCanvas.h" |
| 11 #include "SkShadowShader.h" |
| 12 #include "SkSurface.h" |
| 13 |
| 14 #ifdef SK_EXPERIMENTAL_SHADOWING |
| 15 |
| 16 static sk_sp<SkShader> make_shadow_shader(sk_sp<SkImage> povDepth, |
| 17 sk_sp<SkImage> diffuse, |
| 18 sk_sp<SkLights> lights) { |
| 19 |
| 20 sk_sp<SkShader> povDepthShader = povDepth->makeShader(SkShader::kClamp_TileM
ode, |
| 21 SkShader::kClamp_TileM
ode); |
| 22 |
| 23 sk_sp<SkShader> diffuseShader = diffuse->makeShader(SkShader::kClamp_TileMod
e, |
| 24 SkShader::kClamp_TileMod
e); |
| 25 |
| 26 return SkShadowShader::Make(std::move(povDepthShader), |
| 27 std::move(diffuseShader), |
| 28 std::move(lights), |
| 29 diffuse->width(), diffuse->height()); |
| 30 } |
| 31 |
| 32 class ShadowingView : public SampleView { |
| 33 public: |
| 34 ShadowingView() { |
| 35 |
| 36 this->setBGColor(0xFFCCCCCC); |
| 37 SkLights::Builder builder; |
| 38 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), |
| 39 SkVector3::Make(0.27f, 0.07f, 1.0f))); |
| 40 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), |
| 41 SkVector3::Make(0.03f, 0.27f, 1.0f))); |
| 42 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); |
| 43 fLights = builder.finish(); |
| 44 |
| 45 fTestRects[0].fColor = 0xFFEE8888; |
| 46 fTestRects[0].fDepth = 80; |
| 47 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300); |
| 48 |
| 49 fTestRects[1].fColor = 0xFF88EE88; |
| 50 fTestRects[1].fDepth = 160; |
| 51 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350); |
| 52 |
| 53 fTestRects[2].fColor = 0xFF8888EE; |
| 54 fTestRects[2].fDepth = 240; |
| 55 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); |
| 56 |
| 57 fSceneChanged = true; |
| 58 fLightsChanged = true; |
| 59 |
| 60 fSelectedRect = -1; |
| 61 fMoveLight = false; |
| 62 } |
| 63 |
| 64 protected: |
| 65 bool onQuery(SkEvent *evt) override { |
| 66 if (SampleCode::TitleQ(*evt)) { |
| 67 SampleCode::TitleR(evt, "shadowing"); |
| 68 return true; |
| 69 } |
| 70 |
| 71 SkUnichar uni; |
| 72 if (SampleCode::CharQ(*evt, &uni)) { |
| 73 switch (uni) { |
| 74 case 'L': |
| 75 fMoveLight = !fMoveLight; |
| 76 break; |
| 77 default: |
| 78 break; |
| 79 } |
| 80 } |
| 81 return this->INHERITED::onQuery(evt); |
| 82 } |
| 83 |
| 84 sk_sp<SkPicture> makeTestPicture(int width, int height) { |
| 85 SkPictureRecorder recorder; |
| 86 |
| 87 // LONG RANGE TODO: eventually add SkBBHFactory (bounding box factory) |
| 88 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeIWH(width, height
)); |
| 89 |
| 90 SkASSERT(canvas->getTotalMatrix().isIdentity()); |
| 91 SkPaint paint; |
| 92 paint.setColor(SK_ColorGRAY); |
| 93 |
| 94 // LONG RANGE TODO: tag occluders |
| 95 // LONG RANGE TODO: track number of IDs we need (hopefully less than 256
) |
| 96 // and determinate the mapping from z to id |
| 97 |
| 98 // universal receiver, "ground" |
| 99 canvas->drawRect(SkRect::MakeIWH(width, height), paint); |
| 100 |
| 101 for (int i = 0; i < kNumTestRects; i++) { |
| 102 paint.setColor(fTestRects[i].fColor); |
| 103 if (i == 0) { |
| 104 canvas->translateZ(fTestRects[0].fDepth); |
| 105 } else { |
| 106 canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth
); |
| 107 } |
| 108 canvas->drawRect(fTestRects[i].fGeometry, paint); |
| 109 } |
| 110 |
| 111 return recorder.finishRecordingAsPicture(); |
| 112 } |
| 113 |
| 114 void updateDepthMaps(SkCanvas *canvas) { |
| 115 for (int i = 0; i < fLights->numLights(); ++i) { |
| 116 // skip over ambient lights; they don't cast shadows |
| 117 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type())
{ |
| 118 continue; |
| 119 } |
| 120 |
| 121 // TODO: maybe add a kDepth_8_SkColorType when vertices have depth |
| 122 // assume max depth is 255. |
| 123 // TODO: find actual max depth of picture |
| 124 SkISize shMapSize = SkShadowPaintFilterCanvas:: |
| 125 ComputeDepthMapSize(fLights->light(i), 255, kWid
th, kHeight); |
| 126 |
| 127 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHe
ight, |
| 128 kBGRA_8888_SkColorType, |
| 129 kOpaque_SkAlphaType); |
| 130 |
| 131 // Create a new surface (that matches the backend of canvas) |
| 132 // for each shadow map |
| 133 sk_sp<SkSurface> surf(canvas->makeSurface(info)); |
| 134 |
| 135 // Wrap another SPFCanvas around the surface |
| 136 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
| 137 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| 138 |
| 139 // set the depth map canvas to have the light we're drawing. |
| 140 SkLights::Builder builder; |
| 141 builder.add(fLights->light(i)); |
| 142 sk_sp<SkLights> curLight = builder.finish(); |
| 143 |
| 144 depthMapCanvas->setLights(std::move(curLight)); |
| 145 depthMapCanvas->drawPicture(fPicture); |
| 146 |
| 147 fLights->light(i).setShadowMap(surf->makeImageSnapshot()); |
| 148 } |
| 149 } |
| 150 |
| 151 void updatePovDepthMap(SkCanvas* canvas) { |
| 152 // TODO: pass the depth to the shader in vertices, or uniforms |
| 153 // so we don't have to render depth and color separately |
| 154 |
| 155 SkLights::Builder builder; |
| 156 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
| 157 SkVector3::Make(0.0f, 0.0f, 1.0f))); |
| 158 sk_sp<SkLights> povLight = builder.finish(); |
| 159 |
| 160 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, |
| 161 kBGRA_8888_SkColorType, |
| 162 kOpaque_SkAlphaType); |
| 163 |
| 164 // Create a new surface (that matches the backend of canvas) |
| 165 // to create the povDepthMap |
| 166 sk_sp<SkSurface> surf(canvas->makeSurface(info)); |
| 167 |
| 168 // Wrap another SPFCanvas around the surface |
| 169 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
| 170 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| 171 |
| 172 // set the depth map canvas to have the light as the user's POV |
| 173 depthMapCanvas->setLights(std::move(povLight)); |
| 174 |
| 175 depthMapCanvas->drawPicture(fPicture); |
| 176 |
| 177 fPovDepthMap = surf->makeImageSnapshot(); |
| 178 } |
| 179 |
| 180 void updateDiffuseMap(SkCanvas* canvas) { |
| 181 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, |
| 182 kBGRA_8888_SkColorType, |
| 183 kOpaque_SkAlphaType); |
| 184 |
| 185 sk_sp<SkSurface> surf(canvas->makeSurface(info)); |
| 186 surf->getCanvas()->drawPicture(fPicture); |
| 187 |
| 188 fDiffuseMap = surf->makeImageSnapshot(); |
| 189 } |
| 190 |
| 191 void onDrawContent(SkCanvas *canvas) override { |
| 192 if (fSceneChanged || !fPovDepthMap) { |
| 193 fPicture = this->makeTestPicture(kWidth, kHeight); |
| 194 this->updateDepthMaps(canvas); |
| 195 this->updatePovDepthMap(canvas); |
| 196 this->updateDiffuseMap(canvas); |
| 197 } |
| 198 |
| 199 if (fLightsChanged) { |
| 200 this->updateDepthMaps(canvas); |
| 201 } |
| 202 |
| 203 if (fSceneChanged || fLightsChanged || !fShadowShader) { |
| 204 fShadowShader = make_shadow_shader(fPovDepthMap, fDiffuseMap, fLight
s); |
| 205 } |
| 206 |
| 207 SkPaint paint; |
| 208 paint.setShader(fShadowShader); |
| 209 |
| 210 canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint); |
| 211 } |
| 212 |
| 213 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove
rride { |
| 214 return new SkView::Click(this); |
| 215 } |
| 216 |
| 217 bool onClick(Click *click) override { |
| 218 SkScalar x = click->fCurr.fX; |
| 219 SkScalar y = click->fCurr.fY; |
| 220 |
| 221 SkScalar dx = x - click->fPrev.fX; |
| 222 SkScalar dy = y - click->fPrev.fY; |
| 223 |
| 224 if (fMoveLight) { |
| 225 if (dx != 0 || dy != 0) { |
| 226 float recipX = 1.0f / kWidth; |
| 227 float recipY = 1.0f / kHeight; |
| 228 |
| 229 SkLights::Builder builder; |
| 230 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), |
| 231 SkVector3::Make(0.12f + (200.0f - x)
* recipX, |
| 232 -0.08f + (200.0f - y
) * recipY, |
| 233 1.0f))); |
| 234 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), |
| 235 SkVector3::Make(-0.12f + (200.0f - x
) * recipX, |
| 236 0.12f + (200.0f - y)
* recipY, |
| 237 1.0f))); |
| 238 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); |
| 239 fLights = builder.finish(); |
| 240 |
| 241 fLightsChanged = true; |
| 242 this->inval(nullptr); |
| 243 } |
| 244 return true; |
| 245 } |
| 246 |
| 247 if (click->fState == Click::State::kUp_State) { |
| 248 fSelectedRect = -1; |
| 249 return true; |
| 250 } |
| 251 |
| 252 if (fSelectedRect > -1) { |
| 253 fTestRects[fSelectedRect].fGeometry.offset(dx, dy); |
| 254 |
| 255 fSceneChanged = true; |
| 256 this->inval(nullptr); |
| 257 return true; |
| 258 } |
| 259 |
| 260 // assume last elements are highest |
| 261 for (int i = kNumTestRects - 1; i >= 0; i--) { |
| 262 if (fTestRects[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1)))
{ |
| 263 fSelectedRect = i; |
| 264 fTestRects[i].fGeometry.offset(dx, dy); |
| 265 |
| 266 fSceneChanged = true; |
| 267 this->inval(nullptr); |
| 268 break; |
| 269 } |
| 270 } |
| 271 |
| 272 return true; |
| 273 } |
| 274 |
| 275 private: |
| 276 static constexpr int kNumTestRects = 3; |
| 277 |
| 278 static const int kWidth = 400; |
| 279 static const int kHeight = 400; |
| 280 |
| 281 struct { |
| 282 SkRect fGeometry; |
| 283 int fDepth; |
| 284 SkColor fColor; |
| 285 } fTestRects[kNumTestRects]; |
| 286 |
| 287 int fSelectedRect; |
| 288 bool fMoveLight; |
| 289 |
| 290 sk_sp<SkImage> fPovDepthMap; |
| 291 sk_sp<SkImage> fDiffuseMap; |
| 292 sk_sp<SkPicture> fPicture; |
| 293 sk_sp<SkShader> fShadowShader; |
| 294 |
| 295 bool fSceneChanged; |
| 296 bool fLightsChanged; |
| 297 |
| 298 sk_sp<SkLights> fLights; |
| 299 |
| 300 typedef SampleView INHERITED; |
| 301 }; |
| 302 |
| 303 ////////////////////////////////////////////////////////////////////////////// |
| 304 static SkView* MyFactory() { return new ShadowingView; } |
| 305 static SkViewRegister reg(MyFactory); |
| 306 |
| 307 #endif |
OLD | NEW |