aframe-components.js 15 KB

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