-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
140 lines (113 loc) · 5.11 KB
/
demo.js
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
import PixelPath from "./PixelPath.js";
class TestScene extends PixelPath.Scene
{
constructor()
{
super();
this.key = 'test'
}
preload()
{
console.log('preload');
this.load.image('sky', './sky.jpg');
this.load.image('earth_cloud', './earth_cloud.jpg');
this.load.image('fox', './fox.jpg');
this.load.json('foxjson', './fox.json');
this.load.image('forest', './forest.jpg');
this.load.json('forestjson', './forest.json');
this.load.image('snout', './snout.jpg');
this.load.json('snout-rig', './snout-rig.json');
this.load.json('snout-anim', './snout-anim.json');
}
create()
{
console.log('create');
const gl = this.renderer.gl
// this.renderer.dpr = 2;
this.renderer.gl.clearColor(1, 1, 1, 1);
// this.renderer.setSize(640, 640);
this.camera.fov = 45;
this.camera.position.set(0, 4, 8);
// this.camera.rotation.z = -0.3;
this.camera.lookAt([0, 0, 0]);
this.camera.perspective({ aspect: gl.canvas.width / gl.canvas.height });
this.controls = new PixelPath.Utils.Orbit(this.camera);
const size = 8;
const num = size * size;
let offset = new Float32Array(num * 3);
let random = new Float32Array(num * 3);
for (let i = 0; i < num; i++)
{
// Layout in a grid
offset.set([((i % size) - size * 0.5) * 2, 0, (Math.floor(i / size) - size * 0.5) * 2], i * 3);
random.set([Math.random(), Math.random(), Math.random()], i * 3);
}
const forestjson = this.cache.get('forestjson');
const forestTexture = this.cache.get('forest');
const forestGeometry = new PixelPath.GameObjects.Geometry(gl, {
position: { size: 3, data: new Float32Array(forestjson.position) },
uv: { size: 2, data: new Float32Array(forestjson.uv) },
offset: { instanced: true, size: 3, data: offset },
random: { instanced: true, size: 3, data: random },
});
this.forest = this.add.model({ geometry: forestGeometry, program: 'fog', texture: forestTexture });
const foxJson = this.cache.get('foxjson');
const foxTexture = this.cache.get('fox');
const foxGeometry = new PixelPath.GameObjects.Geometry(gl, {
position: { size: 3, data: new Float32Array(foxJson.position) },
uv: { size: 2, data: new Float32Array(foxJson.uv) },
normal: { size: 3, data: new Float32Array(foxJson.normal) },
});
this.fox = this.add.model({ geometry: foxGeometry, texture: foxTexture })
this.fox.position.set(0, 0, 0);
this.fox.scale.set(0.3, 0.3, 0.3);
const snoutJson = this.cache.get('snout-rig');
const snoutTexture = this.cache.get('snout');
const snoutGeometry = new PixelPath.GameObjects.Geometry(gl, {
position: { size: 3, data: new Float32Array(snoutJson.position) },
uv: { size: 2, data: new Float32Array(snoutJson.uv) },
normal: { size: 3, data: new Float32Array(snoutJson.normal) },
});
this.snout = this.add.model({ geometry: snoutGeometry, texture: snoutTexture })
this.snout.position.set(-2, 0, 0);
this.snout.scale.set(0.01);
const skinGeometry = new PixelPath.GameObjects.Geometry(gl, {
position: { size: 3, data: new Float32Array(snoutJson.position) },
uv: { size: 2, data: new Float32Array(snoutJson.uv) },
normal: { size: 3, data: new Float32Array(snoutJson.normal) },
skinIndex: { size: 4, data: new Float32Array(snoutJson.skinIndex) },
skinWeight: { size: 4, data: new Float32Array(snoutJson.skinWeight) },
});
this.skin = this.add.skin({ rig: snoutJson.rig, geometry: skinGeometry, texture: snoutTexture })
this.skin.position.set(2, 0, 0);
this.skin.scale.set(0.01);
this.animation = this.skin.addAnimation(this.cache.get('snout-anim'));
const skyTexture = this.cache.get('sky');
const skyGeometry = new PixelPath.GameObjects.Sphere(gl, { radius: 1, widthSegments: 64 });
this.skybox = this.add.model({ geometry: skyGeometry, texture: skyTexture })
this.skybox.scale.set(20);
this.add.gridHelper({ size: 20, divisions: 20 });
this.add.axesHelper({ size: 6, symmetric: true });
const mode = [gl.POINTS, gl.LINES, gl.LINE_LOOP, gl.TRIANGLES];
let modeIndex = 3
document.addEventListener("mousedown", () =>
{
modeIndex += 1;
if (modeIndex >= mode.length) modeIndex = 0;
this.fox.mode = mode[modeIndex];
this.snout.mode = mode[modeIndex];
});
}
update(t, d)
{
this.fox.rotation.y -= 0.005;
this.controls.update();
this.animation.elapsed += 0.1;
this.skin.update();
}
}
new PixelPath.Game({
width: 640,
height: 640,
scenes: [TestScene]
})