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 class ShadowingView : public SampleView { | 16 class ShadowingView : public SampleView { |
17 public: | 17 public: |
18 ShadowingView() { | 18 ShadowingView() { |
19 | 19 |
20 this->setBGColor(0xFFCCCCCC); | 20 this->setBGColor(0xFFCCCCCC); |
21 SkLights::Builder builder; | 21 SkLights::Builder builder; |
22 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.2f, 0.3f, 0.4f), | 22 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.3f, 0.5f, 0.7f) , |
23 SkVector3::Make(0.2f, 0.05f , 1.0f))); | 23 SkVector3::Make(150.0f, 150.0f, 3 00.0f), |
24 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.4f, 0.3f, 0.2f), | 24 1024)); |
25 SkVector3::Make(0.05f, 0.2f , 1.0f))); | 25 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.7f, 0.5f, 0.3f) , |
26 builder.add(SkLights::Light::MakeAmbient(SkColor3f::Make(0.4f, 0.4f, 0.4 f))); | 26 SkVector3::Make(250.0f, 250.0f, 3 00.0f), |
27 1024)); | |
27 fLights = builder.finish(); | 28 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 fSceneChanged = true; | 42 fSceneChanged = true; |
jvanverth1
2016/08/17 19:59:02
Ideally these simple types should be in the initia
vjiaoblack
2016/08/18 14:52:19
Done.
| |
42 fLightsChanged = true; | 43 fLightsChanged = true; |
43 | 44 |
44 fSelectedRect = -1; | 45 fSelectedRect = -1; |
45 fMoveLight = false; | 46 fMoveLight = false; |
46 | 47 |
48 fLightDepth = 300.0f; | |
47 fClearShadowMaps = false; | 49 fClearShadowMaps = false; |
48 } | 50 } |
49 | 51 |
50 protected: | 52 protected: |
51 bool onQuery(SkEvent *evt) override { | 53 bool onQuery(SkEvent *evt) override { |
52 if (SampleCode::TitleQ(*evt)) { | 54 if (SampleCode::TitleQ(*evt)) { |
53 SampleCode::TitleR(evt, "shadowing"); | 55 SampleCode::TitleR(evt, "shadowing"); |
54 return true; | 56 return true; |
55 } | 57 } |
56 | 58 |
57 SkUnichar uni; | 59 SkUnichar uni; |
58 if (SampleCode::CharQ(*evt, &uni)) { | 60 if (SampleCode::CharQ(*evt, &uni)) { |
59 switch (uni) { | 61 switch (uni) { |
60 case 'L': | 62 case 'L': |
61 fMoveLight = !fMoveLight; | 63 fMoveLight = !fMoveLight; |
62 break; | 64 break; |
63 case 'd': | 65 case 'd': |
64 // Raster generated shadow maps have their origin in the UL corner | 66 // Raster generated shadow maps have their origin in the UL corner |
65 // GPU shadow maps can have an arbitrary origin. | 67 // GPU shadow maps can have an arbitrary origin. |
66 // We override the 'd' keypress so that when the device is c ycled, | 68 // We override the 'd' keypress so that when the device is c ycled, |
67 // the shadow maps will be re-generated according to the new backend. | 69 // the shadow maps will be re-generated according to the new backend. |
68 fClearShadowMaps = true; | 70 fClearShadowMaps = true; |
69 break; | 71 break; |
72 case 'q': | |
73 fLightDepth += 5.0f; | |
74 fMoveLight = true; | |
75 break; | |
76 case 'w': | |
77 fLightDepth -= 5.0f; | |
78 fMoveLight = true; | |
79 break; | |
70 default: | 80 default: |
71 break; | 81 break; |
72 } | 82 } |
73 } | 83 } |
74 return this->INHERITED::onQuery(evt); | 84 return this->INHERITED::onQuery(evt); |
75 } | 85 } |
76 | 86 |
77 sk_sp<SkPicture> makeTestPicture(int width, int height) { | 87 sk_sp<SkPicture> makeTestPicture(int width, int height) { |
78 SkPictureRecorder recorder; | 88 SkPictureRecorder recorder; |
79 | 89 |
(...skipping 30 matching lines...) Expand all Loading... | |
110 } | 120 } |
111 | 121 |
112 if (fSceneChanged || fLightsChanged || fClearShadowMaps) { | 122 if (fSceneChanged || fLightsChanged || fClearShadowMaps) { |
113 for (int i = 0; i < fLights->numLights(); i++) { | 123 for (int i = 0; i < fLights->numLights(); i++) { |
114 fLights->light(i).setShadowMap(nullptr); | 124 fLights->light(i).setShadowMap(nullptr); |
115 } | 125 } |
116 fSceneChanged = false; | 126 fSceneChanged = false; |
117 fLightsChanged = false; | 127 fLightsChanged = false; |
118 fClearShadowMaps = false; | 128 fClearShadowMaps = false; |
119 | 129 |
120 canvas->setLights(fLights); | |
121 canvas->drawShadowedPicture(fPicture, nullptr, nullptr); | |
122 } | 130 } |
131 canvas->setLights(fLights); | |
132 canvas->drawShadowedPicture(fPicture, nullptr, nullptr); | |
123 } | 133 } |
124 | 134 |
125 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { | 135 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { |
126 return new SkView::Click(this); | 136 return new SkView::Click(this); |
127 } | 137 } |
128 | 138 |
129 bool onClick(Click *click) override { | 139 bool onClick(Click *click) override { |
130 SkScalar x = click->fCurr.fX; | 140 SkScalar x = click->fCurr.fX; |
131 SkScalar y = click->fCurr.fY; | 141 SkScalar y = click->fCurr.fY; |
132 | 142 |
133 SkScalar dx = x - click->fPrev.fX; | 143 SkScalar dx = x - click->fPrev.fX; |
134 SkScalar dy = y - click->fPrev.fY; | 144 SkScalar dy = y - click->fPrev.fY; |
135 | 145 |
136 if (fMoveLight) { | 146 if (fMoveLight) { |
137 if (dx != 0 || dy != 0) { | 147 if (dx != 0 || dy != 0) { |
138 float recipX = 1.0f / kWidth; | 148 SkLights::Builder builder; |
139 float recipY = 1.0f / kHeight; | 149 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.3f, 0.5 f, 0.7f), |
jvanverth1
2016/08/17 19:59:02
If you stick these lines in a method that takes x
vjiaoblack
2016/08/18 14:52:19
Done.
| |
150 SkVector3::Make(x - 50, | |
151 350 - y, | |
152 fLightDep th), | |
153 1024)); | |
140 | 154 |
141 SkLights::Builder builder; | 155 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.7f, 0.5 f, 0.3f), |
142 builder.add(SkLights::Light::MakeDirectional( | 156 SkVector3::Make(x + 50, |
143 SkColor3f::Make(0.2f, 0.3f, 0.4f), | 157 450 - y, |
144 SkVector3::Make(0.2f + (200.0f - x) * recipX, | 158 fLightDep th), |
145 0.05f + (200.0f - y) * recipY, | 159 1024)); |
146 1.0f))); | |
147 builder.add(SkLights::Light::MakeDirectional( | |
148 SkColor3f::Make(0.4f, 0.3f, 0.2f), | |
149 SkVector3::Make(0.05f + (200.0f - x) * recipX, | |
150 0.2f + (200.0f - y) * recipY, | |
151 1.0f))); | |
152 builder.add(SkLights::Light::MakeAmbient( | |
153 SkColor3f::Make(0.4f, 0.4f, 0.4f))); | |
154 fLights = builder.finish(); | 160 fLights = builder.finish(); |
155 | 161 |
156 fLightsChanged = true; | 162 fLightsChanged = true; |
157 this->inval(nullptr); | 163 this->inval(nullptr); |
158 } | 164 } |
159 return true; | 165 return true; |
160 } | 166 } |
161 | 167 |
162 if (click->fState == Click::State::kUp_State) { | 168 if (click->fState == Click::State::kUp_State) { |
163 fSelectedRect = -1; | 169 fSelectedRect = -1; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 } fTestRects[kNumTestRects]; | 207 } fTestRects[kNumTestRects]; |
202 | 208 |
203 int fSelectedRect; | 209 int fSelectedRect; |
204 bool fMoveLight; | 210 bool fMoveLight; |
205 | 211 |
206 sk_sp<SkPicture> fPicture; | 212 sk_sp<SkPicture> fPicture; |
207 | 213 |
208 bool fSceneChanged; | 214 bool fSceneChanged; |
209 bool fLightsChanged; | 215 bool fLightsChanged; |
210 | 216 |
217 SkScalar fLightDepth; | |
218 | |
211 sk_sp<SkLights> fLights; | 219 sk_sp<SkLights> fLights; |
212 | 220 |
213 typedef SampleView INHERITED; | 221 typedef SampleView INHERITED; |
214 }; | 222 }; |
215 | 223 |
216 ////////////////////////////////////////////////////////////////////////////// | 224 ////////////////////////////////////////////////////////////////////////////// |
217 static SkView* MyFactory() { return new ShadowingView; } | 225 static SkView* MyFactory() { return new ShadowingView; } |
218 static SkViewRegister reg(MyFactory); | 226 static SkViewRegister reg(MyFactory); |
219 | 227 |
220 #endif | 228 #endif |
OLD | NEW |