-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
162 lines (132 loc) · 4.44 KB
/
script.js
File metadata and controls
162 lines (132 loc) · 4.44 KB
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
let dragControls;
let draggableObjects = []; // Array to store draggable objects
// Add these variables at the top
let isDragging = false;
let currentDragObject = null;
let mousePosition = new THREE.Vector2();
let raycaster = new THREE.Raycaster();
// Add this near the top with other variables
let plane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
let planeIntersectPoint = new THREE.Vector3();
function init() {
// ... existing init code ...
// Create OrbitControls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.minDistance = 1;
controls.maxDistance = 50;
// Create DragControls
dragControls = new THREE.DragControls(
draggableObjects,
camera,
renderer.domElement
);
// Add drag event listeners
dragControls.addEventListener("dragstart", function (event) {
controls.enabled = false; // Disable orbit controls while dragging
event.object.material.opacity = 0.7; // Optional: show feedback during drag
});
dragControls.addEventListener("dragend", function (event) {
controls.enabled = true; // Re-enable orbit controls
event.object.material.opacity = 1; // Reset opacity
});
dragControls.addEventListener("drag", function (event) {
// Optional: Constrain movement to a plane or add snap-to-grid
event.object.position.y = 0; // Keep objects on the ground
});
// Create a ground plane for raycasting
const groundGeometry = new THREE.PlaneGeometry(100, 100);
const groundMaterial = new THREE.MeshBasicMaterial({
visible: false,
});
floor = new THREE.Mesh(groundGeometry, groundMaterial);
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
// Update gallery items event listeners
const galleryItems = document.querySelectorAll(".furniture-item");
galleryItems.forEach((item) => {
item.addEventListener("mousedown", function (event) {
event.preventDefault(); // Prevent default drag behavior
const modelPath = this.dataset.model;
if (modelPath) {
startDrag(modelPath, event);
}
});
});
document.addEventListener("mousemove", onMouseMove);
document.addEventListener("mouseup", endDrag);
}
function startDrag(modelPath, event) {
isDragging = true;
// Get mouse position for initial placement
mousePosition.x = (event.clientX / window.innerWidth) * 2 - 1;
mousePosition.y = -(event.clientY / window.innerHeight) * 2 + 1;
// Load the 3D model
const loader = new THREE.GLTFLoader();
loader.load(modelPath, function (gltf) {
currentDragObject = gltf.scene;
// Get intersection point with ground
const intersectionPoint = getMouseIntersection();
if (intersectionPoint) {
currentDragObject.position.copy(intersectionPoint);
}
scene.add(currentDragObject);
});
}
function getMouseIntersection() {
raycaster.setFromCamera(mousePosition, camera);
const intersects = raycaster.intersectObject(floor);
if (intersects.length > 0) {
return intersects[0].point;
}
return null;
}
function onMouseMove(event) {
if (!isDragging || !currentDragObject) return;
// Update mouse position
mousePosition.x = (event.clientX / window.innerWidth) * 2 - 1;
mousePosition.y = -(event.clientY / window.innerHeight) * 2 + 1;
// Update object position
const intersectionPoint = getMouseIntersection();
if (intersectionPoint) {
currentDragObject.position.copy(intersectionPoint);
}
}
function endDrag(event) {
if (isDragging && currentDragObject) {
// Get final position
const intersectionPoint = getMouseIntersection();
if (intersectionPoint) {
currentDragObject.position.copy(intersectionPoint);
draggableObjects.push(currentDragObject);
}
}
isDragging = false;
currentDragObject = null;
}
// Modify your addFurniture function to make objects draggable
function addFurniture(modelPath, position) {
const loader = new THREE.GLTFLoader();
loader.load(modelPath, function (gltf) {
const model = gltf.scene;
model.position.copy(position);
// Make the model draggable
draggableObjects.push(model);
scene.add(model);
});
}
// Optional: Add this function to remove furniture
function removeFurniture(object) {
const index = draggableObjects.indexOf(object);
if (index > -1) {
draggableObjects.splice(index, 1);
scene.remove(object);
}
}
// Make sure to call this in your animation loop
function animate() {
requestAnimationFrame(animate);
// Update controls
controls.update();
// Render scene
renderer.render(scene, camera);
}