aframe.js 40 KB

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