aframe.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. "use strict";
  2. // VWF & A-Frame model driver
  3. // Copyright 2017 Krestianstvo.org project
  4. //
  5. // Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  6. // Secretary of Defense (Personnel & Readiness).
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  9. // in compliance with the License. You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software distributed under the License
  14. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  15. // or implied. See the License for the specific language governing permissions and limitations under
  16. // the License.
  17. /// vwf/model/scenejs.js is a placeholder for a 3-D scene manager.
  18. ///
  19. /// @module vwf/model/aframe
  20. /// @requires vwf/model
  21. define(["module", "vwf/model", "vwf/utility"], function (module, model, utility) {
  22. return model.load(module, {
  23. // == Module Definition ====================================================================
  24. // -- initialize ---------------------------------------------------------------------------
  25. initialize: function () {
  26. self = this;
  27. this.state = {
  28. nodes: {},
  29. scenes: {},
  30. prototypes: {},
  31. createLocalNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  32. childSource, childType, childIndex, childName, callback) {
  33. return {
  34. "parentID": nodeID,
  35. "ID": childID,
  36. "extendsID": childExtendsID,
  37. "implementsIDs": childImplementsIDs,
  38. "source": childSource,
  39. "type": childType,
  40. "name": childName,
  41. "prototypes": undefined,
  42. "aframeObj": undefined,
  43. "scene": undefined,
  44. "events": {
  45. "clickable": false
  46. }
  47. };
  48. },
  49. isAFrameClass: function (prototypes, classID) {
  50. if (prototypes) {
  51. for (var i = 0; i < prototypes.length; i++) {
  52. if (prototypes[i] === classID) {
  53. //console.info( "prototypes[ i ]: " + prototypes[ i ] );
  54. return true;
  55. }
  56. }
  57. }
  58. return false;
  59. },
  60. isAFrameComponent: function (prototypes) {
  61. var found = false;
  62. if (prototypes) {
  63. for (var i = 0; i < prototypes.length && !found; i++) {
  64. found = (prototypes[i] === "http://vwf.example.com/aframe/node.vwf");
  65. }
  66. }
  67. return found;
  68. },
  69. isAEntityComponent: function (prototypes) {
  70. var found = false;
  71. if (prototypes) {
  72. for (var i = 0; i < prototypes.length && !found; i++) {
  73. found = (prototypes[i] === "http://vwf.example.com/aframe/aentityComponent.vwf");
  74. }
  75. }
  76. return found;
  77. },
  78. isAFrameEntityComponent: function (prototypes) {
  79. var found = false;
  80. if (prototypes) {
  81. for (var i = 0; i < prototypes.length && !found; i++) {
  82. found = (prototypes[i] === "http://vwf.example.com/aframe/aentityComponent.vwf");
  83. }
  84. }
  85. return found;
  86. }
  87. };
  88. this.state.kernel = this.kernel.kernel.kernel;
  89. },
  90. // == Model API ============================================================================
  91. // -- creatingNode -------------------------------------------------------------------------
  92. creatingNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  93. childSource, childType, childIndex, childName, callback /* ( ready ) */) {
  94. // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
  95. // the scene or a prototype. In either of those cases, save the uri of the new node
  96. var childURI = (nodeID === 0 ? childIndex : undefined);
  97. var appID = this.kernel.application();
  98. // If the node being created is a prototype, construct it and add it to the array of prototypes,
  99. // and then return
  100. var prototypeID = utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
  101. if (prototypeID !== undefined) {
  102. this.state.prototypes[prototypeID] = {
  103. parentID: nodeID,
  104. ID: childID,
  105. extendsID: childExtendsID,
  106. implementsID: childImplementsIDs,
  107. source: childSource,
  108. type: childType,
  109. name: childName
  110. };
  111. return;
  112. }
  113. var protos = getPrototypes(this.kernel, childExtendsID);
  114. //var kernel = this.kernel.kernel.kernel;
  115. var node;
  116. if (this.state.isAFrameComponent(protos) || this.state.isAEntityComponent(protos)) {
  117. // Create the local copy of the node properties
  118. if (this.state.nodes[childID] === undefined) {
  119. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  120. childSource, childType, childIndex, childName, callback);
  121. }
  122. node = this.state.nodes[childID];
  123. node.prototypes = protos;
  124. if ( childType == "component" ){
  125. if ( nodeID !== undefined ) {
  126. setAFrameObjectComponents(node);
  127. }
  128. } else {
  129. node.aframeObj = createAFrameObject(node);
  130. addNodeToHierarchy(node);
  131. //notifyDriverOfPrototypeAndBehaviorProps();
  132. }
  133. }
  134. },
  135. // -- initializingProperty -----------------------------------------------------------------
  136. initializingProperty: function (nodeID, propertyName, propertyValue) {
  137. var value = undefined;
  138. var node = this.state.nodes[nodeID];
  139. if (node !== undefined) {
  140. value = this.settingProperty(nodeID, propertyName, propertyValue);
  141. }
  142. return value;
  143. },
  144. // -- creatingProperty ---------------------------------------------------------------------
  145. creatingProperty: function (nodeID, propertyName, propertyValue) {
  146. return this.initializingProperty(nodeID, propertyName, propertyValue);
  147. },
  148. // -- deletingNode -------------------------------------------------------------------------
  149. //deletingNode: function( nodeID ) {
  150. //},
  151. // -- deletingNode -------------------------------------------------------------------------
  152. deletingNode: function( nodeID ) {
  153. if ( this.state.nodes[ nodeID ] !== undefined ) {
  154. var node = this.state.nodes[ nodeID ];
  155. if ( node.aframeObj !== undefined ) {
  156. // removes and destroys object
  157. node.aframeObj.parentNode.removeChild(node.aframeObj);
  158. node.aframeObj = undefined;
  159. }
  160. delete this.state.nodes[ nodeID ];
  161. }
  162. },
  163. // -- settingProperty ----------------------------------------------------------------------
  164. settingProperty: function (nodeID, propertyName, propertyValue) {
  165. var node = this.state.nodes[nodeID];
  166. var value = undefined;
  167. if (node && node.aframeObj && utility.validObject(propertyValue)) {
  168. var aframeObject = node.aframeObj;
  169. if (isNodeDefinition(node.prototypes)) {
  170. // 'id' will be set to the nodeID
  171. value = propertyValue;
  172. switch (propertyName) {
  173. default:
  174. value = undefined;
  175. break;
  176. }
  177. }
  178. var componentName = "wasd-controls";
  179. if ( value === undefined && aframeObject.attributes.hasOwnProperty(componentName)) {
  180. value = propertyValue;
  181. switch ( propertyName ) {
  182. case "enabled":
  183. aframeObject.setAttribute(componentName, 'enabled', propertyValue);
  184. if (propertyValue){
  185. // aframeObject.addEventListener('componentchanged', function (evt) {
  186. // if (evt.detail.name === 'position') {
  187. // self.kernel.fireEvent(node.parentID, "setPosition", evt.detail.newData);
  188. // }
  189. // if (evt.detail.name === 'rotation') {
  190. // self.kernel.fireEvent(node.ID, "setRotation", evt.detail.newData);
  191. // }
  192. // });
  193. }
  194. break;
  195. default:
  196. value = undefined;
  197. break;
  198. }
  199. }
  200. if ( value === undefined && isAEntityDefinition( node.prototypes ) ) {
  201. value = propertyValue;
  202. switch ( propertyName ) {
  203. case "interpolation":
  204. aframeObject.setAttribute('interpolation', { duration: propertyValue});
  205. break;
  206. case "position":
  207. aframeObject.setAttribute('position', { x: propertyValue[0], y: propertyValue[1], z: propertyValue[2] });
  208. break;
  209. case "rotation":
  210. aframeObject.setAttribute('rotation', { x: propertyValue[0], y: propertyValue[1], z: propertyValue[2] });
  211. break;
  212. case "scale":
  213. aframeObject.setAttribute('scale', { x: propertyValue[0], y: propertyValue[1], z: propertyValue[2] });
  214. break;
  215. case "color":
  216. aframeObject.setAttribute('color', propertyValue);
  217. break;
  218. case "transparent":
  219. aframeObject.setAttribute('material','transparent', propertyValue);
  220. break;
  221. case "opacity":
  222. aframeObject.setAttribute('material','opacity', propertyValue);
  223. break;
  224. case "fog":
  225. aframeObject.setAttribute('material','fog', propertyValue);
  226. break;
  227. case "wireframe":
  228. aframeObject.setAttribute('wireframe', propertyValue);
  229. break;
  230. case "wireframe-linewidth":
  231. aframeObject.setAttribute('wireframeLinewidth', propertyValue);
  232. break;
  233. case "clickable":
  234. if(propertyValue){
  235. aframeObject.setAttribute('class', 'intersectable');
  236. } else {
  237. aframeObject.setAttribute('class', 'nonintersectable');
  238. }
  239. node.events.clickable = propertyValue;
  240. break;
  241. // case "clickable":
  242. // console.log("set clickable!");
  243. // value = propertyValue;
  244. // break;
  245. // case "clickable":
  246. // if (propertyValue) {
  247. // aframeObject.addEventListener('click', function (evt) {
  248. // console.log("click!");
  249. // vwf_view.kernel.fireEvent(node.ID, "clickEvent",evt.detail.cursorEl.id);
  250. // });
  251. // }
  252. // break;
  253. case "src":
  254. aframeObject.setAttribute('src', propertyValue);
  255. break;
  256. case "repeat":
  257. aframeObject.setAttribute('repeat', propertyValue);
  258. break;
  259. case "look-controls-enabled":
  260. aframeObject.setAttribute('look-controls', 'enabled', propertyValue);
  261. break;
  262. case "wasd-controls":
  263. aframeObject.setAttribute('wasd-controls', 'enabled', propertyValue);
  264. break;
  265. default:
  266. value = undefined;
  267. break;
  268. }
  269. }
  270. if ( value === undefined && aframeObject.nodeName == "A-TEXT" ) {
  271. value = propertyValue;
  272. switch ( propertyName ) {
  273. case "value":
  274. aframeObject.setAttribute('value', propertyValue);
  275. break;
  276. case "color":
  277. aframeObject.setAttribute('color', propertyValue);
  278. break;
  279. case "side":
  280. aframeObject.setAttribute('side', propertyValue);
  281. break;
  282. default:
  283. value = undefined;
  284. break;
  285. }
  286. }
  287. if ( value === undefined && aframeObject.nodeName == "A-SCENE" ) {
  288. value = propertyValue;
  289. switch ( propertyName ) {
  290. case "fog":
  291. aframeObject.setAttribute('fog', propertyValue);
  292. break;
  293. case "assets":
  294. var assetsElement = document.createElement('a-assets');
  295. aframeObject.appendChild(assetsElement);
  296. if (propertyValue) {
  297. httpGetJson(propertyValue).then(function (response) {
  298. console.log(JSON.parse(response));
  299. let assets = JSON.parse(response);
  300. for (var prop in assets) {
  301. var elm = document.createElement(assets[prop].tag);
  302. elm.setAttribute('id', prop);
  303. elm.setAttribute('src', assets[prop].src);
  304. assetsElement.appendChild(elm);
  305. }
  306. }).catch(function (error) {
  307. console.log(error);
  308. });
  309. }
  310. break;
  311. default:
  312. value = undefined;
  313. break;
  314. }
  315. }
  316. if ( value === undefined && aframeObject.nodeName == "A-BOX") {
  317. value = propertyValue;
  318. switch ( propertyName ) {
  319. case "depth":
  320. aframeObject.setAttribute('depth', propertyValue);
  321. break;
  322. case "height":
  323. aframeObject.setAttribute('height', propertyValue);
  324. break;
  325. case "width":
  326. aframeObject.setAttribute('width', propertyValue);
  327. break;
  328. default:
  329. value = undefined;
  330. break;
  331. }
  332. }
  333. if ( value === undefined && aframeObject.nodeName == "A-LIGHT" ) {
  334. value = propertyValue;
  335. switch ( propertyName ) {
  336. //"angle", "color", "decay", "distance", "ground-color", "intensity", "penumbra", "type", "target"
  337. case "color":
  338. aframeObject.setAttribute('color', propertyValue);
  339. break;
  340. case "type":
  341. aframeObject.setAttribute('type', propertyValue);
  342. break;
  343. case "intensity":
  344. aframeObject.setAttribute('intensity', propertyValue);
  345. break;
  346. case "distance":
  347. aframeObject.setAttribute('distance', propertyValue);
  348. break;
  349. default:
  350. value = undefined;
  351. break;
  352. }
  353. }
  354. if ( value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL") {
  355. value = propertyValue;
  356. switch ( propertyName ) {
  357. case "src":
  358. aframeObject.setAttribute('src', propertyValue);
  359. break;
  360. default:
  361. value = undefined;
  362. break;
  363. }
  364. }
  365. if ( value === undefined && aframeObject.nodeName == "A-PLANE") {
  366. value = propertyValue;
  367. switch ( propertyName ) {
  368. case "height":
  369. aframeObject.setAttribute('height', propertyValue);
  370. break;
  371. case "width":
  372. aframeObject.setAttribute('width', propertyValue);
  373. break;
  374. default:
  375. value = undefined;
  376. break;
  377. }
  378. }
  379. if (value === undefined && aframeObject.nodeName == "A-SPHERE") {
  380. value = propertyValue;
  381. switch (propertyName) {
  382. case "radius":
  383. aframeObject.setAttribute('radius', propertyValue);
  384. break;
  385. default:
  386. value = undefined;
  387. break;
  388. }
  389. }
  390. if (value === undefined && aframeObject.nodeName == "A-CAMERA") {
  391. value = propertyValue;
  392. switch (propertyName) {
  393. case "userHeight":
  394. aframeObject.setAttribute('camera', 'userHeight', propertyValue);
  395. break;
  396. // case "active":
  397. // aframeObject.setAttribute('camera', 'active', propertyValue);
  398. // break;
  399. default:
  400. value = undefined;
  401. break;
  402. }
  403. }
  404. }
  405. return value;
  406. },
  407. // -- gettingProperty ----------------------------------------------------------------------
  408. gettingProperty: function (nodeID, propertyName, propertyValue) {
  409. var node = this.state.nodes[nodeID];
  410. var value = undefined;
  411. if (node && node.aframeObj) {
  412. var aframeObject = node.aframeObj;
  413. if (isNodeDefinition(node.prototypes)) {
  414. switch ( propertyName ) {
  415. }
  416. }
  417. if ( value === undefined && aframeObject.attributes.hasOwnProperty("wasd-controls")) {
  418. value = propertyValue;
  419. switch ( propertyName ) {
  420. case "enabled":
  421. aframeObject.setAttribute('wasd-controls', 'enabled', propertyValue);
  422. break;
  423. }
  424. }
  425. if ( value === undefined && isAEntityDefinition( node.prototypes ) ) {
  426. switch ( propertyName ) {
  427. case "interpolation":
  428. var interpolation = aframeObject.getAttribute('interpolation');
  429. if (interpolation !== null && interpolation !== undefined) {
  430. value = interpolation.duration;
  431. }
  432. break;
  433. case "position":
  434. var pos = aframeObject.getAttribute('position');
  435. if ( pos !== undefined ){
  436. value = [pos.x, pos.y, pos.z];
  437. }
  438. break;
  439. case "scale":
  440. var scale = aframeObject.getAttribute('scale');
  441. if ( scale !== undefined ){
  442. value = [scale.x, scale.y, scale.z];
  443. }
  444. break;
  445. case "rotation":
  446. var rot = aframeObject.getAttribute('rotation');
  447. if ( rot !== undefined ){
  448. value = [rot.x, rot.y, rot.z];
  449. }
  450. break;
  451. case "color":
  452. value = aframeObject.getAttribute('color');
  453. break;
  454. case "fog":
  455. if (aframeObject.getAttribute('material')){
  456. value = aframeObject.getAttribute('material').fog;
  457. }
  458. break;
  459. case "opacity":
  460. if (aframeObject.getAttribute('material')){
  461. value = aframeObject.getAttribute('material').opacity;
  462. }
  463. break;
  464. case "transparent":
  465. if (aframeObject.getAttribute('material')){
  466. value = aframeObject.getAttribute('material').transparent;
  467. }
  468. break;
  469. case "wireframe":
  470. value = aframeObject.getAttribute('wireframe');
  471. break;
  472. case "wireframe-linewidth":
  473. value = aframeObject.getAttribute('wireframeLinewidth');
  474. break;
  475. case "clickable":
  476. value = node.events.clickable;
  477. break;
  478. case "src":
  479. value = aframeObject.getAttribute('src');
  480. break;
  481. case "repeat":
  482. value = aframeObject.getAttribute('repeat');
  483. case "look-controls-enabled":
  484. var look = aframeObject.getAttribute('look-controls-enabled');
  485. if (look !== null && look !== undefined) {
  486. value = aframeObject.getAttribute('look-controls').enabled;
  487. }
  488. break;
  489. case "wasd-controls":
  490. var wasd = aframeObject.getAttribute('wasd-controls');
  491. if (wasd !== null && wasd !== undefined) {
  492. value = aframeObject.getAttribute('wasd-controls').enabled;
  493. }
  494. break;
  495. }
  496. }
  497. if ( value === undefined && aframeObject.nodeName == "A-SCENE" ) {
  498. switch ( propertyName ) {
  499. case "fog":
  500. value = aframeObject.getAttribute('fog');
  501. break;
  502. }
  503. }
  504. if ( value === undefined && aframeObject.nodeName == "A-BOX" ) {
  505. switch ( propertyName ) {
  506. case "depth":
  507. value = aframeObject.getAttribute('depth');
  508. break;
  509. case "height":
  510. value = aframeObject.getAttribute('height');
  511. break;
  512. case "width":
  513. value = aframeObject.getAttribute('width');
  514. break;
  515. }
  516. }
  517. if ( value === undefined && aframeObject.nodeName == "A-LIGHT" ) {
  518. //"angle", "color", "decay", "distance", "ground-color", "intensity", "penumbra", "type", "target"
  519. switch (propertyName) {
  520. case "color":
  521. value = aframeObject.getAttribute('color');
  522. break;
  523. case "type":
  524. value = aframeObject.getAttribute('type');
  525. break;
  526. case "distance":
  527. value = aframeObject.getAttribute('distance');
  528. break;
  529. case "intensity":
  530. value = aframeObject.getAttribute('intensity');
  531. break;
  532. }
  533. }
  534. if ( value === undefined && aframeObject.nodeName == "A-PLANE" ) {
  535. switch (propertyName) {
  536. case "height":
  537. value = aframeObject.getAttribute('height');
  538. break;
  539. case "width":
  540. value = aframeObject.getAttribute('width');
  541. break;
  542. }
  543. }
  544. if ( value === undefined && aframeObject.nodeName == "A-SPHERE" ) {
  545. switch (propertyName) {
  546. case "radius":
  547. value = aframeObject.getAttribute('radius');
  548. break;
  549. }
  550. }
  551. if ( value === undefined && aframeObject.nodeName == "A-TEXT" ) {
  552. switch (propertyName) {
  553. case "value":
  554. value = aframeObject.getAttribute('value');
  555. break;
  556. case "color":
  557. value = aframeObject.getAttribute('color');
  558. break;
  559. case "side":
  560. value = aframeObject.getAttribute('side');
  561. break;
  562. }
  563. }
  564. if ( value === undefined && aframeObject.nodeName == "A-CAMERA" ) {
  565. switch (propertyName) {
  566. case "userHeight":
  567. value = aframeObject.getAttribute('camera').userHeight;
  568. break;
  569. }
  570. // switch (propertyName) {
  571. // case "active":
  572. // value = aframeObject.getAttribute('camera').active;
  573. // break;
  574. // }
  575. }
  576. if ( value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL" ) {
  577. switch (propertyName) {
  578. case "src":
  579. value = aframeObject.getAttribute('src');
  580. break;
  581. }
  582. }
  583. }
  584. if ( value !== undefined ) {
  585. propertyValue = value;
  586. }
  587. return value;
  588. }
  589. });
  590. function setAFrameObjectComponents(node, config) {
  591. let protos = node.prototypes;
  592. let parentNode = self.state.nodes[node.parentID];
  593. if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/wasd-controls.vwf")) {
  594. parentNode.aframeObj.setAttribute('wasd-controls', {});
  595. }
  596. node.aframeObj = parentNode.aframeObj;
  597. }
  598. function createAFrameObject(node, config) {
  599. var protos = node.prototypes;
  600. var aframeObj = undefined;
  601. if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/ascene.vwf")) {
  602. aframeObj = document.createElement('a-scene');
  603. self.state.scenes[node.ID] = aframeObj;
  604. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acamera.vwf")) {
  605. aframeObj = document.createElement('a-camera');
  606. aframeObj.setAttribute('camera', 'active', false);
  607. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/alight.vwf")) {
  608. aframeObj = document.createElement('a-light');
  609. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acursor.vwf")) {
  610. aframeObj = document.createElement('a-cursor');
  611. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asky.vwf")) {
  612. aframeObj = document.createElement('a-sky');
  613. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/abox.vwf")) {
  614. aframeObj = document.createElement('a-box');
  615. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aplane.vwf")) {
  616. aframeObj = document.createElement('a-plane');
  617. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/atext.vwf")) {
  618. aframeObj = document.createElement('a-text');
  619. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acolladamodel.vwf")) {
  620. aframeObj = document.createElement('a-collada-model');
  621. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asphere.vwf")) {
  622. aframeObj = document.createElement('a-sphere');
  623. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aentity.vwf")) {
  624. aframeObj = document.createElement('a-entity');
  625. }
  626. aframeObj.setAttribute('id', node.ID);
  627. return aframeObj;
  628. }
  629. function addNodeToHierarchy(node) {
  630. if (node.aframeObj) {
  631. if (self.state.nodes[node.parentID] !== undefined) {
  632. var parent = self.state.nodes[node.parentID];
  633. if (parent.aframeObj) {
  634. if (parent.children === undefined) {
  635. parent.children = [];
  636. }
  637. parent.children.push(node.ID);
  638. //console.info( "Adding child: " + childID + " to " + nodeID );
  639. parent.aframeObj.appendChild(node.aframeObj);
  640. }
  641. }
  642. if (node.aframeObj.nodeName !== "A-SCENE") {
  643. node.scene = self.state.scenes[self.kernel.application()];
  644. }
  645. }
  646. }
  647. function getPrototypes(kernel, extendsID) {
  648. var prototypes = [];
  649. var id = extendsID;
  650. while (id !== undefined) {
  651. prototypes.push(id);
  652. id = kernel.prototype(id);
  653. }
  654. return prototypes;
  655. }
  656. function isNodeDefinition(prototypes) {
  657. var found = false;
  658. if (prototypes) {
  659. for (var i = 0; i < prototypes.length && !found; i++) {
  660. found = (prototypes[i] == "http://vwf.example.com/aframe/node.vwf");
  661. }
  662. }
  663. return found;
  664. }
  665. function isAEntityDefinition( prototypes ) {
  666. var found = false;
  667. if ( prototypes ) {
  668. for ( var i = 0; i < prototypes.length && !found; i++ ) {
  669. found = ( prototypes[i] == "http://vwf.example.com/aframe/aentity.vwf" );
  670. }
  671. }
  672. return found;
  673. }
  674. // Changing this function significantly from the GLGE code
  675. // Will search hierarchy down until encountering a matching child
  676. // Will look into nodes that don't match.... this might not be desirable
  677. function FindChildByName(obj, childName, childType, recursive) {
  678. var child = undefined;
  679. if (recursive) {
  680. // TODO: If the obj itself has the child name, the object will be returned by this function
  681. // I don't think this this desirable.
  682. if (nameTest.call(this, obj, childName)) {
  683. child = obj;
  684. } else if (obj.children && obj.children.length > 0) {
  685. for (var i = 0; i < obj.children.length && child === undefined; i++) {
  686. child = FindChildByName(obj.children[i], childName, childType, true);
  687. }
  688. }
  689. } else {
  690. if (obj.children) {
  691. for (var i = 0; i < obj.children.length && child === undefined; i++) {
  692. if (nameTest.call(this, obj.children[i], childName)) {
  693. child = obj.children[i];
  694. }
  695. }
  696. }
  697. }
  698. return child;
  699. }
  700. function nameTest(obj, name) {
  701. if (obj.name == "") {
  702. return (obj.parent.name + "Child" == name);
  703. } else {
  704. return (obj.name == name || obj.id == name || obj.vwfID == name);
  705. }
  706. }
  707. function httpGet(url) {
  708. return new Promise(function (resolve, reject) {
  709. // do the usual Http request
  710. let request = new XMLHttpRequest();
  711. request.open('GET', url);
  712. request.onload = function () {
  713. if (request.status == 200) {
  714. resolve(request.response);
  715. } else {
  716. reject(Error(request.statusText));
  717. }
  718. };
  719. request.onerror = function () {
  720. reject(Error('Network Error'));
  721. };
  722. request.send();
  723. });
  724. }
  725. async function httpGetJson(url) {
  726. // check if the URL looks like a JSON file and call httpGet.
  727. let regex = /\.(json)$/i;
  728. if (regex.test(url)) {
  729. // call the async function, wait for the result
  730. return await httpGet(url);
  731. } else {
  732. throw Error('Bad Url Format');
  733. }
  734. }
  735. });