-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
322 lines (293 loc) · 14 KB
/
test.html
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Babylon Template</title>
<style>
html,
body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
</style>
<script src="https://preview.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
<script>
var canvas = null;
var engine = null;
/******* Add the create scene function ******/
function createScene() {
let scene, camera, light, pilot;
let soundsReady = 0;
function soundReady() {
soundsReady++;
console.log(soundsReady)
if (soundsReady == 5) {
// barryvoxFar.play();
barryvoxClose.play();
barryvoxMedium.play();
barryvoxScanning.play();
}
};
function setupScene() {
scene = new BABYLON.Scene(engine);
scene.clearColor = new BABYLON.Color3(.5, .5, .5);
}
function setupCamera() {
camera = new BABYLON.ArcRotateCamera(
"Camera",
-75 / 180 * Math.PI, // from X to Z
+75 / 180 * Math.PI, // from Y to X
15, // distance
new BABYLON.Vector3(0, 0, 0), // direction
scene
);
camera.attachControl(canvas, true);
}
function setupLights() {
light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0), scene);
}
function localAxes(size) {
var pilot_local_axisX = BABYLON.MeshBuilder.CreateLines("pilot_local_axisX",
{
points: [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(size, 0, 0), new BABYLON.Vector3(size * 0.95, 0.05 * size, 0),
new BABYLON.Vector3(size, 0, 0), new BABYLON.Vector3(size * 0.95, -0.05 * size, 0)
]
}, scene);
pilot_local_axisX.color = new BABYLON.Color3(1, 0, 0);
pilot_local_axisY = BABYLON.MeshBuilder.CreateLines("pilot_local_axisY", {
points: [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, size, 0), new BABYLON.Vector3(-0.05 * size, size * 0.95, 0),
new BABYLON.Vector3(0, size, 0), new BABYLON.Vector3(0.05 * size, size * 0.95, 0)
]
}, scene);
pilot_local_axisY.color = new BABYLON.Color3(0, 1, 0);
var pilot_local_axisZ = BABYLON.MeshBuilder.CreateLines("pilot_local_axisZ", {
points: [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, 0, size), new BABYLON.Vector3(0, -0.05 * size, size * 0.95),
new BABYLON.Vector3(0, 0, size), new BABYLON.Vector3(0, 0.05 * size, size * 0.95)
]
}, scene);
pilot_local_axisZ.color = new BABYLON.Color3(0, 0, 1);
var local_origin = BABYLON.MeshBuilder.CreateBox("local_origin", { size: 1 }, scene);
local_origin.isVisible = false;
pilot_local_axisX.parent = local_origin;
pilot_local_axisY.parent = local_origin;
pilot_local_axisZ.parent = local_origin;
return local_origin;
}
function showAxis(size) {
var makeTextPlane = function (text, color, size) {
var dynamicTexture = new BABYLON.DynamicTexture("DynamicTexture", 50, scene, true);
dynamicTexture.hasAlpha = true;
dynamicTexture.drawText(text, 5, 40, "bold 36px Arial", color, "transparent", true);
var plane = new BABYLON.MeshBuilder.CreatePlane("TextPlane", { size: size, updatable: true }, scene);
plane.material = new BABYLON.StandardMaterial("TextPlaneMaterial", scene);
plane.material.backFaceCulling = false;
plane.material.specularColor = new BABYLON.Color3(0, 0, 0);
plane.material.diffuseTexture = dynamicTexture;
return plane;
};
var axisX = BABYLON.MeshBuilder.CreateLines("axisX", {
points: [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(size, 0, 0), new BABYLON.Vector3(size * 0.95, 0.05 * size, 0),
new BABYLON.Vector3(size, 0, 0), new BABYLON.Vector3(size * 0.95, -0.05 * size, 0)
]
}, scene);
axisX.color = new BABYLON.Color3(1, 0, 0);
var xChar = makeTextPlane("X", "red", size / 10);
xChar.position = new BABYLON.Vector3(0.9 * size, -0.05 * size, 0);
var axisY = BABYLON.MeshBuilder.CreateLines("axisY", {
points: [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, size, 0), new BABYLON.Vector3(-0.05 * size, size * 0.95, 0),
new BABYLON.Vector3(0, size, 0), new BABYLON.Vector3(0.05 * size, size * 0.95, 0)
]
}, scene);
axisY.color = new BABYLON.Color3(0, 1, 0);
var yChar = makeTextPlane("Y", "green", size / 10);
yChar.position = new BABYLON.Vector3(0, 0.9 * size, -0.05 * size);
var axisZ = BABYLON.MeshBuilder.CreateLines("axisZ", {
points: [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, 0, size), new BABYLON.Vector3(0, -0.05 * size, size * 0.95),
new BABYLON.Vector3(0, 0, size), new BABYLON.Vector3(0, 0.05 * size, size * 0.95)
]
}, scene);
axisZ.color = new BABYLON.Color3(0, 0, 1);
var zChar = makeTextPlane("Z", "blue", size / 10);
zChar.position = new BABYLON.Vector3(0, 0.05 * size, 0.9 * size);
};
function addPilot() {
pilot = BABYLON.MeshBuilder.CreateCylinder("pilot",
{ height: 0.75, diameterTop: 0.2, diameterBottom: 0.5, tessellation: 6, subdivisions: 1 },
scene
);
var greyMat = new BABYLON.StandardMaterial("grey", scene);
greyMat.emissiveColor = new BABYLON.Color3(0.2, 0.2, 0.2);
pilot.material = greyMat;
var arm = BABYLON.MeshBuilder.CreateBox("arm", { height: 0.75, width: 0.3, depth: 0.1875 }, scene);
arm.material = greyMat;
arm.position.x = 0.125;
arm.parent = pilot;
var localOrigin = localAxes(2);
localOrigin.parent = pilot;
}
function movePilot() {
for (let i = 1; i <= 400; ++i) {
new Promise((resolve) => setTimeout(resolve, i * 10)).then(() => {
pilot.position.x += 0.01;
pilot.position.y -= 0.01;
pilot.position.z -= 0.01;
});
}
}
function addProbeFromBlender() {
BABYLON.SceneLoader.ImportMesh("", "./models/", "probe.babylon", scene, function (mesh) {
// transforming probe mesh
mesh[0].scaling = new BABYLON.Vector3(0.25, 0.2, 0.25);
// newMeshes[0].parent = camera;
// console.log(newMeshes); // testing
// newMeshes[0].rotation.z = Math.PI / 2
// newMeshes[0].rotation.y = -Math.PI / 2;
// newMeshes[0].position.x = -0.5;
// newMeshes[0].position.y = -0.5;
// newMeshes[0].position.z = 1.5;
console.log("done");
mesh[0].position = new BABYLON.Vector3(0, 10, 0);
mesh[0].checkCollisions = true;
});
};
function animateProbe() {
let frameRate = 10;
let moveY = new BABYLON.Animation("moveY", "position.y", frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
var keyFrames = [];
keyFrames.push({
frame: 0,
value: 10
});
keyFrames.push({
frame: frameRate,
value: -2
});
keyFrames.push({
frame: 2 * frameRate,
value: 10
});
moveY.setKeys(keyFrames);
scene.getMeshByName("Cylinder").animations.push(moveY);
scene.beginAnimation(scene.getMeshByName("Cylinder"), 0, 2 * frameRate, false);
};
function moveProbe() {
scene.registerAfterRender(function () {
// using imprecise for now
// if (scene.getMeshByName("Cylinder").intersectsMesh(ground, false)) {
// console.log("INTERSECTING");
// };
if (scene.getMeshByName("Cylinder").position.y < 5) {
particle();
barryvoxFar.play();
};
});
scene.onKeyboardObservable.add((kbInfo) => {
if ((kbInfo.type == BABYLON.KeyboardEventTypes.KEYDOWN) && (kbInfo.event.key == "p" || kbInfo.event.key == "P")) {
animateProbe();
};
});
};
function particle() {
var emitter = BABYLON.MeshBuilder.CreateBox("emitter", { size: 0.01 }, scene);
var particles = new BABYLON.ParticleSystem("sparks", 1000, scene);
particles.emitter = emitter;
particles.minEmitBox = new BABYLON.Vector3(0, 0, 0);
particles.maxEmitBox = new BABYLON.Vector3(0, 0, 0);
particles.particleTexture = new BABYLON.Texture("./textures/snow.png"); // from the babylon.js demo
particles.minEmitPower = 1;
particles.maxEmitPower = 4;
particles.emitRate = 1500;
particles.color1 = new BABYLON.Color4(1, 1, 1, 1.0);
particles.color2 = new BABYLON.Color4(1, 1, 1, 0.4);
particles.colorDead = new BABYLON.Color4(0.4, 0.4, 0.4, 0.2);
particles.minSize = 0.01;
particles.maxSize = 0.1;
particles.minLifeTime = 0.1;
particles.maxLifeTime = 0.2;
particles.direction1 = new BABYLON.Vector3(1, 0.5, 1);
particles.direction2 = new BABYLON.Vector3(-1, 1, -1);
// particles.minAngularSpeed = 0;
// particles.maxAngularSpeed = 0.5;
particles.targetStopDuration = 0.1;
particles.start();
};
setupScene();
setupCamera();
setupLights();
showAxis(5);
addPilot();
addProbeFromBlender();
// sounds
var barryvoxOn = new BABYLON.Sound("barryvoxOn", "./sounds/BarryvoxOn.wav", scene, soundReady, {
loop: false,
autoplay: true
});
var barryvoxFar = new BABYLON.Sound("barryvoxFar", "./sounds/BarryvoxFar.wav", scene, soundReady, {
loop: true,
autoplay: false
});
var barryvoxMedium = new BABYLON.Sound("barryvoxMedium", "./sounds/BarryvoxMedium.wav", scene, soundReady, {
loop: true,
autoplay: false
});
var barryvoxClose = new BABYLON.Sound("barryvoxClose", "./sounds/BarryvoxClose.wav", scene, soundReady, {
loop: true,
autoplay: false
});
var barryvoxScanning = new BABYLON.Sound("barryvoxScanning", "./sounds/BarryvoxScanning.wav", scene, soundReady, {
loop: true,
autoplay: false
});
pilot.position = new BABYLON.Vector3(0, 0, 0);
let ground = BABYLON.MeshBuilder.CreateGround("Ground", { width: 100, height: 100 });
ground.isPickable = true;
ground.checkCollisions = true;
scene.executeWhenReady(function () {
moveProbe();
});
// scene.onPointerMove = function () {
// };
// pilot.rotation = new BABYLON.Vector3(0 / 180 * Math.PI, 0 / 180 * Math.PI, 0 / 180 * Math.PI);
// pilot.addRotation(0 / 180 * Math.PI, 0 / 180 * Math.PI, 0 / 180 * Math.PI);
// movePilot();
return scene;
};
/******* End of the create scene function ******/
function init() {
canvas = document.getElementById("renderCanvas");
engine = new BABYLON.Engine(
canvas,
true,
{ preserveDrawingBuffer: true, stencil: true, disableWebGL2Support: false }
);
let scene = createScene();
// Register a render loop to repeatedly render the scene
engine.runRenderLoop(function () {
scene.render();
});
// Watch for browser/canvas resize events
window.addEventListener("resize", function () {
engine.resize();
});
}
</script>
</head>
<body onload="init()">
<canvas id="renderCanvas"></canvas>
</body>
</html>