ascene.js 7.4 KB

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