aframe.js 36 KB

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