File size: 9,875 Bytes
6cd9596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const THREE = require("three");
const TWEEN = require("@tweenjs/tween.js");
const scene = new THREE.Scene();
scene.background = new THREE.Color(
// 0xcccccc
'white');
const clock = new THREE.Clock();
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000);
camera.position.set(0, 30, 50);
camera.lookAt(0, 3, 0);
const controls = new THREE.OrbitControls(camera);
const ambientLight = new THREE.AmbientLight(0xffffff, 1);
scene.add(ambientLight);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const stats = new Stats();
document.body.appendChild(stats.dom);
/// Anim mixer
const mixers = [];
class Assets {
    static loadEggMtl() {
        return new Promise((resolve, reject) => {
            const loader = new THREE.MTLLoader();
            loader.load(`models/Egg_from_Poly_uovo/Egg from Poly uovo.mtl`, (materials) => {
                materials.preload();
                resolve(materials);
            }, (xhr) => { }, reject);
        });
    }
    static loadEggObj(materials) {
        return new Promise((resolve, reject) => {
            const loader = new THREE.OBJLoader();
            loader.setMaterials(materials);
            loader.load(`models/Egg_from_Poly_uovo/Egg from Poly uovo.obj`, (object) => {
                resolve(object);
            }, (xhr) => {
                // c.log(`${ xhr.loaded / xhr.total * 100 }% loaded`);
            }, (error) => {
                c.error(error);
                reject(error);
            });
        });
    }
    static async loadEgg() {
        const materialCreator = await this.loadEggMtl();
        return this.loadEggObj(materialCreator);
    }
    static loadEggGltf() {
        return new Promise((resolve, reject) => {
            const loader = new THREE.GLTFLoader();
            loader.load(`models/Egg_gltf/Egg from Poly uovo copy.gltf`, (gltf) => {
                c.log(gltf);
                resolve(gltf.scene);
            });
        });
    }
    static loadDogDae() {
        /// In Dae/Collada: did not manage to get 
        /// either the anims or the texture.
        return new Promise((resolve, reject) => {
            const loader = new THREE.ColladaLoader();
            loader.load(`models/dog/pup_lohound.dae`, (collada) => {
                resolve(collada);
            });
        });
    }
    static loadDogFbx() {
        return new Promise((resolve, reject) => {
            const loader = new THREE.FBXLoader();
            loader.load(`models/dog_fbx/puppy-snapchat.fbx`, (fbx) => {
                resolve(fbx);
            });
        });
    }
    static loadBoloss() {
        /// In Dae/Collada: did not manage to get 
        /// either the anims or the texture.
        return new Promise((resolve, reject) => {
            const loader = new THREE.ColladaLoader();
            loader.load(`models/boloss/Boloss-3d v10.dae`, (collada) => {
                resolve(collada);
            });
        });
    }
}
class TUtils {
    static boundingBox(o) {
        const bbox = new THREE.Box3().setFromObject(o);
        return bbox;
    }
    static flushYZero(o) {
        o.position.y = -(this.boundingBox(o)).min.y;
    }
    static perform(tween) {
        return new Promise(resolve => {
            tween.onComplete(resolve).start();
        });
    }
}
(async () => {
    /**
     * scene construction
     */
    const gridHelper = new THREE.GridHelper(100, 100);
    scene.add(gridHelper);
    const axesHelper = new THREE.AxesHelper(50);
    scene.add(axesHelper);
    {
        const egg = await Assets.loadEgg();
        c.log(egg);
        egg.scale.setScalar(.2);
        egg.rotateX(-Math.PI / 2);
        egg.position.x = -18;
        TUtils.flushYZero(egg);
        const box = new THREE.BoxHelper(egg);
        scene.add(box);
        scene.add(egg);
        ///// Manually set the material, for fun.
        // const eggFace = egg.getObjectByName("CallKit-IconMask") as THREE.Mesh;
        // c.log(eggFace.material);
        // (<THREE.MeshPhongMaterial>(eggFace.material)).color.set(0x000000);
    }
    {
        const egg = await Assets.loadEggGltf();
        c.log(egg);
        egg.scale.setScalar(100);
        egg.position.x = -28;
        TUtils.flushYZero(egg);
        egg.remove(egg.getObjectByName('Camera'));
        scene.add(egg);
        // c.log(Utils.boundingBox(egg));
        const box = new THREE.BoxHelper(egg, new THREE.Color('red'));
        scene.add(box);
    }
    {
        ////// dog_fbx
        const dog = await Assets.loadDogFbx();
        // c.log((<any>dog).animations);
        const mixer = new THREE.AnimationMixer(dog);
        const clip = dog.animations.find(clip => clip.name === "lohound|lohoundAction");
        /// ^^ this is the main parent animation! Do not play all children.
        c.log(clip);
        mixer.clipAction(clip).play();
        mixers.push(mixer);
        const container = new THREE.Group();
        container.add(dog);
        container.scale.setScalar(0.007); /// <- scale a container, not the dog itself or it'll fuck the anims.
        container.position.x = -6;
        scene.add(container);
        const box = new THREE.BoxHelper(container, new THREE.Color('green'));
        scene.add(box);
    }
    {
        const boloss = (await Assets.loadBoloss()).scene;
        c.log(boloss);
        boloss.position.x = 16;
        TUtils.flushYZero(boloss);
        scene.add(boloss);
        const box = new THREE.BoxHelper(boloss, new THREE.Color('blue'));
        scene.add(box);
        /// Anims like in AudioBoloss
        const rootModel = boloss.getObjectByName(`SketchUp`);
        const pupilL = boloss.getObjectByName(`Pupil-left`);
        const pupilR = boloss.getObjectByName(`Pupil-right`);
        const pupils = new THREE.Group();
        pupils.add(pupilL, pupilR);
        rootModel.add(pupils);
        (async () => {
            while (true) {
                const translatePupil = new TWEEN.Tween(pupils.position)
                    .to({ x: "-1", y: "-1" }, 200)
                    .easing(TWEEN.Easing.Quadratic.Out);
                const translatePupilRev = new TWEEN.Tween(pupils.position)
                    .to({ x: "+1", y: "+1" }, 200)
                    .easing(TWEEN.Easing.Quadratic.Out);
                await TUtils.perform(translatePupil);
                await Utils.wait(4, 1);
                await TUtils.perform(translatePupilRev);
                await Utils.wait(8, 3);
            }
        })();
        const eyebrowL = boloss.getObjectByName(`Eyebrow-left`);
        const eyebrowR = boloss.getObjectByName(`Eyebrow-right`);
        const eyebrows = new THREE.Group();
        eyebrows.add(eyebrowL, eyebrowR);
        rootModel.add(eyebrows);
        (async () => {
            while (true) {
                const scaleEyebrow = new TWEEN.Tween(eyebrows.scale)
                    .to({ x: 1.08, y: 1.08, z: 1.08 }, 100)
                    .easing(TWEEN.Easing.Quadratic.InOut);
                const scaleEyebrowRev = new TWEEN.Tween(eyebrows.scale)
                    .to({ x: 1, y: 1, z: 1 }, 100)
                    .easing(TWEEN.Easing.Quadratic.InOut);
                await Utils.wait(6, 6);
                await TUtils.perform(scaleEyebrow);
                await TUtils.perform(scaleEyebrowRev);
                await Utils.wait(0.14);
                await TUtils.perform(scaleEyebrow);
                await TUtils.perform(scaleEyebrowRev);
            }
        })();
        (async () => {
            while (true) {
                const angle = Utils.randomFloat(-0.2, 0.3);
                const dummyL = new THREE.Object3D();
                dummyL.rotateOnAxis(new THREE.Vector3(0, 1, 0.8), angle);
                const dummyR = new THREE.Object3D();
                dummyR.rotateOnAxis(new THREE.Vector3(0, -1, -0.8), angle);
                /// ^^ exact same result as keeping the same vector and negating the angle.
                const rotateBrowL = new TWEEN.Tween(eyebrowL.rotation)
                    .to({
                    x: dummyL.rotation.x,
                    y: dummyL.rotation.y,
                    z: dummyL.rotation.z,
                }, 300);
                const rotateBrowR = new TWEEN.Tween(eyebrowR.rotation)
                    .to({
                    x: dummyR.rotation.x,
                    y: dummyR.rotation.y,
                    z: dummyR.rotation.z,
                }, 300);
                await Promise.all([
                    TUtils.perform(rotateBrowL),
                    TUtils.perform(rotateBrowR),
                ]);
                await Utils.wait(1, 1);
                await Promise.all([
                    TUtils.perform(new TWEEN.Tween(eyebrowL.rotation).to({ x: 0, y: 0, z: 0 }, 300)),
                    TUtils.perform(new TWEEN.Tween(eyebrowR.rotation).to({ x: 0, y: 0, z: 0 }, 300)),
                ]);
                await Utils.wait(1, 1);
                /// ^^ not the exact same behavior as in AudioBoloss (all waits are actually randoms there.)
            }
        })();
    }
})();
/**
 * MAIN()
 */
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
}
function render() {
    const delta = clock.getDelta();
    for (const mixer of mixers) {
        mixer.update(delta);
    }
    renderer.render(scene, camera);
}
function animate() {
    requestAnimationFrame(animate);
    TWEEN.update();
    render();
    stats.update();
}
animate();