xrcontroller.js 4.2 KB

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