ascene.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. this.initialize = function () {
  2. this.future(0).clientWatch();
  3. };
  4. this.clientWatch = function () {
  5. var self = this;
  6. if (this.children.length !== 0) {
  7. var clients = this.find("doc('http://vwf.example.com/clients.vwf')")[0];
  8. if (clients !== undefined) {
  9. //console.log(clients.children);
  10. let clientsArray = [];
  11. clients.children.forEach(function (element) {
  12. clientsArray.push(element.name);
  13. });
  14. this.children.forEach(function (node) {
  15. if (node.id.indexOf('avatar-') != -1) {
  16. if (clientsArray.includes(node.id.slice(7))) {
  17. //console.log(node.id + 'is here!');
  18. } else {
  19. //console.log(node.id + " needed to delete!");
  20. self.children.delete(self.children[node.id]);
  21. //'gearvr-'
  22. let controllerVR = self.children['gearvr-' + node.id.slice(7)];
  23. if (controllerVR) {
  24. self.children.delete(controllerVR);
  25. }
  26. let wmrvR = self.children['wmrvr-right-' + node.id.slice(7)];
  27. if (wmrvR) {
  28. self.children.delete(wmrvR);
  29. }
  30. let wmrvL = self.children['wmrvr-left-' + node.id.slice(7)];
  31. if (wmrvL) {
  32. self.children.delete(wmrvL);
  33. }
  34. }
  35. }
  36. });
  37. }
  38. }
  39. this.future(5).clientWatch();
  40. };
  41. this.sphereProto = function () {
  42. let node = {
  43. "extends": "http://vwf.example.com/aframe/asphere.vwf",
  44. "properties": {
  45. "displayName": "sphere",
  46. "radius": 1,
  47. "clickable": true
  48. },
  49. children: {
  50. "material": {
  51. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  52. "type": "component",
  53. "properties":{
  54. "color": "white"
  55. }
  56. },
  57. "interpolation":
  58. {
  59. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  60. "type": "component",
  61. "properties": {
  62. "enabled": true
  63. }
  64. },
  65. "cursor-listener": {
  66. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  67. "type": "component"
  68. }
  69. },
  70. events: {
  71. "clickEvent": {
  72. "body": ""
  73. }
  74. }
  75. }
  76. return node
  77. }
  78. this.cylinderProto = function () {
  79. let node = {
  80. "extends": "http://vwf.example.com/aframe/acylinder.vwf",
  81. "properties": {
  82. "displayName": "cylinder",
  83. "radius": 1,
  84. "height": 1,
  85. "clickable": true
  86. },
  87. children: {
  88. "material": {
  89. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  90. "type": "component",
  91. "properties":{
  92. "color": "white"
  93. }
  94. },
  95. "interpolation":
  96. {
  97. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  98. "type": "component",
  99. "properties": {
  100. "enabled": true
  101. }
  102. },
  103. "cursor-listener": {
  104. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  105. "type": "component"
  106. }
  107. },
  108. events: {
  109. "clickEvent": {
  110. "body": ""
  111. }
  112. }
  113. }
  114. return node
  115. }
  116. this.coneProto = function () {
  117. let node = {
  118. "extends": "http://vwf.example.com/aframe/acone.vwf",
  119. "properties": {
  120. "displayName": "cone",
  121. "radius-bottom": 1,
  122. "radius-top": 0.01,
  123. "height": 1,
  124. "clickable": true
  125. },
  126. children: {
  127. "material": {
  128. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  129. "type": "component",
  130. "properties":{
  131. "color": "white"
  132. }
  133. },
  134. "interpolation":
  135. {
  136. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  137. "type": "component",
  138. "properties": {
  139. "enabled": true
  140. }
  141. },
  142. "cursor-listener": {
  143. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  144. "type": "component"
  145. }
  146. },
  147. events: {
  148. "clickEvent": {
  149. "body": ""
  150. }
  151. }
  152. }
  153. return node
  154. }
  155. this.textProto = function () {
  156. let node = {
  157. "extends": "http://vwf.example.com/aframe/atext.vwf",
  158. "properties": {
  159. "displayName": "text",
  160. "color": "white",
  161. "value": "Text",
  162. "side": "double",
  163. "clickable": true
  164. },
  165. children: {
  166. "interpolation":
  167. {
  168. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  169. "type": "component",
  170. "properties": {
  171. "enabled": true
  172. }
  173. },
  174. "cursor-listener": {
  175. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  176. "type": "component"
  177. }
  178. },
  179. events: {
  180. "clickEvent": {
  181. "body": ""
  182. }
  183. }
  184. }
  185. return node
  186. }
  187. this.cubeProto = function () {
  188. let node = {
  189. "extends": "http://vwf.example.com/aframe/abox.vwf",
  190. "properties": {
  191. "displayName": "cube",
  192. "height": 1,
  193. "width": 1,
  194. "depth": 1,
  195. "clickable": true
  196. },
  197. children: {
  198. "interpolation":
  199. {
  200. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  201. "type": "component",
  202. "properties": {
  203. "enabled": true
  204. }
  205. },
  206. "cursor-listener": {
  207. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  208. "type": "component"
  209. },
  210. "material": {
  211. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  212. "type": "component",
  213. "properties":{
  214. "color": "white"
  215. }
  216. }
  217. },
  218. events: {
  219. "clickEvent": {
  220. "body": ""
  221. }
  222. }
  223. }
  224. return node
  225. }
  226. this.lightProto = function (lightType) {
  227. var newLightType = lightType
  228. if (!newLightType){
  229. newLightType = "directional"
  230. }
  231. let node = {
  232. "extends": "http://vwf.example.com/aframe/alight.vwf",
  233. "properties": {
  234. "displayName": newLightType,
  235. "type": newLightType,
  236. "clickable": true
  237. },
  238. children: {
  239. "interpolation":
  240. {
  241. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  242. "type": "component",
  243. "properties": {
  244. "enabled": true
  245. }
  246. },
  247. "cursor-listener": {
  248. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  249. "type": "component"
  250. }
  251. },
  252. events: {
  253. "clickEvent": {
  254. "body": ""
  255. }
  256. }
  257. }
  258. return node
  259. }
  260. this.planeProto = function () {
  261. let node = {
  262. "extends": "http://vwf.example.com/aframe/aplane.vwf",
  263. "properties": {
  264. "displayName": "plane",
  265. "height": 1,
  266. "width": 1,
  267. "clickable": true
  268. },
  269. children: {
  270. "material": {
  271. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  272. "type": "component",
  273. "properties": {
  274. "side": "double",
  275. "color": "white"
  276. }
  277. },
  278. "interpolation":
  279. {
  280. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  281. "type": "component",
  282. "properties": {
  283. "enabled": true
  284. }
  285. },
  286. "cursor-listener": {
  287. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  288. "type": "component"
  289. }
  290. },
  291. events: {
  292. "clickEvent": {
  293. "body": ""
  294. }
  295. }
  296. }
  297. return node
  298. }
  299. this.createModelDAE = function (daeSrc, avatar) {
  300. var self = this;
  301. let daeTagName = 'DAE-ASSET-'+this.GUID();
  302. let daeTagNode = {
  303. "extends": "http://vwf.example.com/aframe/a-asset-item.vwf",
  304. "properties": {
  305. "itemID": daeTagName,
  306. "itemSrc": daeSrc,
  307. },
  308. }
  309. this.children.create(daeTagName, daeTagNode, function( child ) {
  310. let daeNodeName = 'DAE-MODEL-'+self.GUID();
  311. var position = "0 0 0";
  312. let myAvatar = self.children[avatar];
  313. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  314. if (cursorNode) {
  315. position = cursorNode.worldPosition;
  316. //console.log(position);
  317. }
  318. let daeNode = {
  319. "extends": "http://vwf.example.com/aframe/acolladamodel.vwf",
  320. "properties": {
  321. "src": '#' + child.itemID,
  322. "position": position
  323. }
  324. }
  325. self.children.create(daeNodeName, daeNode, function( child ) {
  326. if (avatar) child.lookAt(self.children[avatar].worldPosition)
  327. });
  328. });
  329. }
  330. this.createPrimitive = function (type, avatar, params, name, node) {
  331. var position = "0 0 0";
  332. var nodeName = name;
  333. let myAvatar = this.children[avatar];
  334. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  335. if (cursorNode) {
  336. position = cursorNode.worldPosition;
  337. //console.log(position);
  338. }
  339. if (!name) {
  340. nodeName = this.GUID();
  341. }
  342. var newNode = {};
  343. switch (type) {
  344. case "cube":
  345. newNode = this.cubeProto();
  346. break;
  347. case "sphere":
  348. newNode = this.sphereProto();
  349. break;
  350. case "plane":
  351. newNode = this.planeProto();
  352. break;
  353. case "light":
  354. newNode = this.lightProto(params);
  355. break;
  356. case "text":
  357. newNode = this.textProto(params);
  358. break;
  359. case "cylinder":
  360. newNode = this.cylinderProto();
  361. break;
  362. case "cone":
  363. newNode = this.coneProto();
  364. break;
  365. default:
  366. newNode = undefined;
  367. break;
  368. }
  369. var self = this;
  370. if (newNode) {
  371. newNode.properties.position = position;
  372. this.children.create(nodeName, newNode, function( child ) {
  373. child.lookAt(self.children[avatar].worldPosition)
  374. });
  375. }
  376. }
  377. this.GUID = function () {
  378. var self = this;
  379. var S4 = function () {
  380. return Math.floor(
  381. self.random() * 0x10000 /* 65536 */
  382. ).toString(16);
  383. };
  384. return (
  385. S4() + S4() + "-" +
  386. S4() + "-" +
  387. S4() + "-" +
  388. S4() + "-" +
  389. S4() + S4() + S4()
  390. );
  391. }
  392. this.smallRandomId = function() {
  393. return '_' + this.random().toString(36).substr(2, 9);
  394. }
  395. this.assetImgProto = function () {
  396. let node = {
  397. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  398. "properties": {
  399. },
  400. }
  401. return node
  402. }
  403. this.createAssetItemImg = function(){
  404. console.log("create new asset item for img");
  405. let nodeName = "asset-item-img-" + this.smallRandomId();
  406. let newNode = {
  407. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  408. "properties": {
  409. itemID: nodeName,
  410. itemSrc: ""
  411. }
  412. }
  413. this.children.create(nodeName, newNode);
  414. }
  415. this.deleteNode = function(nodeName){
  416. let node = this.children[nodeName];
  417. if (node) this.children.delete(node);
  418. }