Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 "include/core/SkCanvas.h" |
Mike Reed | 46f5c5f | 2020-02-20 15:42:29 -0500 | [diff] [blame] | 9 | #include "include/core/SkM44.h" |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 10 | #include "include/core/SkPaint.h" |
| 11 | #include "include/core/SkRRect.h" |
Brian Osman | aa9983a | 2020-04-02 16:49:59 -0400 | [diff] [blame] | 12 | #include "include/core/SkVertices.h" |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 13 | #include "include/utils/SkRandom.h" |
| 14 | #include "samplecode/Sample.h" |
| 15 | #include "tools/Resources.h" |
| 16 | |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 17 | struct VSphere { |
Mike Reed | 415cace | 2020-02-21 12:03:49 -0500 | [diff] [blame] | 18 | SkV2 fCenter; |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 19 | SkScalar fRadius; |
| 20 | |
Mike Reed | 415cace | 2020-02-21 12:03:49 -0500 | [diff] [blame] | 21 | VSphere(SkV2 center, SkScalar radius) : fCenter(center), fRadius(radius) {} |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 22 | |
Mike Reed | 415cace | 2020-02-21 12:03:49 -0500 | [diff] [blame] | 23 | bool contains(SkV2 v) const { |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 24 | return (v - fCenter).length() <= fRadius; |
| 25 | } |
| 26 | |
Mike Reed | 415cace | 2020-02-21 12:03:49 -0500 | [diff] [blame] | 27 | SkV2 pinLoc(SkV2 p) const { |
Mike Reed | 2d4a28e | 2020-01-25 22:39:43 -0500 | [diff] [blame] | 28 | auto v = p - fCenter; |
| 29 | if (v.length() > fRadius) { |
| 30 | v *= (fRadius / v.length()); |
| 31 | } |
| 32 | return fCenter + v; |
| 33 | } |
| 34 | |
Mike Reed | 415cace | 2020-02-21 12:03:49 -0500 | [diff] [blame] | 35 | SkV3 computeUnitV3(SkV2 v) const { |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 36 | v = (v - fCenter) * (1 / fRadius); |
| 37 | SkScalar len2 = v.lengthSquared(); |
| 38 | if (len2 > 1) { |
Mike Reed | e1a81ba | 2020-02-19 17:40:32 -0500 | [diff] [blame] | 39 | v = v.normalize(); |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 40 | len2 = 1; |
| 41 | } |
| 42 | SkScalar z = SkScalarSqrt(1 - len2); |
| 43 | return {v.x, v.y, z}; |
| 44 | } |
| 45 | |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 46 | struct RotateInfo { |
| 47 | SkV3 fAxis; |
| 48 | SkScalar fAngle; |
| 49 | }; |
| 50 | |
Mike Reed | 415cace | 2020-02-21 12:03:49 -0500 | [diff] [blame] | 51 | RotateInfo computeRotationInfo(SkV2 a, SkV2 b) const { |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 52 | SkV3 u = this->computeUnitV3(a); |
| 53 | SkV3 v = this->computeUnitV3(b); |
| 54 | SkV3 axis = u.cross(v); |
Mike Reed | 2382366 | 2020-02-19 17:07:04 -0500 | [diff] [blame] | 55 | SkScalar length = axis.length(); |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 56 | |
Mike Reed | 2382366 | 2020-02-19 17:07:04 -0500 | [diff] [blame] | 57 | if (!SkScalarNearlyZero(length)) { |
| 58 | return {axis * (1.0f / length), acos(u.dot(v))}; |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 59 | } |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 60 | return {{0, 0, 0}, 0}; |
| 61 | } |
| 62 | |
Mike Reed | 415cace | 2020-02-21 12:03:49 -0500 | [diff] [blame] | 63 | SkM44 computeRotation(SkV2 a, SkV2 b) const { |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 64 | auto [axis, angle] = this->computeRotationInfo(a, b); |
| 65 | return SkM44::Rotate(axis, angle); |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 66 | } |
| 67 | }; |
| 68 | |
Mike Reed | ee3216d | 2020-01-17 17:35:04 -0500 | [diff] [blame] | 69 | static SkM44 inv(const SkM44& m) { |
| 70 | SkM44 inverse; |
| 71 | SkAssertResult(m.invert(&inverse)); |
| 72 | return inverse; |
| 73 | } |
| 74 | |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 75 | class Sample3DView : public Sample { |
| 76 | protected: |
| 77 | float fNear = 0.05f; |
| 78 | float fFar = 4; |
| 79 | float fAngle = SK_ScalarPI / 12; |
| 80 | |
Mike Reed | 00a9764 | 2020-01-25 18:42:23 -0500 | [diff] [blame] | 81 | SkV3 fEye { 0, 0, 1.0f/tan(fAngle/2) - 1 }; |
| 82 | SkV3 fCOA { 0, 0, 0 }; |
| 83 | SkV3 fUp { 0, 1, 0 }; |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 84 | |
Brian Osman | 548de74 | 2020-04-24 12:02:25 -0400 | [diff] [blame] | 85 | const char* kLocalToWorld = "local_to_world"; |
Mike Reed | 7fe6ee3 | 2020-04-09 12:35:09 -0400 | [diff] [blame] | 86 | |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 87 | public: |
Brian Osman | 1985466 | 2020-04-21 14:47:07 -0400 | [diff] [blame] | 88 | void concatCamera(SkCanvas* canvas, const SkRect& area, SkScalar zscale) { |
Mike Reed | 00a9764 | 2020-01-25 18:42:23 -0500 | [diff] [blame] | 89 | SkM44 camera = Sk3LookAt(fEye, fCOA, fUp), |
| 90 | perspective = Sk3Perspective(fNear, fFar, fAngle), |
| 91 | viewport = SkM44::Translate(area.centerX(), area.centerY(), 0) * |
| 92 | SkM44::Scale(area.width()*0.5f, area.height()*0.5f, zscale); |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 93 | |
Mike Reed | 7fe6ee3 | 2020-04-09 12:35:09 -0400 | [diff] [blame] | 94 | canvas->concat(viewport * perspective * camera * inv(viewport)); |
Mike Reed | 7fe6ee3 | 2020-04-09 12:35:09 -0400 | [diff] [blame] | 95 | } |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 96 | }; |
| 97 | |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 98 | struct Face { |
| 99 | SkScalar fRx, fRy; |
Mike Reed | ee3216d | 2020-01-17 17:35:04 -0500 | [diff] [blame] | 100 | SkColor fColor; |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 101 | |
Mike Reed | 00a9764 | 2020-01-25 18:42:23 -0500 | [diff] [blame] | 102 | static SkM44 T(SkScalar x, SkScalar y, SkScalar z) { |
| 103 | return SkM44::Translate(x, y, z); |
Mike Reed | 78184a3 | 2020-01-25 03:20:18 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Mike Reed | 00a9764 | 2020-01-25 18:42:23 -0500 | [diff] [blame] | 106 | static SkM44 R(SkV3 axis, SkScalar rad) { |
| 107 | return SkM44::Rotate(axis, rad); |
Mike Reed | 78184a3 | 2020-01-25 03:20:18 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Mike Reed | 00a9764 | 2020-01-25 18:42:23 -0500 | [diff] [blame] | 110 | SkM44 asM44(SkScalar scale) const { |
| 111 | return R({0,1,0}, fRy) * R({1,0,0}, fRx) * T(0, 0, scale); |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 112 | } |
| 113 | }; |
| 114 | |
Mike Reed | 7543587 | 2020-01-13 21:15:06 -0500 | [diff] [blame] | 115 | static bool front(const SkM44& m) { |
Florin Malita | d589916 | 2020-02-04 10:06:24 -0500 | [diff] [blame] | 116 | SkM44 m2(SkM44::kUninitialized_Constructor); |
| 117 | if (!m.invert(&m2)) { |
| 118 | m2.setIdentity(); |
| 119 | } |
Mike Reed | 7543587 | 2020-01-13 21:15:06 -0500 | [diff] [blame] | 120 | /* |
| 121 | * Classically we want to dot the transpose(inverse(ctm)) with our surface normal. |
| 122 | * In this case, the normal is known to be {0, 0, 1}, so we only actually need to look |
| 123 | * at the z-scale of the inverse (the transpose doesn't change the main diagonal, so |
| 124 | * no need to actually transpose). |
| 125 | */ |
Mike Reed | 07d32b4 | 2020-01-23 11:06:20 -0500 | [diff] [blame] | 126 | return m2.rc(2,2) > 0; |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | const Face faces[] = { |
Mike Reed | ee3216d | 2020-01-17 17:35:04 -0500 | [diff] [blame] | 130 | { 0, 0, SK_ColorRED }, // front |
| 131 | { 0, SK_ScalarPI, SK_ColorGREEN }, // back |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 132 | |
Mike Reed | ee3216d | 2020-01-17 17:35:04 -0500 | [diff] [blame] | 133 | { SK_ScalarPI/2, 0, SK_ColorBLUE }, // top |
| 134 | {-SK_ScalarPI/2, 0, SK_ColorCYAN }, // bottom |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 135 | |
Mike Reed | ee3216d | 2020-01-17 17:35:04 -0500 | [diff] [blame] | 136 | { 0, SK_ScalarPI/2, SK_ColorMAGENTA }, // left |
| 137 | { 0,-SK_ScalarPI/2, SK_ColorYELLOW }, // right |
Mike Reed | 69ace2a | 2020-01-11 15:57:14 -0500 | [diff] [blame] | 138 | }; |
| 139 | |
Mike Reed | ee3216d | 2020-01-17 17:35:04 -0500 | [diff] [blame] | 140 | #include "include/effects/SkRuntimeEffect.h" |
| 141 | |
Mike Reed | 2d4a28e | 2020-01-25 22:39:43 -0500 | [diff] [blame] | 142 | struct LightOnSphere { |
Mike Reed | 415cace | 2020-02-21 12:03:49 -0500 | [diff] [blame] | 143 | SkV2 fLoc; |
Mike Reed | 2d4a28e | 2020-01-25 22:39:43 -0500 | [diff] [blame] | 144 | SkScalar fDistance; |
| 145 | SkScalar fRadius; |
| 146 | |
| 147 | SkV3 computeWorldPos(const VSphere& s) const { |
| 148 | return s.computeUnitV3(fLoc) * fDistance; |
| 149 | } |
| 150 | |
| 151 | void draw(SkCanvas* canvas) const { |
| 152 | SkPaint paint; |
| 153 | paint.setAntiAlias(true); |
| 154 | paint.setColor(SK_ColorWHITE); |
| 155 | canvas->drawCircle(fLoc.x, fLoc.y, fRadius + 2, paint); |
| 156 | paint.setColor(SK_ColorBLACK); |
| 157 | canvas->drawCircle(fLoc.x, fLoc.y, fRadius, paint); |
| 158 | } |
| 159 | }; |
| 160 | |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 161 | #include "include/core/SkTime.h" |
| 162 | |
| 163 | class RotateAnimator { |
| 164 | SkV3 fAxis = {0, 0, 0}; |
| 165 | SkScalar fAngle = 0, |
| 166 | fPrevAngle = 1234567; |
| 167 | double fNow = 0, |
| 168 | fPrevNow = 0; |
| 169 | |
| 170 | SkScalar fAngleSpeed = 0, |
| 171 | fAngleSign = 1; |
| 172 | |
| 173 | static constexpr double kSlowDown = 4; |
| 174 | static constexpr SkScalar kMaxSpeed = 16; |
| 175 | |
| 176 | public: |
| 177 | void update(SkV3 axis, SkScalar angle) { |
| 178 | if (angle != fPrevAngle) { |
| 179 | fPrevAngle = fAngle; |
| 180 | fAngle = angle; |
| 181 | |
| 182 | fPrevNow = fNow; |
| 183 | fNow = SkTime::GetSecs(); |
| 184 | |
| 185 | fAxis = axis; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | SkM44 rotation() { |
| 190 | if (fAngleSpeed > 0) { |
| 191 | double now = SkTime::GetSecs(); |
| 192 | double dtime = now - fPrevNow; |
| 193 | fPrevNow = now; |
| 194 | double delta = fAngleSign * fAngleSpeed * dtime; |
| 195 | fAngle += delta; |
| 196 | fAngleSpeed -= kSlowDown * dtime; |
| 197 | if (fAngleSpeed < 0) { |
| 198 | fAngleSpeed = 0; |
| 199 | } |
| 200 | } |
| 201 | return SkM44::Rotate(fAxis, fAngle); |
| 202 | |
| 203 | } |
| 204 | |
| 205 | void start() { |
| 206 | if (fPrevNow != fNow) { |
| 207 | fAngleSpeed = (fAngle - fPrevAngle) / (fNow - fPrevNow); |
| 208 | fAngleSign = fAngleSpeed < 0 ? -1 : 1; |
| 209 | fAngleSpeed = std::min(kMaxSpeed, std::abs(fAngleSpeed)); |
| 210 | } else { |
| 211 | fAngleSpeed = 0; |
| 212 | } |
| 213 | fPrevNow = SkTime::GetSecs(); |
| 214 | fAngle = 0; |
| 215 | } |
| 216 | |
| 217 | void reset() { |
| 218 | fAngleSpeed = 0; |
| 219 | fAngle = 0; |
| 220 | fPrevAngle = 1234567; |
| 221 | } |
Mike Reed | fab6198 | 2020-02-21 13:25:12 -0500 | [diff] [blame] | 222 | |
| 223 | bool isAnimating() const { return fAngleSpeed != 0; } |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 224 | }; |
| 225 | |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 226 | class SampleCubeBase : public Sample3DView { |
Mike Reed | 2d4a28e | 2020-01-25 22:39:43 -0500 | [diff] [blame] | 227 | enum { |
| 228 | DX = 400, |
| 229 | DY = 300 |
| 230 | }; |
| 231 | |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 232 | SkM44 fRotation; // part of model |
| 233 | |
| 234 | RotateAnimator fRotateAnimator; |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 235 | |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 236 | protected: |
| 237 | enum Flags { |
| 238 | kCanRunOnCPU = 1 << 0, |
| 239 | kShowLightDome = 1 << 1, |
| 240 | }; |
| 241 | |
| 242 | LightOnSphere fLight = {{200 + DX, 200 + DY}, 800, 12}; |
| 243 | |
| 244 | VSphere fSphere; |
| 245 | Flags fFlags; |
| 246 | |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 247 | public: |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 248 | SampleCubeBase(Flags flags) |
| 249 | : fSphere({200 + DX, 200 + DY}, 400) |
| 250 | , fFlags(flags) |
| 251 | {} |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 252 | |
| 253 | bool onChar(SkUnichar uni) override { |
| 254 | switch (uni) { |
Mike Reed | 2d4a28e | 2020-01-25 22:39:43 -0500 | [diff] [blame] | 255 | case 'Z': fLight.fDistance += 10; return true; |
| 256 | case 'z': fLight.fDistance -= 10; return true; |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 257 | } |
| 258 | return this->Sample3DView::onChar(uni); |
| 259 | } |
| 260 | |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 261 | virtual void drawContent(SkCanvas* canvas, SkColor, int index, bool drawFront) = 0; |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 262 | |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 263 | void onDrawContent(SkCanvas* canvas) override { |
Robert Phillips | 30ebcf7 | 2020-07-09 13:25:17 -0400 | [diff] [blame] | 264 | if (!canvas->recordingContext() && !(fFlags & kCanRunOnCPU)) { |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 265 | return; |
| 266 | } |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 267 | |
| 268 | canvas->save(); |
Mike Reed | 2d4a28e | 2020-01-25 22:39:43 -0500 | [diff] [blame] | 269 | canvas->translate(DX, DY); |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 270 | |
Brian Osman | 1985466 | 2020-04-21 14:47:07 -0400 | [diff] [blame] | 271 | this->concatCamera(canvas, {0, 0, 400, 400}, 200); |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 272 | |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 273 | for (bool drawFront : {false, true}) { |
| 274 | int index = 0; |
| 275 | for (auto f : faces) { |
| 276 | SkAutoCanvasRestore acr(canvas, true); |
| 277 | |
| 278 | SkM44 trans = SkM44::Translate(200, 200, 0); // center of the rotation |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 279 | SkM44 m = fRotateAnimator.rotation() * fRotation * f.asM44(200); |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 280 | |
Brian Osman | 1985466 | 2020-04-21 14:47:07 -0400 | [diff] [blame] | 281 | canvas->concat(trans); |
| 282 | |
| 283 | // "World" space - content is centered at the origin, in device scale (+-200) |
Brian Osman | 548de74 | 2020-04-24 12:02:25 -0400 | [diff] [blame] | 284 | canvas->markCTM(kLocalToWorld); |
Brian Osman | 1985466 | 2020-04-21 14:47:07 -0400 | [diff] [blame] | 285 | |
| 286 | canvas->concat(m * inv(trans)); |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 287 | this->drawContent(canvas, f.fColor, index++, drawFront); |
| 288 | } |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Brian Osman | 1985466 | 2020-04-21 14:47:07 -0400 | [diff] [blame] | 291 | canvas->restore(); // camera & center the content in the window |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 292 | |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 293 | if (fFlags & kShowLightDome){ |
| 294 | fLight.draw(canvas); |
| 295 | |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 296 | SkPaint paint; |
Mike Reed | 2d4a28e | 2020-01-25 22:39:43 -0500 | [diff] [blame] | 297 | paint.setAntiAlias(true); |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 298 | paint.setStyle(SkPaint::kStroke_Style); |
Mike Reed | 2d4a28e | 2020-01-25 22:39:43 -0500 | [diff] [blame] | 299 | paint.setColor(0x40FF0000); |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 300 | canvas->drawCircle(fSphere.fCenter.x, fSphere.fCenter.y, fSphere.fRadius, paint); |
| 301 | canvas->drawLine(fSphere.fCenter.x, fSphere.fCenter.y - fSphere.fRadius, |
| 302 | fSphere.fCenter.x, fSphere.fCenter.y + fSphere.fRadius, paint); |
| 303 | canvas->drawLine(fSphere.fCenter.x - fSphere.fRadius, fSphere.fCenter.y, |
| 304 | fSphere.fCenter.x + fSphere.fRadius, fSphere.fCenter.y, paint); |
| 305 | } |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
Mike Reed | 415cace | 2020-02-21 12:03:49 -0500 | [diff] [blame] | 309 | SkV2 p = fLight.fLoc - SkV2{x, y}; |
Mike Reed | 2d4a28e | 2020-01-25 22:39:43 -0500 | [diff] [blame] | 310 | if (p.length() <= fLight.fRadius) { |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 311 | Click* c = new Click(); |
| 312 | c->fMeta.setS32("type", 0); |
| 313 | return c; |
| 314 | } |
| 315 | if (fSphere.contains({x, y})) { |
| 316 | Click* c = new Click(); |
| 317 | c->fMeta.setS32("type", 1); |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 318 | |
| 319 | fRotation = fRotateAnimator.rotation() * fRotation; |
| 320 | fRotateAnimator.reset(); |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 321 | return c; |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 322 | } |
| 323 | return nullptr; |
| 324 | } |
| 325 | bool onClick(Click* click) override { |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 326 | if (click->fMeta.hasS32("type", 0)) { |
Mike Reed | 2d4a28e | 2020-01-25 22:39:43 -0500 | [diff] [blame] | 327 | fLight.fLoc = fSphere.pinLoc({click->fCurr.fX, click->fCurr.fY}); |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 328 | return true; |
| 329 | } |
| 330 | if (click->fMeta.hasS32("type", 1)) { |
| 331 | if (click->fState == skui::InputState::kUp) { |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 332 | fRotation = fRotateAnimator.rotation() * fRotation; |
| 333 | fRotateAnimator.start(); |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 334 | } else { |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 335 | auto [axis, angle] = fSphere.computeRotationInfo( |
| 336 | {click->fOrig.fX, click->fOrig.fY}, |
| 337 | {click->fCurr.fX, click->fCurr.fY}); |
| 338 | fRotateAnimator.update(axis, angle); |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 339 | } |
| 340 | return true; |
| 341 | } |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 342 | return true; |
| 343 | } |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 344 | |
| 345 | bool onAnimate(double nanos) override { |
Mike Reed | fab6198 | 2020-02-21 13:25:12 -0500 | [diff] [blame] | 346 | return fRotateAnimator.isAnimating(); |
Mike Reed | 5263837 | 2020-02-19 13:30:06 -0500 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | private: |
| 350 | typedef Sample3DView INHERITED; |
Mike Reed | f0b7edf | 2020-01-18 14:21:12 -0500 | [diff] [blame] | 351 | }; |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 352 | |
| 353 | class SampleBump3D : public SampleCubeBase { |
| 354 | sk_sp<SkShader> fBmpShader, fImgShader; |
| 355 | sk_sp<SkRuntimeEffect> fEffect; |
| 356 | SkRRect fRR; |
| 357 | |
| 358 | public: |
Brian Osman | ad43e54 | 2020-06-19 15:02:35 -0400 | [diff] [blame] | 359 | SampleBump3D() : SampleCubeBase(Flags(kCanRunOnCPU | kShowLightDome)) {} |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 360 | |
| 361 | SkString name() override { return SkString("bump3d"); } |
| 362 | |
| 363 | void onOnceBeforeDraw() override { |
| 364 | fRR = SkRRect::MakeRectXY({20, 20, 380, 380}, 50, 50); |
| 365 | auto img = GetResourceAsImage("images/brickwork-texture.jpg"); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 366 | fImgShader = img->makeShader(SkMatrix::Scale(2, 2)); |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 367 | img = GetResourceAsImage("images/brickwork_normal-map.jpg"); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 368 | fBmpShader = img->makeShader(SkMatrix::Scale(2, 2)); |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 369 | |
| 370 | const char code[] = R"( |
Brian Osman | 28590d5 | 2020-03-23 16:59:08 -0400 | [diff] [blame] | 371 | in shader color_map; |
| 372 | in shader normal_map; |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 373 | |
Brian Osman | f59a961 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 374 | layout (marker=local_to_world) uniform float4x4 localToWorld; |
| 375 | layout (marker=normals(local_to_world)) uniform float4x4 localToWorldAdjInv; |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 376 | uniform float3 lightPos; |
| 377 | |
| 378 | float3 convert_normal_sample(half4 c) { |
| 379 | float3 n = 2 * c.rgb - 1; |
| 380 | n.y = -n.y; |
| 381 | return n; |
| 382 | } |
| 383 | |
| 384 | void main(float2 p, inout half4 color) { |
| 385 | float3 norm = convert_normal_sample(sample(normal_map, p)); |
Brian Osman | 613deb0 | 2020-04-02 15:55:37 -0400 | [diff] [blame] | 386 | float3 plane_norm = normalize(localToWorldAdjInv * float4(norm, 0)).xyz; |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 387 | |
| 388 | float3 plane_pos = (localToWorld * float4(p, 0, 1)).xyz; |
| 389 | float3 light_dir = normalize(lightPos - plane_pos); |
| 390 | |
| 391 | float ambient = 0.2; |
| 392 | float dp = dot(plane_norm, light_dir); |
| 393 | float scale = min(ambient + max(dp, 0), 1); |
| 394 | |
| 395 | color = sample(color_map, p) * half4(float4(scale, scale, scale, 1)); |
| 396 | } |
| 397 | )"; |
| 398 | auto [effect, error] = SkRuntimeEffect::Make(SkString(code)); |
| 399 | if (!effect) { |
| 400 | SkDebugf("runtime error %s\n", error.c_str()); |
| 401 | } |
| 402 | fEffect = effect; |
| 403 | } |
| 404 | |
| 405 | void drawContent(SkCanvas* canvas, SkColor color, int index, bool drawFront) override { |
Mike Reed | 46f5c5f | 2020-02-20 15:42:29 -0500 | [diff] [blame] | 406 | if (!drawFront || !front(canvas->getLocalToDevice())) { |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 410 | SkRuntimeShaderBuilder builder(fEffect); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 411 | builder.uniform("lightPos") = fLight.computeWorldPos(fSphere); |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 412 | // localToWorld matrices are automatically populated, via layout(marker) |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 413 | |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 414 | builder.child("color_map") = fImgShader; |
| 415 | builder.child("normal_map") = fBmpShader; |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 416 | |
| 417 | SkPaint paint; |
| 418 | paint.setColor(color); |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 419 | paint.setShader(builder.makeShader(nullptr, true)); |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 420 | |
| 421 | canvas->drawRRect(fRR, paint); |
| 422 | } |
| 423 | }; |
Mike Reed | e580995 | 2020-01-25 20:42:51 -0500 | [diff] [blame] | 424 | DEF_SAMPLE( return new SampleBump3D; ) |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 425 | |
Brian Osman | aa9983a | 2020-04-02 16:49:59 -0400 | [diff] [blame] | 426 | class SampleVerts3D : public SampleCubeBase { |
| 427 | sk_sp<SkRuntimeEffect> fEffect; |
| 428 | sk_sp<SkVertices> fVertices; |
| 429 | |
| 430 | public: |
| 431 | SampleVerts3D() : SampleCubeBase(kShowLightDome) {} |
| 432 | |
| 433 | SkString name() override { return SkString("verts3d"); } |
| 434 | |
| 435 | void onOnceBeforeDraw() override { |
| 436 | using Attr = SkVertices::Attribute; |
| 437 | Attr attrs[] = { |
| 438 | Attr(Attr::Type::kFloat3, Attr::Usage::kNormalVector), |
| 439 | }; |
| 440 | |
| 441 | SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 66, 0, attrs, 1); |
| 442 | |
| 443 | SkPoint* pos = builder.positions(); |
| 444 | SkV3* nrm = (SkV3*)builder.customData(); |
| 445 | |
| 446 | SkPoint center = { 200, 200 }; |
| 447 | SkScalar radius = 200; |
| 448 | |
| 449 | pos[0] = center; |
| 450 | nrm[0] = { 0, 0, 1 }; |
| 451 | |
| 452 | for (int i = 0; i < 65; ++i) { |
| 453 | SkScalar t = (i / 64.0f) * 2 * SK_ScalarPI; |
| 454 | SkScalar s = SkScalarSin(t), |
| 455 | c = SkScalarCos(t); |
| 456 | pos[i + 1] = center + SkPoint { c * radius, s * radius }; |
| 457 | nrm[i + 1] = { c, s, 0 }; |
| 458 | } |
| 459 | |
| 460 | fVertices = builder.detach(); |
| 461 | |
| 462 | const char code[] = R"( |
| 463 | varying float3 vtx_normal; |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 464 | |
| 465 | layout (marker=local_to_world) uniform float4x4 localToWorld; |
| 466 | layout (marker=normals(local_to_world)) uniform float4x4 localToWorldAdjInv; |
Brian Osman | aa9983a | 2020-04-02 16:49:59 -0400 | [diff] [blame] | 467 | uniform float3 lightPos; |
| 468 | |
| 469 | void main(float2 p, inout half4 color) { |
| 470 | float3 norm = normalize(vtx_normal); |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 471 | float3 plane_norm = normalize(localToWorldAdjInv * float4(norm, 0)).xyz; |
Brian Osman | aa9983a | 2020-04-02 16:49:59 -0400 | [diff] [blame] | 472 | |
| 473 | float3 plane_pos = (localToWorld * float4(p, 0, 1)).xyz; |
| 474 | float3 light_dir = normalize(lightPos - plane_pos); |
| 475 | |
| 476 | float ambient = 0.2; |
| 477 | float dp = dot(plane_norm, light_dir); |
| 478 | float scale = min(ambient + max(dp, 0), 1); |
| 479 | |
| 480 | color = half4(0.7, 0.9, 0.3, 1) * half4(float4(scale, scale, scale, 1)); |
| 481 | } |
| 482 | )"; |
| 483 | auto [effect, error] = SkRuntimeEffect::Make(SkString(code)); |
| 484 | if (!effect) { |
| 485 | SkDebugf("runtime error %s\n", error.c_str()); |
| 486 | } |
| 487 | fEffect = effect; |
| 488 | } |
| 489 | |
| 490 | void drawContent(SkCanvas* canvas, SkColor color, int index, bool drawFront) override { |
| 491 | if (!drawFront || !front(canvas->getLocalToDevice())) { |
| 492 | return; |
| 493 | } |
| 494 | |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 495 | SkRuntimeShaderBuilder builder(fEffect); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 496 | builder.uniform("lightPos") = fLight.computeWorldPos(fSphere); |
Brian Osman | aa9983a | 2020-04-02 16:49:59 -0400 | [diff] [blame] | 497 | |
| 498 | SkPaint paint; |
| 499 | paint.setColor(color); |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 500 | paint.setShader(builder.makeShader(nullptr, true)); |
Brian Osman | aa9983a | 2020-04-02 16:49:59 -0400 | [diff] [blame] | 501 | |
| 502 | canvas->drawVertices(fVertices, paint); |
| 503 | } |
| 504 | }; |
| 505 | DEF_SAMPLE( return new SampleVerts3D; ) |
| 506 | |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 507 | #include "modules/skottie/include/Skottie.h" |
| 508 | |
| 509 | class SampleSkottieCube : public SampleCubeBase { |
| 510 | sk_sp<skottie::Animation> fAnim[6]; |
| 511 | |
| 512 | public: |
| 513 | SampleSkottieCube() : SampleCubeBase(kCanRunOnCPU) {} |
| 514 | |
| 515 | SkString name() override { return SkString("skottie3d"); } |
| 516 | |
| 517 | void onOnceBeforeDraw() override { |
| 518 | const char* files[] = { |
| 519 | "skottie/skottie-chained-mattes.json", |
| 520 | "skottie/skottie-gradient-ramp.json", |
| 521 | "skottie/skottie_sample_2.json", |
| 522 | "skottie/skottie-3d-3planes.json", |
| 523 | "skottie/skottie-text-animator-4.json", |
| 524 | "skottie/skottie-motiontile-effect-phase.json", |
| 525 | |
| 526 | }; |
| 527 | for (unsigned i = 0; i < SK_ARRAY_COUNT(files); ++i) { |
| 528 | if (auto stream = GetResourceAsStream(files[i])) { |
| 529 | fAnim[i] = skottie::Animation::Make(stream.get()); |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | void drawContent(SkCanvas* canvas, SkColor color, int index, bool drawFront) override { |
Mike Reed | 46f5c5f | 2020-02-20 15:42:29 -0500 | [diff] [blame] | 535 | if (!drawFront || !front(canvas->getLocalToDevice())) { |
Mike Reed | cfee8ee | 2020-02-18 13:05:45 -0500 | [diff] [blame] | 536 | return; |
| 537 | } |
| 538 | |
| 539 | SkPaint paint; |
| 540 | paint.setColor(color); |
| 541 | SkRect r = {0, 0, 400, 400}; |
| 542 | canvas->drawRect(r, paint); |
| 543 | fAnim[index]->render(canvas, &r); |
| 544 | } |
| 545 | |
| 546 | bool onAnimate(double nanos) override { |
| 547 | for (auto& anim : fAnim) { |
| 548 | SkScalar dur = anim->duration(); |
| 549 | SkScalar t = fmod(1e-9 * nanos, dur) / dur; |
| 550 | anim->seek(t); |
| 551 | } |
| 552 | return true; |
| 553 | } |
| 554 | }; |
| 555 | DEF_SAMPLE( return new SampleSkottieCube; ) |