aframe-components.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. // let q = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  101. // (object.rotation.x),
  102. // (object.rotation.y),
  103. // (object.rotation.z), 'XYZ'
  104. // ));
  105. // let angle = (new THREE.Euler()).setFromQuaternion(q, 'YXZ');
  106. // vwf_view.kernel.setProperty(object.el.id, 'rotation', [THREE.Math.radToDeg(angle.x), THREE.Math.radToDeg(angle.y), THREE.Math.radToDeg(angle.z)])
  107. vwf_view.kernel.setProperty(object.el.id, 'rotation',
  108. [THREE.Math.radToDeg(object.rotation.x), THREE.Math.radToDeg(object.rotation.y), THREE.Math.radToDeg(object.rotation.z)])
  109. break;
  110. case 'scale':
  111. vwf_view.kernel.setProperty(object.el.id, 'scale',
  112. [object.scale.x, object.scale.y, object.scale.z])
  113. break;
  114. }
  115. //vwf_view.kernel.fireEvent(evt.detail.target.id, "clickEvent")
  116. });
  117. },
  118. remove: function () {
  119. this.transformControls.detach();
  120. this.el.sceneEl.removeObject3D('control-' + this.el.id);
  121. },
  122. tick: function (t) {
  123. this.transformControls.update();
  124. }
  125. });
  126. AFRAME.registerComponent('cursor-listener', {
  127. init: function () {
  128. this.el.addEventListener('click', function (evt) {
  129. console.log('I was clicked at: ', evt.detail.intersection.point);
  130. let cursorID = 'cursor-avatar-' + vwf_view.kernel.moniker();
  131. if (evt.detail.cursorEl.id.includes(vwf_view.kernel.moniker())) {
  132. vwf_view.kernel.fireEvent(evt.detail.intersection.object.el.id, "clickEvent", [vwf_view.kernel.moniker()])
  133. }
  134. //vwf_view.kernel.fireEvent(evt.detail.target.id, "clickEvent")
  135. });
  136. }
  137. });
  138. AFRAME.registerComponent('raycaster-listener', {
  139. init: function () {
  140. let self = this;
  141. this.intersected = false;
  142. this.casters = {}
  143. this.el.addEventListener('raycaster-intersected', function (evt) {
  144. if (evt.detail.el.nodeName == 'A-CURSOR') {
  145. //console.log('CURSOR was intersected at: ', evt.detail.intersection.point);
  146. } else {
  147. if (self.intersected) {
  148. } else {
  149. //evt.detail.intersection.object.el.id
  150. if (evt.detail.el.id.includes(vwf_view.kernel.moniker())) {
  151. console.log('I was intersected at: ', evt.target);//evt.detail.getIntersection().point);
  152. vwf_view.kernel.fireEvent(evt.target.id, "intersectEvent", [vwf_view.kernel.moniker()]);
  153. }
  154. }
  155. self.casters[evt.target.id] = evt.target;
  156. self.intersected = true;
  157. }
  158. });
  159. this.el.addEventListener('raycaster-intersected-cleared', function (evt) {
  160. if (evt.detail.el.nodeName == 'A-CURSOR') {
  161. //console.log('CURSOR was intersected at: ', evt.detail.intersection.point);
  162. } else {
  163. if (self.intersected) {
  164. console.log('Clear intersection');
  165. if (Object.entries(self.casters).length == 1 && (self.casters[evt.target.id] !== undefined)) {
  166. if (evt.detail.el.id.includes(vwf_view.kernel.moniker())) {
  167. vwf_view.kernel.fireEvent(evt.target.id, "clearIntersectEvent", [vwf_view.kernel.moniker()])
  168. }
  169. }
  170. delete self.casters[evt.target.id]
  171. } else { }
  172. self.intersected = false;
  173. }
  174. });
  175. }
  176. });
  177. AFRAME.registerComponent('envmap', {
  178. /**
  179. * Creates a new THREE.ShaderMaterial using the two shaders defined
  180. * in vertex.glsl and fragment.glsl.
  181. */
  182. init: function () {
  183. const data = this.data;
  184. //this.applyToMesh();
  185. this.el.addEventListener('model-loaded', () => this.applyToMesh());
  186. },
  187. /**
  188. * Update the ShaderMaterial when component data changes.
  189. */
  190. update: function () {
  191. },
  192. getEnvMap: function () {
  193. var path = './assets/textures/skybox2/';
  194. var format = '.jpg';
  195. var urls = [
  196. path + 'px' + format, path + 'nx' + format,
  197. path + 'py' + format, path + 'ny' + format,
  198. path + 'pz' + format, path + 'nz' + format
  199. ];
  200. envMap = new THREE.CubeTextureLoader().load(urls);
  201. envMap.format = THREE.RGBFormat;
  202. return envMap;
  203. },
  204. /**
  205. * Apply the material to the current entity.
  206. */
  207. applyToMesh: function () {
  208. const mesh = this.el.getObject3D('mesh');
  209. //var scene = mesh;
  210. var envMap = this.getEnvMap();
  211. mesh.traverse(function (node) {
  212. if (node.material) {
  213. node.material.side = THREE.BackSide;
  214. node.material.needsUpdate = true;
  215. //side = THREE.DoubleSide; break;
  216. }
  217. });
  218. mesh.traverse(function (node) {
  219. if (node.material && (node.material.isMeshStandardMaterial ||
  220. (node.material.isShaderMaterial && node.material.envMap !== undefined))) {
  221. node.material.envMap = envMap;
  222. node.material.needsUpdate = true;
  223. }
  224. });
  225. // const mesh = this.el.getObject3D('mesh');
  226. // if (mesh) {
  227. // mesh.material = this.material;
  228. // }
  229. },
  230. /**
  231. * On each frame, update the 'time' uniform in the shaders.
  232. */
  233. tick: function (t) {
  234. }
  235. })
  236. //https://threejs.org/examples/webgl_shaders_sky.html
  237. AFRAME.registerComponent('skyshader', {
  238. makeSun: function () {
  239. let sunSphere = new THREE.Mesh(
  240. new THREE.SphereBufferGeometry(20000, 16, 8),
  241. new THREE.MeshBasicMaterial({ color: 0xffffff })
  242. );
  243. sunSphere.position.y = - 700000;
  244. sunSphere.visible = true;
  245. let scene = this.el.sceneEl;
  246. this.el.sceneEl.setObject3D('sun', sunSphere);
  247. },
  248. init: function () {
  249. //let sunSphereEl = document.querySelector('a-scene').querySelector('#sun');
  250. //this.sunSphere = sunSphereEl.object3D;
  251. this.makeSun();
  252. this.sunSphere = this.el.sceneEl.getObject3D('sun');
  253. this.sky = new THREE.Sky();
  254. let scene = this.el.sceneEl;
  255. let effectController = {
  256. turbidity: 5,
  257. rayleigh: 2,
  258. mieCoefficient: 0.005,
  259. mieDirectionalG: 0.8,
  260. luminance: 1,
  261. inclination: 0, // elevation / inclination
  262. azimuth: 0.25, // Facing front,
  263. sun: ! true
  264. };
  265. let uniforms = this.sky.uniforms;
  266. uniforms.turbidity.value = effectController.turbidity;
  267. uniforms.rayleigh.value = effectController.rayleigh;
  268. uniforms.luminance.value = effectController.luminance;
  269. uniforms.mieCoefficient.value = effectController.mieCoefficient;
  270. uniforms.mieDirectionalG.value = effectController.mieDirectionalG;
  271. this.el.setObject3D('mesh', this.sky.mesh);
  272. let distance = 400000;
  273. var theta = Math.PI * (effectController.inclination - 0.5);
  274. var phi = 2 * Math.PI * (effectController.azimuth - 0.5);
  275. this.sunSphere.position.x = distance * Math.cos(phi);
  276. this.sunSphere.position.y = distance * Math.sin(phi) * Math.sin(theta);
  277. this.sunSphere.position.z = distance * Math.sin(phi) * Math.cos(theta);
  278. this.sunSphere.visible = effectController.sun;
  279. this.sky.uniforms.sunPosition.value.copy(this.sunSphere.position);
  280. },
  281. update: function () {
  282. },
  283. tick: function (t) {
  284. }
  285. })
  286. AFRAME.registerComponent('sun', {
  287. init: function () {
  288. this.sunSphere = new THREE.Mesh(
  289. new THREE.SphereBufferGeometry(20000, 16, 8),
  290. new THREE.MeshBasicMaterial({ color: 0xffffff })
  291. );
  292. this.sunSphere.position.y = - 700000;
  293. this.sunSphere.visible = true;
  294. this.el.setObject3D('mesh', this.sunSphere);
  295. },
  296. update: function () {
  297. },
  298. tick: function (t) {
  299. }
  300. })
  301. AFRAME.registerComponent('gearvrcontrol', {
  302. init: function () {
  303. var self = this;
  304. var controllerID = 'gearvr-' + vwf_view.kernel.moniker();
  305. this.el.addEventListener('triggerdown', function (event) {
  306. vwf_view.kernel.callMethod(controllerID, "triggerdown", []);
  307. });
  308. this.el.addEventListener('triggerup', function (event) {
  309. vwf_view.kernel.callMethod(controllerID, "triggerup", []);
  310. });
  311. },
  312. update: function () {
  313. },
  314. tick: function (t) {
  315. }
  316. })
  317. AFRAME.registerComponent('wmrvrcontrol', {
  318. schema: {
  319. hand: { default: 'right' }
  320. },
  321. update: function (old) {
  322. this.hand = this.data.hand;
  323. },
  324. init: function () {
  325. var self = this;
  326. this.hand = this.data.hand;
  327. var controllerID = 'wrmr-' + this.hand + '-' + vwf_view.kernel.moniker();
  328. //this.gearel = document.querySelector('#gearvrcontrol');
  329. this.el.addEventListener('triggerdown', function (event) {
  330. vwf_view.kernel.callMethod(controllerID, "triggerdown", []);
  331. });
  332. this.el.addEventListener('triggerup', function (event) {
  333. vwf_view.kernel.callMethod(controllerID, "triggerup", []);
  334. });
  335. },
  336. tick: function (t) {
  337. }
  338. })
  339. AFRAME.registerComponent('streamsound', {
  340. schema: {
  341. positional: { default: true }
  342. },
  343. init: function () {
  344. var self = this;
  345. let driver = vwf.views["vwf/view/webrtc"];
  346. this.listener = null;
  347. this.stream = null;
  348. if (!this.sound) {
  349. this.setupSound();
  350. }
  351. if (driver) {
  352. //let avatarID = 'avatar-' + vwf.moniker();
  353. let avatarID = this.el.id.slice(0, 27); //avatar-0RtnYBBTBU84OCNcAAFY
  354. let client = driver.state.clients[avatarID];
  355. if (client) {
  356. if (client.connection) {
  357. this.stream = client.connection.stream;
  358. if (this.stream) {
  359. this.audioEl = new Audio();
  360. this.audioEl.srcObject = this.stream;
  361. this.sound.setNodeSource(this.sound.context.createMediaStreamSource(this.stream));
  362. }
  363. }
  364. }
  365. }
  366. },
  367. setupSound: function () {
  368. var el = this.el;
  369. var sceneEl = el.sceneEl;
  370. if (this.sound) {
  371. el.removeObject3D(this.attrName);
  372. }
  373. if (!sceneEl.audioListener) {
  374. sceneEl.audioListener = new THREE.AudioListener();
  375. sceneEl.camera && sceneEl.camera.add(sceneEl.audioListener);
  376. sceneEl.addEventListener('camera-set-active', function (evt) {
  377. evt.detail.cameraEl.getObject3D('camera').add(sceneEl.audioListener);
  378. });
  379. }
  380. this.listener = sceneEl.audioListener;
  381. this.sound = this.data.positional
  382. ? new THREE.PositionalAudio(this.listener)
  383. : new THREE.Audio(this.listener);
  384. el.setObject3D(this.attrName, this.sound);
  385. },
  386. remove: function () {
  387. if (!this.sound) return;
  388. this.el.removeObject3D(this.attrName);
  389. if (this.stream) {
  390. this.sound.disconnect();
  391. }
  392. },
  393. update: function (old) {
  394. },
  395. tick: function (t) {
  396. }
  397. })
  398. AFRAME.registerComponent('viewoffset', {
  399. // fullWidth:
  400. // fullHeight:
  401. // xoffset:
  402. // yoffset:
  403. // width:
  404. // height:
  405. schema: {
  406. fullWidth: { default: window.innerWidth },
  407. fullHeight: { default: window.innerHeight },
  408. xoffset: { default: window.innerWidth / 2 },
  409. yoffset: { default: window.innerHeight / 2 },
  410. width: { default: window.innerWidth },
  411. height: { default: window.innerHeight }
  412. },
  413. init: function () {
  414. var self = this;
  415. this.el.sceneEl.addEventListener('loaded', setOffset);
  416. function setOffset() {
  417. this.setNewOffset();
  418. }
  419. },
  420. update: function (old) {
  421. this.fullWidth = this.data.fullWidth;
  422. this.fullHeight = this.data.fullHeight;
  423. this.xoffset = this.data.xoffset;
  424. this.yoffset = this.data.yoffset;
  425. this.width = this.data.width;
  426. this.height = this.data.height;
  427. //console.log(this.data);
  428. this.setNewOffset();
  429. },
  430. setNewOffset: function () {
  431. this.el.object3DMap.camera.setViewOffset(
  432. this.data.fullWidth,
  433. this.data.fullHeight,
  434. this.data.xoffset,
  435. this.data.yoffset,
  436. this.data.width,
  437. this.data.height)
  438. },
  439. tick: function (t) {
  440. }
  441. })