xrcontroller.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 (modelSrc) {
  19. let self = this;
  20. var newNode = {
  21. "extends": "proxy/aframe/aentity.vwf",
  22. "properties": {
  23. "position": [0, 0, -0.1]
  24. },
  25. children: {}
  26. }
  27. let interpolation = {
  28. "extends": "proxy/aframe/interpolation-component.vwf",
  29. "type": "component",
  30. "properties": {
  31. "enabled": true
  32. }
  33. }
  34. this.children.create( "interpolation", interpolation );
  35. this.children.create("xrnode", newNode, function(child){
  36. if(child) {
  37. //find default costume
  38. self.setControllerNode(modelSrc);
  39. }
  40. });
  41. }
  42. this.moveVRController = function(){
  43. let controller = this.xrnode.controller;
  44. if(controller){
  45. controller.onMove();
  46. }
  47. }
  48. this.updateVRControl = function(position, rotation){
  49. this.rotation = rotation;
  50. this.position = position;
  51. }
  52. // this.createSimpleController = function(){
  53. // if (this.handVRNode.controller) {
  54. // this.handVRNode.children.delete(this.handVRNode.controller);
  55. // let controllerDef = this.simpleDef;
  56. // this.handVRNode.children.create("controller", controllerDef);
  57. // }
  58. // }
  59. this.createAvatarFromGLTF = function(modelSrc){
  60. if (this.handVRNode.controller) {
  61. this.handVRNode.children.delete(this.handVRNode.controller);
  62. let controllerDef = this.modelDef;
  63. controllerDef.properties.src = modelSrc;
  64. this.handVRNode.children.create("controller", controllerDef);
  65. }
  66. }
  67. this.initialize = function() {
  68. // this.future(0).update();
  69. }
  70. this.triggerdown = function() {
  71. let controller = this.xrnode.controller;
  72. if(controller){
  73. controller.triggerdown();
  74. }
  75. //this.xrnode.controller.pointer.material.color = 'red'
  76. }
  77. this.triggerup = function() {
  78. let controller = this.xrnode.controller;
  79. if(controller){
  80. controller.triggerup();
  81. }
  82. //this.xrnode.controller.pointer.material.color = 'green'
  83. }
  84. this.saveToScene = function(){
  85. let scene = this.getScene();
  86. let controller = this.xrnode.controller;
  87. if(controller){
  88. let defNode = this.checkDefaultXRCostume();
  89. if(defNode){
  90. scene.children.delete(defNode);
  91. }
  92. let node = controller.nodeDef();
  93. node.properties.visible = false;
  94. node.properties.displayName = 'defaultXRCostume';
  95. scene.children.create('defaultXRCostume', node, function( child ) {
  96. console.log('Save default controller costume to scene: ', child);
  97. });
  98. }
  99. }
  100. this.checkDefaultXRCostume = function(){
  101. let scene = this.getScene();
  102. let defaultNode = scene.getChildByName('defaultXRCostume');
  103. return defaultNode ? defaultNode : undefined
  104. }
  105. this.setControllerNode = function(modelSrc){
  106. let scene = this.getScene();
  107. let defaultNode = this.checkDefaultXRCostume();
  108. let oldCostume = this.xrnode.controller;
  109. if(oldCostume){
  110. this.xrnode.children.delete(oldCostume);
  111. }
  112. if(defaultNode) {
  113. let def = _app.helpers.getNodeDef(defaultNode.id);
  114. def.properties.position = '0 0 0';
  115. def.properties.rotation = '0 0 0';
  116. def.properties.visible = true;
  117. def.properties.displayName = 'controller';
  118. this.xrnode.children.create('controller', def, function( child ) {
  119. console.log('Restore controller costume: ', child)
  120. });
  121. } else {
  122. let controllerDef = scene.getDefaultXRCostume();
  123. controllerDef.properties.displayName = 'controller';
  124. if (modelSrc) {
  125. let controllerDef = this.modelDef;
  126. controllerDef.properties.src = modelSrc;
  127. }
  128. this.xrnode.children.create('controller', controllerDef, function( child ) {
  129. console.log('New controller costume: ', child)
  130. });
  131. }
  132. }