ascene.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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": false,
  712. "loop": true,
  713. "isPlaying": false,
  714. "src": '#' + child.itemID
  715. }
  716. };
  717. self.children.create(nodeName, newNode, function( child ) {
  718. if (avatar) child.lookAt(self.children[avatar].worldPosition())
  719. });
  720. // }
  721. });
  722. }
  723. this.createGooglePoly = function(polyID, name, node, avatar){
  724. // all done in aframe view driver
  725. let params = [polyID, name, node, avatar];
  726. this.loadGooglePolyAsset(params)
  727. }
  728. this.loadGooglePolyAsset = function( params ) {
  729. var self = this;
  730. const API_KEY = "AIzaSyCGx2_idlUJ88yW5GBkOllIkyxJyKbEgDk";
  731. const id = params[0];
  732. const avatarID = params[3];
  733. var url = `https://poly.googleapis.com/v1/assets/${id}/?key=${API_KEY}`;
  734. var request = new XMLHttpRequest();
  735. request.open( 'GET', url, true );
  736. request.addEventListener( 'load', function ( event ) {
  737. var asset = JSON.parse( event.target.response );
  738. // asset_name.textContent = asset.displayName;
  739. // asset_author.textContent = asset.authorName;
  740. var format = asset.formats.find( format => { return format.formatType === 'OBJ'; } );
  741. if ( format !== undefined ) {
  742. var obj = format.root;
  743. var mtl = format.resources.find( resource => { return resource.url.endsWith( 'mtl' ) } );
  744. var path = obj.url.slice( 0, obj.url.indexOf( obj.relativePath ) );
  745. //const createOnNodeID = vwf.application();
  746. let mtlName = 'MTL-ASSET-'+ self.GUID();
  747. let mtlNode = {
  748. "extends": "http://vwf.example.com/aframe/a-asset-item.vwf",
  749. "properties": {
  750. "itemID": mtlName,
  751. "itemSrc": mtl.url
  752. }
  753. }
  754. self.children.create(mtlName, mtlNode, function( mtlChild ) {
  755. let objName = 'OBJ-ASSET-'+ self.GUID();
  756. let objNode = {
  757. "extends": "http://vwf.example.com/aframe/a-asset-item.vwf",
  758. "properties": {
  759. "itemID": objName,
  760. "itemSrc": obj.url
  761. }
  762. }
  763. self.children.create(objName, objNode, function( objChild ) {
  764. self.createModelObj(mtlChild.itemID, objChild.itemID, asset.displayName, avatarID);
  765. })
  766. })
  767. }
  768. } );
  769. request.send( null );
  770. }
  771. this.GUID = function () {
  772. var self = this;
  773. var S4 = function () {
  774. return Math.floor(
  775. self.random() * 0x10000 /* 65536 */
  776. ).toString(16);
  777. };
  778. return (
  779. S4() + S4() + "-" +
  780. S4() + "-" +
  781. S4() + "-" +
  782. S4() + "-" +
  783. S4() + S4() + S4()
  784. );
  785. }
  786. this.smallRandomId = function() {
  787. return '_' + this.random().toString(36).substr(2, 9);
  788. }
  789. this.assetImgProto = function () {
  790. let node = {
  791. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  792. "properties": {
  793. },
  794. }
  795. return node
  796. }
  797. this.createAssetItemImg = function(){
  798. console.log("create new asset item for img");
  799. let nodeName = "asset-item-img-" + this.smallRandomId();
  800. let newNode = {
  801. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  802. "properties": {
  803. itemID: nodeName,
  804. itemSrc: ""
  805. }
  806. }
  807. this.children.create(nodeName, newNode);
  808. }
  809. this.deleteNode = function(nodeName){
  810. let node = this.children[nodeName];
  811. if (node) this.children.delete(node);
  812. }
  813. this.enterVR = function(){
  814. console.log("ENTER VR");
  815. }
  816. this.exitVR = function(){
  817. console.log("EXIT VR");
  818. }