Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Side by Side Diff: samplecode/SampleShadowing.cpp

Issue 2246463004: Added distance attenuation and diffuse shading to PointLights (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: disabled shadows: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/core/SkLights.h ('k') | src/core/SkLights.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 class ShadowingView : public SampleView { 16 class ShadowingView : public SampleView {
17 public: 17 public:
18 ShadowingView() { 18 ShadowingView()
19 : fSceneChanged(true)
20 , fLightsChanged(true)
21 , fMoveLight(false)
22 , fClearShadowMaps(false)
23 , fSelectedRectID(-1)
24 , fSelectedSliderID(-1)
25 , fLightDepth(300.0f) {
26 this->setBGColor(0xFFCCCCCC);
19 27
20 this->setBGColor(0xFFCCCCCC); 28 this->updateLights(200, 200);
21 SkLights::Builder builder;
22 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.2f, 0.3f, 0.4f),
23 SkVector3::Make(0.2f, 0.05f , 1.0f)));
24 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.4f, 0.3f, 0.2f),
25 SkVector3::Make(0.05f, 0.2f , 1.0f)));
26 builder.add(SkLights::Light::MakeAmbient(SkColor3f::Make(0.4f, 0.4f, 0.4 f)));
27 fLights = builder.finish();
28 29
29 fTestRects[0].fColor = 0xFFEE8888; 30 fTestRects[0].fColor = 0xFFEE8888;
30 fTestRects[0].fDepth = 80; 31 fTestRects[0].fDepth = 80;
31 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300); 32 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300);
32 33
33 fTestRects[1].fColor = 0xFF88EE88; 34 fTestRects[1].fColor = 0xFF88EE88;
34 fTestRects[1].fDepth = 160; 35 fTestRects[1].fDepth = 160;
35 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350); 36 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350);
36 37
37 fTestRects[2].fColor = 0xFF8888EE; 38 fTestRects[2].fColor = 0xFF8888EE;
38 fTestRects[2].fDepth = 240; 39 fTestRects[2].fDepth = 240;
39 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); 40 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250);
40 41
41 fSliders[0].fGeometry = SkRect::MakeLTRB(20, 400, 30, 420); 42 fSliders[0].fGeometry = SkRect::MakeLTRB(20, 400, 30, 420);
42 fSliders[0].fOffset = 0.0f; 43 fSliders[0].fOffset = 0.0f;
43 fSliders[0].fScale = 0.1f; 44 fSliders[0].fScale = 0.1f;
44 45
45 fSliders[1].fGeometry = SkRect::MakeLTRB(100, 420, 110, 440); 46 fSliders[1].fGeometry = SkRect::MakeLTRB(100, 420, 110, 440);
46 fSliders[1].fOffset = 0.0f; 47 fSliders[1].fOffset = 0.0f;
47 fSliders[1].fScale = 10.0f; 48 fSliders[1].fScale = 10.0f;
48 49
49 fSliders[2].fGeometry = SkRect::MakeLTRB(0, 440, 10, 460); 50 fSliders[2].fGeometry = SkRect::MakeLTRB(0, 440, 10, 460);
50 fSliders[2].fOffset = 0.0f; 51 fSliders[2].fOffset = 0.0f;
51 fSliders[2].fScale = 0.0025f; 52 fSliders[2].fScale = 0.0025f;
52 53
53 fSceneChanged = true; 54 fShadowParams.fShadowRadius = 4.0f;
54 fLightsChanged = true;
55
56 fSelectedRect = -1;
57 fSelectedSlider = -1;
58 fMoveLight = false;
59
60 fClearShadowMaps = false;
61
62 fShadowParams.fShadowRadius = 2.0f;
63 fShadowParams.fBiasingConstant = 0.3f; 55 fShadowParams.fBiasingConstant = 0.3f;
64 fShadowParams.fMinVariance = 1024; 56 fShadowParams.fMinVariance = 1024;
65 fShadowParams.fType = SkShadowParams::kVariance_ShadowType; 57 fShadowParams.fType = SkShadowParams::kVariance_ShadowType;
66 } 58 }
67 59
68 protected: 60 protected:
69 bool onQuery(SkEvent *evt) override { 61 bool onQuery(SkEvent *evt) override {
70 if (SampleCode::TitleQ(*evt)) { 62 if (SampleCode::TitleQ(*evt)) {
71 SampleCode::TitleR(evt, "shadowing"); 63 SampleCode::TitleR(evt, "shadowing");
72 return true; 64 return true;
73 } 65 }
74 66
75 SkUnichar uni; 67 SkUnichar uni;
76 if (SampleCode::CharQ(*evt, &uni)) { 68 if (SampleCode::CharQ(*evt, &uni)) {
77 switch (uni) { 69 switch (uni) {
78 case 'L': 70 case 'L':
79 fMoveLight = !fMoveLight; 71 fMoveLight = !fMoveLight;
80 break; 72 break;
81 case 'd': 73 case 'd':
82 // Raster generated shadow maps have their origin in the UL corner 74 // Raster generated shadow maps have their origin in the UL corner
83 // GPU shadow maps can have an arbitrary origin. 75 // GPU shadow maps can have an arbitrary origin.
84 // We override the 'd' keypress so that when the device is c ycled, 76 // We override the 'd' keypress so that when the device is c ycled,
85 // the shadow maps will be re-generated according to the new backend. 77 // the shadow maps will be re-generated according to the new backend.
86 fClearShadowMaps = true; 78 fClearShadowMaps = true;
87 break; 79 break;
80 case 'q':
81 fLightDepth += 5.0f;
82 fMoveLight = true;
88 case 'B': 83 case 'B':
89 if (SkShadowParams::kVariance_ShadowType == fShadowParams.fT ype) { 84 if (SkShadowParams::kVariance_ShadowType == fShadowParams.fT ype) {
90 fShadowParams.fType = SkShadowParams::kNoBlur_ShadowType ; 85 fShadowParams.fType = SkShadowParams::kNoBlur_ShadowType ;
91 } else if (SkShadowParams::kNoBlur_ShadowType == 86 } else if (SkShadowParams::kNoBlur_ShadowType ==
92 fShadowParams.fType) { 87 fShadowParams.fType) {
93 fShadowParams.fType = SkShadowParams::kVariance_ShadowTy pe; 88 fShadowParams.fType = SkShadowParams::kVariance_ShadowTy pe;
94 } 89 }
95 fLightsChanged = true; 90 fLightsChanged = true;
96 break; 91 break;
92 case 'w':
93 fLightDepth -= 5.0f;
94 fMoveLight = true;
95 break;
97 default: 96 default:
98 break; 97 break;
99 } 98 }
100 } 99 }
101 return this->INHERITED::onQuery(evt); 100 return this->INHERITED::onQuery(evt);
102 } 101 }
103 102
104 sk_sp<SkPicture> makeTestPicture(int width, int height) { 103 sk_sp<SkPicture> makeTestPicture(int width, int height) {
105 SkPictureRecorder recorder; 104 SkPictureRecorder recorder;
106 105
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 SkPaint paint; 152 SkPaint paint;
154 paint.setColor(SK_ColorBLACK); 153 paint.setColor(SK_ColorBLACK);
155 canvas->drawRect(fSliders[i].fGeometry, paint); 154 canvas->drawRect(fSliders[i].fGeometry, paint);
156 } 155 }
157 } 156 }
158 157
159 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { 158 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride {
160 return new SkView::Click(this); 159 return new SkView::Click(this);
161 } 160 }
162 161
162 void updateLights(int x, int y) {
163 SkLights::Builder builder;
164 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.2f, 0.4f, 0.6f) ,
165 SkVector3::Make(x - 50,
166 350 - y,
167 fLightDepth),
168 1024));
169 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.6f, 0.4f, 0.2f) ,
170 SkVector3::Make(x + 50,
171 450 - y,
172 fLightDepth),
173 1024));
174 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.2f, 0.2f, 0.2f),
175 SkVector3::Make(0.2f, 0.2f, 1.0f)));
176 fLights = builder.finish();
177 }
178
163 void updateFromSelectedSlider() { 179 void updateFromSelectedSlider() {
164 SkScalar newValue = fSliders[fSelectedSlider].fGeometry.fLeft * 180 SkScalar newValue = fSliders[fSelectedSliderID].fGeometry.fLeft *
165 fSliders[fSelectedSlider].fScale + 181 fSliders[fSelectedSliderID].fScale +
166 fSliders[fSelectedSlider].fOffset; 182 fSliders[fSelectedSliderID].fOffset;
167 183
168 switch (fSelectedSlider) { 184 switch (fSelectedSliderID) {
169 case 0: 185 case 0:
170 fShadowParams.fShadowRadius = newValue; 186 fShadowParams.fShadowRadius = newValue;
171 break; 187 break;
172 case 1: 188 case 1:
173 fShadowParams.fMinVariance = newValue; 189 fShadowParams.fMinVariance = newValue;
174 break; 190 break;
175 case 2: 191 case 2:
176 fShadowParams.fBiasingConstant = newValue; 192 fShadowParams.fBiasingConstant = newValue;
177 break; 193 break;
178 default: 194 default:
179 break; 195 break;
180 } 196 }
181 } 197 }
182 198
183 bool onClick(Click *click) override { 199 bool onClick(Click *click) override {
184 SkScalar x = click->fCurr.fX; 200 SkScalar x = click->fCurr.fX;
185 SkScalar y = click->fCurr.fY; 201 SkScalar y = click->fCurr.fY;
186 202
187 SkScalar dx = x - click->fPrev.fX; 203 SkScalar dx = x - click->fPrev.fX;
188 SkScalar dy = y - click->fPrev.fY; 204 SkScalar dy = y - click->fPrev.fY;
189 205
190 if (fMoveLight) { 206 if (fMoveLight) {
191 if (dx != 0 || dy != 0) { 207 if (dx != 0 || dy != 0) {
192 float recipX = 1.0f / kWidth; 208 this->updateLights(x, y);
193 float recipY = 1.0f / kHeight;
194
195 SkLights::Builder builder;
196 builder.add(SkLights::Light::MakeDirectional(
197 SkColor3f::Make(0.2f, 0.3f, 0.4f),
198 SkVector3::Make(0.2f + (200.0f - x) * recipX,
199 0.05f + (200.0f - y) * recipY,
200 1.0f)));
201 builder.add(SkLights::Light::MakeDirectional(
202 SkColor3f::Make(0.4f, 0.3f, 0.2f),
203 SkVector3::Make(0.05f + (200.0f - x) * recipX,
204 0.2f + (200.0f - y) * recipY,
205 1.0f)));
206 builder.add(SkLights::Light::MakeAmbient(
207 SkColor3f::Make(0.4f, 0.4f, 0.4f)));
208 fLights = builder.finish();
209
210 fLightsChanged = true; 209 fLightsChanged = true;
211 this->inval(nullptr); 210 this->inval(nullptr);
212 } 211 }
213 return true; 212 return true;
214 } 213 }
215 214
216 if (click->fState == Click::State::kUp_State) { 215 if (click->fState == Click::State::kUp_State) {
217 fSelectedRect = -1; 216 fSelectedRectID = -1;
218 fSelectedSlider = -1; 217 fSelectedSliderID = -1;
219 return true; 218 return true;
220 } 219 }
221 220
222 if (fSelectedRect > -1) { 221 if (fSelectedRectID > -1) {
223 fTestRects[fSelectedRect].fGeometry.offset(dx, dy); 222 fTestRects[fSelectedRectID].fGeometry.offset(dx, dy);
224 223
225 fSceneChanged = true; 224 fSceneChanged = true;
226 this->inval(nullptr); 225 this->inval(nullptr);
227 return true; 226 return true;
228 } 227 }
229 228
230 if (fSelectedSlider > -1) { 229 if (fSelectedSliderID > -1) {
231 fSliders[fSelectedSlider].fGeometry.offset(dx, 0); 230 fSliders[fSelectedSliderID].fGeometry.offset(dx, 0);
232 231
233 this->updateFromSelectedSlider(); 232 this->updateFromSelectedSlider();
234 233
235 fLightsChanged = true; 234 fLightsChanged = true;
236 this->inval(nullptr); 235 this->inval(nullptr);
237 return true; 236 return true;
238 } 237 }
239 238
240 // assume last elements are highest 239 // assume last elements are highest
241 for (int i = kNumTestRects - 1; i >= 0; i--) { 240 for (int i = kNumTestRects - 1; i >= 0; i--) {
242 if (fTestRects[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1))) { 241 if (fTestRects[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1))) {
243 fSelectedRect = i; 242 fSelectedRectID = i;
244 fTestRects[i].fGeometry.offset(dx, dy); 243 fTestRects[i].fGeometry.offset(dx, dy);
245 244
246 fSceneChanged = true; 245 fSceneChanged = true;
247 this->inval(nullptr); 246 this->inval(nullptr);
248 break; 247 break;
249 } 248 }
250 } 249 }
251 250
252 for (int i = 0; i <= kNumSliders; i++) { 251 for (int i = 0; i <= kNumSliders; i++) {
253 if (fSliders[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1))) { 252 if (fSliders[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1))) {
254 fSelectedSlider = i; 253 fSelectedSliderID = i;
255 fSliders[i].fGeometry.offset(dx, 0); 254 fSliders[i].fGeometry.offset(dx, 0);
256 255
257 this->updateFromSelectedSlider(); 256 this->updateFromSelectedSlider();
258 257
259 fLightsChanged = true; 258 fLightsChanged = true;
260 259
261 this->inval(nullptr); 260 this->inval(nullptr);
262 break; 261 break;
263 } 262 }
264 } 263 }
265 264
266 return true; 265 return true;
267 } 266 }
268 267
269 private: 268 private:
270 static constexpr int kNumTestRects = 3; 269 static constexpr int kNumTestRects = 3;
271 static constexpr int kNumSliders = 3; 270 static constexpr int kNumSliders = 3;
272 271
273 static const int kWidth = 400; 272 static const int kWidth = 400;
274 static const int kHeight = 400; 273 static const int kHeight = 400;
275 274
275 bool fSceneChanged;
276 bool fLightsChanged;
277 bool fMoveLight;
278 bool fClearShadowMaps;
279
276 struct { 280 struct {
277 SkRect fGeometry; 281 SkRect fGeometry;
278 int fDepth; 282 int fDepth;
279 SkColor fColor; 283 SkColor fColor;
280 } fTestRects[kNumTestRects]; 284 } fTestRects[kNumTestRects];
281 int fSelectedRect; 285 int fSelectedRectID;
282 286
283 struct { 287 struct {
284 SkRect fGeometry; 288 SkRect fGeometry;
285 SkScalar fOffset; 289 SkScalar fOffset;
286 SkScalar fScale; 290 SkScalar fScale;
287 } fSliders[kNumSliders]; 291 } fSliders[kNumSliders];
288 int fSelectedSlider; 292 int fSelectedSliderID;
289 293
290 bool fClearShadowMaps; 294 SkScalar fLightDepth;
291 bool fMoveLight;
292 bool fSceneChanged;
293 bool fLightsChanged;
294 295
295 sk_sp<SkPicture> fPicture; 296 sk_sp<SkPicture> fPicture;
296 SkShadowParams fShadowParams; 297 SkShadowParams fShadowParams;
297 sk_sp<SkLights> fLights; 298 sk_sp<SkLights> fLights;
298 299
299 typedef SampleView INHERITED; 300 typedef SampleView INHERITED;
300 }; 301 };
301 302
302 ////////////////////////////////////////////////////////////////////////////// 303 //////////////////////////////////////////////////////////////////////////////
303 static SkView* MyFactory() { return new ShadowingView; } 304 static SkView* MyFactory() { return new ShadowingView; }
304 static SkViewRegister reg(MyFactory); 305 static SkViewRegister reg(MyFactory);
305 306
306 #endif 307 #endif
OLDNEW
« no previous file with comments | « include/core/SkLights.h ('k') | src/core/SkLights.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698