-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
236 lines (197 loc) · 7.73 KB
/
script.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
import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js';
import { TransformControls } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/TransformControls.js';
import { CatmullRomCurve3 } from 'https://cdn.jsdelivr.net/npm/[email protected]/src/extras/curves/CatmullRomCurve3.js';
let currentMesh, transformControl;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const onUpPosition = new THREE.Vector2();
const onDownPosition = new THREE.Vector2();
let instructionsShown = false;
function showInstructions() {
if (!instructionsShown) {
instructionsShown = true;
const instructions = document.getElementById('instructions');
}
}
showInstructions();
//scene
var scene = new THREE.Scene();
//camera
var camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.set(0, 250, 1000);
//renderer
var renderer = new THREE.WebGLRenderer({ canvas: my_canvas, antialiasing: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//controls
var controls = new OrbitControls(camera, renderer.domElement);
//background
scene.background = new THREE.Color(0xf0f0f0);
//light
var light = new THREE.PointLight(0xffffff, 1, 0);
light.position.set(50, 200, 0);
scene.add(light);
//materials
var redMaterial = new THREE.MeshStandardMaterial({ color: 0xFF0000, emissive: 0xFF0000 });
var blueMaterial = new THREE.MeshStandardMaterial({ color: 0x0000FF, emissive: 0x0000FF });
//meshes
var planeGeometry = new THREE.PlaneGeometry(1500, 1500);
var planeMaterial = new THREE.MeshBasicMaterial({ color: 0x808080, side: THREE.DoubleSide });
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -Math.PI / 2;
plane.position.y = 0;
scene.add(plane);
//crate objects[]
var objects = [];
objects.push(plane);
let createobject = null;
var connects = [];
//choose object
$(".object").click(function () {
if (($(this).attr("id")) === "box") {
createobject = "box";
}
if (($(this).attr("id")) === "sphere") {
createobject = "sphere";
}
if (($(this).attr("id")) === "connect") {
connectObjects();
}
});
//click events
transformControl = new TransformControls(camera, renderer.domElement);
transformControl.showY = false
transformControl.addEventListener('dragging-changed', function (event) {
controls.enabled = !event.value;
});
scene.add(transformControl);
document.addEventListener('mousedown', onPointerDown);
document.addEventListener('mouseup', onPointerUp);
document.addEventListener('contextmenu', onRightClick);
document.addEventListener('dblclick', onPointerClick);
window.addEventListener('resize', onWindowResize);
function onPointerDown(event) {
onDownPosition.x = event.clientX;
onDownPosition.y = event.clientY;
}
function onPointerUp(event) {
onUpPosition.x = event.clientX;
onUpPosition.y = event.clientY;
if (onDownPosition.distanceTo(onUpPosition) === 0) {
transformControl.detach();
render();
}
}
let tubeMesh;
function connectObjects() {
if (connects.length >= 2) {
const points = [];
for (let i = 0; i < connects.length; i++) {
const object = connects[i];
points.push(new THREE.Vector3(object.position.x, object.position.y, object.position.z));
}
const curve = new CatmullRomCurve3(points);
if (tubeMesh) {
scene.remove(tubeMesh);
}
const tubeGeometry = new THREE.TubeGeometry(curve, 1000, 1, 10, false); //(path,tubularSegments , radius,radialSegments , closed )
const tubeMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
tubeMesh = new THREE.Mesh(tubeGeometry, tubeMaterial);
scene.add(tubeMesh);
render();
} else {
scene.remove(tubeMesh);
console.log("At least two objects are required to connect.");
}
}
function onPointerClick(event) {
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
let intersects = raycaster.intersectObjects(objects, false);
const intersectionPoint = intersects[0].point;
// console.log('Grid Position:', intersectionPoint);
placeobject(intersects, intersectionPoint);
}
function placeobject(intersects, intersectionPoint) {
if (intersects.length > 1) {
const object = intersects[0].object;
if (transformControl) {
transformControl.attach(object);
// console.log(object);
}
} else {
if (transformControl) {
transformControl.detach();
if (createobject === "sphere") {
var sphereGeometry = new THREE.SphereGeometry(10);
currentMesh = new THREE.Mesh(sphereGeometry, redMaterial);
currentMesh.position.set(intersectionPoint.x, 10, intersectionPoint.z);
scene.add(currentMesh);
objects.push(currentMesh);
} else if (createobject === "box") {
var boxGeometry = new THREE.BoxGeometry(20, 20, 20);
currentMesh = new THREE.Mesh(boxGeometry, blueMaterial);
currentMesh.position.set(intersectionPoint.x, 10, intersectionPoint.z);
scene.add(currentMesh);
objects.push(currentMesh);
}
render();
}
}
}
function onRightClick(event) {
if (event.altKey) {
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
let intersects = raycaster.intersectObjects(objects, false);
if (intersects.length > 1) {
const object = intersects[0].object;
scene.remove(object);
objects = objects.filter(item => item !== object);
connects = connects.filter(item => item !== object);
connectObjects();
console.log(intersects);
render();
}
}
else {
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
let intersects = raycaster.intersectObjects(objects, false);
// const intersectionPoint = intersects[0].point;
if (intersects.length > 1) {
const object = intersects[0].object;
// console.log(object);
connects.push(object);
// console.log(connects);
}
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
render();
}
//Render
function render() {
requestAnimationFrame(render);
if (tubeMesh && connects.length >= 2) {
const points = [];
for (let i = 0; i < connects.length; i++) {
const object = connects[i];
points.push(new THREE.Vector3(object.position.x, object.position.y, object.position.z));
}
const curve = new CatmullRomCurve3(points);
tubeMesh.geometry.dispose();
tubeMesh.geometry = new THREE.TubeGeometry(curve, 100, 1, 10, false);
} else {
scene.remove(tubeMesh);
}
renderer.render(scene, camera);
}
render();