ascene.js 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. this.initialize = function () {
  2. this.future(0).clientWatch();
  3. };
  4. this.clientWatch = function () {
  5. var self = this;
  6. if (this.children.length !== 0) {
  7. var clients = this.find("doc('http://vwf.example.com/clients.vwf')")[0];
  8. if (clients !== undefined) {
  9. //console.log(clients.children);
  10. let clientsArray = [];
  11. clients.children.forEach(function (element) {
  12. clientsArray.push(element.name);
  13. });
  14. this.children.forEach(function (node) {
  15. if (node.id.indexOf('avatar-') != -1) {
  16. if (clientsArray.includes(node.id.slice(7))) {
  17. //console.log(node.id + 'is here!');
  18. } else {
  19. //console.log(node.id + " needed to delete!");
  20. self.children.delete(self.children[node.id]);
  21. //'gearvr-'
  22. let controllerVR = self.children['gearvr-' + node.id.slice(7)];
  23. if (controllerVR) {
  24. self.children.delete(controllerVR);
  25. }
  26. let wmrvR = self.children['wmrvr-right-' + node.id.slice(7)];
  27. if (wmrvR) {
  28. self.children.delete(wmrvR);
  29. }
  30. let wmrvL = self.children['wmrvr-left-' + node.id.slice(7)];
  31. if (wmrvL) {
  32. self.children.delete(wmrvL);
  33. }
  34. }
  35. }
  36. });
  37. }
  38. }
  39. this.future(5).clientWatch();
  40. };
  41. this.sphereProto = function () {
  42. let node = {
  43. "extends": "http://vwf.example.com/aframe/asphere.vwf",
  44. "properties": {
  45. "displayName": "sphere",
  46. "radius": 1,
  47. "clickable": true
  48. },
  49. children: {
  50. "material": {
  51. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  52. "type": "component",
  53. "properties":{
  54. "color": "white"
  55. }
  56. },
  57. "interpolation":
  58. {
  59. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  60. "type": "component",
  61. "properties": {
  62. "enabled": true
  63. }
  64. },
  65. "cursor-listener": {
  66. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  67. "type": "component"
  68. }
  69. },
  70. events: {
  71. "clickEvent": {
  72. "body": ""
  73. }
  74. }
  75. }
  76. return node
  77. }
  78. this.cylinderProto = function () {
  79. let node = {
  80. "extends": "http://vwf.example.com/aframe/acylinder.vwf",
  81. "properties": {
  82. "displayName": "cylinder",
  83. "radius": 1,
  84. "height": 1,
  85. "clickable": true
  86. },
  87. children: {
  88. "material": {
  89. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  90. "type": "component",
  91. "properties":{
  92. "color": "white"
  93. }
  94. },
  95. "interpolation":
  96. {
  97. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  98. "type": "component",
  99. "properties": {
  100. "enabled": true
  101. }
  102. },
  103. "cursor-listener": {
  104. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  105. "type": "component"
  106. }
  107. },
  108. events: {
  109. "clickEvent": {
  110. "body": ""
  111. }
  112. }
  113. }
  114. return node
  115. }
  116. this.coneProto = function () {
  117. let node = {
  118. "extends": "http://vwf.example.com/aframe/acone.vwf",
  119. "properties": {
  120. "displayName": "cone",
  121. "radius-bottom": 1,
  122. "radius-top": 0.01,
  123. "height": 1,
  124. "clickable": true
  125. },
  126. children: {
  127. "material": {
  128. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  129. "type": "component",
  130. "properties":{
  131. "color": "white"
  132. }
  133. },
  134. "interpolation":
  135. {
  136. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  137. "type": "component",
  138. "properties": {
  139. "enabled": true
  140. }
  141. },
  142. "cursor-listener": {
  143. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  144. "type": "component"
  145. }
  146. },
  147. events: {
  148. "clickEvent": {
  149. "body": ""
  150. }
  151. }
  152. }
  153. return node
  154. }
  155. this.textProto = function () {
  156. let node = {
  157. "extends": "http://vwf.example.com/aframe/atext.vwf",
  158. "properties": {
  159. "displayName": "text",
  160. "color": "white",
  161. "value": "Text",
  162. "side": "double",
  163. "clickable": true
  164. },
  165. children: {
  166. "interpolation":
  167. {
  168. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  169. "type": "component",
  170. "properties": {
  171. "enabled": true
  172. }
  173. },
  174. "cursor-listener": {
  175. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  176. "type": "component"
  177. }
  178. },
  179. events: {
  180. "clickEvent": {
  181. "body": ""
  182. }
  183. }
  184. }
  185. return node
  186. }
  187. this.cubeProto = function () {
  188. let node = {
  189. "extends": "http://vwf.example.com/aframe/abox.vwf",
  190. "properties": {
  191. "displayName": "cube",
  192. "height": 1,
  193. "width": 1,
  194. "depth": 1,
  195. "clickable": true
  196. },
  197. children: {
  198. "interpolation":
  199. {
  200. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  201. "type": "component",
  202. "properties": {
  203. "enabled": true
  204. }
  205. },
  206. "cursor-listener": {
  207. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  208. "type": "component"
  209. },
  210. "material": {
  211. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  212. "type": "component",
  213. "properties":{
  214. "color": "white"
  215. }
  216. }
  217. },
  218. events: {
  219. "clickEvent": {
  220. "body": ""
  221. }
  222. }
  223. }
  224. return node
  225. }
  226. this.lightProto = function (lightType) {
  227. var newLightType = lightType
  228. if (!newLightType){
  229. newLightType = "directional"
  230. }
  231. let node = {
  232. "extends": "http://vwf.example.com/aframe/alight.vwf",
  233. "properties": {
  234. "displayName": newLightType,
  235. "type": newLightType,
  236. "clickable": true
  237. },
  238. children: {
  239. "interpolation":
  240. {
  241. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  242. "type": "component",
  243. "properties": {
  244. "enabled": true
  245. }
  246. },
  247. "cursor-listener": {
  248. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  249. "type": "component"
  250. }
  251. },
  252. events: {
  253. "clickEvent": {
  254. "body": ""
  255. }
  256. }
  257. }
  258. return node
  259. }
  260. this.cameraProto = function () {
  261. let newNode = this.cubeProto();
  262. newNode.properties.width = 0.3;
  263. newNode.properties.height = 0.3;
  264. newNode.properties.depth= 0.5;
  265. newNode.children.material.properties.opacity = 0.5;
  266. newNode.children.material.properties.color = "red";
  267. newNode.children.camera = {
  268. "extends": "http://vwf.example.com/aframe/acamera.vwf",
  269. "properties": {
  270. "look-controls-enabled": false,
  271. "wasd-controls-enabled": false,
  272. "user-height": 0
  273. }
  274. }
  275. return newNode
  276. }
  277. this.cameraProtoWithOffset = function () {
  278. let newNode = this.cubeProto();
  279. newNode.properties.width = 0.3;
  280. newNode.properties.height = 0.3;
  281. newNode.properties.depth= 0.5;
  282. newNode.children.material.properties.opacity = 0.5;
  283. newNode.children.material.properties.color = "red";
  284. newNode.children.camera = {
  285. "extends": "http://vwf.example.com/aframe/acamera.vwf",
  286. "properties": {
  287. "look-controls-enabled": false,
  288. "wasd-controls-enabled": false,
  289. "user-height": 0
  290. },
  291. children: {
  292. viewoffset: {
  293. extends: "http://vwf.example.com/aframe/viewOffsetCamera-component.vwf",
  294. properties: {
  295. }
  296. }
  297. }
  298. }
  299. return newNode
  300. }
  301. this.planeProto = function () {
  302. let node = {
  303. "extends": "http://vwf.example.com/aframe/aplane.vwf",
  304. "properties": {
  305. "displayName": "plane",
  306. "height": 1,
  307. "width": 1,
  308. "clickable": true
  309. },
  310. children: {
  311. "material": {
  312. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  313. "type": "component",
  314. "properties": {
  315. "side": "double",
  316. "color": "white"
  317. }
  318. },
  319. "interpolation":
  320. {
  321. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  322. "type": "component",
  323. "properties": {
  324. "enabled": true
  325. }
  326. },
  327. "cursor-listener": {
  328. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  329. "type": "component"
  330. }
  331. },
  332. events: {
  333. "clickEvent": {
  334. "body": ""
  335. }
  336. }
  337. }
  338. return node
  339. }
  340. this.createModelObj = function (mtlSrc, objSrc, name, avatar) {
  341. var self = this;
  342. var position = "0 0 0";
  343. var nodeName = this.GUID();
  344. if (avatar) {
  345. let myAvatar = this.children[avatar];
  346. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  347. if (cursorNode) {
  348. position = cursorNode.worldPosition;
  349. //console.log(position);
  350. }
  351. }
  352. let modelNode = {
  353. "extends": "http://vwf.example.com/aframe/aobjmodel.vwf",
  354. "properties": {
  355. "src": '#' + objSrc,
  356. "mtl": '#' + mtlSrc,
  357. "position": position
  358. },
  359. children:{
  360. "interpolation":
  361. {
  362. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  363. "type": "component",
  364. "properties": {
  365. "enabled": true
  366. }
  367. }
  368. }
  369. }
  370. if (name) {
  371. modelNode.properties.displayName = name;
  372. }
  373. self.children.create(nodeName, modelNode, function( child ) {
  374. if (avatar) child.lookAt(self.children[avatar].worldPosition)
  375. });
  376. }
  377. this.createModel = function (modelType, modelSrc, avatar) {
  378. var self = this;
  379. let tagName = modelType + '-ASSET-'+ this.GUID();
  380. let tagNode = {
  381. "extends": "http://vwf.example.com/aframe/a-asset-item.vwf",
  382. "properties": {
  383. "itemID": tagName,
  384. "itemSrc": modelSrc
  385. }
  386. }
  387. this.children.create(tagName, tagNode, function( child ) {
  388. let nodeName = modelType + '-MODEL-'+self.GUID();
  389. var position = "0 0 0";
  390. if (avatar) {
  391. let myAvatar = self.children[avatar];
  392. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  393. if (cursorNode) {
  394. position = cursorNode.worldPosition;
  395. //console.log(position);
  396. }
  397. }
  398. const protos = {
  399. DAE: "http://vwf.example.com/aframe/acolladamodel.vwf",
  400. OBJ: "http://vwf.example.com/aframe/aobjmodel.vwf",
  401. GLTF: "http://vwf.example.com/aframe/agltfmodel.vwf"
  402. }
  403. let extendsName = Object.entries(protos).filter(el => el[0] == modelType)[0];
  404. let modelNode = {
  405. "extends": extendsName[1],
  406. "properties": {
  407. "src": '#' + child.itemID,
  408. "position": position
  409. },
  410. children:{
  411. "interpolation":
  412. {
  413. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  414. "type": "component",
  415. "properties": {
  416. "enabled": true
  417. }
  418. }
  419. }
  420. }
  421. self.children.create(nodeName, modelNode, function( child ) {
  422. if (avatar) child.lookAt(self.children[avatar].worldPosition)
  423. });
  424. });
  425. }
  426. this.createAssetResource = function(resType, resSrc){
  427. var self = this;
  428. const protos = {
  429. IMG: "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  430. AUDIO: "http://vwf.example.com/aframe/a-asset-audio-item.vwf",
  431. VIDEO: "http://vwf.example.com/aframe/a-asset-video-item.vwf",
  432. ITEM: "http://vwf.example.com/aframe/a-asset-item.vwf"
  433. };
  434. let extendsName = Object.entries(protos).filter(el => el[0] == resType)[0];
  435. let tagName = resType + '-ASSET-'+ this.GUID();
  436. let tagNode = {
  437. "extends": extendsName[1],
  438. "properties": {
  439. "itemID": tagName,
  440. "itemSrc": resSrc
  441. }
  442. }
  443. this.children.create(tagName, tagNode);
  444. }
  445. this.createPrimitive = function (type, params, name, node, avatar) {
  446. var position = "0 0 0";
  447. var nodeName = name;
  448. if (!nodeName) {
  449. nodeName = this.GUID();
  450. }
  451. if (avatar) {
  452. let myAvatar = this.children[avatar];
  453. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  454. if (cursorNode) {
  455. position = cursorNode.worldPosition;
  456. //console.log(position);
  457. }
  458. }
  459. var newNode = {};
  460. switch (type) {
  461. case "cube":
  462. newNode = this.cubeProto();
  463. break;
  464. case "sphere":
  465. newNode = this.sphereProto();
  466. break;
  467. case "plane":
  468. newNode = this.planeProto();
  469. break;
  470. case "light":
  471. newNode = this.lightProto(params);
  472. break;
  473. case "text":
  474. newNode = this.textProto(params);
  475. break;
  476. case "cylinder":
  477. newNode = this.cylinderProto();
  478. break;
  479. case "cone":
  480. newNode = this.coneProto();
  481. break;
  482. default:
  483. newNode = undefined;
  484. break;
  485. }
  486. var self = this;
  487. if (newNode) {
  488. newNode.properties.position = position;
  489. this.children.create(nodeName, newNode, function( child ) {
  490. if (avatar) child.lookAt(self.children[avatar].worldPosition);
  491. });
  492. }
  493. }
  494. this.createCamera = function (name, node, avatar) {
  495. var position = "0 0 0";
  496. var nodeName = name;
  497. if (!nodeName) {
  498. nodeName = this.GUID();
  499. }
  500. if (avatar) {
  501. let myAvatar = this.children[avatar];
  502. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  503. if (cursorNode) {
  504. position = cursorNode.worldPosition;
  505. //console.log(position);
  506. }
  507. }
  508. var newNode = this.cameraProto();
  509. newNode.properties.displayName = "camera";
  510. var self = this;
  511. if (newNode) {
  512. newNode.properties.position = position;
  513. this.children.create(nodeName, newNode, function( child ) {
  514. if (avatar) child.lookAt(self.children[avatar].worldPosition);
  515. });
  516. }
  517. }
  518. this.createCameraWithOffset = function (name, node, avatar) {
  519. var position = "0 0 0";
  520. var nodeName = name;
  521. if (!nodeName) {
  522. nodeName = this.GUID();
  523. }
  524. if (avatar) {
  525. let myAvatar = this.children[avatar];
  526. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  527. if (cursorNode) {
  528. position = cursorNode.worldPosition;
  529. //console.log(position);
  530. }
  531. }
  532. var newNode = this.cameraProtoWithOffset();
  533. newNode.properties.displayName = "cameraWithOffset";
  534. var self = this;
  535. if (newNode) {
  536. newNode.properties.position = position;
  537. this.children.create(nodeName, newNode, function( child ) {
  538. if (avatar) child.lookAt(self.children[avatar].worldPosition);
  539. });
  540. }
  541. }
  542. this.createImage = function (imgSrc, name, node, avatar) {
  543. var self = this;
  544. var position = "0 0 0";
  545. var nodeName = name;
  546. if (!nodeName) {
  547. nodeName = this.GUID();
  548. }
  549. if (avatar) {
  550. let myAvatar = this.children[avatar];
  551. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  552. if (cursorNode) {
  553. position = cursorNode.worldPosition;
  554. //console.log(position);
  555. }
  556. }
  557. let tagName = 'IMG-ASSET-'+ this.GUID();
  558. let tagNode = {
  559. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  560. "properties": {
  561. "itemID": tagName,
  562. "itemSrc": imgSrc
  563. }
  564. }
  565. this.children.create(tagName, tagNode, function( child ) {
  566. var nodeName = 'IMAGE-'+self.GUID();
  567. var position = "0 0 0";
  568. if (avatar) {
  569. let myAvatar = self.children[avatar];
  570. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  571. if (cursorNode) {
  572. position = cursorNode.worldPosition;
  573. //console.log(position);
  574. }
  575. }
  576. let newNode = self.planeProto();
  577. newNode.properties.displayName = "image";
  578. newNode.children.material.properties.src = '#' + child.itemID;
  579. newNode.properties.position = position;
  580. newNode.properties.scale = [0.003, 0.003, 0.003];
  581. if(child.width && child.height){
  582. newNode.properties.width = child.width;
  583. newNode.properties.height = child.height;
  584. self.children.create(nodeName, newNode, function( child ) {
  585. if (avatar) child.lookAt(self.children[avatar].worldPosition)
  586. });
  587. } else {
  588. let allNodes = vwf.models["vwf/model/aframe"].model.state.nodes;
  589. let imgAssetNode = allNodes[child.id];
  590. imgAssetNode.aframeObj.onload = function(){
  591. // console.log(imgAssetNode);
  592. newNode.properties.width = child.width;
  593. newNode.properties.height = child.height;
  594. self.children.create(nodeName, newNode, function( child ) {
  595. if (avatar) child.lookAt(self.children[avatar].worldPosition)
  596. });
  597. }
  598. }
  599. });
  600. }
  601. this.createVideo = function (vidSrc, name, node, avatar) {
  602. var self = this;
  603. var position = "0 0 0";
  604. var nodeName = name;
  605. if (!nodeName) {
  606. nodeName = this.GUID();
  607. }
  608. if (avatar) {
  609. let myAvatar = this.children[avatar];
  610. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  611. if (cursorNode) {
  612. position = cursorNode.worldPosition;
  613. //console.log(position);
  614. }
  615. }
  616. let tagName = 'VIDEO-ASSET-'+ this.GUID();
  617. let tagNode = {
  618. "extends": "http://vwf.example.com/aframe/a-asset-video-item.vwf",
  619. "properties": {
  620. "itemID": tagName,
  621. "itemSrc": vidSrc
  622. }
  623. }
  624. this.children.create(tagName, tagNode, function( child ) {
  625. let nodeName = 'VIDEO-'+self.GUID();
  626. var position = "0 0 0";
  627. if (avatar) {
  628. let myAvatar = self.children[avatar];
  629. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  630. if (cursorNode) {
  631. position = cursorNode.worldPosition;
  632. //console.log(position);
  633. }
  634. }
  635. let newNode = self.planeProto();
  636. newNode.properties.displayName = "video";
  637. newNode.children.material.properties.src = '#' + child.itemID;
  638. newNode.properties.position = position;
  639. // newNode.properties.width = 3;
  640. // newNode.properties.height = 1.75;
  641. newNode.properties.scale = [0.003, 0.003, 0.003];
  642. if (child.videoWidth && child.videoHeight) {
  643. newNode.properties.width = child.videoWidth;
  644. newNode.properties.height = child.videoHeight;
  645. self.children.create(nodeName, newNode, function( child ) {
  646. if (avatar) child.lookAt(self.children[avatar].worldPosition)
  647. });
  648. } else {
  649. let allNodes = vwf.models["vwf/model/aframe"].model.state.nodes;
  650. let imgAssetNode = allNodes[child.id];
  651. imgAssetNode.aframeObj.onloadeddata = function(){
  652. newNode.properties.width = child.videoWidth;
  653. newNode.properties.height = child.videoHeight;
  654. self.children.create(nodeName, newNode, function( child ) {
  655. if (avatar) child.lookAt(self.children[avatar].worldPosition)
  656. });
  657. }
  658. }
  659. });
  660. }
  661. this.createAudio = function (itemSrc, name, node, avatar) {
  662. var self = this;
  663. var position = "0 0 0";
  664. var nodeName = name;
  665. if (!nodeName) {
  666. nodeName = this.GUID();
  667. }
  668. if (avatar) {
  669. let myAvatar = this.children[avatar];
  670. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  671. if (cursorNode) {
  672. position = cursorNode.worldPosition;
  673. //console.log(position);
  674. }
  675. }
  676. let tagName = 'AUDIO-ASSET-'+ this.GUID();
  677. let tagNode = {
  678. "extends": "http://vwf.example.com/aframe/a-asset-audio-item.vwf",
  679. "properties": {
  680. "itemID": tagName,
  681. "itemSrc": itemSrc
  682. }
  683. }
  684. this.children.create(tagName, tagNode, function( child ) {
  685. // let allNodes = vwf.models["vwf/model/aframe"].model.state.nodes;
  686. // let itemAssetNode = allNodes[child.id];
  687. // itemAssetNode.aframeObj.onload = function(){
  688. // console.log(itemAssetNode);
  689. let nodeName = 'AUDIO-'+self.GUID();
  690. var position = "0 0 0";
  691. if (avatar) {
  692. let myAvatar = self.children[avatar];
  693. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  694. if (cursorNode) {
  695. position = cursorNode.worldPosition;
  696. //console.log(position);
  697. }
  698. }
  699. let newNode = self.cubeProto();
  700. newNode.properties.displayName = "audio";
  701. newNode.properties.position = position;
  702. newNode.properties.width = 0.3;
  703. newNode.properties.height = 0.3;
  704. newNode.properties.depth= 0.3;
  705. newNode.children.material.properties.opacity = 0.5;
  706. newNode.children.material.properties.color = "yellow";
  707. newNode.children.sound = {
  708. "extends": "http://vwf.example.com/aframe/a-sound-component.vwf",
  709. "type": "component",
  710. "properties": {
  711. "autoplay": true,
  712. "loop": true,
  713. "src": '#' + child.itemID
  714. }
  715. };
  716. self.children.create(nodeName, newNode, function( child ) {
  717. if (avatar) child.lookAt(self.children[avatar].worldPosition)
  718. });
  719. // }
  720. });
  721. }
  722. this.createGooglePoly = function(polyID, name, node, avatar){
  723. // all done in aframe view driver
  724. let params = [polyID, name, node, avatar];
  725. this.loadGooglePolyAsset(params)
  726. }
  727. this.loadGooglePolyAsset = function( params ) {
  728. var self = this;
  729. const API_KEY = "AIzaSyCGx2_idlUJ88yW5GBkOllIkyxJyKbEgDk";
  730. const id = params[0];
  731. const avatarID = params[3];
  732. var url = `https://poly.googleapis.com/v1/assets/${id}/?key=${API_KEY}`;
  733. var request = new XMLHttpRequest();
  734. request.open( 'GET', url, true );
  735. request.addEventListener( 'load', function ( event ) {
  736. var asset = JSON.parse( event.target.response );
  737. // asset_name.textContent = asset.displayName;
  738. // asset_author.textContent = asset.authorName;
  739. var format = asset.formats.find( format => { return format.formatType === 'OBJ'; } );
  740. if ( format !== undefined ) {
  741. var obj = format.root;
  742. var mtl = format.resources.find( resource => { return resource.url.endsWith( 'mtl' ) } );
  743. var path = obj.url.slice( 0, obj.url.indexOf( obj.relativePath ) );
  744. //const createOnNodeID = vwf.application();
  745. let mtlName = 'MTL-ASSET-'+ self.GUID();
  746. let mtlNode = {
  747. "extends": "http://vwf.example.com/aframe/a-asset-item.vwf",
  748. "properties": {
  749. "itemID": mtlName,
  750. "itemSrc": mtl.url
  751. }
  752. }
  753. self.children.create(mtlName, mtlNode, function( mtlChild ) {
  754. let objName = 'OBJ-ASSET-'+ self.GUID();
  755. let objNode = {
  756. "extends": "http://vwf.example.com/aframe/a-asset-item.vwf",
  757. "properties": {
  758. "itemID": objName,
  759. "itemSrc": obj.url
  760. }
  761. }
  762. self.children.create(objName, objNode, function( objChild ) {
  763. self.createModelObj(mtlChild.itemID, objChild.itemID, asset.displayName, avatarID);
  764. })
  765. })
  766. }
  767. } );
  768. request.send( null );
  769. }
  770. this.GUID = function () {
  771. var self = this;
  772. var S4 = function () {
  773. return Math.floor(
  774. self.random() * 0x10000 /* 65536 */
  775. ).toString(16);
  776. };
  777. return (
  778. S4() + S4() + "-" +
  779. S4() + "-" +
  780. S4() + "-" +
  781. S4() + "-" +
  782. S4() + S4() + S4()
  783. );
  784. }
  785. this.smallRandomId = function() {
  786. return '_' + this.random().toString(36).substr(2, 9);
  787. }
  788. this.assetImgProto = function () {
  789. let node = {
  790. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  791. "properties": {
  792. },
  793. }
  794. return node
  795. }
  796. this.createAssetItemImg = function(){
  797. console.log("create new asset item for img");
  798. let nodeName = "asset-item-img-" + this.smallRandomId();
  799. let newNode = {
  800. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  801. "properties": {
  802. itemID: nodeName,
  803. itemSrc: ""
  804. }
  805. }
  806. this.children.create(nodeName, newNode);
  807. }
  808. this.deleteNode = function(nodeName){
  809. let node = this.children[nodeName];
  810. if (node) this.children.delete(node);
  811. }