blob: 8014c246d3b2cbc7b3309207dee2592b1ccd9ff7 [file] [log] [blame]
Mike Reed27714a02018-05-30 16:02:48 -04001/*
2 * Copyright 2018 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#include "Sk3D.h"
10#include "SkPath.h"
11#include "SkPoint3.h"
12
Mike Reed2f0edd52018-05-31 14:22:30 -040013#ifdef SK_ENABLE_SKOTTIE
14
15#include "SkAnimTimer.h"
16#include "Resources.h"
17#include "SkStream.h"
18#include "Skottie.h"
19
20static SkMatrix operator*(const SkMatrix& a, const SkMatrix& b) {
21 SkMatrix44 c;
22 c.setConcat(a, b);
23 return c;
24}
25
Mike Reed27714a02018-05-30 16:02:48 -040026class GM3d : public skiagm::GM {
27 float fNear = 0.5;
28 float fFar = 4;
29 float fAngle = SK_ScalarPI / 4;
30
31 SkPoint3 fEye { 0, 0, 4 };
32 SkPoint3 fCOA {0,0,0};//{ 0.5f, 0.5f, 0.5f };
33 SkPoint3 fUp { 0, 1, 0 };
34
Mike Reed27714a02018-05-30 16:02:48 -040035 SkPoint3 fP3[8];
Mike Reed2f0edd52018-05-31 14:22:30 -040036
37 sk_sp<skottie::Animation> fAnim;
38 SkScalar fAnimT = 0;
39
Mike Reed27714a02018-05-30 16:02:48 -040040public:
Mike Reed2f0edd52018-05-31 14:22:30 -040041 GM3d() {}
Mike Reed27714a02018-05-30 16:02:48 -040042 ~GM3d() override {}
43
44protected:
45 void onOnceBeforeDraw() override {
Mike Reed2f0edd52018-05-31 14:22:30 -040046 auto stream = GetResourceAsStream("skotty/skotty_sample_2.json");
47 fAnim = skottie::Animation::Make(stream.get());
48
Mike Reed27714a02018-05-30 16:02:48 -040049 int index = 0;
50 for (float x = 0; x <= 1; ++x) {
51 for (float y = 0; y <= 1; ++y) {
52 for (float z = 0; z <= 1; ++z) {
53 fP3[index++] = { x, y, z };
54 }
55 }
56 }
Mike Reed27714a02018-05-30 16:02:48 -040057 }
58
59 static void draw_viewport(SkCanvas* canvas, const SkMatrix& viewport) {
60 SkPaint p;
61 p.setColor(0x10FF0000);
62
63 canvas->save();
64 canvas->concat(viewport);
65 canvas->drawRect({-1, -1, 1, 1}, p);
66
67 p.setColor(0x80FF0000);
68 canvas->drawLine({-1, -1}, {1, 1}, p);
69 canvas->drawLine({1, -1}, {-1, 1}, p);
70 canvas->restore();
71 }
72
Mike Reed2f0edd52018-05-31 14:22:30 -040073 static void draw_skia(SkCanvas* canvas, const SkMatrix44& m4, const SkMatrix& vp,
74 skottie::Animation* anim) {
75 auto proc = [canvas, vp, anim](SkColor c, const SkMatrix44& m4) {
76 SkPaint p;
77 p.setColor(c);
78 SkRect r = { 0, 0, 1, 1 };
79 canvas->save();
80 canvas->concat(vp * SkMatrix(m4));
81 anim->render(canvas, &r);
82// canvas->drawRect({0, 0, 1, 1}, p);
83 canvas->restore();
84 };
85
86 SkMatrix44 tmp(SkMatrix44::kIdentity_Constructor);
87
88 proc(0x400000FF, m4);
89 tmp.setTranslate(0, 0, 1);
90 proc(0xC00000FF, m4 * tmp);
91 tmp.setRotateAboutUnit(1, 0, 0, SK_ScalarPI/2);
92 proc(0x4000FF00, m4 * tmp);
93 tmp.postTranslate(0, 1, 0);
94 proc(0xC000FF00, m4 * tmp);
95 tmp.setRotateAboutUnit(0, 1, 0, -SK_ScalarPI/2);
96 proc(0x40FF0000, m4 * tmp);
97 tmp.postTranslate(1, 0, 0);
98 proc(0xC0FF0000, m4 * tmp);
99 }
100
Mike Reed27714a02018-05-30 16:02:48 -0400101 void onDraw(SkCanvas* canvas) override {
102 SkMatrix44 camera(SkMatrix44::kIdentity_Constructor),
103 perspective(SkMatrix44::kIdentity_Constructor),
Mike Reed2f0edd52018-05-31 14:22:30 -0400104 mv(SkMatrix44::kIdentity_Constructor);
Mike Reed27714a02018-05-30 16:02:48 -0400105 SkMatrix viewport;
106
107 {
108 float w = this->width();
109 float h = this->height();
110 float s = std::min(w, h);
111 viewport.setTranslate(1, -1);
112 viewport.postScale(s/2, -s/2);
113
114 draw_viewport(canvas, viewport);
115 }
116
117 Sk3Perspective(&perspective, fNear, fFar, fAngle);
118 Sk3LookAt(&camera, fEye, fCOA, fUp);
119 mv.postConcat(camera);
120 mv.postConcat(perspective);
121 SkPoint pts[8];
122 Sk3MapPts(pts, mv, fP3, 8);
123 viewport.mapPoints(pts, 8);
124
125 SkPaint paint;
126 paint.setStyle(SkPaint::kStroke_Style);
127
128 SkPath cube;
129
130 cube.moveTo(pts[0]);
131 cube.lineTo(pts[2]);
132 cube.lineTo(pts[6]);
133 cube.lineTo(pts[4]);
134 cube.close();
135
136 cube.moveTo(pts[1]);
137 cube.lineTo(pts[3]);
138 cube.lineTo(pts[7]);
139 cube.lineTo(pts[5]);
140 cube.close();
141
142 cube.moveTo(pts[0]); cube.lineTo(pts[1]);
143 cube.moveTo(pts[2]); cube.lineTo(pts[3]);
144 cube.moveTo(pts[4]); cube.lineTo(pts[5]);
145 cube.moveTo(pts[6]); cube.lineTo(pts[7]);
146
147 canvas->drawPath(cube, paint);
148
149 {
150 SkPoint3 src[4] = {
151 { 0, 0, 0 }, { 2, 0, 0 }, { 0, 2, 0 }, { 0, 0, 2 },
152 };
153 SkPoint dst[4];
154 mv.setConcat(perspective, camera);
155 Sk3MapPts(dst, mv, src, 4);
156 viewport.mapPoints(dst, 4);
157 const char str[] = "XYZ";
158 for (int i = 1; i <= 3; ++i) {
159 canvas->drawLine(dst[0], dst[i], paint);
160 }
161
162 for (int i = 1; i <= 3; ++i) {
163 canvas->drawText(&str[i-1], 1, dst[i].fX, dst[i].fY, paint);
164 }
165 }
Mike Reed2f0edd52018-05-31 14:22:30 -0400166
167 fAnim->seek(fAnimT);
168 draw_skia(canvas, mv, viewport, fAnim.get());
Mike Reed27714a02018-05-30 16:02:48 -0400169 }
170
171 SkISize onISize() override { return { 1024, 768 }; }
172
173 SkString onShortName() override { return SkString("3dgm"); }
174
Mike Reed2f0edd52018-05-31 14:22:30 -0400175 bool onAnimate(const SkAnimTimer& timer) override {
Jim Van Verth70276912018-06-01 13:46:46 -0400176 if (!fAnim) {
177 return false;
178 }
Mike Reed2f0edd52018-05-31 14:22:30 -0400179 SkScalar dur = fAnim->duration();
180 fAnimT = fmod(timer.secs(), dur) / dur;
181 return true;
182 }
Mike Reed27714a02018-05-30 16:02:48 -0400183 bool onHandleKey(SkUnichar uni) override {
184 switch (uni) {
185 case 'a': fEye.fX += 0.125f; return true;
186 case 'd': fEye.fX -= 0.125f; return true;
187 case 'w': fEye.fY += 0.125f; return true;
188 case 's': fEye.fY -= 0.125f; return true;
189 case 'q': fEye.fZ += 0.125f; return true;
190 case 'z': fEye.fZ -= 0.125f; return true;
191 default: break;
192 }
193 return false;
194 }
195
196 bool onGetControls(SkMetaData*) override { return false; }
197 void onSetControls(const SkMetaData&) override {
198
199 }
200};
201
202DEF_GM(return new GM3d;)
Mike Reed2f0edd52018-05-31 14:22:30 -0400203
204#endif
205