-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathraycasting.js
124 lines (115 loc) · 3.27 KB
/
raycasting.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
function getRaycastIntersection(instancedChunks) {
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
// this could be the mouse pos, but for MC, the ray is always in the middle of the screen
pointer.x = (0.5) * 2 - 1;
pointer.y = -1 *0.5 * 2 + 1;
raycaster.setFromCamera(pointer, camera);
var minIntersect = undefined;
for( var i = 0; i < instancedChunks.length; i++) {
var intersect = raycaster.intersectObject(instancedChunks[i]);
if( (minIntersect === undefined)
|| (intersect[0] !== undefined && minIntersect.distance > intersect[0].distance) ) {
minIntersect = intersect[0];
}
}
return minIntersect;
}
function getRaycastBlockInc(instancedChunks, inc) {
var intersection = getRaycastIntersection(instancedChunks);
if( intersection === undefined || intersection.distance >= 40)
return undefined;
plane.visible = true;
var materialIndex = intersection.face.materialIndex;
var position = intersection.point;
var x = Math.round(position.x/blockSize)*blockSize;
var y = Math.round(position.y/blockSize)*blockSize;
var z = Math.round(position.z/blockSize)*blockSize;
switch(materialIndex) {
case 0: // right
x = position.x + inc;
break;
case 1: // left
x = position.x - inc;
break;
case 2: // top
y = position.y + inc;
break;
case 3: // bottom
y = position.y - inc;
break;
case 4: // front
z = position.z + inc;
break;
case 5: // back
z = position.z - inc;
break;
}
x = x | 0;
y = y | 0;
z = z | 0;
return {x:x, y:y, z:z}
}
function getRaycastBlockAdjacent(instancedChunks) {
return getRaycastBlockInc(instancedChunks, -blockSize/2);
}
function getRaycastBlock(instancedChunks) {
return getRaycastBlockInc(instancedChunks, +blockSize/2);
}
var plane;
function raycasting(instancedChunks) {
var intersection = getRaycastIntersection(instancedChunks);
if( intersection != undefined && intersection.distance < 40) {
//console.log(intersection[0]);
if( !scene.children.includes(plane) )
{
var planeG = new THREE.PlaneGeometry(5, 5);
var planeM = new THREE.MeshBasicMaterial({color : 0xffffff, side: THREE.DoubleSide});
planeM.transparent = true;
planeM.opacity = 0.5;
plane = new THREE.Mesh(planeG, planeM);
scene.add(plane);
}
else {
plane.visible = true;
var materialIndex = intersection.face.materialIndex;
var position = intersection.point;
const inc = 0.1;
plane.rotation.x = 0;
plane.rotation.y = 0;
plane.rotation.z = 0;
plane.position.x = Math.round(position.x/5)*5;
plane.position.y = Math.round(position.y/5)*5;
plane.position.z = Math.round(position.z/5)*5;
switch(materialIndex) {
case 0: // right
plane.rotation.y = Math.PI/2;
plane.position.x = position.x + inc;
break;
case 1: // left
plane.rotation.y = Math.PI/2;
plane.position.x = position.x - inc;
break;
case 2: // top
plane.rotation.x = Math.PI/2;
plane.position.y = position.y + inc;
break;
case 3: // bottom
plane.rotation.x = Math.PI/2;
plane.position.y = position.y - inc;
break;
case 4: // front
plane.position.z = position.z + inc;
break;
case 5: // back
plane.position.z = position.z - inc;
break;
}
}
}
else {
if(plane) {
plane.visible = false;
}
}
}