aframe.js 55 KB

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