Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've wanted to implement something like this and I choose a very different method - maybe much worse, I don't really know - but much easier to do IMO, so I put it here in case someone wants it.</p> <p>Basically, I used only 2 raycasts to know the first and last points of the selection rectangle, projected on my ground plane, and iterate over my objects to know which ones are in.</p> <p>Some very basic code:</p> <pre><code> function onDocumentMouseDown(event) { // usual Raycaster stuff ... // get the ground intersection var intersects = raycaster.intersectObject(ground); GlobalGroundSelection = { screen: { x: event.clientX, y: event.clientY }, ground: intersects[0].point }; } function onDocumentMouseUp(event) { // ends a ground selection if (GlobalGroundSelection) { // usual Raycaster stuff ... // get the ground intersection var intersects = raycaster.intersectObjects(ground); var selection = { begins: GlobalGroundSelection.ground, ends: intersects[0].point }; GlobalGroundSelection = null; selectCharactersInZone(selection.begins, selection.ends); } } function onDocumentMouseMove(event) { if (GlobalGroundSelection) { // in a selection, draw a rectangle var p1 = GlobalGroundSelection.screen, p2 = { x: event.clientX, y: event.clientY }; /* with these coordinates left: p1.x &gt; p2.x ? p2.x : p1.x, top: p1.y &gt; p2.y ? p2.y : p1.y, width: Math.abs(p1.x - p2.x), height: Math.abs(p1.y - p2.y) */ } } </code></pre> <p>Here is my select function:</p> <pre><code>function selectCharactersInZone (start, end) { var selected = _.filter( SELECTABLE_OBJECTS , function(object) { // warning: this ignore the Y elevation value var itsin = object.position.x &gt; start.x &amp;&amp; object.position.z &gt; start.z &amp;&amp; object.position.x &lt; end.x &amp;&amp; object.position.z &lt; end.z; return itsin; }); return selected; } </code></pre> <p>Some warnings: as far as I know, this technique is only usable when you don't care about Y positions AND your selection is a basic rectangle.</p> <p>My 2c</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload