forked from reality3d/3d-piano-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
225 lines (182 loc) · 7.88 KB
/
index.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
<html>
<head>
<title>3D Piano Player (Three.js)</title>
<script src="js/three.js"></script>
<script src="js/OrbitAndPanControls.js"></script>
<script src="js/ColladaLoader.js"></script>
<!-- extras -->
<script src="js/base64binary.js" type="text/javascript"></script>
<script src="js/Window/DOMLoader.script.js" type="text/javascript"></script>
<script type="text/javascript" src="js/dat.gui.js"></script>
<style>
canvas
{
width: 100%; height: 100%
}
body
{
color: rgb(0, 0, 0);
font-family:Monospace;
font-size:13px;
text-align:center;
background-color: #fff;
margin: 0px;
overflow: hidden;
}
a
{
color: #1e83ff;
}
</style>
</head>
<body>
<script type="text/javascript">
var keyState = Object.freeze ({unpressed: {}, note_on: {}, pressed:{}, note_off:{} });
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(30, window.innerWidth/window.innerHeight, 2.0, 5000);
var keys_down = [];
var keys_obj = [];
var renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true;
renderer.shadowMapSoft = true;
renderer.shadowMapType = THREE.PCFSoftShadowMap;
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.physicallyBasedShading = true;
document.body.appendChild(renderer.domElement);
var material = new THREE.MeshLambertMaterial( { color: 0x606060} )
floor = new THREE.Mesh( new THREE.PlaneGeometry( 8000, 8000 ), new THREE.MeshBasicMaterial( { color: 0xf0f0f0 } ) );
floor.rotation.x = - 90 * ( Math.PI / 180 );
floor.position.y = -0.45;
floor.receiveShadow = true;
floor.castShadow = true;
scene.add( floor );
scene.fog = new THREE.Fog( 0xffffff, 40, 50 );
var controls = new function() {
this.noteOnColor = [ 255, 0, 0, 1.0 ];
};
noteOnColor = new THREE.Color().setRGB(controls.noteOnColor[0]/256.0, controls.noteOnColor[1]/256.0, controls.noteOnColor[2]/256.0);
init_lights();
var loader = new THREE.ColladaLoader();
loader.load( 'obj/piano.dae', prepare_scene );
camera.position.x = -2.77;
camera.position.z = 10.04;
camera.position.y = 5.51;
var cameraControls = new THREE.OrbitAndPanControls(camera, renderer.domElement);
cameraControls.target.set(4.5,0,0);
var clock = new THREE.Clock();
function on_window_resize()
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function init_lights()
{
//var spotlight = new THREE.SpotLight(0xffffff);
var spotlight = new THREE.DirectionalLight(0xffffff);
spotlight.position.set(1.0,2.4,-7.5);
spotlight.target.position.set(6.0,-6,7);
spotlight.shadowCameraVisible = false;
spotlight.shadowDarkness = 0.75;
spotlight.intensity = 1;
spotlight.castShadow = true;
spotlight.shadowMapWidth = 2048;
spotlight.shadowMapHeight = 2048;
spotlight.shadowCameraNear = 5.0;
spotlight.shadowCameraFar = 20.0;
spotlight.shadowBias = 0.0025;
spotlight.shadowCameraLeft = -8.85;
spotlight.shadowCameraRight = 5.5;
spotlight.shadowCameraTop = 4;
spotlight.shadowCameraBottom = 0;
scene.add(spotlight);
var light = new THREE.DirectionalLight( 0xddffff, 0.5 );
light.position.set( 1, 1, 1 ).normalize();
scene.add( light );
var light = new THREE.DirectionalLight( 0xff5555, 0.5 );
light.position.set( -1, -1, -1 ).normalize();
scene.add( light );
}
function prepare_scene( collada )
{
collada.scene.traverse(initialize_keys);
scene.add(collada.scene);
}
function initialize_keys( obj)
{
keys_obj.push(obj);
obj.rotation.x = -Math.PI/4.0;
obj.rotation.y = 0;
obj.rotation.z = 0;
obj.keyState = keyState.unpressed;
obj.clock = new THREE.Clock(false);
obj.castShadow = true;
obj.receiveShadow = true;
// only add meshes in the material redefinition (to make keys change their color when pressed)
if (obj instanceof THREE.Mesh)
{
old_material = obj.material;
obj.material = new THREE.MeshPhongMaterial( { color:old_material.color} );
obj.material.shininess = 35.0;
obj.material.specular = new THREE.Color().setRGB(0.25, 0.25, 0.25);;
obj.material.note_off = obj.material.color.clone();
}
}
function key_status (keyName, status)
{
var obj = scene.getObjectByName(keyName, true);
if (obj != undefined)
{
obj.clock.start();
obj.clock.elapsedTime = 0;
obj.keyState = status;
}
}
function frame()
{
requestAnimationFrame(frame);
var delta = clock.getDelta();
update(delta);
render(delta);
}
function update_key( obj, delta )
{
if (obj.keyState == keyState.note_on)
{
obj.rotation.x = mix(-Math.PI/4.0, -controls.key_max_rotation, smoothstep(0.0, 1.0, controls.key_attack_time*obj.clock.getElapsedTime()));
if (obj.rotation.x >= -controls.key_max_rotation)
{
obj.keyState = keyState.pressed;
obj.clock.elapsedTime = 0;
}
obj.material.color = noteOnColor;
}
else if (obj.keyState == keyState.note_off)
{
obj.rotation.x = mix(-controls.key_max_rotation, -Math.PI/4.0, smoothstep(0.0, 1.0, controls.key_attack_time*obj.clock.getElapsedTime()));
if (obj.rotation.x <= -Math.PI/4.0)
{
obj.keyState = keyState.unpressed;
obj.clock.elapsedTime = 0;
}
obj.material.color = obj.material.note_off;
}
}
function update( delta )
{
cameraControls.update(delta);
for(i in keys_obj)
{
update_key(keys_obj[i], delta);
}
}
function render( delta )
{
renderer.render(scene, camera);
};
frame();
</script>
</body>
</html>