-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow-magnet-range.js
52 lines (42 loc) · 1.85 KB
/
show-magnet-range.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
AFRAME.registerComponent('magnet-range-debug', {
schema: {
range: {type: 'vec2', default: {x: 0.2, y: 0.1}} // radial (X), vertical (Y)
},
init: function () {
// Create a separate <a-entity> for the debug guide
this.guideEl = document.createElement('a-entity');
this.el.sceneEl.appendChild(this.guideEl);
const rangeX = this.data.range.x; // Radial range
const rangeY = this.data.range.y; // Vertical range
// Use a cylinder wireframe to visualize the range
// radius = rangeX, height = rangeY*2
const geometryStr = `primitive: cylinder; radius: ${rangeX}; height: ${rangeY * 2}; segmentsRadial: 32;`;
const materialStr = `color: #00ff00; wireframe: true; transparent: true; opacity: 0.5;`;
// Set up the guide entity
this.guideEl.setAttribute('geometry', geometryStr);
this.guideEl.setAttribute('material', materialStr);
this.guideEl.setAttribute('rotation', '90 0 0'); // Rotate so cylinder is vertical
this.guideEl.setAttribute('physx-body', 'none'); // Ensure no physics on the guide
this.guideEl.classList.add('magnet-guide');
},
tick: function () {
// Each frame, copy the parent's world transform to the guide
const obj = this.el.object3D;
obj.updateMatrixWorld(true);
// Decompose the parent's matrixWorld
const pos = new THREE.Vector3();
const rot = new THREE.Quaternion();
const scale = new THREE.Vector3();
obj.matrixWorld.decompose(pos, rot, scale);
// Apply those transforms to the guide entity
this.guideEl.object3D.position.copy(pos);
this.guideEl.object3D.quaternion.copy(rot);
this.guideEl.object3D.scale.copy(scale);
},
remove: function () {
// Cleanup: remove the guide entity if this component is removed
if (this.guideEl && this.guideEl.parentNode) {
this.guideEl.parentNode.removeChild(this.guideEl);
}
}
});