-
Notifications
You must be signed in to change notification settings - Fork 711
/
scenejs.html
179 lines (141 loc) · 6.5 KB
/
scenejs.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
<!DOCTYPE html>
<html>
<head>
<title>SceneJS / cannon.js example</title>
<meta charset="utf-8">
<style>
* {margin:0;padding:0}
</style>
</head>
<body>
<script src="../libs/scenejs.js"></script>
<script src="../build/cannon.js"></script>
<canvas id="theCanvas" width="600" height="400"/>
<script>
var scene, quaternionNode, translationNode,
world, body, shape, timeStep=1/60;
initCannon();
initSceneJS();
animate();
function initCannon() {
world = new CANNON.World();
world.gravity.set(0,0,0);
shape = new CANNON.Box(new CANNON.Vec3(1,1,1));
body = new CANNON.Body({
mass: 1
});
body.addShape(shape);
body.angularVelocity.set(0,10,0);
body.angularDamping = 0.01;
world.addBody(body);
}
function initSceneJS() {
SceneJS.createScene({
id: "theScene",
canvasId: "theCanvas",
nodes: [
{
type: "lookAt",
eye : { x: 0.0, y: 10.0, z: 15 },
look : { y:1.0 },
up : { y: 1.0 },
nodes: [
{
type: "camera",
optics: {
type: "perspective",
fovy : 25.0,
aspect : 1.47,
near : 0.10,
far : 300.0
},
nodes: [
{
type: "renderer",
clearColor: { r: 1.0, g: 1.0, b: 1.0 },
clear: {
depth : true,
color : true
},
nodes: [
{
type: "light",
mode: "dir",
color: { r: 1.0, g: 1.0, b: 1.0 },
diffuse: true,
specular: true,
dir: { x: 1.0, y: -0.5, z: -1.0 }
},
{
type: "light",
mode: "dir",
color: { r: 1.0, g: 1.0, b: 0.8 },
diffuse: true,
specular: false,
dir: { x: 0.0, y: -0.5, z: -1.0 }
},
{
type: "translate",
id: "translate",
x : 0.0, y : 0.0, z : 0.0,
nodes: [
{
type: "quaternion",
id: "quaternion",
x : 1.0, y : 0.0, z : 0.0, angle : 0.0,
nodes: [
{
type: "material",
emit: 0,
baseColor: { r: 0.5, g: 0.5, b: 0.6 },
specularColor: { r: 0.9, g: 0.9, b: 0.9 },
specular: 1.0,
shine: 70.0,
nodes: [
{
type : "cube",
xSize: shape.halfExtents.x,
ySize: shape.halfExtents.y,
zSize: shape.halfExtents.z,
}
]
}
]
}
]
}
]
}
]
}
]
}
]
});
// Get handles to some nodes
scene = SceneJS.scene("theScene");
quaternionNode = scene.findNode("quaternion");
translationNode = scene.findNode("translate");
}
function animate() {
// Start the scene rendering
scene.start({
idleFunc: updatePhysics
});
}
function updatePhysics() {
var axisAndAngle, axis, angle;
// Step the physics world
world.step(timeStep);
// Copy position from cannon to SceneJS translation node
translationNode.set("xyz",body.position);
// Get orientation of the body from cannon.js
axisAndAngle = body.quaternion.toAxisAngle();
axis = axisAndAngle[0];
angle = axisAndAngle[1];
// Copy orientation to the SceneJS quaternion node
quaternionNode.set("rotation",{ x: axis.x, y: axis.y, z: axis.z, angle: angle*180 / Math.PI });
}
</script>
</body>
</html>