ascene.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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.createClock = function (parentName, avatar) {
  404. var self = this;
  405. let nodeName = 'clock-' + this.smallRandomId();
  406. let node = {
  407. "extends": "proxy/objects/clock.vwf",
  408. "properties": {
  409. "displayName": nodeName
  410. }
  411. }
  412. let rootNode = parentName ? this.getChildByName(parentName) : this;
  413. rootNode.children.create(nodeName, node, function( child ) {
  414. if (avatar) child.placeInFrontOf(avatar, -2);
  415. child.init();
  416. child.run();
  417. })
  418. }
  419. this.createLegoBoost = function (boostID, parentName) {
  420. var self = this;
  421. let nodeName = 'legoboost-' + this.smallRandomId();
  422. let legoBoostNode = {
  423. "extends": "proxy/objects/legoboost.vwf",
  424. "properties": {
  425. "boostID": boostID,
  426. "position": "0 1 0",
  427. "displayName": boostID,
  428. "tracking": false
  429. }
  430. }
  431. let rootNode = parentName ? this.getChildByName(parentName) : this;
  432. rootNode.children.create(nodeName, legoBoostNode, function( child ) {
  433. child.createVisual();
  434. child.trackLego();
  435. })
  436. }
  437. this.createModel = function (modelType, modelSrc, avatar) {
  438. var self = this;
  439. let tagName = modelType + '-ASSET-'+ this.GUID();
  440. let tagNode = {
  441. "extends": "proxy/aframe/a-asset-item.vwf",
  442. "properties": {
  443. "itemID": tagName,
  444. "itemSrc": modelSrc
  445. }
  446. }
  447. this.children.create(tagName, tagNode, function( child ) {
  448. let nodeName = modelType + '-MODEL-'+self.GUID();
  449. const protos = {
  450. DAE: "proxy/aframe/acolladamodel.vwf",
  451. OBJ: "proxy/aframe/aobjmodel.vwf",
  452. GLTF: "proxy/aframe/agltfmodel.vwf"
  453. }
  454. let extendsName = Object.entries(protos).filter(el => el[0] == modelType)[0];
  455. let modelNode = {
  456. "extends": extendsName[1],
  457. "properties": {
  458. "src": '#' + child.itemID
  459. },
  460. children:{
  461. "interpolation":
  462. {
  463. "extends": "proxy/aframe/interpolation-component.vwf",
  464. "type": "component",
  465. "properties": {
  466. "enabled": true
  467. }
  468. }
  469. }
  470. }
  471. self.children.create(nodeName, modelNode, function( child ) {
  472. //if (avatar) child.lookAt(self.children[avatar].worldPosition())
  473. if (avatar) child.placeInFrontOf(avatar, -2)
  474. });
  475. });
  476. }
  477. this.createAssetResource = function(resType, resSrc){
  478. var self = this;
  479. const protos = {
  480. IMG: "proxy/aframe/a-asset-image-item.vwf",
  481. AUDIO: "proxy/aframe/a-asset-audio-item.vwf",
  482. VIDEO: "proxy/aframe/a-asset-video-item.vwf",
  483. ITEM: "proxy/aframe/a-asset-item.vwf"
  484. };
  485. let extendsName = Object.entries(protos).filter(el => el[0] == resType)[0];
  486. let tagName = resType + '-ASSET-'+ this.GUID();
  487. let tagNode = {
  488. "extends": extendsName[1],
  489. "properties": {
  490. "itemID": tagName,
  491. "itemSrc": resSrc
  492. }
  493. }
  494. this.children.create(tagName, tagNode);
  495. }
  496. this.createDrawNode = function(node, name, color, width, pos) {
  497. let newNode = {
  498. extends: "proxy/aframe/aentity.vwf",
  499. properties: {
  500. position: pos
  501. },
  502. children: {
  503. linepath: {
  504. extends: "proxy/aframe/linepath.vwf",
  505. properties: {
  506. color: color,
  507. path: [],
  508. width: width
  509. }
  510. }
  511. }
  512. }
  513. node.children.create(name, newNode);
  514. }
  515. this.createPrimitive = function (type, params, name, node, avatar) {
  516. var displayName = name;
  517. let nodeName = this.GUID();
  518. // if (!nodeName) {
  519. // nodeName = this.GUID();
  520. // }
  521. var newNode = {};
  522. switch (type) {
  523. case "cube":
  524. newNode = this.cubeProto();
  525. break;
  526. case "sphere":
  527. newNode = this.sphereProto();
  528. break;
  529. case "plane":
  530. newNode = this.planeProto();
  531. break;
  532. case "light":
  533. newNode = this.lightProto(params.type);
  534. break;
  535. case "text":
  536. newNode = this.textProto(params.text);
  537. break;
  538. case "cylinder":
  539. newNode = this.cylinderProto();
  540. break;
  541. case "cone":
  542. newNode = this.coneProto();
  543. break;
  544. default:
  545. newNode = undefined;
  546. break;
  547. }
  548. var self = this;
  549. if (newNode) {
  550. if (displayName) {
  551. newNode.properties.displayName = displayName;
  552. }
  553. this.children.create(nodeName, newNode, function( child ) {
  554. if (avatar) child.placeInFrontOf(avatar, -2)
  555. //child.lookAt(self.children[avatar].worldPosition());
  556. if (type == "text"){
  557. child.properties.font = "/drivers/model/aframe/fonts/custom-msdf.json"
  558. }
  559. });
  560. }
  561. }
  562. this.createMirror = function (name, node, avatar) {
  563. var nodeName = name;
  564. if (!nodeName) {
  565. nodeName = this.GUID();
  566. }
  567. // if (avatar) {
  568. // let myAvatar = this.children[avatar];
  569. // let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  570. // if (cursorNode) {
  571. // position = cursorNode.worldPosition();
  572. // //console.log(position);
  573. // }
  574. // }
  575. var newNode = this.mirrorProto();
  576. newNode.properties.displayName = "mirror";
  577. var self = this;
  578. if (newNode) {
  579. this.children.create(nodeName, newNode, function( child ) {
  580. if (avatar) child.placeInFrontOf(avatar, -2)
  581. //if (avatar) child.lookAt(self.children[avatar].worldPosition());
  582. });
  583. }
  584. }
  585. this.createCamera = function (name, node, avatar) {
  586. var nodeName = name;
  587. if (!nodeName) {
  588. nodeName = this.GUID();
  589. }
  590. var newNode = this.cameraProto();
  591. newNode.properties.displayName = "camera";
  592. var self = this;
  593. if (newNode) {
  594. this.children.create(nodeName, newNode, function( child ) {
  595. if (avatar) child.placeInFrontOf(avatar, -2)
  596. //if (avatar) child.lookAt(self.children[avatar].worldPosition());
  597. });
  598. }
  599. }
  600. this.createCameraWithOffset = function (name, node, avatar) {
  601. var position = "0 0 0";
  602. var nodeName = name;
  603. if (!nodeName) {
  604. nodeName = this.GUID();
  605. }
  606. var newNode = this.cameraProtoWithOffset();
  607. newNode.properties.displayName = "cameraWithOffset";
  608. var self = this;
  609. if (newNode) {
  610. newNode.properties.position = position;
  611. this.children.create(nodeName, newNode, function( child ) {
  612. //if (avatar) child.lookAt(self.children[avatar].worldPosition());
  613. if (avatar) child.placeInFrontOf(avatar, -2)
  614. });
  615. }
  616. }
  617. this.createImage = function (imgSrc, name, node, avatar) {
  618. var self = this;
  619. var nodeName = name;
  620. if (!nodeName) {
  621. nodeName = this.GUID();
  622. }
  623. let tagName = 'IMG-ASSET-'+ this.GUID();
  624. let tagNode = {
  625. "extends": "proxy/aframe/a-asset-image-item.vwf",
  626. "properties": {
  627. "itemID": tagName,
  628. "itemSrc": imgSrc
  629. }
  630. }
  631. this.children.create(tagName, tagNode, function( child ) {
  632. var nodeName = 'IMAGE-'+self.GUID();
  633. let newNode = self.planeProto();
  634. newNode.properties.displayName = "image";
  635. newNode.children.material.properties.src = '#' + child.itemID;
  636. //newNode.properties.position = position;
  637. newNode.properties.scale = [0.003, 0.003, 0.003];
  638. if(child.width && child.height){
  639. newNode.properties.width = child.width;
  640. newNode.properties.height = child.height;
  641. self.children.create(nodeName, newNode, function( child ) {
  642. //if (avatar) child.lookAt(self.children[avatar].worldPosition())
  643. if (avatar) child.placeInFrontOf(avatar, -2)
  644. });
  645. } else {
  646. let allNodes = vwf.models["/drivers/model/aframe"].model.state.nodes;
  647. let imgAssetNode = allNodes[child.id];
  648. imgAssetNode.aframeObj.onload = function(){
  649. // console.log(imgAssetNode);
  650. newNode.properties.width = child.width;
  651. newNode.properties.height = child.height;
  652. self.children.create(nodeName, newNode, function( child ) {
  653. //if (avatar) child.lookAt(self.children[avatar].worldPosition())
  654. if (avatar) child.placeInFrontOf(avatar, -2)
  655. });
  656. }
  657. }
  658. });
  659. }
  660. this.createVideo = function (vidSrc, name, node, avatar) {
  661. var self = this;
  662. var nodeName = name;
  663. if (!nodeName) {
  664. nodeName = this.GUID();
  665. }
  666. let tagName = 'VIDEO-ASSET-'+ this.GUID();
  667. let tagNode = {
  668. "extends": "proxy/aframe/a-asset-video-item.vwf",
  669. "properties": {
  670. "itemID": tagName,
  671. "itemSrc": vidSrc
  672. }
  673. }
  674. this.children.create(tagName, tagNode, function( child ) {
  675. let nodeName = 'VIDEO-'+self.GUID();
  676. let newNode = self.planeProto();
  677. newNode.properties.displayName = "video";
  678. newNode.children.material.properties.src = '#' + child.itemID;
  679. //newNode.properties.position = position;
  680. // newNode.properties.width = 3;
  681. // newNode.properties.height = 1.75;
  682. newNode.properties.scale = [0.003, 0.003, 0.003];
  683. if (child.videoWidth && child.videoHeight) {
  684. newNode.properties.width = child.videoWidth;
  685. newNode.properties.height = child.videoHeight;
  686. self.children.create(nodeName, newNode, function( child ) {
  687. //if (avatar) child.lookAt(self.children[avatar].worldPosition())
  688. if (avatar) child.placeInFrontOf(avatar, -2)
  689. });
  690. } else {
  691. let allNodes = vwf.models["/drivers/model/aframe"].model.state.nodes;
  692. let imgAssetNode = allNodes[child.id];
  693. imgAssetNode.aframeObj.onloadeddata = function(){
  694. newNode.properties.width = child.videoWidth;
  695. newNode.properties.height = child.videoHeight;
  696. self.children.create(nodeName, newNode, function( child ) {
  697. //if (avatar) child.lookAt(self.children[avatar].worldPosition())
  698. if (avatar) child.placeInFrontOf(avatar, -2)
  699. });
  700. }
  701. }
  702. });
  703. }
  704. this.createAudio = function (itemSrc, name, node, avatar) {
  705. var self = this;
  706. var nodeName = name;
  707. if (!nodeName) {
  708. nodeName = this.GUID();
  709. }
  710. let tagName = 'AUDIO-ASSET-'+ this.GUID();
  711. let tagNode = {
  712. "extends": "proxy/aframe/a-asset-audio-item.vwf",
  713. "properties": {
  714. "itemID": tagName,
  715. "itemSrc": itemSrc
  716. }
  717. }
  718. this.children.create(tagName, tagNode, function( child ) {
  719. // let allNodes = vwf.models["vwf/model/aframe"].model.state.nodes;
  720. // let itemAssetNode = allNodes[child.id];
  721. // itemAssetNode.aframeObj.onload = function(){
  722. // console.log(itemAssetNode);
  723. let nodeName = 'AUDIO-'+self.GUID();
  724. let newNode = self.cubeProto();
  725. newNode.properties.displayName = "audio";
  726. // newNode.properties.position = position;
  727. newNode.properties.width = 0.3;
  728. newNode.properties.height = 0.3;
  729. newNode.properties.depth= 0.3;
  730. newNode.children.material.properties.opacity = 0.5;
  731. newNode.children.material.properties.color = "yellow";
  732. newNode.children.sound = {
  733. "extends": "proxy/aframe/a-sound-component.vwf",
  734. "type": "component",
  735. "properties": {
  736. "autoplay": false,
  737. "loop": true,
  738. "isPlaying": false,
  739. "src": '#' + child.itemID
  740. }
  741. };
  742. self.children.create(nodeName, newNode, function( child ) {
  743. //if (avatar) child.lookAt(self.children[avatar].worldPosition())
  744. if (avatar) child.placeInFrontOf(avatar, -2)
  745. });
  746. // }
  747. });
  748. }
  749. this.createGooglePoly = function(polyID, name, node, avatar){
  750. // all done in aframe view driver
  751. let params = [polyID, name, node, avatar];
  752. this.loadGooglePolyAsset(params)
  753. }
  754. this.loadGooglePolyAsset = function( params ) {
  755. var self = this;
  756. const API_KEY = "AIzaSyCGx2_idlUJ88yW5GBkOllIkyxJyKbEgDk";
  757. const id = params[0];
  758. const avatarID = params[3];
  759. var url = `https://poly.googleapis.com/v1/assets/${id}/?key=${API_KEY}`;
  760. var request = new XMLHttpRequest();
  761. request.open( 'GET', url, true );
  762. request.addEventListener( 'load', function ( event ) {
  763. var asset = JSON.parse( event.target.response );
  764. // asset_name.textContent = asset.displayName;
  765. // asset_author.textContent = asset.authorName;
  766. var format = asset.formats.find( format => { return format.formatType === 'OBJ'; } );
  767. if ( format !== undefined ) {
  768. var obj = format.root;
  769. var mtl = format.resources.find( resource => { return resource.url.endsWith( 'mtl' ) } );
  770. var path = obj.url.slice( 0, obj.url.indexOf( obj.relativePath ) );
  771. //const createOnNodeID = vwf.application();
  772. let mtlName = 'MTL-ASSET-'+ self.GUID();
  773. let mtlNode = {
  774. "extends": "proxy/aframe/a-asset-item.vwf",
  775. "properties": {
  776. "itemID": mtlName,
  777. "itemSrc": mtl.url
  778. }
  779. }
  780. self.children.create(mtlName, mtlNode, function( mtlChild ) {
  781. let objName = 'OBJ-ASSET-'+ self.GUID();
  782. let objNode = {
  783. "extends": "proxy/aframe/a-asset-item.vwf",
  784. "properties": {
  785. "itemID": objName,
  786. "itemSrc": obj.url
  787. }
  788. }
  789. self.children.create(objName, objNode, function( objChild ) {
  790. self.createModelObj(mtlChild.itemID, objChild.itemID, asset.displayName, avatarID);
  791. })
  792. })
  793. }
  794. } );
  795. request.send( null );
  796. }
  797. this.GUID = function () {
  798. var self = this;
  799. var S4 = function () {
  800. return Math.floor(
  801. self.random() * 0x10000 /* 65536 */
  802. ).toString(16);
  803. };
  804. return (
  805. S4() + S4() + "-" +
  806. S4() + "-" +
  807. S4() + "-" +
  808. S4() + "-" +
  809. S4() + S4() + S4()
  810. );
  811. }
  812. this.smallRandomId = function() {
  813. return '_' + this.random().toString(36).substr(2, 9);
  814. }
  815. this.assetImgProto = function () {
  816. let node = {
  817. "extends": "proxy/aframe/a-asset-image-item.vwf",
  818. "properties": {
  819. },
  820. }
  821. return node
  822. }
  823. this.createAssetItemImg = function(){
  824. console.log("create new asset item for img");
  825. let nodeName = "asset-item-img-" + this.smallRandomId();
  826. let newNode = {
  827. "extends": "proxy/aframe/a-asset-image-item.vwf",
  828. "properties": {
  829. itemID: nodeName,
  830. itemSrc: ""
  831. }
  832. }
  833. this.children.create(nodeName, newNode);
  834. }
  835. this.deleteNode = function(nodeName){
  836. let node = this.children[nodeName];
  837. if (node) this.children.delete(node);
  838. }
  839. this.enterVR = function(){
  840. console.log("ENTER VR");
  841. }
  842. this.exitVR = function(){
  843. console.log("EXIT VR");
  844. }
  845. this.getChildByName = function (name) {
  846. let nodes = this.children.filter(el => el.displayName == name);
  847. return nodes[0]
  848. }
  849. this.getAllChildsByName = function (name) {
  850. let nodes = this.children.filter(el => el.displayName == name);
  851. return nodes
  852. }
  853. this.getDefaultXRCostume = function(){
  854. let myColor = "white";
  855. let defaultXRCostume = {
  856. "extends": "proxy/aframe/aentity.vwf",
  857. "properties": {
  858. displayName: "defaultXRCostume",
  859. "position": "0 0 0",
  860. "avatarColor": myColor,
  861. "mousedown_state": false,
  862. "triggerdown_state": false
  863. // "height": 0.01,
  864. // "width": 0.01,
  865. // "depth": 1
  866. },
  867. "methods":{
  868. mousedownAction:{
  869. body:`
  870. this.mousedown_state = true;
  871. if(elID){
  872. //let node = this.findNodeByID(elID);
  873. vwf.callMethod(elID, "mousedownAction",[])
  874. }
  875. `,
  876. parameters:["point", "elID"],
  877. type: "application/javascript"
  878. },
  879. mouseupAction:{
  880. body:`
  881. if(elID){
  882. //let node = this.findNodeByID(elID);
  883. vwf.callMethod(elID, "mouseupAction",[])
  884. }
  885. this.mousedown_state = false;
  886. `,
  887. parameters:["point", "elID"],
  888. type: "application/javascript"
  889. },
  890. triggerdownAction:{
  891. body:`
  892. //do on trigger down
  893. this.triggerdown_state = true;
  894. this.cursorVisual.color = "red";
  895. if(elID){
  896. //let node = this.findNodeByID(elID);
  897. let pointData = AFRAME.utils.coordinates.parse(point);
  898. vwf.callMethod(elID, "triggerdownAction",[pointData])
  899. }
  900. `,
  901. parameters:["point", "elID"],
  902. type: "application/javascript"
  903. },
  904. triggerupAction:{
  905. body:`
  906. //do on trigger up
  907. this.cursorVisual.color = this.cursorVisual.avatarColor;
  908. if(elID){
  909. //let node = this.findNodeByID(elID);
  910. let pointData = AFRAME.utils.coordinates.parse(point);
  911. vwf.callMethod(elID, "triggerupAction",[pointData])
  912. }
  913. this.triggerdown_state = false;
  914. `,
  915. parameters:["point", "elID"],
  916. type: "application/javascript"
  917. },
  918. onMove:{
  919. body:`
  920. if(this.mousedown_state || this.triggerdown_state){
  921. if(idata){
  922. //console.log('Move POINT: ', idata.point, + ' on ' + idata.elID);
  923. let point = AFRAME.utils.coordinates.parse(idata.point);
  924. vwf.callMethod(idata.elID, "moveAction",[point])
  925. }
  926. }
  927. `,
  928. parameters:["idata"],
  929. type: "application/javascript"
  930. }
  931. },
  932. children: {
  933. "cursorVisual": {
  934. "extends": "proxy/objects/cursorVisual.vwf",
  935. "type": "",
  936. "properties": {
  937. "color": myColor,
  938. "position": "0 0 0"
  939. }
  940. },
  941. "aabb-collider": {
  942. "extends": "proxy/aframe/aabb-collider-component.vwf",
  943. "type": "component",
  944. "properties": {
  945. debug: false,
  946. interval: 10,
  947. objects: ".hit"
  948. }
  949. }
  950. }
  951. }
  952. return defaultXRCostume
  953. }