-
Notifications
You must be signed in to change notification settings - Fork 13
/
ThrreeMoveSave.html
281 lines (234 loc) · 9.04 KB
/
ThrreeMoveSave.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - equirectangular video panorama</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
color: #ffffff;
padding: 5px;
font-family:Monospace;
font-size:13px;
font-weight: bold;
text-align:center;
}
a {
color: #ffffff;
}
</style>
</head>
<body>
<div>
<input type="text" id="name" name="name" value="joe">
<button type="button" id= "save">Save</button>
<select id="others" name="others"></select>
</div>
<div id="container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r75/three.min.js"></script>
<script src="loaders/OBJLoader.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.23/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.23/addons/p5.dom.js"></script>
<script src="P5Sketch.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
var camera, scene, renderer;
var p5Texture;
var myHead;
var videoImageContext, foregroundVideo, videoTexture;
isUserInteracting = false,
onMouseDownMouseX = 0, onMouseDownMouseY = 0,
lon = 0, onMouseDownLon = 0,
lat = 0, onMouseDownLat = 0,
phi = 0, theta = 0,
distance = 500;
function getOtherUsers(){
console.log("Getting Users");
$.ajax( { url: "https://api.mlab.com/api/1/databases/osc/collections/osc_collection?apiKey=COrdiz9qAt5OlZOLRyoKaaaG-60PkkxN",
type: "GET",
success: function (data){ //create the select ui element based on what came back from db
$.each(data, function(index,obj){
$("#others").append("'<option value = '" + obj.camera + "'>" + obj._id + "</option>");
console.log(obj);
})
},
contentType: "application/json" } );
}
var sender= function sendPosition( ){
var myName = $("#name").val(); //get the name from the input element
console.log("Saving for " + myName);
$.ajax( { url: "https://api.mlab.com/api/1/databases/osc/collections/osc_collection?apiKey=COrdiz9qAt5OlZOLRyoKaaaG-60PkkxN",
// data: JSON.stringify( { "_id":myName, "x" : myHead.position.y , "y": myHead.position.y , "z": myHead.position.z } ),
data: JSON.stringify( { "_id":myName,"camera_rot_x": myHead.rotation.x, "camera" : JSON.stringify(camera.matrix.toArray()) } ),
type: "POST",
contentType: "application/json" } );
}
function init() {
getOtherUsers(); //populate the pull down
$("#save").click(sender); //attache a script pulll downfor ressetting camera
$("#others").change(function(){
var cameraState = $("#others").val(); //get the camera state from the select element
var jsonCamera = JSON.parse(cameraState); //parse it into json
console.log(jsonCamera);
camera.matrix.fromArray(jsonCamera); // set the camera using saved camera settings
camera.matrix.decompose(camera.position,camera.quaternion,camera.scale);
}); //attache a script to button
///GET THREE.JS GOING
var container, mesh;
container = document.getElementById( 'container' );
camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1100 );
camera.target = new THREE.Vector3( 0, 0, 0 );
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
////////360 VIDEO BACkGROUND, video texture mapped on a sphere
var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 );
geometry.scale( - 1, 1, 1 );
var video = document.createElement( 'video' );
video.width = 640;
video.height = 360;
video.autoplay = true;
video.loop = true;
video.src = "dano.MP4";
var texture = new THREE.VideoTexture( video );
texture.minFilter = THREE.LinearFilter;
texture.format = THREE.RGBFormat;
var material = new THREE.MeshBasicMaterial( { map : texture } );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
/////THROW IN A LIGHT
var ambient = new THREE.AmbientLight( 0x101030 );
scene.add( ambient );
var directionalLight = new THREE.DirectionalLight( 0xffeedd );
directionalLight.position.set(1, 1, -10 );
scene.add( directionalLight );
////PUT IN A P5 SKETCH USING THE CANVAS MADE IN P5 AS A TEXTURE
geo = new THREE.PlaneGeometry(100,100);
p5Texture = new THREE.Texture( p5cs.elt );
mat = new THREE.MeshBasicMaterial({ map: p5Texture, transparent: true, opacity: 1, side: THREE.DoubleSide });
var plane = new THREE.Mesh(geo, mat);
plane.position.y = 0;
plane.position.z = -100;
plane.rotation.x = -Math.PI/2;
plane.rotation.y = -Math.PI/2;
plane.scale.set(2,2,2);
scene.add(plane);
///START LOADING MODELS
var manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {
console.log( item, loaded, total );
};
var onProgress = function ( xhr ) {
if ( xhr.lengthComputable ) {
var percentComplete = xhr.loaded / xhr.total * 100;
console.log( Math.round(percentComplete, 2) + '% downloaded' );
}
};
var onError = function ( xhr ) {
};
///// LOAD IN ANOTHER OBJ MODLE MADE IN MICROSOFT 3D SCAN
// model
var bustLoader = new THREE.OBJLoader( manager );
bustLoader.load( 'danoBustObj4.obj', function ( object ) {
//loader.load( 'male02/male02.obj', function ( object ) {
myHead = object;
object.traverse( function ( child ) {
if ( child instanceof THREE.Mesh ) {
child.material.map = texture;
}
} );
object.position.y = -200;
object.position.z = 300;
object.position.x = -100;
object.rotation.x = -Math.PI/2;
object.rotation.z= Math.PI/1;
object.scale.y = 1;
object.scale.z = 1;
scene.add( object );
}, onProgress, onError );
//////USER INTERFACE
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
document.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
document.addEventListener( 'MozMousePixelScroll', onDocumentMouseWheel, false);
window.addEventListener( 'resize', onWindowResize, false );
update();
animate();
}
function animate() {
requestAnimationFrame( animate );
p5Animation()
}
function update() {
console.log("he");
lat = Math.max( - 85, Math.min( 85, lat ) );
phi = THREE.Math.degToRad( 90 - lat );
theta = THREE.Math.degToRad( lon );
camera.position.x = distance * Math.sin( phi ) * Math.cos( theta );
camera.position.y = distance * Math.cos( phi );
camera.position.z = distance * Math.sin( phi ) * Math.sin( theta );
camera.lookAt( camera.target );
}
function p5Animation(){
// P5 ANIMATION
clear();
background( 'rgba(50, 89, 100, 0)' );
bug1.move();
bug1.display();
bug2.move();
bug2.display();
bug3.move();
bug3.display();
bug4.move();
bug4.display();
p5Texture.needsUpdate = true;
renderer.render( scene, camera );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight);
}
function onDocumentMouseDown( event ) {
//event.preventDefault();
isUserInteracting = true;
onPointerDownPointerX = event.clientX;
onPointerDownPointerY = event.clientY;
onPointerDownLon = lon;
onPointerDownLat = lat;
}
function onDocumentMouseMove( event ) {
if ( isUserInteracting === true ) {
lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
update();
}
}
function onDocumentMouseUp( event ) {
isUserInteracting = false;
}
function onDocumentMouseWheel( event ) {
// WebKit
if ( event.wheelDeltaY ) {
distance -= event.wheelDeltaY * 0.05;
// Opera / Explorer 9
} else if ( event.wheelDelta ) {
distance -= event.wheelDelta * 0.05;
// Firefox
} else if ( event.detail ) {
distance += event.detail * 1.0;
}
}
</script>
</body>
</html>