ascene.js 28 KB

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