ascene.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. "color": "white",
  47. "radius": 1,
  48. "clickable": true
  49. },
  50. children: {
  51. "material": {
  52. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  53. "type": "component"
  54. },
  55. "interpolation":
  56. {
  57. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  58. "type": "component",
  59. "properties": {
  60. "enabled": true
  61. }
  62. },
  63. "cursor-listener": {
  64. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  65. "type": "component"
  66. }
  67. },
  68. events: {
  69. "clickEvent": {
  70. "body": ""
  71. }
  72. }
  73. }
  74. return node
  75. }
  76. this.textProto = function () {
  77. let node = {
  78. "extends": "http://vwf.example.com/aframe/atext.vwf",
  79. "properties": {
  80. "displayName": "text",
  81. "color": "white",
  82. "value": "Text",
  83. "side": "double",
  84. "clickable": true
  85. },
  86. children: {
  87. "material": {
  88. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  89. "type": "component"
  90. },
  91. "interpolation":
  92. {
  93. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  94. "type": "component",
  95. "properties": {
  96. "enabled": true
  97. }
  98. },
  99. "cursor-listener": {
  100. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  101. "type": "component"
  102. }
  103. },
  104. events: {
  105. "clickEvent": {
  106. "body": ""
  107. }
  108. }
  109. }
  110. return node
  111. }
  112. this.cubeProto = function () {
  113. let node = {
  114. "extends": "http://vwf.example.com/aframe/abox.vwf",
  115. "properties": {
  116. "displayName": "cube",
  117. "color": "white",
  118. "height": 1,
  119. "width": 1,
  120. "depth": 1,
  121. "clickable": true
  122. },
  123. children: {
  124. "interpolation":
  125. {
  126. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  127. "type": "component",
  128. "properties": {
  129. "enabled": true
  130. }
  131. },
  132. "cursor-listener": {
  133. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  134. "type": "component"
  135. },
  136. "material": {
  137. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  138. "type": "component"
  139. }
  140. },
  141. events: {
  142. "clickEvent": {
  143. "body": ""
  144. }
  145. }
  146. }
  147. return node
  148. }
  149. this.lightProto = function (lightType) {
  150. var newLightType = lightType
  151. if (!newLightType){
  152. newLightType = "directional"
  153. }
  154. let node = {
  155. "extends": "http://vwf.example.com/aframe/alight.vwf",
  156. "properties": {
  157. "displayName": newLightType,
  158. "type": newLightType,
  159. "clickable": true
  160. },
  161. children: {
  162. "interpolation":
  163. {
  164. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  165. "type": "component",
  166. "properties": {
  167. "enabled": true
  168. }
  169. },
  170. "cursor-listener": {
  171. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  172. "type": "component"
  173. }
  174. },
  175. events: {
  176. "clickEvent": {
  177. "body": ""
  178. }
  179. }
  180. }
  181. return node
  182. }
  183. this.planeProto = function () {
  184. let node = {
  185. "extends": "http://vwf.example.com/aframe/aplane.vwf",
  186. "properties": {
  187. "displayName": "plane",
  188. "color": "white",
  189. "height": 1,
  190. "width": 1,
  191. "clickable": true
  192. },
  193. children: {
  194. "material": {
  195. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  196. "type": "component",
  197. "properties": {
  198. "side": "double"
  199. }
  200. },
  201. "interpolation":
  202. {
  203. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  204. "type": "component",
  205. "properties": {
  206. "enabled": true
  207. }
  208. },
  209. "cursor-listener": {
  210. "extends": "http://vwf.example.com/aframe/app-cursor-listener-component.vwf",
  211. "type": "component"
  212. }
  213. },
  214. events: {
  215. "clickEvent": {
  216. "body": ""
  217. }
  218. }
  219. }
  220. return node
  221. }
  222. this.createPrimitive = function (type, avatar, params, name, node) {
  223. var position = "0 0 0";
  224. var nodeName = name;
  225. let myAvatar = this.children[avatar];
  226. let cursorNode = myAvatar.avatarNode.myHead.myCursor.vis;
  227. if (cursorNode) {
  228. position = cursorNode.worldPosition;
  229. //console.log(position);
  230. }
  231. if (!name) {
  232. nodeName = this.GUID();
  233. }
  234. var newNode = {};
  235. switch (type) {
  236. case "cube":
  237. newNode = this.cubeProto();
  238. break;
  239. case "sphere":
  240. newNode = this.sphereProto();
  241. break;
  242. case "plane":
  243. newNode = this.planeProto();
  244. break;
  245. case "light":
  246. newNode = this.lightProto(params);
  247. break;
  248. case "text":
  249. newNode = this.textProto(params);
  250. break;
  251. default:
  252. newNode = undefined;
  253. break;
  254. }
  255. var self = this;
  256. if (newNode) {
  257. newNode.properties.position = position;
  258. this.children.create(nodeName, newNode, function( child ) {
  259. // self.select( self.clickedID );
  260. child.lookAt(this.children[avatar].worldPosition)
  261. //console.log(avatar);
  262. });
  263. }
  264. }
  265. this.GUID = function () {
  266. var self = this;
  267. var S4 = function () {
  268. return Math.floor(
  269. self.random() * 0x10000 /* 65536 */
  270. ).toString(16);
  271. };
  272. return (
  273. S4() + S4() + "-" +
  274. S4() + "-" +
  275. S4() + "-" +
  276. S4() + "-" +
  277. S4() + S4() + S4()
  278. );
  279. }
  280. this.smallRandomId = function() {
  281. return '_' + this.random().toString(36).substr(2, 9);
  282. }
  283. this.assetImgProto = function () {
  284. let node = {
  285. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  286. "properties": {
  287. },
  288. }
  289. return node
  290. }
  291. this.createAssetItemImg = function(){
  292. console.log("create new asset item for img");
  293. let nodeName = "asset-item-img-" + this.smallRandomId();
  294. let newNode = {
  295. "extends": "http://vwf.example.com/aframe/a-asset-image-item.vwf",
  296. "properties": {
  297. itemID: nodeName,
  298. itemSrc: ""
  299. }
  300. }
  301. this.children.create(nodeName, newNode);
  302. }
  303. this.deleteNode = function(nodeName){
  304. let node = this.children[nodeName];
  305. if (node) this.children.delete(node);
  306. }