aframe-components.js 15 KB

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