aframe-components.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. */
  5. if (typeof AFRAME === 'undefined') {
  6. throw new Error('Component attempted to register before AFRAME was available.');
  7. }
  8. AFRAME.registerComponent('scene-utils', {
  9. init: function () {
  10. const sceneEnterVR = (e) => {
  11. //vwf_view.kernel.callMethod(vwf.application(), "enterVR");
  12. }
  13. const sceneExitVR = (e) => {
  14. //vwf_view.kernel.callMethod(vwf.application(), "exitVR");
  15. }
  16. this.el.sceneEl.addEventListener('enter-vr', sceneEnterVR);
  17. this.el.sceneEl.addEventListener('exit-vr', sceneExitVR);
  18. },
  19. update: function () {
  20. },
  21. tick: function (t) {
  22. }
  23. })
  24. AFRAME.registerComponent('linepath', {
  25. schema: {
  26. color: { default: '#000' },
  27. width: { default: 0.01 },
  28. path: {
  29. default: [
  30. { x: -0.5, y: 0, z: 0 },
  31. { x: 0.5, y: 0, z: 0 }
  32. ]
  33. // Deserialize path in the form of comma-separated vec3s: `0 0 0, 1 1 1, 2 0 3`.
  34. // parse: function (value) {
  35. // return value.split(',').map(coordinates.parse);
  36. // },
  37. // Serialize array of vec3s in case someone does setAttribute('line', 'path', [...]).
  38. // stringify: function (data) {
  39. // return data.map(coordinates.stringify).join(',');
  40. // }
  41. }
  42. },
  43. update: function () {
  44. var material = new MeshLineMaterial({
  45. color: new THREE.Color(this.data.color), //this.data.color
  46. lineWidth: this.data.width
  47. });
  48. var geometry = new THREE.Geometry();
  49. this.data.path.forEach(function (vec3) {
  50. geometry.vertices.push(
  51. new THREE.Vector3(vec3.x, vec3.y, vec3.z)
  52. );
  53. });
  54. let line = new MeshLine();
  55. line.setGeometry(geometry);
  56. //new THREE.Line(geometry, material)
  57. this.el.setObject3D('mesh', new THREE.Mesh(line.geometry, material));
  58. },
  59. remove: function () {
  60. this.el.removeObject3D('mesh');
  61. }
  62. });
  63. AFRAME.registerComponent('gizmo', {
  64. schema: {
  65. mode: { default: 'translate' }
  66. },
  67. update: function (old) {
  68. let modes = ['translate', 'rotate', 'scale'];
  69. if (!this.gizmo) {
  70. let newMode = modes.filter(el => {
  71. return el == this.data.mode
  72. })
  73. if (newMode.length !== 0) {
  74. this.mode = this.data.mode
  75. this.transformControls.setMode(this.mode)
  76. }
  77. }
  78. },
  79. init: function () {
  80. let self = this
  81. this.mode = this.data.mode
  82. let activeCamera = document.querySelector('#avatarControl').getObject3D('camera');
  83. let renderer = this.el.sceneEl.renderer;
  84. this.transformControls = new THREE.TransformControls(activeCamera, renderer.domElement);
  85. this.transformControls.attach(this.el.object3D);
  86. this.el.sceneEl.setObject3D('control-' + this.el.id, this.transformControls);
  87. this.transformControls.addEventListener('change', function (evt) {
  88. // console.log('changed');
  89. var object = self.transformControls.object;
  90. if (object === undefined) {
  91. return;
  92. }
  93. var transformMode = self.transformControls.getMode();
  94. switch (transformMode) {
  95. case 'translate':
  96. vwf_view.kernel.setProperty(object.el.id, 'position',
  97. [object.position.x, object.position.y, object.position.z])
  98. break;
  99. case 'rotate':
  100. vwf_view.kernel.setProperty(object.el.id, 'rotation',
  101. [THREE.Math.radToDeg(object.rotation.x), THREE.Math.radToDeg(object.rotation.y), THREE.Math.radToDeg(object.rotation.z)])
  102. break;
  103. case 'scale':
  104. vwf_view.kernel.setProperty(object.el.id, 'scale',
  105. [object.scale.x, object.scale.y, object.scale.z])
  106. break;
  107. }
  108. //vwf_view.kernel.fireEvent(evt.detail.target.id, "clickEvent")
  109. });
  110. },
  111. remove: function () {
  112. this.transformControls.detach();
  113. this.el.sceneEl.removeObject3D('control-' + this.el.id);
  114. },
  115. tick: function (t) {
  116. this.transformControls.update();
  117. }
  118. });
  119. AFRAME.registerComponent('cursor-listener', {
  120. init: function () {
  121. this.el.addEventListener('click', function (evt) {
  122. console.log('I was clicked at: ', evt.detail.intersection.point);
  123. let cursorID = 'cursor-avatar-' + vwf_view.kernel.moniker();
  124. if (evt.detail.cursorEl.id.includes(vwf_view.kernel.moniker())) {
  125. vwf_view.kernel.fireEvent(evt.detail.intersection.object.el.id, "clickEvent", [vwf_view.kernel.moniker()])
  126. }
  127. //vwf_view.kernel.fireEvent(evt.detail.target.id, "clickEvent")
  128. });
  129. }
  130. });
  131. AFRAME.registerComponent('raycaster-listener', {
  132. init: function () {
  133. let self = this;
  134. this.intersected = false;
  135. this.casters = {}
  136. this.el.addEventListener('raycaster-intersected', function (evt) {
  137. if (evt.detail.el.nodeName == 'A-CURSOR') {
  138. //console.log('CURSOR was intersected at: ', evt.detail.intersection.point);
  139. } else {
  140. if (self.intersected) {
  141. } else {
  142. console.log('I was intersected at: ', evt.target);//evt.detail.getIntersection().point);
  143. //evt.detail.intersection.object.el.id
  144. vwf_view.kernel.fireEvent(evt.target.id, "intersectEvent")
  145. }
  146. self.casters[evt.target.id] = evt.target;
  147. self.intersected = true;
  148. }
  149. });
  150. this.el.addEventListener('raycaster-intersected-cleared', function (evt) {
  151. if (evt.detail.el.nodeName == 'A-CURSOR') {
  152. //console.log('CURSOR was intersected at: ', evt.detail.intersection.point);
  153. } else {
  154. if (self.intersected) {
  155. console.log('Clear intersection');
  156. if (Object.entries(self.casters).length == 1 && (self.casters[evt.target.id] !== undefined)) {
  157. vwf_view.kernel.fireEvent(evt.target.id, "clearIntersectEvent")
  158. }
  159. delete self.casters[evt.target.id]
  160. } else { }
  161. self.intersected = false;
  162. }
  163. });
  164. }
  165. });
  166. AFRAME.registerComponent('envmap', {
  167. /**
  168. * Creates a new THREE.ShaderMaterial using the two shaders defined
  169. * in vertex.glsl and fragment.glsl.
  170. */
  171. init: function () {
  172. const data = this.data;
  173. //this.applyToMesh();
  174. this.el.addEventListener('model-loaded', () => this.applyToMesh());
  175. },
  176. /**
  177. * Update the ShaderMaterial when component data changes.
  178. */
  179. update: function () {
  180. },
  181. getEnvMap: function () {
  182. var path = './assets/textures/skybox2/';
  183. var format = '.jpg';
  184. var urls = [
  185. path + 'px' + format, path + 'nx' + format,
  186. path + 'py' + format, path + 'ny' + format,
  187. path + 'pz' + format, path + 'nz' + format
  188. ];
  189. envMap = new THREE.CubeTextureLoader().load(urls);
  190. envMap.format = THREE.RGBFormat;
  191. return envMap;
  192. },
  193. /**
  194. * Apply the material to the current entity.
  195. */
  196. applyToMesh: function () {
  197. const mesh = this.el.getObject3D('mesh');
  198. //var scene = mesh;
  199. var envMap = this.getEnvMap();
  200. mesh.traverse(function (node) {
  201. if (node.material) {
  202. node.material.side = THREE.BackSide;
  203. node.material.needsUpdate = true;
  204. //side = THREE.DoubleSide; break;
  205. }
  206. });
  207. mesh.traverse(function (node) {
  208. if (node.material && (node.material.isMeshStandardMaterial ||
  209. (node.material.isShaderMaterial && node.material.envMap !== undefined))) {
  210. node.material.envMap = envMap;
  211. node.material.needsUpdate = true;
  212. }
  213. });
  214. // const mesh = this.el.getObject3D('mesh');
  215. // if (mesh) {
  216. // mesh.material = this.material;
  217. // }
  218. },
  219. /**
  220. * On each frame, update the 'time' uniform in the shaders.
  221. */
  222. tick: function (t) {
  223. }
  224. })
  225. //https://threejs.org/examples/webgl_shaders_sky.html
  226. AFRAME.registerComponent('skyshader', {
  227. makeSun: function () {
  228. let sunSphere = new THREE.Mesh(
  229. new THREE.SphereBufferGeometry(20000, 16, 8),
  230. new THREE.MeshBasicMaterial({ color: 0xffffff })
  231. );
  232. sunSphere.position.y = - 700000;
  233. sunSphere.visible = true;
  234. let scene = this.el.sceneEl;
  235. this.el.sceneEl.setObject3D('sun', sunSphere);
  236. },
  237. init: function () {
  238. //let sunSphereEl = document.querySelector('a-scene').querySelector('#sun');
  239. //this.sunSphere = sunSphereEl.object3D;
  240. this.makeSun();
  241. this.sunSphere = this.el.sceneEl.getObject3D('sun');
  242. this.sky = new THREE.Sky();
  243. let scene = this.el.sceneEl;
  244. let effectController = {
  245. turbidity: 5,
  246. rayleigh: 2,
  247. mieCoefficient: 0.005,
  248. mieDirectionalG: 0.8,
  249. luminance: 1,
  250. inclination: 0, // elevation / inclination
  251. azimuth: 0.25, // Facing front,
  252. sun: ! true
  253. };
  254. let uniforms = this.sky.uniforms;
  255. uniforms.turbidity.value = effectController.turbidity;
  256. uniforms.rayleigh.value = effectController.rayleigh;
  257. uniforms.luminance.value = effectController.luminance;
  258. uniforms.mieCoefficient.value = effectController.mieCoefficient;
  259. uniforms.mieDirectionalG.value = effectController.mieDirectionalG;
  260. this.el.setObject3D('mesh', this.sky.mesh);
  261. let distance = 400000;
  262. var theta = Math.PI * (effectController.inclination - 0.5);
  263. var phi = 2 * Math.PI * (effectController.azimuth - 0.5);
  264. this.sunSphere.position.x = distance * Math.cos(phi);
  265. this.sunSphere.position.y = distance * Math.sin(phi) * Math.sin(theta);
  266. this.sunSphere.position.z = distance * Math.sin(phi) * Math.cos(theta);
  267. this.sunSphere.visible = effectController.sun;
  268. this.sky.uniforms.sunPosition.value.copy(this.sunSphere.position);
  269. },
  270. update: function () {
  271. },
  272. tick: function (t) {
  273. }
  274. })
  275. AFRAME.registerComponent('sun', {
  276. init: function () {
  277. this.sunSphere = new THREE.Mesh(
  278. new THREE.SphereBufferGeometry(20000, 16, 8),
  279. new THREE.MeshBasicMaterial({ color: 0xffffff })
  280. );
  281. this.sunSphere.position.y = - 700000;
  282. this.sunSphere.visible = true;
  283. this.el.setObject3D('mesh', this.sunSphere);
  284. },
  285. update: function () {
  286. },
  287. tick: function (t) {
  288. }
  289. })
  290. AFRAME.registerComponent('gearvrcontrol', {
  291. init: function () {
  292. var self = this;
  293. var controllerID = 'gearvr-' + vwf_view.kernel.moniker();
  294. this.el.addEventListener('triggerdown', function (event) {
  295. vwf_view.kernel.callMethod(controllerID, "triggerdown", []);
  296. });
  297. this.el.addEventListener('triggerup', function (event) {
  298. vwf_view.kernel.callMethod(controllerID, "triggerup", []);
  299. });
  300. },
  301. update: function () {
  302. },
  303. tick: function (t) {
  304. }
  305. })
  306. AFRAME.registerComponent('wmrvrcontrol', {
  307. schema: {
  308. hand: { default: 'right' }
  309. },
  310. update: function (old) {
  311. this.hand = this.data.hand;
  312. },
  313. init: function () {
  314. var self = this;
  315. this.hand = this.data.hand;
  316. var controllerID = 'wrmr-' + this.hand + '-' + vwf_view.kernel.moniker();
  317. //this.gearel = document.querySelector('#gearvrcontrol');
  318. this.el.addEventListener('triggerdown', function (event) {
  319. vwf_view.kernel.callMethod(controllerID, "triggerdown", []);
  320. });
  321. this.el.addEventListener('triggerup', function (event) {
  322. vwf_view.kernel.callMethod(controllerID, "triggerup", []);
  323. });
  324. },
  325. tick: function (t) {
  326. }
  327. })
  328. AFRAME.registerComponent('streamsound', {
  329. schema: {
  330. positional: { default: true }
  331. },
  332. init: function () {
  333. var self = this;
  334. let driver = vwf.views["vwf/view/webrtc"];
  335. this.listener = null;
  336. this.stream = null;
  337. if (!this.sound) {
  338. this.setupSound();
  339. }
  340. if (driver) {
  341. //let avatarID = 'avatar-' + vwf.moniker();
  342. let avatarID = this.el.id.slice(0, 27); //avatar-0RtnYBBTBU84OCNcAAFY
  343. let client = driver.state.clients[avatarID];
  344. if (client) {
  345. if (client.connection) {
  346. this.stream = client.connection.stream;
  347. if (this.stream) {
  348. this.audioEl = new Audio();
  349. this.audioEl.srcObject = this.stream;
  350. this.sound.setNodeSource(this.sound.context.createMediaStreamSource(this.stream));
  351. }
  352. }
  353. }
  354. }
  355. },
  356. setupSound: function () {
  357. var el = this.el;
  358. var sceneEl = el.sceneEl;
  359. if (this.sound) {
  360. el.removeObject3D(this.attrName);
  361. }
  362. if (!sceneEl.audioListener) {
  363. sceneEl.audioListener = new THREE.AudioListener();
  364. sceneEl.camera && sceneEl.camera.add(sceneEl.audioListener);
  365. sceneEl.addEventListener('camera-set-active', function (evt) {
  366. evt.detail.cameraEl.getObject3D('camera').add(sceneEl.audioListener);
  367. });
  368. }
  369. this.listener = sceneEl.audioListener;
  370. this.sound = this.data.positional
  371. ? new THREE.PositionalAudio(this.listener)
  372. : new THREE.Audio(this.listener);
  373. el.setObject3D(this.attrName, this.sound);
  374. },
  375. remove: function () {
  376. if (!this.sound) return;
  377. this.el.removeObject3D(this.attrName);
  378. if (this.stream) {
  379. this.sound.disconnect();
  380. }
  381. },
  382. update: function (old) {
  383. },
  384. tick: function (t) {
  385. }
  386. })
  387. AFRAME.registerComponent('viewoffset', {
  388. // fullWidth:
  389. // fullHeight:
  390. // xoffset:
  391. // yoffset:
  392. // width:
  393. // height:
  394. schema: {
  395. fullWidth: { default: window.innerWidth },
  396. fullHeight: { default: window.innerHeight },
  397. xoffset: { default: window.innerWidth / 2 },
  398. yoffset: { default: window.innerHeight / 2 },
  399. width: { default: window.innerWidth },
  400. height: { default: window.innerHeight }
  401. },
  402. init: function () {
  403. var self = this;
  404. this.el.sceneEl.addEventListener('loaded', setOffset);
  405. function setOffset() {
  406. this.setNewOffset();
  407. }
  408. },
  409. update: function (old) {
  410. this.fullWidth = this.data.fullWidth;
  411. this.fullHeight = this.data.fullHeight;
  412. this.xoffset = this.data.xoffset;
  413. this.yoffset = this.data.yoffset;
  414. this.width = this.data.width;
  415. this.height = this.data.height;
  416. //console.log(this.data);
  417. this.setNewOffset();
  418. },
  419. setNewOffset: function () {
  420. this.el.object3DMap.camera.setViewOffset(
  421. this.data.fullWidth,
  422. this.data.fullHeight,
  423. this.data.xoffset,
  424. this.data.yoffset,
  425. this.data.width,
  426. this.data.height)
  427. },
  428. tick: function (t) {
  429. }
  430. })