ascene.js 31 KB

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