@@ -34,7 +34,7 @@ import {
3434 LinearSRGBColorSpace
3535} from "../libs/three/three.module.min.js" ;
3636
37- import { ANTI_ALIASING , VERBOSE_LEVEL } from "./core/Enums.js" ;
37+ import { ANTI_ALIASING , VERBOSE_LEVEL , COLLIDER_TYPE } from "./core/Enums.js" ;
3838import { pxlOptions } from "./core/Options.js" ;
3939
4040import { EffectComposer } from '../libs/three/EffectComposer.js' ;
@@ -108,6 +108,7 @@ export class Environment{
108108 this . pxlUtils = null ;
109109 this . pxlTimer = null ;
110110 this . pxlAnim = null ;
111+ this . pxlColliders = null ;
111112 this . pxlAutoCam = null ;
112113 this . pxlAudio = null ;
113114 this . pxlFile = null ;
@@ -271,6 +272,7 @@ export class Environment{
271272 this . pxlUtils = pxlNav . pxlUtils ;
272273 this . pxlTimer = pxlNav . pxlTimer ;
273274 this . pxlAnim = pxlNav . pxlAnim ;
275+ this . pxlColliders = pxlNav . pxlColliders ;
274276 this . pxlAutoCam = pxlNav . pxlAutoCam ;
275277 this . pxlAudio = pxlNav . pxlAudio ;
276278 this . pxlFile = pxlNav . pxlFile ;
@@ -725,54 +727,39 @@ export class Environment{
725727 // In-Scene clickables
726728 clickUserDetect ( ) {
727729
728- if ( this . roomSceneList [ this . currentRoom ] . castRay ) {
729- this . roomSceneList [ this . currentRoom ] . castRay ( true , this . pxlDevice . touchMouseData . button )
730- }
731-
732- if ( this . pxlDevice . mobile ) {
733- return ;
734- }
735-
736- let objHit = null ;
737- let mouseScreenSpace = new Vector2 ( this . pxlDevice . mouseX / this . pxlDevice . sW * 2 - 1 , - this . pxlDevice . mouseY / this . pxlDevice . sH * 2 + 1 ) ;
738- this . pxlCamera . objRaycast . setFromCamera ( mouseScreenSpace , this . pxlCamera . camera ) ;
739- var rayHits = [ ] ;
740- //if(this.camScreenData.screenClickable.length>0) rayHits=this.pxlCamera.objRaycast.intersectObjects(this.camScreenData.screenClickable);//this.scene.children);
741- if ( this . objectClickable . length > 0 ) rayHits = this . pxlCamera . objRaycast . intersectObjects ( this . objectClickable ) ; //this.scene.children);
742- if ( rayHits . length > 0 ) {
743- let closestHit = 99999 ;
744- for ( var x = 0 ; x < rayHits . length ; ++ x ) {
745- var obj = rayHits [ x ] ; //.object;
746- let curDist = obj . distance ;
747- if ( curDist < closestHit ) {
748- objHit = obj . object ;
749- closestHit = Math . min ( closestHit , curDist ) ;
750- }
751- }
730+ // Current Room Obj
731+ let curRoomObj = this . roomSceneList [ this . currentRoom ] ;
732+
733+ // Cast mouse or touch position to NDC
734+ let mouseScreenSpace = this . pxlUtils . screenToNDC ( this . pxlDevice . mouseX , this . pxlDevice . mouseY , this . pxlDevice . sW , this . pxlDevice . sH ) ;
735+
736+ // Get clickable objects under mouse
737+ let rayHits = { } ;
738+ if ( curRoomObj . hasColliderType ( COLLIDER_TYPE . CLICKABLE ) ) {
739+ let curObjList = curRoomObj . getColliders ( COLLIDER_TYPE . CLICKABLE ) ;
740+ rayHits = this . pxlColliders . castInteractRay ( this . currentRoom , curObjList , this . pxlCamera . camera , mouseScreenSpace ) ;
752741 }
753- if ( objHit ) {
742+
743+ // Get nearest object hit,
744+ // rayHits.order is an array of objects hit, in order of distance
745+ if ( rayHits . hasOwnProperty ( "order" ) && rayHits . order . length > 0 ) {
746+ let objHit = rayHits . order [ 0 ] ;
754747 this . clickableActions ( objHit . name ) ;
748+ return ;
755749 }
756-
757-
758- let promoHit = null ;
759- if ( this . promoClickable . length > 0 ) rayHits = this . pxlCamera . objRaycast . intersectObjects ( this . promoClickable ) ; //this.scene.children);
760- if ( rayHits . length > 0 ) {
761- let closestHit = 99999 ;
762- for ( var x = 0 ; x < rayHits . length ; ++ x ) {
763- var obj = rayHits [ x ] ; //.object;
764- let curDist = obj . distance ;
765- if ( curDist < closestHit ) {
766- promoHit = obj . object ;
767- closestHit = Math . min ( closestHit , curDist ) ;
768- }
769- }
770- }
771- if ( promoHit ) {
772- this . promoActions ( promoHit ) ;
750+
751+ // -- -- --
752+
753+ // If no clickable object hit, check for promo clickables
754+ rayHits = { } ;
755+ rayHits = this . pxlColliders . castInteractRay ( this . currentRoom , this . promoClickable , this . pxlCamera . camera , mouseScreenSpace ) ;
756+
757+ if ( rayHits . hasOwnProperty ( "order" ) && rayHits . order . length > 0 ) {
758+ let promoHit = rayHits . order [ 0 ] ;
759+ this . promoActions ( promoHit ) ;
773760 }
774-
775761 }
762+
776763 clickableActions ( action = null ) {
777764 if ( action == "CallToAction" && this . clickablePrevActiveObject ) {
778765 this . pxlGuiDraws . ctaBuildPopup ( ) ;
@@ -794,28 +781,24 @@ export class Environment{
794781 // Hover over clickable
795782 hoverUserDetect ( ) {
796783
797- if ( this . roomSceneList [ this . currentRoom ] . castRay ) {
798- this . roomSceneList [ this . currentRoom ] . castRay ( false , this . pxlDevice . touchMouseData . button )
799- }
800-
801- let objHit = null ;
802- let mouseScreenSpace = new Vector2 ( this . pxlDevice . mouseX / this . pxlDevice . sW * 2 - 1 , - this . pxlDevice . mouseY / this . pxlDevice . sH * 2 + 1 ) ;
803- this . pxlCamera . objRaycast . setFromCamera ( mouseScreenSpace , this . pxlCamera . camera ) ;
804- var rayHits = [ ] ;
805- //if(this.camScreenData.screenClickable.length>0) rayHits=this.pxlCamera.objRaycast.intersectObjects(this.camScreenData.screenClickable);//this.scene.children);
806- if ( this . objectClickable . length > 0 ) rayHits = this . pxlCamera . objRaycast . intersectObjects ( this . objectClickable ) ;
807- if ( rayHits . length > 0 ) {
808- let closestHit = 99999 ;
809- for ( var x = 0 ; x < rayHits . length ; ++ x ) {
810- var obj = rayHits [ x ] ; //.object;
811- let curDist = obj . distance ;
812- if ( curDist < closestHit ) {
813- objHit = obj . object ;
814- closestHit = Math . min ( closestHit , curDist ) ;
815- }
816- }
784+ // Current Room Obj
785+ let curRoomObj = this . roomSceneList [ this . currentRoom ] ;
786+
787+ // Cast mouse or touch position to NDC
788+ let mouseScreenSpace = this . pxlUtils . screenToNDC ( this . pxlDevice . mouseX , this . pxlDevice . mouseY , this . pxlDevice . sW , this . pxlDevice . sH ) ;
789+
790+ // Get hoverable objects under mouse
791+ let rayHits = { } ;
792+ if ( curRoomObj . hasColliderType ( COLLIDER_TYPE . CLICKABLE ) || curRoomObj . hasColliderType ( COLLIDER_TYPE . HOVERABLE ) ) {
793+ // Combine objectClickable with objectHoverable This may change
794+ let curObjList = [ ...curRoomObj . getColliders ( COLLIDER_TYPE . CLICKABLE ) , ...curRoomObj . getColliders ( COLLIDER_TYPE . HOVERABLE ) ] ;
795+ rayHits = this . pxlColliders . castInteractRay ( this . currentRoom , curObjList , this . pxlCamera . camera , mouseScreenSpace ) ;
817796 }
818- if ( objHit ) {
797+
798+ // Get nearest object hit,
799+ // rayHits.order is an array of objects hit, in order of distance
800+ if ( rayHits . hasOwnProperty ( "order" ) && rayHits . order . length > 0 ) {
801+ let objHit = rayHits . order [ 0 ] ;
819802 this . pxlDevice . setCursor ( "help" ) ;
820803 if ( this . objectClickableObjectList [ objHit . name ] ) {
821804 if ( this . clickablePrevActiveObject == null ) {
@@ -824,7 +807,7 @@ export class Environment{
824807 this . objectClickableObjectList [ objHit . name ] [ 'Inactive' ] . visible = false ;
825808 this . objectClickableObjectList [ objHit . name ] [ 'Hover' ] . visible = true ;
826809 }
827- return ;
810+ return ;
828811 } else {
829812 if ( this . clickablePrevActiveObject ) {
830813 this . objectClickableObjectList [ this . clickablePrevActiveObject ] [ 'Inactive' ] . visible = true ;
@@ -833,22 +816,15 @@ export class Environment{
833816 }
834817 this . pxlDevice . setCursor ( "grab" ) ;
835818 }
836-
837-
838- let promoHit = null ;
839- if ( this . promoClickable . length > 0 ) rayHits = this . pxlCamera . objRaycast . intersectObjects ( this . promoClickable ) ;
840- if ( rayHits . length > 0 ) {
841- let closestHit = 99999 ;
842- for ( var x = 0 ; x < rayHits . length ; ++ x ) {
843- var obj = rayHits [ x ] ; //.object;
844- let curDist = obj . distance ;
845- if ( curDist < closestHit ) {
846- promoHit = obj . object ;
847- closestHit = Math . min ( closestHit , curDist ) ;
848- }
849- }
850- }
851- if ( promoHit ) {
819+
820+ // -- -- --
821+
822+ // If no clickable object hit, check for promo clickables
823+ rayHits = { } ;
824+ rayHits = this . pxlColliders . castInteractRay ( this . currentRoom , this . promoClickable , this . pxlCamera . camera , mouseScreenSpace ) ;
825+
826+ if ( rayHits . hasOwnProperty ( "order" ) && rayHits . order . length > 0 ) {
827+ let promoHit = rayHits . order [ 0 ] ;
852828 this . pxlDevice . setCursor ( "alias" ) ;
853829 if ( this . promoClickableObjectList [ promoHit . name ] ) {
854830 if ( this . promoPrevActiveObject == null ) {
0 commit comments