aframe.js 62 KB

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