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 "gm.h" | |
9 | |
10 #include "SkBitmapProcShader.h" | |
11 #include "SkLightingShader.h" | |
12 #include "SkNormalSource.h" | |
13 #include "SkPath.h" | |
14 #include "SkPoint3.h" | |
15 #include "SkShader.h" | |
16 | |
17 | |
18 namespace skiagm { | |
19 | |
20 // This GM exercises lighting shaders when used with bevel SkNormalSource object s. | |
21 class LightingShaderBevelGM : public GM { | |
22 public: | |
23 LightingShaderBevelGM() { | |
24 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | |
25 } | |
26 | |
27 protected: | |
28 SkString onShortName() override { | |
29 return SkString("lightingshaderbevel"); | |
30 } | |
31 | |
32 SkISize onISize() override { | |
33 return SkISize::Make(SkScalarCeilToInt(GRID_NUM_COLUMNS * GRID_CELL_WIDT H), | |
34 SkScalarCeilToInt(GRID_NUM_ROWS * GRID_CELL_WIDT H)); | |
35 } | |
36 | |
37 void onOnceBeforeDraw() override { | |
38 SkLights::Builder builder; | |
39 const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0 .473f); | |
40 | |
41 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), | |
42 kLightFromUpperRight)); | |
43 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); | |
44 fLights = builder.finish(); | |
45 | |
46 // fRect is assumed to be square throughout this file | |
47 fRect = SkRect::MakeIWH(kTexSize, kTexSize); | |
48 SkMatrix matrix; | |
49 SkRect bitmapBounds = SkRect::MakeIWH(kTexSize, kTexSize); | |
50 matrix.setRectToRect(bitmapBounds, fRect, SkMatrix::kFill_ScaleToFit); | |
51 | |
52 SkBitmap diffuseMap = sk_tool_utils::create_checkerboard_bitmap( | |
53 kTexSize, kTexSize, | |
54 sk_tool_utils::color_to_565(0x0), | |
55 sk_tool_utils::color_to_565(0xFF804020), | |
56 8); | |
57 fDiffuse = SkMakeBitmapShader(diffuseMap, SkShader::kClamp_TileMode, | |
58 SkShader::kClamp_TileMode, &matrix, nullpt r); | |
59 | |
60 fConvexPath.moveTo(fRect.width() / 2.0f, 0.0f); | |
61 fConvexPath.lineTo(0.0f, fRect.height()); | |
62 fConvexPath.lineTo(fRect.width(), fRect.height()); | |
63 fConvexPath.close(); | |
64 | |
65 // Creating concave path | |
66 { | |
67 SkScalar x = 0.0f; | |
68 SkScalar y = fRect.height() / 2.0f; | |
69 | |
70 const int NUM_SPIKES = 8; | |
71 | |
72 const SkScalar x0 = x; | |
73 const SkScalar dx = fRect.width() / (NUM_SPIKES * 2); | |
74 const SkScalar dy = SK_Scalar1 * 10; | |
75 | |
76 | |
77 fConcavePath.moveTo(x, y + dy); | |
78 for (int i = 0; i < NUM_SPIKES; i++) { | |
79 x += dx; | |
80 fConcavePath.lineTo(x, y - dy); | |
81 x += dx; | |
82 fConcavePath.lineTo(x, y + dy); | |
83 } | |
84 fConcavePath.lineTo(x, y + (2 * dy)); | |
85 fConcavePath.lineTo(x0, y + (2 * dy)); | |
86 fConcavePath.close(); | |
87 } | |
88 } | |
89 | |
90 // Scales shape around origin, rotates shape around origin, then translates shape to origin | |
91 void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScala r rotate) const { | |
92 canvas->translate(kTexSize/2.0f, kTexSize/2.0f); | |
93 canvas->scale(scaleX, scaleY); | |
94 canvas->rotate(rotate); | |
95 canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f); | |
96 } | |
97 | |
98 enum Shape { | |
99 kCircle_Shape, | |
100 kRect_Shape, | |
101 kRRect_Shape, | |
102 kConvexPath_Shape, | |
103 kConcavePath_Shape, | |
104 | |
105 kLast_Shape = kConcavePath_Shape | |
106 }; | |
107 void drawShape(enum Shape shape, SkCanvas* canvas, SkScalar scaleX, SkScalar scaleY, | |
108 SkScalar rotate, SkNormalSource::BevelType bevelType, bool us eNegativeHeight) { | |
109 canvas->save(); | |
110 | |
111 this->positionCTM(canvas, scaleX, scaleY, rotate); | |
112 | |
113 SkPaint paint; | |
114 | |
115 int width = 10.0f; | |
116 int height = useNegativeHeight ? -7.0f : 7.0f; | |
117 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeBevel(bevelType , width, height); | |
118 | |
119 paint.setShader(SkLightingShader::Make(fDiffuse, std::move(normalSource) , fLights)); | |
120 switch(shape) { | |
121 case kCircle_Shape: | |
122 paint.setAntiAlias(true); | |
egdaniel
2016/07/27 17:57:28
I would set AntiAlias for all the shapes
dvonbeck
2016/07/27 18:46:36
Done.
| |
123 canvas->drawCircle(fRect.centerX(), fRect.centerY(), fRect.width ()/2.0f, paint); | |
124 break; | |
125 case kRect_Shape: | |
126 canvas->drawRect(fRect, paint); | |
127 break; | |
128 case kRRect_Shape: | |
129 canvas->drawRoundRect(fRect, 5.0f, 5.0f, paint); | |
130 break; | |
131 case kConvexPath_Shape: | |
132 canvas->drawPath(fConvexPath, paint); | |
133 break; | |
134 case kConcavePath_Shape: | |
135 canvas->drawPath(fConcavePath, paint); | |
136 break; | |
137 default: | |
138 SkDEBUGFAIL("Invalid shape enum for drawShape"); | |
139 } | |
140 | |
141 canvas->restore(); | |
142 } | |
143 | |
144 void onDraw(SkCanvas* canvas) override { | |
145 SkPaint labelPaint; | |
146 labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-ser if", | |
147 SkFontSty le())); | |
148 labelPaint.setAntiAlias(true); | |
149 labelPaint.setTextSize(LABEL_SIZE); | |
150 | |
151 int gridNum = 0; | |
152 | |
153 // Running through all possible parameter combinations | |
154 for (int shapeInt = 0; shapeInt < NUM_SHAPES; shapeInt++) { | |
155 Shape shape = (Shape)shapeInt; | |
156 for (auto bevelType : {SkNormalSource::BevelType::kLinear, | |
157 SkNormalSource::BevelType::kRoundedIn, | |
158 SkNormalSource::BevelType::kRoundedOut}) { | |
159 for (bool useNegativeHeight : {true, false}) { | |
160 | |
161 // Making sure NUM_COMBINATIONS_PER_SHAPE is set correctly | |
162 SkASSERT(gridNum < (NUM_COMBINATIONS_PER_SHAPE*NUM_SHAPES)); | |
163 | |
164 // Determining position | |
165 SkScalar xPos = (gridNum % GRID_NUM_COLUMNS) * GRID_CELL_WID TH; | |
166 SkScalar yPos = (gridNum / GRID_NUM_COLUMNS) * GRID_CELL_WID TH; | |
167 | |
168 canvas->save(); | |
169 | |
170 canvas->translate(xPos, yPos); | |
171 this->drawShape(shape, canvas, 1.0f, 1.0f, 0.f, bevelType, u seNegativeHeight); | |
172 // Drawing labels | |
173 canvas->translate(0.0f, SkIntToScalar(kTexSize)); | |
174 { | |
175 canvas->translate(0.0f, LABEL_SIZE); | |
176 SkString label; | |
177 label.appendf("useNegativeHeight: %d", useNegativeHeight ); | |
egdaniel
2016/07/27 17:57:28
info on the bevel height would be interesting to r
dvonbeck
2016/07/27 18:46:36
Done.
| |
178 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f , labelPaint); | |
179 } | |
180 | |
181 canvas->restore(); | |
182 | |
183 gridNum++; | |
184 } | |
185 } | |
egdaniel
2016/07/27 17:57:28
lets also add a rotated column at the end, but we
dvonbeck
2016/07/27 18:46:36
Done.
| |
186 } | |
187 } | |
188 | |
189 private: | |
190 static constexpr int kTexSize = 96; | |
191 static constexpr int NUM_SHAPES = kLast_Shape + 1; | |
192 static constexpr int NUM_COMBINATIONS_PER_SHAPE = 6; | |
193 static constexpr int GRID_NUM_ROWS = NUM_SHAPES; | |
194 static constexpr int GRID_NUM_COLUMNS = NUM_COMBINATIONS_PER_SHAPE; | |
195 static constexpr SkScalar LABEL_SIZE = 10.0f; | |
196 static constexpr int NUM_LABELS_PER_CELL = 2; | |
197 static constexpr SkScalar GRID_CELL_WIDTH = kTexSize + 20.0f + NUM_LABELS_PE R_CELL * LABEL_SIZE; | |
198 | |
199 sk_sp<SkShader> fDiffuse; | |
200 | |
201 SkRect fRect; | |
202 SkPath fConvexPath; | |
203 SkPath fConcavePath; | |
204 sk_sp<SkLights> fLights; | |
205 | |
206 typedef GM INHERITED; | |
207 }; | |
208 | |
209 ////////////////////////////////////////////////////////////////////////////// | |
210 | |
211 DEF_GM(return new LightingShaderBevelGM;) | |
212 } | |
OLD | NEW |