From d71c932e5064922f58af8944b3ec0c49642c6bae Mon Sep 17 00:00:00 2001 From: Fabien Benetou Date: Fri, 13 Jan 2023 18:42:28 +0100 Subject: [PATCH] grid to snap as searchable datastructure --- index.html | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index c068127..d0059bc 100644 --- a/index.html +++ b/index.html @@ -750,7 +750,7 @@ function getClosestTargetElements( pos, threshold=0.05 ){ .filter( t => t.dist < threshold ) .sort( (a,b) => a.dist > b.dist) } - + function getClosestTargetElement( pos, threshold=0.05 ){ // 10x lower threshold for flight mode var res = null const matches = getClosestTargetElements( pos, threshold) @@ -1007,6 +1007,7 @@ AFRAME.registerComponent('pinchprimary', { // currently only 1 hand, the right o immersClient.sendChatMessage(content, "public"); } selectedElements.push({element:selectedElement, timestamp:Date.now(), primary:true}) + selectedElement.emit('released') } // unselect current target if any selectedElement = null; @@ -1903,6 +1904,7 @@ function displayAllTiles(){ n++ } ) // could consider a new spawner type so that picking a tile clones it first + // could do same behavior as on release or on picked, namely register listener then act on event } // try generating at scale, e.g 2, a landscape to explore based on type // with scale adjusting as jxr line to be the Wondering pills/drinks/mushroom to change scale @@ -1912,23 +1914,55 @@ function randomTileFull(){ return tiles_full[Math.floor(Math.random()*tiles_full.length)] } -function generateRandomPlace(){ - let scale = 1/10 - // let y = -2 // lifesize, scale 1 - let y = 1.4 // dollhouse, scale 1/10 - for (let i=0;i<10;i++){ - for (let j=0;j<10;j++){ +var tiles_snapping_grid = [] + +function getClosestTilesSnappingPosition( t, threshold=0.05 ){ + let point = null + let found = tiles_snapping_grid.map( i => { return { pos:i, dist: i.distanceTo(t) } } ) + .filter( t => t.dist < threshold ) + .sort( (a,b) => a.dist > b.dist) + if (found && found[0]) point = found[0].pos + return point +} + +AFRAME.registerComponent('snap-on-pinchended', { + init: function(){ + this.el.addEventListener('released', function (event) { + setFeedbackHUD("should snap to iso grid"); // TODO test + // if works, generalize and add to https://git.benetou.fr/utopiah/text-code-xr-engine/issues/66 + // should come back from emit('released') + // could rely on getClosestTilesSnappingPosition() + // if it works, might check if position is not already used by a tile + }) + } +}) + +function generateRandomPlace(max_i=10, max_j=10, scale=1/10, y=1.4){ + // lifesize, y : -2, scale 1 + // dollhouse, y : 1.4, scale 1/10 + for (let i=0;i { scl = e.getAttribute("scale"); e.setAttribute("scale", scl.x*scale+ " " + scl.y*scale + " " + scl.z*scale) } ) - places.map( e => { pos = e.getAttribute("position"); e.setAttribute("position", pos.x*scale+ " " + (pos.y+yoffset) + " " + pos.z*scale) } ) + tiles_snapping_grid = [] + places.map( e => { + scl = e.getAttribute("scale"); e.setAttribute("scale", scl.x*scale+ " " + scl.y*scale + " " + scl.z*scale) + pos = e.getAttribute("position"); e.setAttribute("position", pos.x*scale+ " " + (pos.y+yoffset) + " " + pos.z*scale) + let pos3 = new THREE.Vector3( pos.x*scale, pos.y+yoffset, pos.z*scale ) + tiles_snapping_grid.push( pos3 ) + } ) } // could change model opacity based on hand position, fading out when within a (very small here) safe space