-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrendering.js
298 lines (249 loc) · 10.8 KB
/
rendering.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// importing three rendering library
import * as THREE from 'three';
// check if the canvas is loaded
document.addEventListener('DOMContentLoaded', function() {
// load in the things you need at the start
const canvas = document.getElementById('render');
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('render') });
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const canvasBounds = canvas.getBoundingClientRect();
let isMouseDown = false;
let previousMouseX = 0;
let previousMouseY = 0;
let inCanvas = false;
let oldCameraPosition;
// function to check if the canvas was clicked on or not
document.addEventListener('click', function(event){
if(canvasBounds.left <= pointer.x && pointer.x <= canvasBounds.right && canvasBounds.top <= pointer.y && pointer.y <= canvasBounds.bottom){
inCanvas = true;
console.log(inCanvas);
}
else{
inCanvas = false;
console.log(inCanvas);
}
});
renderer.setSize(renderer.domElement.clientWidth, renderer.domElement.clientHeight);
// create the starter cube
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
// set the material
const material = new THREE.MeshPhysicalMaterial( {color: 0xffffff,
transparent: true,
metalness: 0.5,
clearcoat: 1,
clearcoatRoughness: 0.2,
roughness: 0.5
} );
// declare the cube
const cube = new THREE.Mesh( geometry, material );
// this is for the raycaster to sort out selectable objects
var objects = []
// ambient light for some environment lighting
const ambient = new THREE.AmbientLight(0xffffff, 0.2);
scene.add(ambient);
// directional light on top of the cube
const DL = new THREE.DirectionalLight(0xffffff, 1.8, 5);
scene.add(DL);
DL.position.set(0, 2, 0);
// adding the cube to the scene and pushing it to be a selectable object
scene.add( cube );
objects.push(cube);
// selectable variables
var selectedobject;
var haswire = false;
var wire;
// camera position
camera.position.z = 5;
// create a grid for the user
const helpergrid = new THREE.GridHelper(10, 10);
scene.add(helpergrid);
helpergrid.position.y = -1
// create the axes marker
const xyz = new THREE.AxesHelper( 5 );
scene.add( xyz );
xyz.position.y = -1
// movement helpers
let ArrowX, ArrowY, ArrowZ;
// render the scene constantly
function render() {
requestAnimationFrame( render );
renderer.render( scene, camera );
}
render();
var button = document.getElementById("NewCube");
button.addEventListener("click", function(){
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
// set the material
const material = new THREE.MeshPhysicalMaterial( {color: 0xffffff,
transparent: true,
metalness: 0.5,
clearcoat: 1,
clearcoatRoughness: 0.2,
roughness: 0.5
} );
// declare the cube
const newcube = new THREE.Mesh( geometry, material );
scene.add(cube);
objects.push(cube);
cube.name = "NewCube";
});
// function to check if cookie canvas_selected is set to true or not
// function checkSelected(){
// // if (document.cookie === "canvas_selected=true"){
// // console.log("canvas is selected");
// // }else{
// // console.log("canvas is not selected");
// // }
// }
// keyboard controls for the camera
document.addEventListener('keydown', function(event) {
if (document.activeElement !== document.getElementsByTagName('input')[0]) {
if (event.key === 's' || event.key === 'S') {
const movementSpeed = 0.1; // Adjust the movement speed as needed
// Get the camera's direction
const cameraDirection = new THREE.Vector3();
camera.getWorldDirection(cameraDirection);
// Calculate the movement vector
const movementVector = cameraDirection.multiplyScalar(-movementSpeed);
// Move the camera along the movement vector
camera.position.add(movementVector);
}
if (event.key === 'w' || event.key === 'W') {
const movementSpeed = -0.1; // Adjust the movement speed as needed
// Get the camera's direction
const cameraDirection = new THREE.Vector3();
camera.getWorldDirection(cameraDirection);
// Calculate the movement vector
const movementVector = cameraDirection.multiplyScalar(-movementSpeed);
// Move the camera along the movement vector
camera.position.add(movementVector);
}
if (event.key === 'a' || event.key === 'A') {
// Move left
const movementSpeed = 0.1; // Adjust the movement speed as needed
const cameraDirection = new THREE.Vector3();
camera.getWorldDirection(cameraDirection);
const cameraRight = new THREE.Vector3();
cameraRight.crossVectors(cameraDirection, camera.up).normalize();
const movementVector = cameraRight.clone().multiplyScalar(-movementSpeed);
camera.position.add(movementVector);
}
if (event.key === 'd' || event.key === 'D') {
// Move left
const movementSpeed = -0.1; // Adjust the movement speed as needed
const cameraDirection = new THREE.Vector3();
camera.getWorldDirection(cameraDirection);
const cameraRight = new THREE.Vector3();
cameraRight.crossVectors(cameraDirection, camera.up).normalize();
const movementVector = cameraRight.clone().multiplyScalar(-movementSpeed);
camera.position.add(movementVector);
}
if (event.key === 'E' || event.key === 'e') {
camera.position.y += 0.1;
}
if (event.key === 'Q' || event.key === 'q') {
camera.position.y -= 0.1;
}
if(event.key == "Delete"){
if (selectedobject != null) {
scene.remove(selectedobject);
objects.splice(objects.indexOf(selectedobject), 1);
}
}
}
});
// Function to handle mouse down event
function onMouseDown(event) {
if (event.button === 0) { // Left mouse button
isMouseDown = true;
previousMouseX = event.clientX;
previousMouseY = event.clientY;
}
}
// Function to handle mouse up event
function onMouseUp(event) {
if (event.button === 0) { // Left mouse button
isMouseDown = false;
}
}
// Function to handle mouse move event
function onMouseMove(event) {
if (isMouseDown) {
const deltaX = event.clientX - previousMouseX;
const deltaY = event.clientY - previousMouseY;
previousMouseX = event.clientX;
previousMouseY = event.clientY;
// Calculate the rotation angles based on the mouse movement
const rotationSpeed = 0.005; // Adjust the rotation speed as needed
const angleX = -deltaY * rotationSpeed;
const angleY = -deltaX * rotationSpeed;
camera.rotateX(angleX);
camera.rotateY(angleY);
}
}
// Add event listeners to the document
canvas.addEventListener('mousedown', onMouseDown);
canvas.addEventListener('mouseup', onMouseUp);
canvas.addEventListener('mousemove', onMouseMove);
// event listener to check if M was pressed
document.addEventListener('keydown', function(event) {
if (event.key === 'm' || event.key === 'M') {
console.log('M was pressed');
}
})
setInterval(function(){
oldCameraPosition = camera.position
}, 1000)
// handle selecting objects
function onMouseClick(event){
const canvas = document.getElementById('render'); // Replace 'myCanvas' with the ID of your canvas element
// setup mouse coords
var intersect = null;
pointer.x = ((event.clientX - canvasBounds.left) / canvas.clientWidth) * 2 - 1;
pointer.y = -((event.clientY - canvasBounds.top) / canvas.clientHeight) * 2 + 1;
// create a raycaster
raycaster.setFromCamera(pointer, camera);
intersect = raycaster.intersectObjects(objects, false);
// check if the raycaster intersects with an object
if(intersect.length > 0){
console.log(intersect)
// check if the object is already selected
selectedobject = intersect[0].object;
if (haswire == false) {
const geo = new THREE.EdgesGeometry( intersect[0].object.geometry );
const mat = new THREE.LineBasicMaterial( { color: 0x00ffea} );
const wireframe = new THREE.LineSegments( geo, mat );
selectedobject.add(wireframe);
haswire = true;
selectedobject.material.opacity = 0.9
ArrowX = new THREE.ArrowHelper( new THREE.Vector3(1, 0, 0), selectedobject.position, 3, 0xfc0303, 0.7 );
ArrowY = new THREE.ArrowHelper( new THREE.Vector3(0, 1, 0), selectedobject.position, 3, 0xfce803, 0.7 );
ArrowZ = new THREE.ArrowHelper( new THREE.Vector3(0, 0, 1), selectedobject.position, 3, 0x036ffc, 0.7 );
scene.add(ArrowX);
scene.add(ArrowY);
scene.add(ArrowZ);
}
// if the user clicks away from the object then deselect it
}else {
if (selectedobject != null) {
if (oldCameraPosition == camera.position)
haswire = false;
selectedobject.material.opacity = 1
console.log(selectedobject.children)
selectedobject.remove(selectedobject.children[0]);
selectedobject = null;
wire = null
console.log(selectedobject)
console.log(haswire)
scene.remove(ArrowX);
scene.remove(ArrowY);
scene.remove(ArrowZ);
}
}
// Check if canvas is selected
}
canvas.addEventListener('click', onMouseClick);
});