aframe.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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 "active":
  394. aframeObject.setAttribute('active', propertyValue);
  395. break;
  396. default:
  397. value = undefined;
  398. break;
  399. }
  400. }
  401. }
  402. return value;
  403. },
  404. // -- gettingProperty ----------------------------------------------------------------------
  405. gettingProperty: function (nodeID, propertyName, propertyValue) {
  406. var node = this.state.nodes[nodeID];
  407. var value = undefined;
  408. if (node && node.aframeObj) {
  409. var aframeObject = node.aframeObj;
  410. if (isNodeDefinition(node.prototypes)) {
  411. switch ( propertyName ) {
  412. }
  413. }
  414. if ( value === undefined && aframeObject.attributes.hasOwnProperty("wasd-controls")) {
  415. value = propertyValue;
  416. switch ( propertyName ) {
  417. case "enabled":
  418. aframeObject.setAttribute('wasd-controls', 'enabled', propertyValue);
  419. break;
  420. }
  421. }
  422. if ( value === undefined && isAEntityDefinition( node.prototypes ) ) {
  423. switch ( propertyName ) {
  424. case "interpolation":
  425. var interpolation = aframeObject.getAttribute('interpolation');
  426. if (interpolation !== null && interpolation !== undefined) {
  427. value = interpolation.duration;
  428. }
  429. break;
  430. case "position":
  431. var pos = aframeObject.getAttribute('position');
  432. if ( pos !== undefined ){
  433. value = [pos.x, pos.y, pos.z];
  434. }
  435. break;
  436. case "scale":
  437. var scale = aframeObject.getAttribute('scale');
  438. if ( scale !== undefined ){
  439. value = [scale.x, scale.y, scale.z];
  440. }
  441. break;
  442. case "rotation":
  443. var rot = aframeObject.getAttribute('rotation');
  444. if ( rot !== undefined ){
  445. value = [rot.x, rot.y, rot.z];
  446. }
  447. break;
  448. case "color":
  449. value = aframeObject.getAttribute('color');
  450. break;
  451. case "fog":
  452. if (aframeObject.getAttribute('material')){
  453. value = aframeObject.getAttribute('material').fog;
  454. }
  455. break;
  456. case "opacity":
  457. if (aframeObject.getAttribute('material')){
  458. value = aframeObject.getAttribute('material').opacity;
  459. }
  460. break;
  461. case "transparent":
  462. if (aframeObject.getAttribute('material')){
  463. value = aframeObject.getAttribute('material').transparent;
  464. }
  465. break;
  466. case "wireframe":
  467. value = aframeObject.getAttribute('wireframe');
  468. break;
  469. case "wireframe-linewidth":
  470. value = aframeObject.getAttribute('wireframeLinewidth');
  471. break;
  472. case "clickable":
  473. value = node.events.clickable;
  474. break;
  475. case "src":
  476. value = aframeObject.getAttribute('src');
  477. break;
  478. case "repeat":
  479. value = aframeObject.getAttribute('repeat');
  480. case "look-controls-enabled":
  481. var look = aframeObject.getAttribute('look-controls-enabled');
  482. if (look !== null && look !== undefined) {
  483. value = aframeObject.getAttribute('look-controls').enabled;
  484. }
  485. break;
  486. case "wasd-controls":
  487. var wasd = aframeObject.getAttribute('wasd-controls');
  488. if (wasd !== null && wasd !== undefined) {
  489. value = aframeObject.getAttribute('wasd-controls').enabled;
  490. }
  491. break;
  492. }
  493. }
  494. if ( value === undefined && aframeObject.nodeName == "A-SCENE" ) {
  495. switch ( propertyName ) {
  496. case "fog":
  497. value = aframeObject.getAttribute('fog');
  498. break;
  499. }
  500. }
  501. if ( value === undefined && aframeObject.nodeName == "A-BOX" ) {
  502. switch ( propertyName ) {
  503. case "depth":
  504. value = aframeObject.getAttribute('depth');
  505. break;
  506. case "height":
  507. value = aframeObject.getAttribute('height');
  508. break;
  509. case "width":
  510. value = aframeObject.getAttribute('width');
  511. break;
  512. }
  513. }
  514. if ( value === undefined && aframeObject.nodeName == "A-LIGHT" ) {
  515. //"angle", "color", "decay", "distance", "ground-color", "intensity", "penumbra", "type", "target"
  516. switch (propertyName) {
  517. case "color":
  518. value = aframeObject.getAttribute('color');
  519. break;
  520. case "type":
  521. value = aframeObject.getAttribute('type');
  522. break;
  523. case "distance":
  524. value = aframeObject.getAttribute('distance');
  525. break;
  526. case "intensity":
  527. value = aframeObject.getAttribute('intensity');
  528. break;
  529. }
  530. }
  531. if ( value === undefined && aframeObject.nodeName == "A-PLANE" ) {
  532. switch (propertyName) {
  533. case "height":
  534. value = aframeObject.getAttribute('height');
  535. break;
  536. case "width":
  537. value = aframeObject.getAttribute('width');
  538. break;
  539. }
  540. }
  541. if ( value === undefined && aframeObject.nodeName == "A-SPHERE" ) {
  542. switch (propertyName) {
  543. case "radius":
  544. value = aframeObject.getAttribute('radius');
  545. break;
  546. }
  547. }
  548. if ( value === undefined && aframeObject.nodeName == "A-TEXT" ) {
  549. switch (propertyName) {
  550. case "value":
  551. value = aframeObject.getAttribute('value');
  552. break;
  553. case "color":
  554. value = aframeObject.getAttribute('color');
  555. break;
  556. case "side":
  557. value = aframeObject.getAttribute('side');
  558. break;
  559. }
  560. }
  561. if ( value === undefined && aframeObject.nodeName == "A-CAMERA" ) {
  562. switch (propertyName) {
  563. case "active":
  564. value = aframeObject.getAttribute('active');
  565. break;
  566. }
  567. }
  568. if ( value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL" ) {
  569. switch (propertyName) {
  570. case "src":
  571. value = aframeObject.getAttribute('src');
  572. break;
  573. }
  574. }
  575. }
  576. if ( value !== undefined ) {
  577. propertyValue = value;
  578. }
  579. return value;
  580. }
  581. });
  582. function setAFrameObjectComponents(node, config) {
  583. let protos = node.prototypes;
  584. let parentNode = self.state.nodes[node.parentID];
  585. if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/wasd-controls.vwf")) {
  586. parentNode.aframeObj.setAttribute('wasd-controls', {});
  587. }
  588. node.aframeObj = parentNode.aframeObj;
  589. }
  590. function createAFrameObject(node, config) {
  591. var protos = node.prototypes;
  592. var aframeObj = undefined;
  593. if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/ascene.vwf")) {
  594. aframeObj = document.createElement('a-scene');
  595. self.state.scenes[node.ID] = aframeObj;
  596. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acamera.vwf")) {
  597. aframeObj = document.createElement('a-camera');
  598. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/alight.vwf")) {
  599. aframeObj = document.createElement('a-light');
  600. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acursor.vwf")) {
  601. aframeObj = document.createElement('a-cursor');
  602. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asky.vwf")) {
  603. aframeObj = document.createElement('a-sky');
  604. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/abox.vwf")) {
  605. aframeObj = document.createElement('a-box');
  606. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aplane.vwf")) {
  607. aframeObj = document.createElement('a-plane');
  608. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/atext.vwf")) {
  609. aframeObj = document.createElement('a-text');
  610. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acolladamodel.vwf")) {
  611. aframeObj = document.createElement('a-collada-model');
  612. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asphere.vwf")) {
  613. aframeObj = document.createElement('a-sphere');
  614. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aentity.vwf")) {
  615. aframeObj = document.createElement('a-entity');
  616. }
  617. aframeObj.setAttribute('id', node.ID);
  618. return aframeObj;
  619. }
  620. function addNodeToHierarchy(node) {
  621. if (node.aframeObj) {
  622. if (self.state.nodes[node.parentID] !== undefined) {
  623. var parent = self.state.nodes[node.parentID];
  624. if (parent.aframeObj) {
  625. if (parent.children === undefined) {
  626. parent.children = [];
  627. }
  628. parent.children.push(node.ID);
  629. //console.info( "Adding child: " + childID + " to " + nodeID );
  630. parent.aframeObj.appendChild(node.aframeObj);
  631. }
  632. }
  633. if (node.aframeObj.nodeName !== "A-SCENE") {
  634. node.scene = self.state.scenes[self.kernel.application()];
  635. }
  636. }
  637. }
  638. function getPrototypes(kernel, extendsID) {
  639. var prototypes = [];
  640. var id = extendsID;
  641. while (id !== undefined) {
  642. prototypes.push(id);
  643. id = kernel.prototype(id);
  644. }
  645. return prototypes;
  646. }
  647. function isNodeDefinition(prototypes) {
  648. var found = false;
  649. if (prototypes) {
  650. for (var i = 0; i < prototypes.length && !found; i++) {
  651. found = (prototypes[i] == "http://vwf.example.com/aframe/node.vwf");
  652. }
  653. }
  654. return found;
  655. }
  656. function isAEntityDefinition( 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/aentity.vwf" );
  661. }
  662. }
  663. return found;
  664. }
  665. // Changing this function significantly from the GLGE code
  666. // Will search hierarchy down until encountering a matching child
  667. // Will look into nodes that don't match.... this might not be desirable
  668. function FindChildByName(obj, childName, childType, recursive) {
  669. var child = undefined;
  670. if (recursive) {
  671. // TODO: If the obj itself has the child name, the object will be returned by this function
  672. // I don't think this this desirable.
  673. if (nameTest.call(this, obj, childName)) {
  674. child = obj;
  675. } else if (obj.children && obj.children.length > 0) {
  676. for (var i = 0; i < obj.children.length && child === undefined; i++) {
  677. child = FindChildByName(obj.children[i], childName, childType, true);
  678. }
  679. }
  680. } else {
  681. if (obj.children) {
  682. for (var i = 0; i < obj.children.length && child === undefined; i++) {
  683. if (nameTest.call(this, obj.children[i], childName)) {
  684. child = obj.children[i];
  685. }
  686. }
  687. }
  688. }
  689. return child;
  690. }
  691. function nameTest(obj, name) {
  692. if (obj.name == "") {
  693. return (obj.parent.name + "Child" == name);
  694. } else {
  695. return (obj.name == name || obj.id == name || obj.vwfID == name);
  696. }
  697. }
  698. function httpGet(url) {
  699. return new Promise(function (resolve, reject) {
  700. // do the usual Http request
  701. let request = new XMLHttpRequest();
  702. request.open('GET', url);
  703. request.onload = function () {
  704. if (request.status == 200) {
  705. resolve(request.response);
  706. } else {
  707. reject(Error(request.statusText));
  708. }
  709. };
  710. request.onerror = function () {
  711. reject(Error('Network Error'));
  712. };
  713. request.send();
  714. });
  715. }
  716. async function httpGetJson(url) {
  717. // check if the URL looks like a JSON file and call httpGet.
  718. let regex = /\.(json)$/i;
  719. if (regex.test(url)) {
  720. // call the async function, wait for the result
  721. return await httpGet(url);
  722. } else {
  723. throw Error('Bad Url Format');
  724. }
  725. }
  726. });