aframe.js 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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. setAFrameProperty: function (propertyName, propertyValue, aframeObject) {
  72. //console.log(propertyValue);
  73. if (propertyValue.hasOwnProperty('x')) {
  74. aframeObject.setAttribute(propertyName, propertyValue)
  75. } else
  76. if (Array.isArray(propertyValue)) {
  77. aframeObject.setAttribute(propertyName, { x: propertyValue[0], y: propertyValue[1], z: propertyValue[2] })
  78. } else if (typeof propertyValue === 'string') {
  79. aframeObject.setAttribute(propertyName, AFRAME.utils.coordinates.parse(propertyValue))
  80. }
  81. }
  82. };
  83. this.state.kernel = this.kernel.kernel.kernel;
  84. this.aframeDef = {
  85. 'A-BOX': [
  86. 'depth', 'height', 'segments-depth',
  87. 'segments-height', 'segments-width',
  88. 'width'],
  89. 'A-SPHERE': [
  90. 'phi-length', 'phi-start', 'radius',
  91. 'segments-depth',
  92. 'segments-height', 'segments-width',
  93. 'theta-length', 'theta-start'
  94. ],
  95. 'A-CYLINDER': [
  96. 'height', 'radius',
  97. 'open-ended', 'radius-bottom', 'radius-top',
  98. 'segments-height', 'segments-radial',
  99. 'theta-length', 'theta-start'
  100. ],
  101. 'A-CONE': [
  102. 'height',
  103. 'open-ended', 'radius-bottom', 'radius-top',
  104. 'segments-height', 'segments-radial',
  105. 'theta-length', 'theta-start'
  106. ],
  107. 'A-PLANE': [
  108. 'height', 'segments-height', 'segments-width','width'
  109. ],
  110. 'A-TEXT': [
  111. 'align', 'alpha-test', 'anchor',
  112. 'baseline', 'color', 'font',
  113. 'font-image', 'height',
  114. 'letter-spacing', 'line-height',
  115. 'opacity', 'shader',
  116. 'side', 'tab-size',
  117. 'transparent', 'value',
  118. 'white-space', 'width',
  119. 'wrap-count', 'wrap-pixels',
  120. 'z-offset'
  121. ],
  122. 'A-SKY': [
  123. 'phi-length', 'phi-start', 'radius','segments-height',
  124. 'segments-width',
  125. 'theta-length', 'theta-start',
  126. ],
  127. 'A-LIGHT': [
  128. 'angle', 'color', 'decay', 'distance',
  129. 'ground-color', 'intensity', 'penumbra',
  130. 'type', 'target'
  131. ]
  132. }
  133. },
  134. // == Model API ============================================================================
  135. // -- creatingNode -------------------------------------------------------------------------
  136. creatingNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  137. childSource, childType, childIndex, childName, callback /* ( ready ) */) {
  138. // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
  139. // the scene or a prototype. In either of those cases, save the uri of the new node
  140. var childURI = (nodeID === 0 ? childIndex : undefined);
  141. var appID = this.kernel.application();
  142. // If the node being created is a prototype, construct it and add it to the array of prototypes,
  143. // and then return
  144. var prototypeID = utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
  145. if (prototypeID !== undefined) {
  146. this.state.prototypes[prototypeID] = {
  147. parentID: nodeID,
  148. ID: childID,
  149. extendsID: childExtendsID,
  150. implementsID: childImplementsIDs,
  151. source: childSource,
  152. type: childType,
  153. name: childName
  154. };
  155. return;
  156. }
  157. var protos = getPrototypes(this.kernel, childExtendsID);
  158. //var kernel = this.kernel.kernel.kernel;
  159. var node;
  160. if (this.state.isAFrameComponent(protos)) {
  161. // Create the local copy of the node properties
  162. if (this.state.nodes[childID] === undefined) {
  163. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  164. childSource, childType, childIndex, childName, callback);
  165. }
  166. node = this.state.nodes[childID];
  167. node.prototypes = protos;
  168. // if (childType == "component") {
  169. // if (nodeID !== undefined) {
  170. // node.aframeObj = setAFrameObjectComponents(node);
  171. // addNodeToHierarchy(node);
  172. // }
  173. // } else {
  174. node.aframeObj = createAFrameObject(node);
  175. addNodeToHierarchy(node);
  176. //notifyDriverOfPrototypeAndBehaviorProps();
  177. // }
  178. //addNodeToHierarchy(node);
  179. }
  180. },
  181. // initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  182. // childSource, childType, childIndex, childName ) {
  183. // var node = this.state.nodes[childID];
  184. // if ( node && childType == "component" ) {
  185. // }
  186. // },
  187. // -- initializingProperty -----------------------------------------------------------------
  188. initializingProperty: function (nodeID, propertyName, propertyValue) {
  189. var value = undefined;
  190. var node = this.state.nodes[nodeID];
  191. if (node !== undefined) {
  192. value = this.settingProperty(nodeID, propertyName, propertyValue);
  193. }
  194. return value;
  195. },
  196. // -- creatingProperty ---------------------------------------------------------------------
  197. creatingProperty: function (nodeID, propertyName, propertyValue) {
  198. return this.initializingProperty(nodeID, propertyName, propertyValue);
  199. },
  200. // -- callingMethod ------------------------------------------------------------------------
  201. callingMethod: function( nodeID, methodName, methodParameters ) {
  202. var node = this.state.nodes[nodeID];
  203. if(!node) return;
  204. if (node && node.aframeObj ) {
  205. if (methodName == 'lookAt') {
  206. console.log('lookAt: ' + methodParameters[0]);
  207. let target = methodParameters[0];
  208. node.aframeObj.object3D.lookAt(new THREE.Vector3(target.x, target.y, target.z));
  209. let newRotation = node.aframeObj.getAttribute('rotation');
  210. self.kernel.setProperty(nodeID, "rotation", {x: 0, y: newRotation.y, z: 0});
  211. }
  212. }
  213. },
  214. // -- deletingNode -------------------------------------------------------------------------
  215. //deletingNode: function( nodeID ) {
  216. //},
  217. // -- deletingNode -------------------------------------------------------------------------
  218. deletingNode: function (nodeID) {
  219. if (nodeID) {
  220. var childNode = this.state.nodes[nodeID];
  221. if (!childNode) return;
  222. if (childNode !== undefined) {
  223. if (childNode.children) {
  224. for (var i = 0; i < childNode.children.length; i++) {
  225. this.deletingNode(childNode.children[i]);
  226. }
  227. }
  228. if (childNode.aframeObj !== undefined) {
  229. // removes and destroys object
  230. childNode.aframeObj.parentNode.removeChild(childNode.aframeObj);
  231. childNode.aframeObj = undefined;
  232. }
  233. delete this.state.nodes[nodeID];
  234. }
  235. }
  236. },
  237. // -- settingProperty ----------------------------------------------------------------------
  238. settingProperty: function (nodeID, propertyName, propertyValue) {
  239. var node = this.state.nodes[nodeID];
  240. var value = undefined;
  241. // if (node.componentName == 'line') {
  242. // console.log(node.aframeObj);
  243. // }
  244. if (node && node.aframeObj && utility.validObject(propertyValue)) {
  245. var aframeObject = node.aframeObj;
  246. if (isNodeDefinition(node.prototypes)) {
  247. // 'id' will be set to the nodeID
  248. value = propertyValue;
  249. switch (propertyName) {
  250. default:
  251. value = undefined;
  252. break;
  253. }
  254. }
  255. if (value === undefined && isAEntityDefinition(node.prototypes)) {
  256. value = propertyValue;
  257. switch (propertyName) {
  258. case "worldPosition":
  259. break;
  260. case "position":
  261. this.state.setAFrameProperty('position', propertyValue, aframeObject);
  262. break;
  263. case "rotation":
  264. this.state.setAFrameProperty('rotation', propertyValue, aframeObject);
  265. break;
  266. case "scale":
  267. this.state.setAFrameProperty('scale', propertyValue, aframeObject);
  268. break;
  269. case "clickable":
  270. if (propertyValue) {
  271. aframeObject.setAttribute('class', 'intersectable');
  272. } else {
  273. aframeObject.setAttribute('class', 'nonintersectable');
  274. }
  275. node.events.clickable = propertyValue;
  276. break;
  277. case "visible":
  278. aframeObject.setAttribute('visible', propertyValue);
  279. break;
  280. // case "clickable":
  281. // console.log("set clickable!");
  282. // value = propertyValue;
  283. // break;
  284. // case "clickable":
  285. // if (propertyValue) {
  286. // aframeObject.addEventListener('click', function (evt) {
  287. // console.log("click!");
  288. // vwf_view.kernel.fireEvent(node.ID, "clickEvent",evt.detail.cursorEl.id);
  289. // });
  290. // }
  291. // break;
  292. // case "look-controls-enabled":
  293. // aframeObject.setAttribute('look-controls', 'enabled', propertyValue);
  294. // break;
  295. case "wasd-controls":
  296. aframeObject.setAttribute('wasd-controls', 'enabled', propertyValue);
  297. break;
  298. default:
  299. value = undefined;
  300. break;
  301. }
  302. }
  303. if (value === undefined && aframeObject.nodeName == "A-ASSET-ITEM") {
  304. value = propertyValue;
  305. switch (propertyName) {
  306. case "itemID":
  307. aframeObject.setAttribute('id', propertyValue);
  308. break;
  309. case "itemSrc":
  310. aframeObject.setAttribute('src', propertyValue);
  311. break;
  312. default:
  313. value = undefined;
  314. break;
  315. }
  316. }
  317. if (value === undefined && aframeObject.nodeName == "IMG") {
  318. value = propertyValue;
  319. switch (propertyName) {
  320. case "itemID":
  321. aframeObject.setAttribute('id', propertyValue);
  322. break;
  323. case "itemSrc":
  324. aframeObject.setAttribute('src', propertyValue);
  325. break;
  326. // case "width":
  327. // aframeObject.width = propertyValue;
  328. // break;
  329. // case "height":
  330. // aframeObject.height = propertyValue;
  331. // break;
  332. default:
  333. value = undefined;
  334. break;
  335. }
  336. }
  337. if (value === undefined && aframeObject.nodeName == "AUDIO") {
  338. value = propertyValue;
  339. switch (propertyName) {
  340. case "itemID":
  341. aframeObject.setAttribute('id', propertyValue);
  342. break;
  343. case "itemSrc":
  344. aframeObject.setAttribute('src', propertyValue);
  345. break;
  346. default:
  347. value = undefined;
  348. break;
  349. }
  350. }
  351. if (value === undefined && aframeObject.nodeName == "VIDEO") {
  352. value = propertyValue;
  353. switch (propertyName) {
  354. case "itemID":
  355. aframeObject.setAttribute('id', propertyValue);
  356. break;
  357. case "itemSrc":
  358. aframeObject.setAttribute('src', propertyValue);
  359. break;
  360. default:
  361. value = undefined;
  362. break;
  363. }
  364. }
  365. if (value === undefined && aframeObject.nodeName == "A-SKY") {
  366. value = propertyValue;
  367. self.aframeDef['A-SKY'].forEach(element => {
  368. element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
  369. value = undefined;
  370. })
  371. }
  372. if (value === undefined && aframeObject.nodeName == "A-TEXT") {
  373. value = propertyValue;
  374. self.aframeDef['A-TEXT'].forEach(element => {
  375. element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
  376. value = undefined;
  377. })
  378. }
  379. if (value === undefined && aframeObject.nodeName == "A-SCENE") {
  380. value = propertyValue;
  381. switch (propertyName) {
  382. case "color":
  383. aframeObject.setAttribute('background', {'color': propertyValue} );
  384. break;
  385. case "transparent":
  386. aframeObject.setAttribute('background', {'transparent': propertyValue} );
  387. break;
  388. // case "fog":
  389. // aframeObject.setAttribute('fog', propertyValue);
  390. // break;
  391. case "assets":
  392. let assetsEl = document.querySelector('a-assets');
  393. if (!assetsEl) {
  394. let newAssetsEl = document.createElement('a-assets');
  395. aframeObject.appendChild(newAssetsEl);
  396. }
  397. var assetsElement = document.querySelector('a-assets');
  398. if (propertyValue) {
  399. httpGetJson(propertyValue).then(function (response) {
  400. console.log(JSON.parse(response));
  401. let assets = JSON.parse(response);
  402. for (var prop in assets) {
  403. var elm = document.createElement(assets[prop].tag);
  404. elm.setAttribute('id', prop);
  405. elm.setAttribute('src', assets[prop].src);
  406. assetsElement.appendChild(elm);
  407. }
  408. }).catch(function (error) {
  409. console.log(error);
  410. });
  411. }
  412. break;
  413. default:
  414. value = undefined;
  415. break;
  416. }
  417. }
  418. if (value === undefined && aframeObject.nodeName == "A-BOX") {
  419. value = propertyValue;
  420. self.aframeDef['A-BOX'].forEach(element => {
  421. element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
  422. value = undefined;
  423. })
  424. }
  425. if (value === undefined && aframeObject.nodeName == "A-LIGHT") {
  426. value = propertyValue;
  427. self.aframeDef['A-LIGHT'].forEach(element => {
  428. element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
  429. value = undefined;
  430. })
  431. switch (propertyName) {
  432. case "castShadow":
  433. aframeObject.setAttribute('light', 'castShadow', propertyValue);
  434. break;
  435. case "shadowCameraVisible":
  436. aframeObject.setAttribute('light', 'shadowCameraVisible', propertyValue);
  437. break;
  438. default:
  439. value = undefined;
  440. break;
  441. }
  442. }
  443. if (value === undefined && aframeObject.nodeName == "A-GLTF-MODEL") {
  444. value = propertyValue;
  445. switch (propertyName) {
  446. case "src":
  447. aframeObject.setAttribute('src', propertyValue);
  448. break;
  449. default:
  450. value = undefined;
  451. break;
  452. }
  453. }
  454. if (value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL") {
  455. value = propertyValue;
  456. switch (propertyName) {
  457. case "src":
  458. aframeObject.setAttribute('src', propertyValue);
  459. break;
  460. default:
  461. value = undefined;
  462. break;
  463. }
  464. }
  465. if (value === undefined && aframeObject.nodeName == "A-OBJ-MODEL") {
  466. value = propertyValue;
  467. switch (propertyName) {
  468. case "src":
  469. aframeObject.setAttribute('src', propertyValue);
  470. break;
  471. case "mtl":
  472. aframeObject.setAttribute('mtl', propertyValue);
  473. break;
  474. default:
  475. value = undefined;
  476. break;
  477. }
  478. }
  479. if (value === undefined && aframeObject.nodeName == "A-SOUND") {
  480. value = propertyValue;
  481. switch (propertyName) {
  482. case "src":
  483. aframeObject.setAttribute('src', propertyValue);
  484. break;
  485. case "on":
  486. aframeObject.setAttribute('on', propertyValue);
  487. break;
  488. case "autoplay":
  489. aframeObject.setAttribute('autoplay', propertyValue);
  490. break;
  491. default:
  492. value = undefined;
  493. break;
  494. }
  495. }
  496. if (value === undefined && aframeObject.nodeName == "A-PLANE") {
  497. value = propertyValue;
  498. self.aframeDef['A-PLANE'].forEach(element => {
  499. element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
  500. value = undefined;
  501. })
  502. }
  503. if (value === undefined && aframeObject.nodeName == "A-SPHERE") {
  504. value = propertyValue;
  505. self.aframeDef['A-SPHERE'].forEach(element => {
  506. element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
  507. value = undefined;
  508. })
  509. }
  510. if (value === undefined && aframeObject.nodeName == "A-CYLINDER") {
  511. value = propertyValue;
  512. self.aframeDef['A-CYLINDER'].forEach(element => {
  513. element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
  514. value = undefined;
  515. })
  516. }
  517. if (value === undefined && aframeObject.nodeName == "A-CONE") {
  518. value = propertyValue;
  519. self.aframeDef['A-CONE'].forEach(element => {
  520. element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
  521. value = undefined;
  522. })
  523. }
  524. if (value === undefined && aframeObject.nodeName == "A-ANIMATION") {
  525. value = propertyValue;
  526. switch (propertyName) {
  527. // attribute:
  528. // dur:
  529. // from:
  530. // to:
  531. // repeat:
  532. case "dur":
  533. aframeObject.setAttribute('dur', propertyValue);
  534. break;
  535. case "from":
  536. aframeObject.setAttribute('from', propertyValue);
  537. break;
  538. case "to":
  539. aframeObject.setAttribute('to', propertyValue);
  540. break;
  541. case "repeat":
  542. aframeObject.setAttribute('repeat', propertyValue);
  543. break;
  544. case "attribute":
  545. aframeObject.setAttribute('attribute', propertyValue);
  546. break;
  547. case "begin":
  548. aframeObject.setAttribute('begin', propertyValue);
  549. break;
  550. default:
  551. value = undefined;
  552. break;
  553. }
  554. }
  555. if (value === undefined && aframeObject.nodeName == "A-CAMERA") {
  556. value = propertyValue;
  557. switch (propertyName) {
  558. case "user-height":
  559. aframeObject.setAttribute('user-height', propertyValue);
  560. break;
  561. case "look-controls-enabled":
  562. aframeObject.setAttribute('look-controls-enabled', propertyValue);
  563. break;
  564. case "wasd-controls-enabled":
  565. aframeObject.setAttribute('wasd-controls-enabled', propertyValue);
  566. break;
  567. // case "active":
  568. // aframeObject.setAttribute('camera', 'active', propertyValue);
  569. // break;
  570. default:
  571. value = undefined;
  572. break;
  573. }
  574. }
  575. if (value === undefined && aframeObject.nodeName == "A-SUN-SKY") {
  576. value = propertyValue;
  577. switch (propertyName) {
  578. case "sunPosition":
  579. aframeObject.setAttribute('material', 'sunPosition', propertyValue);
  580. break;
  581. // case "active":
  582. // aframeObject.setAttribute('camera', 'active', propertyValue);
  583. // break;
  584. default:
  585. value = undefined;
  586. break;
  587. }
  588. }
  589. }
  590. return value;
  591. },
  592. // -- gettingProperty ----------------------------------------------------------------------
  593. gettingProperty: function (nodeID, propertyName, propertyValue) {
  594. var node = this.state.nodes[nodeID];
  595. var value = undefined;
  596. if (node && node.aframeObj) {
  597. var aframeObject = node.aframeObj;
  598. if (isNodeDefinition(node.prototypes)) {
  599. switch (propertyName) {
  600. }
  601. }
  602. if (value === undefined && isAEntityDefinition(node.prototypes)) {
  603. switch (propertyName) {
  604. case "worldPosition":
  605. var pos = aframeObject.object3D.getWorldPosition();
  606. if (pos !== undefined) {
  607. value = pos;
  608. }
  609. break;
  610. case "position":
  611. var pos = aframeObject.getAttribute('position');
  612. if (pos !== undefined) {
  613. value = pos//[pos.x, pos.y, pos.z]//AFRAME.utils.coordinates.stringify(pos);
  614. }
  615. break;
  616. case "scale":
  617. var scale = aframeObject.getAttribute('scale');
  618. if (scale !== undefined) {
  619. value = scale//AFRAME.utils.coordinates.stringify(scale);
  620. }
  621. break;
  622. case "rotation":
  623. var rot = aframeObject.getAttribute('rotation');
  624. if (rot !== undefined) {
  625. value = rot//AFRAME.utils.coordinates.stringify(rot);
  626. }
  627. break;
  628. case "clickable":
  629. value = node.events.clickable;
  630. break;
  631. // case "look-controls-enabled":
  632. // var look = aframeObject.getAttribute('look-controls-enabled');
  633. // if (look !== null && look !== undefined) {
  634. // value = aframeObject.getAttribute('look-controls').enabled;
  635. // }
  636. // break;
  637. // case "wasd-controls":
  638. // var wasd = aframeObject.getAttribute('wasd-controls');
  639. // if (wasd !== null && wasd !== undefined) {
  640. // value = aframeObject.getAttribute('wasd-controls').enabled;
  641. // }
  642. // break;
  643. case "visible":
  644. value = aframeObject.getAttribute('visible');
  645. break;
  646. }
  647. }
  648. if (value === undefined && aframeObject.nodeName == "A-ASSET-ITEM") {
  649. switch (propertyName) {
  650. case "itemID":
  651. value = aframeObject.getAttribute('id');
  652. break;
  653. case "itemSrc":
  654. value = aframeObject.getAttribute('src');
  655. break;
  656. }
  657. }
  658. if (value === undefined && aframeObject.nodeName == "IMG") {
  659. switch (propertyName) {
  660. case "itemID":
  661. value = aframeObject.getAttribute('id');
  662. break;
  663. case "itemSrc":
  664. value = aframeObject.getAttribute('src');
  665. break;
  666. case "width":
  667. value = aframeObject.width;
  668. break;
  669. case "height":
  670. value = aframeObject.height;
  671. break;
  672. }
  673. }
  674. if (value === undefined && aframeObject.nodeName == "AUDIO") {
  675. switch (propertyName) {
  676. case "itemID":
  677. value = aframeObject.getAttribute('id');
  678. break;
  679. case "itemSrc":
  680. value = aframeObject.getAttribute('src');
  681. break;
  682. }
  683. }
  684. if (value === undefined && aframeObject.nodeName == "VIDEO") {
  685. switch (propertyName) {
  686. case "itemID":
  687. value = aframeObject.getAttribute('id');
  688. break;
  689. case "itemSrc":
  690. value = aframeObject.getAttribute('src');
  691. break;
  692. case "videoWidth":
  693. value = aframeObject.videoWidth;
  694. break;
  695. case "videoHeight":
  696. value = aframeObject.videoHeight;
  697. break;
  698. }
  699. }
  700. if (value === undefined && aframeObject.nodeName == "A-SCENE") {
  701. switch (propertyName) {
  702. // case "fog":
  703. // value = aframeObject.getAttribute('fog');
  704. // break;
  705. case "color":
  706. if (aframeObject.getAttribute('background')) {
  707. value = aframeObject.getAttribute('background').color;
  708. }
  709. break;
  710. case "transparent":
  711. if (aframeObject.getAttribute('background')) {
  712. value = aframeObject.getAttribute('background').transparent;
  713. }
  714. break;
  715. }
  716. }
  717. if (value === undefined && aframeObject.nodeName == "A-SKY") {
  718. self.aframeDef['A-SKY'].forEach(element => {
  719. if (element == propertyName) {
  720. value = aframeObject.getAttribute(element);
  721. }
  722. })
  723. }
  724. if (value === undefined && aframeObject.nodeName == "A-LIGHT") {
  725. self.aframeDef['A-LIGHT'].forEach(element => {
  726. if (element == propertyName) {
  727. value = aframeObject.getAttribute(element);
  728. }
  729. })
  730. switch (propertyName) {
  731. case "castShadow":
  732. value = aframeObject.getAttribute('light').castShadow;
  733. break;
  734. case "shadowCameraVisible":
  735. value = aframeObject.getAttribute('light').shadowCameraVisible;
  736. break;
  737. }
  738. }
  739. if (value === undefined && aframeObject.nodeName == "A-BOX") {
  740. self.aframeDef['A-BOX'].forEach(element => {
  741. if (element == propertyName) {
  742. value = aframeObject.getAttribute(element);
  743. }
  744. })
  745. }
  746. if (value === undefined && aframeObject.nodeName == "A-PLANE") {
  747. self.aframeDef['A-PLANE'].forEach(element => {
  748. if (element == propertyName) {
  749. value = aframeObject.getAttribute(element);
  750. }
  751. })
  752. }
  753. if (value === undefined && aframeObject.nodeName == "A-SPHERE") {
  754. self.aframeDef['A-SPHERE'].forEach(element => {
  755. if (element == propertyName) {
  756. value = aframeObject.getAttribute(element);
  757. }
  758. })
  759. }
  760. if (value === undefined && aframeObject.nodeName == "A-CYLINDER") {
  761. self.aframeDef['A-CYLINDER'].forEach(element => {
  762. if (element == propertyName) {
  763. value = aframeObject.getAttribute(element);
  764. }
  765. })
  766. }
  767. if (value === undefined && aframeObject.nodeName == "A-CONE") {
  768. self.aframeDef['A-CONE'].forEach(element => {
  769. if (element == propertyName) {
  770. value = aframeObject.getAttribute(element);
  771. }
  772. })
  773. }
  774. if (value === undefined && aframeObject.nodeName == "A-TEXT") {
  775. self.aframeDef['A-TEXT'].forEach(element => {
  776. if (element == propertyName) {
  777. value = aframeObject.getAttribute(element);
  778. }
  779. })
  780. }
  781. if (value === undefined && aframeObject.nodeName == "A-ANIMATION") {
  782. switch (propertyName) {
  783. case "attribute":
  784. value = aframeObject.getAttribute('attribute');
  785. break;
  786. case "dur":
  787. value = aframeObject.getAttribute('dur');
  788. break;
  789. case "from":
  790. value = aframeObject.getAttribute('from');
  791. break;
  792. case "to":
  793. value = aframeObject.getAttribute('to');
  794. break;
  795. case "repeat":
  796. value = aframeObject.getAttribute('repeat');
  797. break;
  798. case "begin":
  799. value = aframeObject.getAttribute('begin');
  800. break;
  801. }
  802. }
  803. if (value === undefined && aframeObject.nodeName == "A-CAMERA") {
  804. switch (propertyName) {
  805. case "user-height":
  806. value = aframeObject.getAttribute('user-height');
  807. break;
  808. case "look-controls-enabled":
  809. value = aframeObject.getAttribute('look-controls-enabled');
  810. break;
  811. case "wasd-controls-enabled":
  812. value = aframeObject.getAttribute('wasd-controls-enabled');
  813. break;
  814. }
  815. // switch (propertyName) {
  816. // case "active":
  817. // value = aframeObject.getAttribute('camera').active;
  818. // break;
  819. // }
  820. }
  821. if (value === undefined && aframeObject.nodeName == "A-SUN-SKY") {
  822. switch (propertyName) {
  823. case "sunPosition":
  824. value = aframeObject.getAttribute('material').sunPosition;
  825. break;
  826. }
  827. // switch (propertyName) {
  828. // case "active":
  829. // value = aframeObject.getAttribute('camera').active;
  830. // break;
  831. // }
  832. }
  833. if (value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL") {
  834. switch (propertyName) {
  835. case "src":
  836. value = aframeObject.getAttribute('src');
  837. break;
  838. }
  839. }
  840. if (value === undefined && aframeObject.nodeName == "A-OBJ-MODEL") {
  841. switch (propertyName) {
  842. case "src":
  843. value = aframeObject.getAttribute('src');
  844. break;
  845. case "mtl":
  846. value = aframeObject.getAttribute('mtl');
  847. break;
  848. }
  849. }
  850. if (value === undefined && aframeObject.nodeName == "A-SOUND") {
  851. switch (propertyName) {
  852. case "src":
  853. value = aframeObject.getAttribute('src');
  854. break;
  855. case "on":
  856. value = aframeObject.getAttribute('on');
  857. break;
  858. case "autoplay":
  859. value = aframeObject.getAttribute('autoplay');
  860. break;
  861. }
  862. }
  863. if (value === undefined && aframeObject.nodeName == "A-GLTF-MODEL") {
  864. switch (propertyName) {
  865. case "src":
  866. value = aframeObject.getAttribute('src');
  867. break;
  868. }
  869. }
  870. }
  871. if (value !== undefined) {
  872. propertyValue = value;
  873. }
  874. return value;
  875. }
  876. });
  877. function createAFrameObject(node, config) {
  878. var protos = node.prototypes;
  879. var aframeObj = undefined;
  880. if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/ascene.vwf")) {
  881. aframeObj = document.createElement('a-scene');
  882. let assetsElement = document.createElement('a-assets');
  883. aframeObj.appendChild(assetsElement);
  884. self.state.scenes[node.ID] = aframeObj;
  885. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/a-asset-item.vwf")) {
  886. let assets = document.querySelector('a-assets');
  887. if (assets){
  888. aframeObj = document.createElement('a-asset-item');
  889. aframeObj.setAttribute('id', "item-"+GUID());
  890. aframeObj.setAttribute('src', "");
  891. aframeObj.setAttribute('crossorigin', "anonymous");
  892. assets.appendChild(aframeObj);
  893. }
  894. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/a-asset-image-item.vwf")) {
  895. let assets = document.querySelector('a-assets');
  896. if (assets){
  897. aframeObj = document.createElement('img');
  898. aframeObj.setAttribute('id', "item-"+GUID());
  899. aframeObj.setAttribute('src', "");
  900. aframeObj.setAttribute('crossorigin', "anonymous");
  901. assets.appendChild(aframeObj);
  902. }
  903. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/a-asset-audio-item.vwf")) {
  904. let assets = document.querySelector('a-assets');
  905. if (assets){
  906. aframeObj = document.createElement('audio');
  907. aframeObj.setAttribute('id', "item-"+GUID());
  908. aframeObj.setAttribute('src', "");
  909. aframeObj.setAttribute('crossorigin', "anonymous");
  910. assets.appendChild(aframeObj);
  911. }
  912. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/a-asset-video-item.vwf")) {
  913. let assets = document.querySelector('a-assets');
  914. if (assets){
  915. aframeObj = document.createElement('video');
  916. aframeObj.setAttribute('id', "item-"+GUID());
  917. aframeObj.setAttribute('src', "");
  918. aframeObj.setAttribute('crossorigin', "anonymous");
  919. aframeObj.setAttribute('autoplay', "");
  920. aframeObj.setAttribute('loop', true);
  921. assets.appendChild(aframeObj);
  922. }
  923. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asky.vwf")) {
  924. aframeObj = document.createElement('a-sky');
  925. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acamera.vwf")) {
  926. aframeObj = document.createElement('a-camera');
  927. aframeObj.setAttribute('camera', 'active', false);
  928. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/alight.vwf")) {
  929. aframeObj = document.createElement('a-light');
  930. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acursor.vwf")) {
  931. aframeObj = document.createElement('a-cursor');
  932. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/a-sun-sky.vwf")) {
  933. aframeObj = document.createElement('a-sun-sky');
  934. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/abox.vwf")) {
  935. aframeObj = document.createElement('a-box');
  936. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aplane.vwf")) {
  937. aframeObj = document.createElement('a-plane');
  938. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acylinder.vwf")) {
  939. aframeObj = document.createElement('a-cylinder');
  940. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acone.vwf")) {
  941. aframeObj = document.createElement('a-cone');
  942. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/atext.vwf")) {
  943. aframeObj = document.createElement('a-text');
  944. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acolladamodel.vwf")) {
  945. aframeObj = document.createElement('a-collada-model');
  946. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aobjmodel.vwf")) {
  947. aframeObj = document.createElement('a-obj-model');
  948. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asound.vwf")) {
  949. aframeObj = document.createElement('a-sound');
  950. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/agltfmodel.vwf")) {
  951. aframeObj = document.createElement('a-gltf-model');
  952. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asphere.vwf")) {
  953. aframeObj = document.createElement('a-sphere');
  954. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aanimation.vwf")) {
  955. aframeObj = document.createElement('a-animation');
  956. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aentity.vwf")) {
  957. aframeObj = document.createElement('a-entity');
  958. }
  959. if (aframeObj.nodeName !== "A-ASSET-ITEM" &&
  960. aframeObj.nodeName !== "IMG" &&
  961. aframeObj.nodeName !== "AUIDO" &&
  962. aframeObj.nodeName !== "VIDEO"
  963. ){
  964. aframeObj.setAttribute('id', node.ID);
  965. }
  966. return aframeObj;
  967. }
  968. function addNodeToHierarchy(node) {
  969. if (node.aframeObj) {
  970. if (self.state.nodes[node.parentID] !== undefined) {
  971. var parent = self.state.nodes[node.parentID];
  972. if (parent.aframeObj) {
  973. if (parent.children === undefined) {
  974. parent.children = [];
  975. }
  976. parent.children.push(node.ID);
  977. //console.info( "Adding child: " + childID + " to " + nodeID );
  978. if (node.aframeObj.nodeName !== "A-ASSET-ITEM" &&
  979. node.aframeObj.nodeName !== "IMG" &&
  980. node.aframeObj.nodeName !== "AUIDO" &&
  981. node.aframeObj.nodeName !== "VIDEO"
  982. ){
  983. parent.aframeObj.appendChild(node.aframeObj);
  984. }
  985. }
  986. }
  987. if (node.aframeObj.nodeName !== "A-SCENE") {
  988. node.scene = self.state.scenes[self.kernel.application()];
  989. }
  990. }
  991. }
  992. function getPrototypes(kernel, extendsID) {
  993. var prototypes = [];
  994. var id = extendsID;
  995. while (id !== undefined) {
  996. prototypes.push(id);
  997. id = kernel.prototype(id);
  998. }
  999. return prototypes;
  1000. }
  1001. function isNodeDefinition(prototypes) {
  1002. var found = false;
  1003. if (prototypes) {
  1004. for (var i = 0; i < prototypes.length && !found; i++) {
  1005. found = (prototypes[i] == "http://vwf.example.com/aframe/node.vwf");
  1006. }
  1007. }
  1008. return found;
  1009. }
  1010. function isAEntityDefinition(prototypes) {
  1011. var found = false;
  1012. if (prototypes) {
  1013. for (var i = 0; i < prototypes.length && !found; i++) {
  1014. found = (prototypes[i] == "http://vwf.example.com/aframe/aentity.vwf");
  1015. }
  1016. }
  1017. return found;
  1018. }
  1019. // Changing this function significantly from the GLGE code
  1020. // Will search hierarchy down until encountering a matching child
  1021. // Will look into nodes that don't match.... this might not be desirable
  1022. function FindChildByName(obj, childName, childType, recursive) {
  1023. var child = undefined;
  1024. if (recursive) {
  1025. // TODO: If the obj itself has the child name, the object will be returned by this function
  1026. // I don't think this this desirable.
  1027. if (nameTest.call(this, obj, childName)) {
  1028. child = obj;
  1029. } else if (obj.children && obj.children.length > 0) {
  1030. for (var i = 0; i < obj.children.length && child === undefined; i++) {
  1031. child = FindChildByName(obj.children[i], childName, childType, true);
  1032. }
  1033. }
  1034. } else {
  1035. if (obj.children) {
  1036. for (var i = 0; i < obj.children.length && child === undefined; i++) {
  1037. if (nameTest.call(this, obj.children[i], childName)) {
  1038. child = obj.children[i];
  1039. }
  1040. }
  1041. }
  1042. }
  1043. return child;
  1044. }
  1045. function nameTest(obj, name) {
  1046. if (obj.name == "") {
  1047. return (obj.parent.name + "Child" == name);
  1048. } else {
  1049. return (obj.name == name || obj.id == name || obj.vwfID == name);
  1050. }
  1051. }
  1052. function httpGet(url) {
  1053. return new Promise(function (resolve, reject) {
  1054. // do the usual Http request
  1055. let request = new XMLHttpRequest();
  1056. request.open('GET', url);
  1057. request.onload = function () {
  1058. if (request.status == 200) {
  1059. resolve(request.response);
  1060. } else {
  1061. reject(Error(request.statusText));
  1062. }
  1063. };
  1064. request.onerror = function () {
  1065. reject(Error('Network Error'));
  1066. };
  1067. request.send();
  1068. });
  1069. }
  1070. async function httpGetJson(url) {
  1071. // check if the URL looks like a JSON file and call httpGet.
  1072. let regex = /\.(json)$/i;
  1073. if (regex.test(url)) {
  1074. // call the async function, wait for the result
  1075. return await httpGet(url);
  1076. } else {
  1077. throw Error('Bad Url Format');
  1078. }
  1079. }
  1080. function GUID() {
  1081. var S4 = function () {
  1082. return Math.floor(
  1083. Math.random() * 0x10000 /* 65536 */
  1084. ).toString(16);
  1085. };
  1086. return (
  1087. S4() + S4() + "-" +
  1088. S4() + "-" +
  1089. S4() + "-" +
  1090. S4() + "-" +
  1091. S4() + S4() + S4()
  1092. );
  1093. }
  1094. });