Thanks, Will for helping me out, I have done this part already, and it’s working. But I want like to intersect or maybe collide in runtime once I drag the model . This means if I can drag the model and on the left side it collides with the wall, then it should not overlap means not cross the wall . This part I need this Here is it code
var GizmoManager = pc.createScript('gizmoManager');
// Create a PlayCanvas script
// Attribute to reference the existing camera entity
GizmoManager.attributes.add('cameraEntity', { type: 'entity' });
// Initialize the script
GizmoManager.prototype.initialize = async function() {
GizmoManager.Instance=this;
const app = this.app;
const canvas = app.graphicsDevice.canvas;
const cameraEntity = this.cameraEntity;
// Local state management
const state = {
gizmo: {
type: 'translate',
coordSpace: 'world'
},
camera: {
proj: pc.PROJECTION_PERSPECTIVE,
fov: 45
}
};
// GizmoHandler class
class GizmoHandler {
_type = 'translate';
_gizmos;
_nodes = [];
_ignorePicker = false;
_skipSetFire = false;
constructor(app, camera, layer) {
this._gizmos = {
translate: new pc.TranslateGizmo(app, camera, layer),
rotate: new pc.RotateGizmo(app, camera, layer),
scale: new pc.ScaleGizmo(app, camera, layer)
};
for (const type in this._gizmos) {
const gizmo = this._gizmos[type];
console.log(gizmo.on);
gizmo.on(
'pointer:down',
(x, y, meshInstance) => {
this._ignorePicker = !!meshInstance;
//console.log(this._gizmos);
}
);
gizmo.on('pointer:up', () => {
this._ignorePicker = false;
});
}
}
get gizmo() {
return this._gizmos[this._type];
}
get ignorePicker() {
return this._ignorePicker;
}
get skipSetFire() {
return this._skipSetFire;
}
_updateData(type) {
const gizmo = this.gizmo;
this._skipSetFire = true;
state.gizmo = {
type: type,
size: gizmo.size,
snapIncrement: gizmo.snapIncrement,
xAxisColor: Object.values(gizmo.xAxisColor),
yAxisColor: Object.values(gizmo.yAxisColor),
zAxisColor: Object.values(gizmo.zAxisColor),
colorAlpha: gizmo.colorAlpha,
coordSpace: gizmo.coordSpace,
axisLineTolerance: gizmo.axisLineTolerance,
axisCenterTolerance: gizmo.axisCenterTolerance,
ringTolerance: gizmo.ringTolerance,
axisGap: gizmo.axisGap,
axisLineThickness: gizmo.axisLineThickness,
axisLineLength: gizmo.axisLineLength,
axisArrowThickness: gizmo.axisArrowThickness,
axisArrowLength: gizmo.axisArrowLength,
axisBoxSize: gizmo.axisBoxSize,
axisPlaneSize: gizmo.axisPlaneSize,
axisPlaneGap: gizmo.axisPlaneGap,
axisCenterSize: gizmo.axisCenterSize,
xyzTubeRadius: gizmo.xyzTubeRadius,
xyzRingRadius: gizmo.xyzRingRadius,
faceTubeRadius: gizmo.faceTubeRadius,
faceRingRadius: gizmo.faceRingRadius
};
this._skipSetFire = false;
}
add(node, clear = false) {
if (clear) {
this._nodes.length = 0;
}
if (this._nodes.indexOf(node) === -1) {
this._nodes.push(node);
}
this.gizmo.attach(this._nodes);
}
clear() {
this._nodes.length = 0;
this.gizmo.detach();
}
switch(type) {
this.gizmo.detach();
this._type = type ?? 'translate';
this.gizmo.attach(this._nodes);
this._updateData(type);
}
destroy() {
for (const type in this._gizmos) {
this._gizmos[type].destroy();
}
}
}
const layers = app.scene.layers;
const gizmoLayer = new pc.Layer({
name: 'Gizmo',
clearDepthBuffer: true,
opaqueSortMode: pc.SORTMODE_NONE,
transparentSortMode: pc.SORTMODE_NONE
});
layers.push(gizmoLayer);
cameraEntity.camera.layers = cameraEntity.camera.layers.concat(gizmoLayer.id);
const gizmoHandler = new GizmoHandler(app, cameraEntity.camera, gizmoLayer);
gizmoHandler.switch('translate');
// var hasnainBox = this.app.root.findByName("hasnainBox");
// gizmoHandler.add(hasnainBox);
window.focus();
const setType = (value) => {
state.gizmo.type = value;
gizmoHandler.switch(value);
};
const keydown = (e) => {
gizmoHandler.gizmo.snap = !!e.shiftKey;
gizmoHandler.gizmo.uniform = !e.ctrlKey;
};
const keyup = (e) => {
gizmoHandler.gizmo.snap = !!e.shiftKey;
gizmoHandler.gizmo.uniform = !e.ctrlKey;
};
const keypress = (e) => {
switch (e.key) {
case 'x':
state.gizmo.coordSpace = state.gizmo.coordSpace === 'world' ? 'local' : 'world';
gizmoHandler.gizmo.coordSpace = state.gizmo.coordSpace;
break;
case '1':
setType('translate');
break;
case '2':
setType('rotate');
break;
case '3':
setType('scale');
break;
}
};
window.addEventListener('keydown', keydown);
window.addEventListener('keyup', keyup);
window.addEventListener('keypress', keypress);
const picker = new pc.Picker(app, canvas.clientWidth, canvas.clientHeight);
const worldLayer = layers.getLayerByName('World');
const pickerLayers = [worldLayer];
const onPointerDown = (e) => {
if (gizmoHandler.ignorePicker) {
return;
}
if (picker) {
picker.resize(canvas.clientWidth, canvas.clientHeight);
picker.prepare(cameraEntity.camera, app.scene, pickerLayers);
}
const selection = picker.getSelection(e.clientX - 1, e.clientY - 1, 2, 2);
if (!selection[0]) {
gizmoHandler.clear();
return;
}
// console.log("value of picker is ",selection[0].node);
var nodeName = selection[0].node._parent._parent.name;
var nodeNumber = parseInt(nodeName, 10);
if (nodeNumber >= 1 && nodeNumber <= 18) {
var selected = selection[0].node._parent._parent;
}
else
{
gizmoHandler.clear();
return;
}
gizmoHandler.add(selected, !e.ctrlKey && !e.metaKey);
};
window.addEventListener('pointerdown', onPointerDown);
app.on('destroy', () => {
gizmoHandler.destroy();
window.removeEventListener('keydown', keydown);
window.removeEventListener('keyup', keyup);
window.removeEventListener('keypress', keypress);
window.removeEventListener('pointerdown', onPointerDown);
});
};