ascene.js 32 KB

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