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