xrcontroller.js 4.8 KB

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