@@ -37,33 +37,38 @@ AFRAME.registerComponent('grabbable', inherit(base, {
3737 this . zFactor = ( this . data . invert ) ? - 1 : 1
3838 this . yFactor = ( ( this . data . invert ) ? - 1 : 1 ) * ! this . data . suppressY
3939 } ,
40- tick : function ( ) {
41- var entityPosition
42- if ( this . grabber ) {
43- // reflect on z-axis to point in same direction as the laser
44- this . targetPosition . copy ( this . grabDirection )
45- this . targetPosition
46- . applyQuaternion ( this . grabber . object3D . getWorldQuaternion ( ) )
47- . setLength ( this . grabDistance )
48- . add ( this . grabber . object3D . getWorldPosition ( ) )
49- . add ( this . grabOffset )
50- if ( this . deltaPositionIsValid ) {
51- // relative position changes work better with nested entities
52- this . deltaPosition . sub ( this . targetPosition )
53- entityPosition = this . el . getAttribute ( 'position' )
54- this . destPosition . x =
55- entityPosition . x - this . deltaPosition . x * this . xFactor
56- this . destPosition . y =
57- entityPosition . y - this . deltaPosition . y * this . yFactor
58- this . destPosition . z =
59- entityPosition . z - this . deltaPosition . z * this . zFactor
60- this . el . setAttribute ( 'position' , this . destPosition )
61- } else {
62- this . deltaPositionIsValid = true
40+ tick : ( function ( ) {
41+ var q = new THREE . Quaternion ( )
42+ var v = new THREE . Vector3 ( )
43+
44+ return function ( ) {
45+ var entityPosition
46+ if ( this . grabber ) {
47+ // reflect on z-axis to point in same direction as the laser
48+ this . targetPosition . copy ( this . grabDirection )
49+ this . targetPosition
50+ . applyQuaternion ( this . grabber . object3D . getWorldQuaternion ( q ) )
51+ . setLength ( this . grabDistance )
52+ . add ( this . grabber . object3D . getWorldPosition ( v ) )
53+ . add ( this . grabOffset )
54+ if ( this . deltaPositionIsValid ) {
55+ // relative position changes work better with nested entities
56+ this . deltaPosition . sub ( this . targetPosition )
57+ entityPosition = this . el . getAttribute ( 'position' )
58+ this . destPosition . x =
59+ entityPosition . x - this . deltaPosition . x * this . xFactor
60+ this . destPosition . y =
61+ entityPosition . y - this . deltaPosition . y * this . yFactor
62+ this . destPosition . z =
63+ entityPosition . z - this . deltaPosition . z * this . zFactor
64+ this . el . setAttribute ( 'position' , this . destPosition )
65+ } else {
66+ this . deltaPositionIsValid = true
67+ }
68+ this . deltaPosition . copy ( this . targetPosition )
6369 }
64- this . deltaPosition . copy ( this . targetPosition )
6570 }
66- } ,
71+ } ) ( ) ,
6772 remove : function ( ) {
6873 this . el . removeEventListener ( this . GRAB_EVENT , this . start )
6974 this . el . removeEventListener ( this . UNGRAB_EVENT , this . end )
@@ -109,20 +114,23 @@ AFRAME.registerComponent('grabbable', inherit(base, {
109114 if ( evt . preventDefault ) { evt . preventDefault ( ) }
110115 } ,
111116 resetGrabber : function ( ) {
112- let raycaster
113- if ( ! this . grabber ) {
114- return false
115- }
116- raycaster = this . grabber . getAttribute ( 'raycaster' )
117- this . deltaPositionIsValid = false
118- this . grabDistance = this . el . object3D . getWorldPosition ( )
119- . distanceTo ( this . grabber . object3D . getWorldPosition ( ) )
120- if ( raycaster ) {
121- this . grabDirection = raycaster . direction
122- this . grabOffset = raycaster . origin
123- }
124- return true
125- } ,
117+ var objPos = new THREE . Vector3 ( )
118+ var grabPos = new THREE . Vector3 ( )
119+ return function ( ) {
120+ let raycaster
121+ if ( ! this . grabber ) {
122+ return false
123+ }
124+ raycaster = this . grabber . getAttribute ( 'raycaster' )
125+ this . deltaPositionIsValid = false
126+ this . grabDistance = this . el . object3D . getWorldPosition ( objPos ) . distanceTo ( this . grabber . object3D . getWorldPosition ( grabPos ) )
127+ if ( raycaster ) {
128+ this . grabDirection = raycaster . direction
129+ this . grabOffset = raycaster . origin
130+ }
131+ return true
132+ } ;
133+ } ( ) ,
126134 lostGrabber : function ( evt ) {
127135 let i = this . grabbers . indexOf ( evt . relatedTarget )
128136 // if a queued, non-physics grabber leaves the collision zone, forget it
0 commit comments