xrcontroller.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. this.modelDef = {
  2. "extends": "proxy/aframe/agltfmodel.vwf",
  3. "properties": {
  4. "src": "#xrcontroller",
  5. "position": "0 0 0",
  6. "rotation": "0 180 0"
  7. },
  8. "children": {
  9. "animation-mixer": {
  10. "extends": "proxy/aframe/anim-mixer-component.vwf",
  11. "properties": {
  12. "clip": "*",
  13. "duration": 1
  14. }
  15. }
  16. }
  17. }
  18. this.createController = function (pos, modelSrc) {
  19. let self = this;
  20. let position = pos ? pos : [0, 0, 0]
  21. var newNode = {
  22. "extends": "proxy/aframe/aentity.vwf",
  23. "properties": {
  24. "position": position
  25. },
  26. children: {}
  27. }
  28. let interpolation = {
  29. "extends": "proxy/aframe/interpolation-component.vwf",
  30. "type": "component",
  31. "properties": {
  32. "enabled": true
  33. }
  34. }
  35. // let myRayCaster = {
  36. // "extends": "proxy/aframe/raycasterComponent.vwf",
  37. // "type": "component",
  38. // "properties": {
  39. // recursive: false,
  40. // interval: 0,
  41. // far: 10,
  42. // objects: ".intersectable",
  43. // showLine: false
  44. // }
  45. // }
  46. // this.children.create( "raycaster", myRayCaster );
  47. this.createLocalRaycaster();
  48. this.children.create( "interpolation", interpolation );
  49. this.children.create("xrnode", newNode, function(child){
  50. if(child) {
  51. //find default costume
  52. self.setControllerNode(modelSrc);
  53. }
  54. });
  55. }
  56. this.moveVRController = function(idata){
  57. let controller = this.xrnode.controller;
  58. if(controller){
  59. controller.onMove(idata);
  60. }
  61. // let point = this.raycaster.getIntersectionPoint();
  62. // console.log('POINT: ', point);
  63. }
  64. this.updateVRControl = function(position, rotation){
  65. this.rotation = rotation;
  66. this.position = position;
  67. }
  68. // this.createSimpleController = function(){
  69. // if (this.handVRNode.controller) {
  70. // this.handVRNode.children.delete(this.handVRNode.controller);
  71. // let controllerDef = this.simpleDef;
  72. // this.handVRNode.children.create("controller", controllerDef);
  73. // }
  74. // }
  75. this.createAvatarFromGLTF = function(modelSrc){
  76. if (this.handVRNode.controller) {
  77. this.handVRNode.children.delete(this.handVRNode.controller);
  78. let controllerDef = this.modelDef;
  79. controllerDef.properties.src = modelSrc;
  80. this.handVRNode.children.create("controller", controllerDef);
  81. }
  82. }
  83. this.initialize = function() {
  84. // this.future(0).update();
  85. }
  86. this.mousedown = function(point, elID, controllerID) {
  87. let controller = this.xrnode.controller;
  88. if(controller){
  89. this.showHandSelection(point);
  90. controller.mousedownAction(point, elID, controllerID);
  91. }
  92. //this.xrnode.controller.pointer.material.color = 'red'
  93. }
  94. this.mouseup = function(point, elID, controllerID) {
  95. if(this.dragID)
  96. this.dragID = false;
  97. let controller = this.xrnode.controller;
  98. if(controller){
  99. this.resetHandSelection();
  100. controller.mouseupAction(point, elID, controllerID);
  101. }
  102. //this.xrnode.controller.pointer.material.color = 'green'
  103. }
  104. // this.triggerup = function() {
  105. // let controller = this.xrnode.controller;
  106. // if(controller){
  107. // controller.triggerup();
  108. // }
  109. // //this.xrnode.controller.pointer.material.color = 'green'
  110. // }
  111. this.triggerdown = function(point, elID, controllerID) {
  112. let controller = this.xrnode.controller;
  113. if(controller){
  114. this.showHandSelection(point);
  115. controller.triggerdownAction(point, elID, controllerID);
  116. }
  117. //this.xrnode.controller.pointer.material.color = 'red'
  118. }
  119. this.triggerup = function(point, elID, controllerID) {
  120. if(this.dragID)
  121. this.dragID = false;
  122. let controller = this.xrnode.controller;
  123. if(controller){
  124. this.resetHandSelection();
  125. controller.triggerupAction(point, elID, controllerID);
  126. }
  127. //this.xrnode.controller.pointer.material.color = 'green'
  128. }
  129. this.saveToScene = function(){
  130. let scene = this.getScene();
  131. let controller = this.xrnode.controller;
  132. if(controller){
  133. let defNode = this.checkDefaultXRCostume();
  134. if(defNode){
  135. scene.children.delete(defNode);
  136. }
  137. let node = controller.nodeDef();
  138. node.properties.visible = false;
  139. node.properties.displayName = 'defaultXRCostume';
  140. scene.children.create('defaultXRCostume', node, function( child ) {
  141. console.log('Save default controller costume to scene: ', child);
  142. });
  143. }
  144. }
  145. this.checkDefaultXRCostume = function(){
  146. let scene = this.getScene();
  147. let defaultNode = scene.getChildByName('defaultXRCostume');
  148. return defaultNode ? defaultNode : undefined
  149. }
  150. this.setControllerNode = function(modelSrc){
  151. let scene = this.getScene();
  152. let defaultNode = this.checkDefaultXRCostume();
  153. let oldCostume = this.xrnode.controller;
  154. if(oldCostume){
  155. this.xrnode.children.delete(oldCostume);
  156. }
  157. if(defaultNode) {
  158. let def = _app.helpers.getNodeDef(defaultNode.id);
  159. def.properties.position = '0 0 0';
  160. def.properties.rotation = '0 0 0';
  161. def.properties.visible = true;
  162. def.properties.displayName = 'controller';
  163. this.xrnode.children.create('controller', def, function( child ) {
  164. console.log('Restore controller costume: ', child);
  165. if(child.cursorVisual){
  166. child.cursorVisual.createVisual();
  167. }
  168. });
  169. } else {
  170. let controllerDef = scene.getDefaultXRCostume();
  171. controllerDef.properties.displayName = 'controller';
  172. if (modelSrc) {
  173. let controllerDef = this.modelDef;
  174. controllerDef.properties.src = modelSrc;
  175. }
  176. this.xrnode.children.create('controller', controllerDef, function( child ) {
  177. console.log('New controller costume: ', child);
  178. child.cursorVisual.createVisual();
  179. });
  180. }
  181. }
  182. this.showHandSelection = function (point) {
  183. //let data = this.raycaster.getIntersectionPoint();
  184. if(point){
  185. let end = this.xrnode.controller.cursorVisual.worldToLocal(point);
  186. //this.xrnode.controller.line.end = end;
  187. this.xrnode.controller.cursorVisual.end = end;
  188. }
  189. }
  190. this.resetHandSelection = function () {
  191. //this.xrnode.controller.line.end = "0 0 -3";
  192. if(this.xrnode.controller.cursorVisual){
  193. this.xrnode.controller.cursorVisual.end = "0 0 -0.2"
  194. }
  195. }
  196. this.createLocalRaycaster = function () {
  197. //only on view
  198. }