aframe-components.js 16 KB

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