ascene.js 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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. "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. },
  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. "class": "clickable"
  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. "class": "clickable"
  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. "class": "clickable"
  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 displayName = name;
  448. let nodeName = this.GUID();
  449. // if (!nodeName) {
  450. // nodeName = this.GUID();
  451. // }
  452. if (avatar) {
  453. let myAvatar = this.children[avatar];
  454. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  455. if (cursorNode) {
  456. position = cursorNode.worldPosition();
  457. //console.log(position);
  458. }
  459. }
  460. var newNode = {};
  461. switch (type) {
  462. case "cube":
  463. newNode = this.cubeProto();
  464. break;
  465. case "sphere":
  466. newNode = this.sphereProto();
  467. break;
  468. case "plane":
  469. newNode = this.planeProto();
  470. break;
  471. case "light":
  472. newNode = this.lightProto(params);
  473. break;
  474. case "text":
  475. newNode = this.textProto(params);
  476. break;
  477. case "cylinder":
  478. newNode = this.cylinderProto();
  479. break;
  480. case "cone":
  481. newNode = this.coneProto();
  482. break;
  483. default:
  484. newNode = undefined;
  485. break;
  486. }
  487. var self = this;
  488. if (newNode) {
  489. newNode.properties.position = position;
  490. if (displayName) {
  491. newNode.properties.displayName = displayName;
  492. }
  493. this.children.create(nodeName, newNode, function( child ) {
  494. if (avatar) child.lookAt(self.children[avatar].worldPosition());
  495. });
  496. }
  497. }
  498. this.createCamera = function (name, node, avatar) {
  499. var position = "0 0 0";
  500. var nodeName = name;
  501. if (!nodeName) {
  502. nodeName = this.GUID();
  503. }
  504. if (avatar) {
  505. let myAvatar = this.children[avatar];
  506. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  507. if (cursorNode) {
  508. position = cursorNode.worldPosition();
  509. //console.log(position);
  510. }
  511. }
  512. var newNode = this.cameraProto();
  513. newNode.properties.displayName = "camera";
  514. var self = this;
  515. if (newNode) {
  516. newNode.properties.position = position;
  517. this.children.create(nodeName, newNode, function( child ) {
  518. if (avatar) child.lookAt(self.children[avatar].worldPosition());
  519. });
  520. }
  521. }
  522. this.createCameraWithOffset = function (name, node, avatar) {
  523. var position = "0 0 0";
  524. var nodeName = name;
  525. if (!nodeName) {
  526. nodeName = this.GUID();
  527. }
  528. if (avatar) {
  529. let myAvatar = this.children[avatar];
  530. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  531. if (cursorNode) {
  532. position = cursorNode.worldPosition();
  533. //console.log(position);
  534. }
  535. }
  536. var newNode = this.cameraProtoWithOffset();
  537. newNode.properties.displayName = "cameraWithOffset";
  538. var self = this;
  539. if (newNode) {
  540. newNode.properties.position = position;
  541. this.children.create(nodeName, newNode, function( child ) {
  542. if (avatar) child.lookAt(self.children[avatar].worldPosition());
  543. });
  544. }
  545. }
  546. this.createImage = function (imgSrc, name, node, avatar) {
  547. var self = this;
  548. var position = "0 0 0";
  549. var nodeName = name;
  550. if (!nodeName) {
  551. nodeName = this.GUID();
  552. }
  553. if (avatar) {
  554. let myAvatar = this.children[avatar];
  555. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  556. if (cursorNode) {
  557. position = cursorNode.worldPosition();
  558. //console.log(position);
  559. }
  560. }
  561. let tagName = 'IMG-ASSET-'+ this.GUID();
  562. let tagNode = {
  563. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  564. "properties": {
  565. "itemID": tagName,
  566. "itemSrc": imgSrc
  567. }
  568. }
  569. this.children.create(tagName, tagNode, function( child ) {
  570. var nodeName = 'IMAGE-'+self.GUID();
  571. var position = "0 0 0";
  572. if (avatar) {
  573. let myAvatar = self.children[avatar];
  574. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  575. if (cursorNode) {
  576. position = cursorNode.worldPosition();
  577. //console.log(position);
  578. }
  579. }
  580. let newNode = self.planeProto();
  581. newNode.properties.displayName = "image";
  582. newNode.children.material.properties.src = '#' + child.itemID;
  583. newNode.properties.position = position;
  584. newNode.properties.scale = [0.003, 0.003, 0.003];
  585. if(child.width && child.height){
  586. newNode.properties.width = child.width;
  587. newNode.properties.height = child.height;
  588. self.children.create(nodeName, newNode, function( child ) {
  589. if (avatar) child.lookAt(self.children[avatar].worldPosition())
  590. });
  591. } else {
  592. let allNodes = vwf.models["vwf/model/aframe"].model.state.nodes;
  593. let imgAssetNode = allNodes[child.id];
  594. imgAssetNode.aframeObj.onload = function(){
  595. // console.log(imgAssetNode);
  596. newNode.properties.width = child.width;
  597. newNode.properties.height = child.height;
  598. self.children.create(nodeName, newNode, function( child ) {
  599. if (avatar) child.lookAt(self.children[avatar].worldPosition())
  600. });
  601. }
  602. }
  603. });
  604. }
  605. this.createVideo = function (vidSrc, name, node, avatar) {
  606. var self = this;
  607. var position = "0 0 0";
  608. var nodeName = name;
  609. if (!nodeName) {
  610. nodeName = this.GUID();
  611. }
  612. if (avatar) {
  613. let myAvatar = this.children[avatar];
  614. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  615. if (cursorNode) {
  616. position = cursorNode.worldPosition();
  617. //console.log(position);
  618. }
  619. }
  620. let tagName = 'VIDEO-ASSET-'+ this.GUID();
  621. let tagNode = {
  622. "extends": "http://vwf.example.com/aframe/a-asset-video-item.vwf",
  623. "properties": {
  624. "itemID": tagName,
  625. "itemSrc": vidSrc
  626. }
  627. }
  628. this.children.create(tagName, tagNode, function( child ) {
  629. let nodeName = 'VIDEO-'+self.GUID();
  630. var position = "0 0 0";
  631. if (avatar) {
  632. let myAvatar = self.children[avatar];
  633. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  634. if (cursorNode) {
  635. position = cursorNode.worldPosition();
  636. //console.log(position);
  637. }
  638. }
  639. let newNode = self.planeProto();
  640. newNode.properties.displayName = "video";
  641. newNode.children.material.properties.src = '#' + child.itemID;
  642. newNode.properties.position = position;
  643. // newNode.properties.width = 3;
  644. // newNode.properties.height = 1.75;
  645. newNode.properties.scale = [0.003, 0.003, 0.003];
  646. if (child.videoWidth && child.videoHeight) {
  647. newNode.properties.width = child.videoWidth;
  648. newNode.properties.height = child.videoHeight;
  649. self.children.create(nodeName, newNode, function( child ) {
  650. if (avatar) child.lookAt(self.children[avatar].worldPosition())
  651. });
  652. } else {
  653. let allNodes = vwf.models["vwf/model/aframe"].model.state.nodes;
  654. let imgAssetNode = allNodes[child.id];
  655. imgAssetNode.aframeObj.onloadeddata = function(){
  656. newNode.properties.width = child.videoWidth;
  657. newNode.properties.height = child.videoHeight;
  658. self.children.create(nodeName, newNode, function( child ) {
  659. if (avatar) child.lookAt(self.children[avatar].worldPosition())
  660. });
  661. }
  662. }
  663. });
  664. }
  665. this.createAudio = function (itemSrc, name, node, avatar) {
  666. var self = this;
  667. var position = "0 0 0";
  668. var nodeName = name;
  669. if (!nodeName) {
  670. nodeName = this.GUID();
  671. }
  672. if (avatar) {
  673. let myAvatar = this.children[avatar];
  674. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  675. if (cursorNode) {
  676. position = cursorNode.worldPosition();
  677. //console.log(position);
  678. }
  679. }
  680. let tagName = 'AUDIO-ASSET-'+ this.GUID();
  681. let tagNode = {
  682. "extends": "http://vwf.example.com/aframe/a-asset-audio-item.vwf",
  683. "properties": {
  684. "itemID": tagName,
  685. "itemSrc": itemSrc
  686. }
  687. }
  688. this.children.create(tagName, tagNode, function( child ) {
  689. // let allNodes = vwf.models["vwf/model/aframe"].model.state.nodes;
  690. // let itemAssetNode = allNodes[child.id];
  691. // itemAssetNode.aframeObj.onload = function(){
  692. // console.log(itemAssetNode);
  693. let nodeName = 'AUDIO-'+self.GUID();
  694. var position = "0 0 0";
  695. if (avatar) {
  696. let myAvatar = self.children[avatar];
  697. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  698. if (cursorNode) {
  699. position = cursorNode.worldPosition();
  700. //console.log(position);
  701. }
  702. }
  703. let newNode = self.cubeProto();
  704. newNode.properties.displayName = "audio";
  705. newNode.properties.position = position;
  706. newNode.properties.width = 0.3;
  707. newNode.properties.height = 0.3;
  708. newNode.properties.depth= 0.3;
  709. newNode.children.material.properties.opacity = 0.5;
  710. newNode.children.material.properties.color = "yellow";
  711. newNode.children.sound = {
  712. "extends": "http://vwf.example.com/aframe/a-sound-component.vwf",
  713. "type": "component",
  714. "properties": {
  715. "autoplay": false,
  716. "loop": true,
  717. "isPlaying": false,
  718. "src": '#' + child.itemID
  719. }
  720. };
  721. self.children.create(nodeName, newNode, function( child ) {
  722. if (avatar) child.lookAt(self.children[avatar].worldPosition())
  723. });
  724. // }
  725. });
  726. }
  727. this.createGooglePoly = function(polyID, name, node, avatar){
  728. // all done in aframe view driver
  729. let params = [polyID, name, node, avatar];
  730. this.loadGooglePolyAsset(params)
  731. }
  732. this.loadGooglePolyAsset = function( params ) {
  733. var self = this;
  734. const API_KEY = "AIzaSyCGx2_idlUJ88yW5GBkOllIkyxJyKbEgDk";
  735. const id = params[0];
  736. const avatarID = params[3];
  737. var url = `https://poly.googleapis.com/v1/assets/${id}/?key=${API_KEY}`;
  738. var request = new XMLHttpRequest();
  739. request.open( 'GET', url, true );
  740. request.addEventListener( 'load', function ( event ) {
  741. var asset = JSON.parse( event.target.response );
  742. // asset_name.textContent = asset.displayName;
  743. // asset_author.textContent = asset.authorName;
  744. var format = asset.formats.find( format => { return format.formatType === 'OBJ'; } );
  745. if ( format !== undefined ) {
  746. var obj = format.root;
  747. var mtl = format.resources.find( resource => { return resource.url.endsWith( 'mtl' ) } );
  748. var path = obj.url.slice( 0, obj.url.indexOf( obj.relativePath ) );
  749. //const createOnNodeID = vwf.application();
  750. let mtlName = 'MTL-ASSET-'+ self.GUID();
  751. let mtlNode = {
  752. "extends": "http://vwf.example.com/aframe/a-asset-item.vwf",
  753. "properties": {
  754. "itemID": mtlName,
  755. "itemSrc": mtl.url
  756. }
  757. }
  758. self.children.create(mtlName, mtlNode, function( mtlChild ) {
  759. let objName = 'OBJ-ASSET-'+ self.GUID();
  760. let objNode = {
  761. "extends": "http://vwf.example.com/aframe/a-asset-item.vwf",
  762. "properties": {
  763. "itemID": objName,
  764. "itemSrc": obj.url
  765. }
  766. }
  767. self.children.create(objName, objNode, function( objChild ) {
  768. self.createModelObj(mtlChild.itemID, objChild.itemID, asset.displayName, avatarID);
  769. })
  770. })
  771. }
  772. } );
  773. request.send( null );
  774. }
  775. this.GUID = function () {
  776. var self = this;
  777. var S4 = function () {
  778. return Math.floor(
  779. self.random() * 0x10000 /* 65536 */
  780. ).toString(16);
  781. };
  782. return (
  783. S4() + S4() + "-" +
  784. S4() + "-" +
  785. S4() + "-" +
  786. S4() + "-" +
  787. S4() + S4() + S4()
  788. );
  789. }
  790. this.smallRandomId = function() {
  791. return '_' + this.random().toString(36).substr(2, 9);
  792. }
  793. this.assetImgProto = function () {
  794. let node = {
  795. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  796. "properties": {
  797. },
  798. }
  799. return node
  800. }
  801. this.createAssetItemImg = function(){
  802. console.log("create new asset item for img");
  803. let nodeName = "asset-item-img-" + this.smallRandomId();
  804. let newNode = {
  805. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  806. "properties": {
  807. itemID: nodeName,
  808. itemSrc: ""
  809. }
  810. }
  811. this.children.create(nodeName, newNode);
  812. }
  813. this.deleteNode = function(nodeName){
  814. let node = this.children[nodeName];
  815. if (node) this.children.delete(node);
  816. }
  817. this.enterVR = function(){
  818. console.log("ENTER VR");
  819. }
  820. this.exitVR = function(){
  821. console.log("EXIT VR");
  822. }