xrcontroller.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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) {
  87. let controller = this.xrnode.controller;
  88. if(controller){
  89. this.showHandSelection(point);
  90. controller.mousedownAction(point, elID);
  91. }
  92. //this.xrnode.controller.pointer.material.color = 'red'
  93. }
  94. this.mouseup = function(point, elID) {
  95. let controller = this.xrnode.controller;
  96. if(controller){
  97. this.resetHandSelection();
  98. controller.mouseupAction(point, elID);
  99. }
  100. //this.xrnode.controller.pointer.material.color = 'green'
  101. }
  102. // this.triggerup = function() {
  103. // let controller = this.xrnode.controller;
  104. // if(controller){
  105. // controller.triggerup();
  106. // }
  107. // //this.xrnode.controller.pointer.material.color = 'green'
  108. // }
  109. this.triggerdown = function(point, elID) {
  110. let controller = this.xrnode.controller;
  111. if(controller){
  112. this.showHandSelection(point);
  113. controller.triggerdownAction(point, elID);
  114. }
  115. //this.xrnode.controller.pointer.material.color = 'red'
  116. }
  117. this.triggerup = function(point, elID) {
  118. let controller = this.xrnode.controller;
  119. if(controller){
  120. this.resetHandSelection();
  121. controller.triggerupAction(point, elID);
  122. }
  123. //this.xrnode.controller.pointer.material.color = 'green'
  124. }
  125. this.saveToScene = function(){
  126. let scene = this.getScene();
  127. let controller = this.xrnode.controller;
  128. if(controller){
  129. let defNode = this.checkDefaultXRCostume();
  130. if(defNode){
  131. scene.children.delete(defNode);
  132. }
  133. let node = controller.nodeDef();
  134. node.properties.visible = false;
  135. node.properties.displayName = 'defaultXRCostume';
  136. scene.children.create('defaultXRCostume', node, function( child ) {
  137. console.log('Save default controller costume to scene: ', child);
  138. });
  139. }
  140. }
  141. this.checkDefaultXRCostume = function(){
  142. let scene = this.getScene();
  143. let defaultNode = scene.getChildByName('defaultXRCostume');
  144. return defaultNode ? defaultNode : undefined
  145. }
  146. this.setControllerNode = function(modelSrc){
  147. let scene = this.getScene();
  148. let defaultNode = this.checkDefaultXRCostume();
  149. let oldCostume = this.xrnode.controller;
  150. if(oldCostume){
  151. this.xrnode.children.delete(oldCostume);
  152. }
  153. if(defaultNode) {
  154. let def = _app.helpers.getNodeDef(defaultNode.id);
  155. def.properties.position = '0 0 0';
  156. def.properties.rotation = '0 0 0';
  157. def.properties.visible = true;
  158. def.properties.displayName = 'controller';
  159. this.xrnode.children.create('controller', def, function( child ) {
  160. console.log('Restore controller costume: ', child);
  161. if(child.cursorVisual){
  162. child.cursorVisual.createVisual();
  163. }
  164. });
  165. } else {
  166. let controllerDef = scene.getDefaultXRCostume();
  167. controllerDef.properties.displayName = 'controller';
  168. if (modelSrc) {
  169. let controllerDef = this.modelDef;
  170. controllerDef.properties.src = modelSrc;
  171. }
  172. this.xrnode.children.create('controller', controllerDef, function( child ) {
  173. console.log('New controller costume: ', child);
  174. child.cursorVisual.createVisual();
  175. });
  176. }
  177. }
  178. this.showHandSelection = function (point) {
  179. //let data = this.raycaster.getIntersectionPoint();
  180. if(point){
  181. let end = this.xrnode.controller.cursorVisual.worldToLocal(point);
  182. //this.xrnode.controller.line.end = end;
  183. this.xrnode.controller.cursorVisual.end = end;
  184. }
  185. }
  186. this.resetHandSelection = function () {
  187. //this.xrnode.controller.line.end = "0 0 -3";
  188. if(this.xrnode.controller.cursorVisual){
  189. this.xrnode.controller.cursorVisual.end = "0 0 -0.2"
  190. }
  191. }
  192. this.createLocalRaycaster = function () {
  193. //only on view
  194. }