-
Notifications
You must be signed in to change notification settings - Fork 1
/
babylonScene.js
133 lines (99 loc) · 4.42 KB
/
babylonScene.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
var createScene = function () {
// This creates a basic Babylon Scene object (non-mesh)
var scene = new BABYLON.Scene(engine);
// This creates and positions a free camera (non-mesh)
var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
// This targets the camera to scene origin
camera.setTarget(BABYLON.Vector3.Zero());
// This attaches the camera to the canvas
//camera.attachControl(canvas, true);
// This creates a light, aiming 0,1,0 - to the sky (non-mesh)
var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
// Default intensity is 1. Let's dim the light a small amount
light.intensity = 0.7;
// Our built-in 'sphere' shape. Params: name, subdivs, size, scene
var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);
// Move the sphere upward 1/2 its height
sphere.position.y = 1;
// Our built-in 'ground' shape. Params: name, width, depth, subdivs, scene
var ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);
// Add elements
var blueMat = new BABYLON.StandardMaterial("blue", scene);
blueMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
blueMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
blueMat.emissiveColor = BABYLON.Color3.Blue();
var purpleMat = new BABYLON.StandardMaterial("purple", scene);
purpleMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
purpleMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
purpleMat.emissiveColor = BABYLON.Color3.Purple();
var blueBox = BABYLON.MeshBuilder.CreateBox("box", {size:1}, scene);
blueBox.material = blueMat;
blueBox.position.x = -2.5;
blueBox.position.y = 0.5;
blueBox.position.z = 2;
var purpleDonut = BABYLON.MeshBuilder.CreateTorus("torus", {diameter:1, thickness:0.5}, scene);
purpleDonut.material = purpleMat;
purpleDonut.position.x = 2;
purpleDonut.position.y = 0.5;
purpleDonut.position.z = -2;
var redMat = new BABYLON.StandardMaterial("red", scene);
redMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
redMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
redMat.emissiveColor = BABYLON.Color3.Red();
for(var i=0;i<3;i++){
var redBox = BABYLON.Mesh.CreateBox("redBox"+i, 0.5, scene);
redBox.material = redMat;
redBox.position.x = -3;
redBox.position.x += i;
redBox.position.y = 3;
}
var pickedObject = sphere;
var actionPointerDown = (obj) => {
if(obj != null){
pickedObject = obj;
if (pickedObject == sphere){
speed = 0.5;
}
else if (pickedObject == purpleDonut){
speed = 1.5;
}
else if (pickedObject == blueBox){
speed = 0.1;
}
}
}
// Add keyboard control
var speed = 0.5;
scene.onKeyboardObservable.add((keyboardInfo) => {
if(keyboardInfo.type == BABYLON.KeyboardEventTypes.KEYDOWN){
if(keyboardInfo.event.key == "ArrowLeft" || keyboardInfo.event.key == "q"){
pickedObject.position.x-=speed
}
else if(keyboardInfo.event.key == "ArrowRight" || keyboardInfo.event.key == "d")
{
pickedObject.position.x+=speed
}
else if(keyboardInfo.event.key == "ArrowUp" || keyboardInfo.event.key == "z"){
pickedObject.position.z+=speed
}
else if(keyboardInfo.event.key == "ArrowDown" || keyboardInfo.event.key == "s")
{
pickedObject.position.z-=speed
}
}
});
// Add pointer selection
scene.onPointerObservable.add((pointerInfo) => {
if (pointerInfo.type == BABYLON.PointerEventTypes.POINTERDOWN) {
actionPointerDown(pointerInfo.pickInfo.pickedMesh);
}
});
// Animate
scene.actionManager = new BABYLON.ActionManager(scene);
var rotate = function (mesh) {
scene.actionManager.registerAction(new BABYLON.IncrementValueAction(BABYLON.ActionManager.OnEveryFrameTrigger, mesh, "rotation.y", 0.01));
}
rotate(blueBox);
rotate(purpleDonut);
return scene;
};