OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "SkPictureRecorder.h" | 9 #include "SkPictureRecorder.h" |
10 #include "SkShadowPaintFilterCanvas.h" | 10 #include "SkShadowPaintFilterCanvas.h" |
11 #include "SkShadowShader.h" | 11 #include "SkShadowShader.h" |
12 #include "SkSurface.h" | 12 #include "SkSurface.h" |
13 | 13 |
14 #ifdef SK_EXPERIMENTAL_SHADOWING | 14 #ifdef SK_EXPERIMENTAL_SHADOWING |
15 | 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 { | 16 class ShadowingView : public SampleView { |
33 public: | 17 public: |
34 ShadowingView() { | 18 ShadowingView() { |
35 | 19 |
36 this->setBGColor(0xFFCCCCCC); | 20 this->setBGColor(0xFFCCCCCC); |
37 SkLights::Builder builder; | 21 SkLights::Builder builder; |
38 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), | 22 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), |
39 SkVector3::Make(0.27f, 0.07f, 1.0f))); | 23 SkVector3::Make(0.27f, 0.07f, 1.0f))); |
40 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), | 24 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), |
41 SkVector3::Make(0.03f, 0.27f, 1.0f))); | 25 SkVector3::Make(0.03f, 0.27f, 1.0f))); |
(...skipping 10 matching lines...) Expand all Loading... | |
52 | 36 |
53 fTestRects[2].fColor = 0xFF8888EE; | 37 fTestRects[2].fColor = 0xFF8888EE; |
54 fTestRects[2].fDepth = 240; | 38 fTestRects[2].fDepth = 240; |
55 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); | 39 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); |
56 | 40 |
57 fSceneChanged = true; | 41 fSceneChanged = true; |
58 fLightsChanged = true; | 42 fLightsChanged = true; |
59 | 43 |
60 fSelectedRect = -1; | 44 fSelectedRect = -1; |
61 fMoveLight = false; | 45 fMoveLight = false; |
46 | |
47 fClearShadowMaps = false; | |
62 } | 48 } |
63 | 49 |
64 protected: | 50 protected: |
65 bool onQuery(SkEvent *evt) override { | 51 bool onQuery(SkEvent *evt) override { |
66 if (SampleCode::TitleQ(*evt)) { | 52 if (SampleCode::TitleQ(*evt)) { |
67 SampleCode::TitleR(evt, "shadowing"); | 53 SampleCode::TitleR(evt, "shadowing"); |
68 return true; | 54 return true; |
69 } | 55 } |
70 | 56 |
71 SkUnichar uni; | 57 SkUnichar uni; |
72 if (SampleCode::CharQ(*evt, &uni)) { | 58 if (SampleCode::CharQ(*evt, &uni)) { |
73 switch (uni) { | 59 switch (uni) { |
74 case 'L': | 60 case 'L': |
75 fMoveLight = !fMoveLight; | 61 fMoveLight = !fMoveLight; |
76 break; | 62 break; |
63 case 'd': | |
robertphillips
2016/08/08 16:28:25
// Raster generated shadow maps have their origin
vjiaoblack
2016/08/09 13:28:03
Done.
| |
64 // the draw canvas and default canvas have different origins | |
65 // thus this is necessary to redraw the shadowmaps to the | |
66 // correct orientation | |
67 fClearShadowMaps = true; | |
68 break; | |
77 default: | 69 default: |
78 break; | 70 break; |
79 } | 71 } |
80 } | 72 } |
81 return this->INHERITED::onQuery(evt); | 73 return this->INHERITED::onQuery(evt); |
82 } | 74 } |
83 | 75 |
84 sk_sp<SkPicture> makeTestPicture(int width, int height) { | 76 sk_sp<SkPicture> makeTestPicture(int width, int height) { |
85 SkPictureRecorder recorder; | 77 SkPictureRecorder recorder; |
86 | 78 |
(...skipping 17 matching lines...) Expand all Loading... | |
104 canvas->translateZ(fTestRects[0].fDepth); | 96 canvas->translateZ(fTestRects[0].fDepth); |
105 } else { | 97 } else { |
106 canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth ); | 98 canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth ); |
107 } | 99 } |
108 canvas->drawRect(fTestRects[i].fGeometry, paint); | 100 canvas->drawRect(fTestRects[i].fGeometry, paint); |
109 } | 101 } |
110 | 102 |
111 return recorder.finishRecordingAsPicture(); | 103 return recorder.finishRecordingAsPicture(); |
112 } | 104 } |
113 | 105 |
114 void updateDepthMaps(SkCanvas *canvas) { | 106 void onDrawContent(SkCanvas *canvas) override { |
115 for (int i = 0; i < fLights->numLights(); ++i) { | 107 if (fSceneChanged) { |
116 // skip over ambient lights; they don't cast shadows | 108 fPicture = this->makeTestPicture(kWidth, kHeight); |
117 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type()) { | 109 } |
118 continue; | 110 |
111 if (fSceneChanged || fLightsChanged) { | |
112 if (fClearShadowMaps) { | |
113 for (int i = 0; i < fLights->numLights(); i++) { | |
114 fLights->light(i).setShadowMap(nullptr); | |
115 } | |
116 fClearShadowMaps = false; | |
119 } | 117 } |
120 | 118 |
121 // TODO: maybe add a kDepth_8_SkColorType when vertices have depth | 119 canvas->setLights(fLights); |
122 // assume max depth is 255. | 120 canvas->drawShadowedPicture(fPicture, nullptr, nullptr); |
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 } | 121 } |
149 } | 122 } |
150 | 123 |
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 { | 124 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { |
214 return new SkView::Click(this); | 125 return new SkView::Click(this); |
215 } | 126 } |
216 | 127 |
217 bool onClick(Click *click) override { | 128 bool onClick(Click *click) override { |
218 SkScalar x = click->fCurr.fX; | 129 SkScalar x = click->fCurr.fX; |
219 SkScalar y = click->fCurr.fY; | 130 SkScalar y = click->fCurr.fY; |
220 | 131 |
221 SkScalar dx = x - click->fPrev.fX; | 132 SkScalar dx = x - click->fPrev.fX; |
222 SkScalar dy = y - click->fPrev.fY; | 133 SkScalar dy = y - click->fPrev.fY; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 } | 181 } |
271 | 182 |
272 return true; | 183 return true; |
273 } | 184 } |
274 | 185 |
275 private: | 186 private: |
276 static constexpr int kNumTestRects = 3; | 187 static constexpr int kNumTestRects = 3; |
277 | 188 |
278 static const int kWidth = 400; | 189 static const int kWidth = 400; |
279 static const int kHeight = 400; | 190 static const int kHeight = 400; |
191 bool fClearShadowMaps; | |
280 | 192 |
281 struct { | 193 struct { |
282 SkRect fGeometry; | 194 SkRect fGeometry; |
283 int fDepth; | 195 int fDepth; |
284 SkColor fColor; | 196 SkColor fColor; |
285 } fTestRects[kNumTestRects]; | 197 } fTestRects[kNumTestRects]; |
286 | 198 |
287 int fSelectedRect; | 199 int fSelectedRect; |
288 bool fMoveLight; | 200 bool fMoveLight; |
289 | 201 |
290 sk_sp<SkImage> fPovDepthMap; | |
291 sk_sp<SkImage> fDiffuseMap; | |
292 sk_sp<SkPicture> fPicture; | 202 sk_sp<SkPicture> fPicture; |
293 sk_sp<SkShader> fShadowShader; | |
294 | 203 |
295 bool fSceneChanged; | 204 bool fSceneChanged; |
296 bool fLightsChanged; | 205 bool fLightsChanged; |
297 | 206 |
298 sk_sp<SkLights> fLights; | 207 sk_sp<SkLights> fLights; |
299 | 208 |
300 typedef SampleView INHERITED; | 209 typedef SampleView INHERITED; |
301 }; | 210 }; |
302 | 211 |
303 ////////////////////////////////////////////////////////////////////////////// | 212 ////////////////////////////////////////////////////////////////////////////// |
304 static SkView* MyFactory() { return new ShadowingView; } | 213 static SkView* MyFactory() { return new ShadowingView; } |
305 static SkViewRegister reg(MyFactory); | 214 static SkViewRegister reg(MyFactory); |
306 | 215 |
307 #endif | 216 #endif |
OLD | NEW |