xrcontroller.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.children.create( "interpolation", interpolation );
  48. this.children.create("xrnode", newNode, function(child){
  49. if(child) {
  50. //find default costume
  51. self.setControllerNode(modelSrc);
  52. }
  53. });
  54. }
  55. this.moveVRController = function(){
  56. let controller = this.xrnode.controller;
  57. if(controller){
  58. controller.onMove();
  59. }
  60. }
  61. this.updateVRControl = function(position, rotation){
  62. this.rotation = rotation;
  63. this.position = position;
  64. }
  65. // this.createSimpleController = function(){
  66. // if (this.handVRNode.controller) {
  67. // this.handVRNode.children.delete(this.handVRNode.controller);
  68. // let controllerDef = this.simpleDef;
  69. // this.handVRNode.children.create("controller", controllerDef);
  70. // }
  71. // }
  72. this.createAvatarFromGLTF = function(modelSrc){
  73. if (this.handVRNode.controller) {
  74. this.handVRNode.children.delete(this.handVRNode.controller);
  75. let controllerDef = this.modelDef;
  76. controllerDef.properties.src = modelSrc;
  77. this.handVRNode.children.create("controller", controllerDef);
  78. }
  79. }
  80. this.initialize = function() {
  81. // this.future(0).update();
  82. }
  83. this.triggerdown = function() {
  84. let controller = this.xrnode.controller;
  85. if(controller){
  86. controller.triggerdown();
  87. }
  88. //this.xrnode.controller.pointer.material.color = 'red'
  89. }
  90. this.triggerup = function() {
  91. let controller = this.xrnode.controller;
  92. if(controller){
  93. controller.triggerup();
  94. }
  95. //this.xrnode.controller.pointer.material.color = 'green'
  96. }
  97. this.saveToScene = function(){
  98. let scene = this.getScene();
  99. let controller = this.xrnode.controller;
  100. if(controller){
  101. let defNode = this.checkDefaultXRCostume();
  102. if(defNode){
  103. scene.children.delete(defNode);
  104. }
  105. let node = controller.nodeDef();
  106. node.properties.visible = false;
  107. node.properties.displayName = 'defaultXRCostume';
  108. scene.children.create('defaultXRCostume', node, function( child ) {
  109. console.log('Save default controller costume to scene: ', child);
  110. });
  111. }
  112. }
  113. this.checkDefaultXRCostume = function(){
  114. let scene = this.getScene();
  115. let defaultNode = scene.getChildByName('defaultXRCostume');
  116. return defaultNode ? defaultNode : undefined
  117. }
  118. this.setControllerNode = function(modelSrc){
  119. let scene = this.getScene();
  120. let defaultNode = this.checkDefaultXRCostume();
  121. let oldCostume = this.xrnode.controller;
  122. if(oldCostume){
  123. this.xrnode.children.delete(oldCostume);
  124. }
  125. if(defaultNode) {
  126. let def = _app.helpers.getNodeDef(defaultNode.id);
  127. def.properties.position = '0 0 0';
  128. def.properties.rotation = '0 0 0';
  129. def.properties.visible = true;
  130. def.properties.displayName = 'controller';
  131. this.xrnode.children.create('controller', def, function( child ) {
  132. console.log('Restore controller costume: ', child);
  133. if(child.cursorVisual){
  134. child.cursorVisual.createVisual();
  135. }
  136. });
  137. } else {
  138. let controllerDef = scene.getDefaultXRCostume();
  139. controllerDef.properties.displayName = 'controller';
  140. if (modelSrc) {
  141. let controllerDef = this.modelDef;
  142. controllerDef.properties.src = modelSrc;
  143. }
  144. this.xrnode.children.create('controller', controllerDef, function( child ) {
  145. console.log('New controller costume: ', child);
  146. child.cursorVisual.createVisual();
  147. });
  148. }
  149. }
  150. this.showHandSelection = function (point) {
  151. let end = this.xrnode.controller.cursorVisual.worldToLocal(point);
  152. //this.xrnode.controller.line.end = end;
  153. this.xrnode.controller.cursorVisual.end = end;
  154. }
  155. this.resetHandSelection = function () {
  156. //this.xrnode.controller.line.end = "0 0 -3";
  157. this.xrnode.controller.cursorVisual.end = "0 0 -0.2";
  158. }