-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex23.js
107 lines (98 loc) · 3.25 KB
/
ex23.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
import * as THREE from 'three';
import * as OIMO from 'oimo'
import TWEEN from 'tween';
//init
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000)
const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
//camera
camera.position.set(0,20,50)
camera.lookAt(0,0,0)
//--------------------------------------------------------------------------
//OIMO World Setup
const world = new OIMO.World({
timestep: 1/60,
iterations: 8,
broadphase: 2,
worldscale: 1,
random: true,
info: false,
gravity: [0,-9.8,0]
})
//--------------------------------------------------------------------------
const boxGroup = new THREE.Group()
let mouse = new THREE.Vector2()
let worldMeshs = [],
allowDrag = false,
mouse2 = {x: 0, y: 0, z: 0}
//Spheres
let geometry = new THREE.SphereGeometry( 1, 8, 8 )
let material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } )
for (let i = 0; i < 100; i++) {
let boxes = new THREE.Mesh( geometry, material )
let cx = THREE.Math.randInt( -30, 30 )
let cy = THREE.Math.randInt( 30, 100 )
let cz = THREE.Math.randInt( -30, 30 )
boxes.position.set(cx, cy, cz)
worldMeshs[i] = world.add({type:'sphere', size:[1,1,1], pos:[cx,cy,cz], move:true})
boxGroup.add( boxes )
}
scene.add( boxGroup )
console.log(worldMeshs)
//Ground Plane
let gGeometry = new THREE.BoxGeometry( 65, 1, 65 )
let gMaterial = new THREE.MeshBasicMaterial( { color: 0xffff00 } )
let ground = new THREE.Mesh( gGeometry, gMaterial )
ground.position.set(0, 0, 0)
scene.add(ground)
let boxGround = world.add({
type:'box', size:[65,1,65], pos:[0,0,0], move:false
})
//Draggable Box
let dGeometry = new THREE.BoxGeometry( 2, 6, 60 )
let dMaterial = new THREE.MeshBasicMaterial( { color: 0x0000ff } )
let dragBox = new THREE.Mesh( dGeometry, dMaterial )
dragBox.position.set(0, 6,0)
scene.add(dragBox)
let dragBoxP = world.add({
type:'box', size:[2,6,60], pos:[0,6,0], move:true
})
//Mouse Events
const onDocumentMouseMove = ( event ) => {
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
let vector = new THREE.Vector3(mouse.x, mouse.y, 0.5);
vector.unproject( camera );
let dir = vector.sub( camera.position ).normalize();
let distance = - camera.position.z / dir.z;
let pos = camera.position.clone().add( dir.multiplyScalar( distance ) );
if(allowDrag) {
mouse2.x = pos.x
}
console.log(pos)
}
const onDocumentMouseDown = ( event ) => {
allowDrag = true
}
const onDocumentMouseUp = ( event ) => {
allowDrag = false
}
const animate = () => {
requestAnimationFrame( animate )
render()
}
function render() {
world.step()
boxGroup.children.forEach((mesh, index) => {
mesh.position.copy(worldMeshs[index].getPosition())
})
dragBoxP.setPosition(mouse2)
dragBox.position.copy(dragBoxP.getPosition())
renderer.render( scene, camera )
}
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener('mousedown', onDocumentMouseDown, false);
document.addEventListener('mouseup', onDocumentMouseUp, false);
animate()