aframe-components.js 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  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('avatar', {
  9. init: function () {
  10. },
  11. tick: function () {
  12. }
  13. })
  14. AFRAME.registerComponent('desktop-controls', {
  15. init: function () {
  16. this.mouseMoveEvent();
  17. },
  18. mouseMoveEvent(){
  19. let self = this;
  20. this.avatar = document.querySelector('#avatarControl');
  21. this.cursor = document.querySelector ('#mouse-' + vwf_view.kernel.moniker());
  22. this.domElement = this.el.sceneEl.renderer.domElement;
  23. this.raycaster = new THREE.Raycaster();
  24. this.mouse = new THREE.Vector3();
  25. this.handDirection = new THREE.Vector3();
  26. // this.plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 5); //this.plane = new THREE.Plane();
  27. // var helper = new THREE.PlaneHelper( this.plane, 1, 0xffff00 );
  28. // this.el.sceneEl.object3D.add( helper );
  29. //helper
  30. // this.arrayHelper = new THREE.ArrowHelper( this.raycaster.ray.direction, this.raycaster.ray.origin, 100, Math.random() * 0xffffff );
  31. // this.el.sceneEl.object3D.add(this.arrayHelper);
  32. let controllerID = 'mouse-' + vwf_view.kernel.moniker();
  33. this.domElement.addEventListener('mousedown', function (e) {
  34. if(!this.xrcontroller){
  35. this.xrcontroller = document.querySelector('#'+ controllerID);
  36. }
  37. if(this.xrcontroller) {
  38. let intersection = this.xrcontroller.components.raycaster.intersections[0];
  39. let point = intersection ? intersection.point : null;
  40. let elID = intersection ? intersection.object.el.id : null;
  41. if(point) console.log('Point to: ', point);
  42. if (e.button == 1) {
  43. vwf_view.kernel.callMethod(controllerID, "triggerdown", [point, elID]);
  44. }
  45. if (e.button == 0) {
  46. vwf_view.kernel.callMethod(controllerID, "mousedown", [point, elID]);
  47. }
  48. }
  49. });
  50. this.domElement.addEventListener('mouseup', function (e) {
  51. if(!this.xrcontroller){
  52. this.xrcontroller = document.querySelector('#'+ controllerID);
  53. }
  54. if(this.xrcontroller) {
  55. let intersection = this.xrcontroller.components.raycaster.intersections[0];
  56. let point = intersection ? intersection.point : null;
  57. let elID = intersection ? intersection.object.el.id : null;
  58. if(point) console.log('Point to: ', point);
  59. if (e.button == 1) {
  60. vwf_view.kernel.callMethod(controllerID, "triggerup", [point, elID]);
  61. }
  62. if (e.button == 0) {
  63. vwf_view.kernel.callMethod(controllerID, "mouseup", [point, elID]);
  64. }
  65. }
  66. });
  67. const onDocumentMouseMove = (event) => {
  68. event.preventDefault();
  69. if(this.camera){
  70. let rect = this.domElement.getBoundingClientRect();
  71. this.mouse.x = ( ( event.clientX - rect.left ) / rect.width ) * 2 - 1;
  72. this.mouse.y = - ( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;
  73. this.raycaster.setFromCamera( this.mouse, this.camera );
  74. this.handDirection.copy(this.raycaster.ray.direction);
  75. this.el.object3D.lookAt(this.handDirection.negate());
  76. if(!self.xrcontroller){
  77. self.xrcontroller = document.querySelector('#'+ controllerID);
  78. }
  79. if(self.xrcontroller){
  80. let intersection = this.xrcontroller.components.raycaster.intersections[0];
  81. let point = intersection ? intersection.point : null;
  82. let elID = intersection ? intersection.object.el.id : null;
  83. if(point) {
  84. //console.log('Point to: ', point, ' intersect ', elID);
  85. self.intersectionData = {
  86. point: point,
  87. elID: elID
  88. }
  89. }
  90. else {
  91. self.intersectionData = null;
  92. }
  93. }
  94. }
  95. }
  96. this.domElement.addEventListener( 'mousemove', onDocumentMouseMove, false );
  97. },
  98. tick: function () {
  99. if(!this.camera){
  100. this.camera = document.querySelector('#avatarControl').getObject3D('camera');
  101. }
  102. }
  103. })
  104. AFRAME.registerComponent('scene-utils', {
  105. init: function () {
  106. this.mirrors = {};
  107. this.interpolationComponents = {};
  108. this.checkRenderer();
  109. //document.querySelector('a-scene').renderer.capabilities
  110. //this.setCameraControl();
  111. const sceneEnterVR = (e) => {
  112. let driver = vwf.views["/drivers/view/aframe"];
  113. // let avatarEl = document.querySelector('#avatarControlParent');
  114. let avatarID = 'avatar-' + vwf_view.kernel.moniker();
  115. driver.hmd = true;
  116. if (driver.threeDoFMobile || _app.config.d3DoF ) {
  117. driver.threeDoF = true;
  118. //vwf_view.kernel.callMethod(avatarID, "updateYPositionForXR", [0.0]);
  119. } else if (driver.sixDoFMobile || driver.sixDoFDesktop || _app.config.d6DoF ) {
  120. driver.sixDoF = true;
  121. }
  122. }
  123. const sceneExitVR = (e) => {
  124. let driver = vwf.views["/drivers/view/aframe"];
  125. let avatarID = 'avatar-' + vwf_view.kernel.moniker();
  126. driver.hmd = false;
  127. if (driver.threeDoFMobile || _app.config.d3DoF ) {
  128. driver.threeDoF = false;
  129. //vwf_view.kernel.callMethod(avatarID, "updateYPositionForXR", [-1.6]);
  130. } else if (driver.sixDoFMobile || driver.sixDoFDesktop || _app.config.d6DoF ) {
  131. driver.sixDoF = false;
  132. }
  133. }
  134. this.el.sceneEl.addEventListener('enter-vr', sceneEnterVR);
  135. this.el.sceneEl.addEventListener('exit-vr', sceneExitVR);
  136. },
  137. setCameraControl(){
  138. let avatarEl = document.querySelector('#avatarControl');
  139. document.addEventListener('keydown', (event) => {
  140. const keyName = event.key;
  141. if (keyName === 'Alt') {
  142. // do not alert when only Control key is pressed.
  143. console.log(keyName, ' pressed');
  144. avatarEl.setAttribute('look-controls', 'enabled', true)
  145. return;
  146. }
  147. })
  148. document.addEventListener('keyup', (event) => {
  149. const keyName = event.key;
  150. if (keyName === 'Alt') {
  151. // do not alert when only Control key is pressed.
  152. console.log(keyName, ' released');
  153. avatarEl.setAttribute('look-controls', 'enabled', false)
  154. return;
  155. }
  156. })
  157. },
  158. checkRenderer: function(){
  159. if(this.el.sceneEl.renderer){
  160. //FIX For Safari WebGL1 renderer context
  161. let webgl2 = this.el.sceneEl.renderer.capabilities.isWebGL2;
  162. if(!webgl2){
  163. AFRAME.registerShader('msdf1', {
  164. schema: {
  165. alphaTest: {type: 'number', is: 'uniform', default: 0.5},
  166. color: {type: 'color', is: 'uniform', default: 'white'},
  167. map: {type: 'map', is: 'uniform'},
  168. negate: {type: 'boolean', is: 'uniform', default: true},
  169. opacity: {type: 'number', is: 'uniform', default: 1.0}
  170. },
  171. raw: true,
  172. vertexShader: [
  173. 'attribute vec2 uv;',
  174. 'attribute vec3 position;',
  175. 'uniform mat4 projectionMatrix;',
  176. 'uniform mat4 modelViewMatrix;',
  177. 'varying vec2 vUV;',
  178. 'void main(void) {',
  179. ' gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);',
  180. ' vUV = uv;',
  181. '}'
  182. ].join('\n'),
  183. fragmentShader: [
  184. '#ifdef GL_OES_standard_derivatives',
  185. '#extension GL_OES_standard_derivatives: enable',
  186. '#endif',
  187. 'precision highp float;',
  188. 'uniform bool negate;',
  189. 'uniform float alphaTest;',
  190. 'uniform float opacity;',
  191. 'uniform sampler2D map;',
  192. 'uniform vec3 color;',
  193. 'varying vec2 vUV;',
  194. 'float median(float r, float g, float b) {',
  195. ' return max(min(r, g), min(max(r, g), b));',
  196. '}',
  197. // FIXME: Experimentally determined constants.
  198. '#define BIG_ENOUGH 0.001',
  199. '#define MODIFIED_ALPHATEST (0.02 * isBigEnough / BIG_ENOUGH)',
  200. 'void main() {',
  201. ' vec3 sampleColor = texture2D(map, vUV).rgb;',
  202. ' if (negate) { sampleColor = 1.0 - sampleColor; }',
  203. ' float sigDist = median(sampleColor.r, sampleColor.g, sampleColor.b) - 0.5;',
  204. ' float alpha = clamp(sigDist / fwidth(sigDist) + 0.5, 0.0, 1.0);',
  205. ' float dscale = 0.353505;',
  206. ' vec2 duv = dscale * (dFdx(vUV) + dFdy(vUV));',
  207. ' float isBigEnough = max(abs(duv.x), abs(duv.y));',
  208. // When texel is too small, blend raw alpha value rather than supersampling.
  209. // FIXME: Experimentally determined constant.
  210. ' // Do modified alpha test.',
  211. ' if (isBigEnough > BIG_ENOUGH) {',
  212. ' float ratio = BIG_ENOUGH / isBigEnough;',
  213. ' alpha = ratio * alpha + (1.0 - ratio) * (sigDist + 0.5);',
  214. ' }',
  215. ' // Do modified alpha test.',
  216. ' if (alpha < alphaTest * MODIFIED_ALPHATEST) { discard; return; }',
  217. ' gl_FragColor = vec4(color.xyz, alpha * opacity);',
  218. '}'
  219. ].join('\n')
  220. });
  221. }
  222. }
  223. },
  224. update: function () {
  225. },
  226. tick: function (t, dt) {
  227. Object.values(this.mirrors).forEach(el => {
  228. el.mirrorTick.call(el)
  229. });
  230. // Object.values(this.interpolationComponents).forEach(el => {
  231. // el.interpolationTick.call(el, t, dt)
  232. // });
  233. },
  234. tock: function (t, dt) {
  235. // Object.values(this.interpolationComponents).forEach(el => {
  236. // el.interpolationTock.call(el, t, dt)
  237. // });
  238. }
  239. })
  240. AFRAME.registerComponent('linepath', {
  241. schema: {
  242. color: { default: '#000' },
  243. transparent: { default: false },
  244. opacity: {default: 1 },
  245. width: { default: 0.01 },
  246. taper: { default: false },
  247. path: {
  248. default: [
  249. { x: -0.5, y: 0, z: 0 },
  250. { x: 0.5, y: 0, z: 0 }
  251. ]
  252. // Deserialize path in the form of comma-separated vec3s: `0 0 0, 1 1 1, 2 0 3`.
  253. // parse: function (value) {
  254. // return value.split(',').map(coordinates.parse);
  255. // },
  256. // Serialize array of vec3s in case someone does setAttribute('line', 'path', [...]).
  257. // stringify: function (data) {
  258. // return data.map(coordinates.stringify).join(',');
  259. // }
  260. }
  261. },
  262. update: function () {
  263. //TODO: parse array too
  264. var material = new MeshLineMaterial({
  265. color: new THREE.Color(this.data.color), //this.data.color
  266. lineWidth: this.data.width,
  267. transparent: this.data.transparent,
  268. opacity: this.data.opacity
  269. });
  270. let points = [];
  271. this.data.path.forEach(function (vec3) {
  272. points.push(
  273. new THREE.Vector3(vec3.x, vec3.y, vec3.z)
  274. );
  275. });
  276. // var geometry = new THREE.Geometry();
  277. // this.data.path.forEach(function (vec3) {
  278. // geometry.vertices.push(
  279. // new THREE.Vector3(vec3.x, vec3.y, vec3.z)
  280. // );
  281. // });
  282. let line = new MeshLine();
  283. if(this.data.taper){
  284. line.setPoints(points, p => 1 - p); //p => 2 + Math.sin(50 * p)
  285. } else {
  286. line.setPoints(points);
  287. }
  288. //line.setGeometry(geometry);
  289. //new THREE.Line(geometry, material)
  290. this.el.setObject3D('mesh', new THREE.Mesh(line, material)); //new THREE.Mesh(line.geometry, material)
  291. },
  292. remove: function () {
  293. this.el.removeObject3D('mesh');
  294. }
  295. });
  296. AFRAME.registerComponent('gizmo', {
  297. schema: {
  298. mode: { default: 'translate' }
  299. },
  300. update: function (old) {
  301. let modes = ['translate', 'rotate', 'scale'];
  302. if (!this.gizmo) {
  303. let newMode = modes.filter(el => {
  304. return el == this.data.mode
  305. })
  306. if (newMode.length !== 0) {
  307. this.mode = this.data.mode
  308. this.transformControls.setMode(this.mode)
  309. }
  310. }
  311. },
  312. init: function () {
  313. let self = this
  314. this.mode = this.data.mode
  315. let activeCamera = document.querySelector('#avatarControl').getObject3D('camera');
  316. let renderer = this.el.sceneEl.renderer;
  317. this.transformControls = new THREE.TransformControls(activeCamera, renderer.domElement);
  318. this.transformControls.attach(this.el.object3D);
  319. this.el.sceneEl.setObject3D('control-' + this.el.id, this.transformControls);
  320. this.transformControls.addEventListener('change', function (evt) {
  321. // console.log('changed');
  322. var object = self.transformControls.object;
  323. if (object === undefined) {
  324. return;
  325. }
  326. var transformMode = self.transformControls.getMode();
  327. switch (transformMode) {
  328. case 'translate':
  329. vwf_view.kernel.setProperty(object.el.id, 'position',
  330. [object.position.x, object.position.y, object.position.z])
  331. break;
  332. case 'rotate':
  333. // let q = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  334. // (object.rotation.x),
  335. // (object.rotation.y),
  336. // (object.rotation.z), 'XYZ'
  337. // ));
  338. // let angle = (new THREE.Euler()).setFromQuaternion(q, 'YXZ');
  339. // vwf_view.kernel.setProperty(object.el.id, 'rotation', [THREE.Math.radToDeg(angle.x), THREE.Math.radToDeg(angle.y), THREE.Math.radToDeg(angle.z)])
  340. vwf_view.kernel.setProperty(object.el.id, 'rotation',
  341. [THREE.Math.radToDeg(object.rotation.x), THREE.Math.radToDeg(object.rotation.y), THREE.Math.radToDeg(object.rotation.z)])
  342. break;
  343. case 'scale':
  344. vwf_view.kernel.setProperty(object.el.id, 'scale',
  345. [object.scale.x, object.scale.y, object.scale.z])
  346. break;
  347. }
  348. //vwf_view.kernel.fireEvent(evt.detail.target.id, "clickEvent")
  349. });
  350. },
  351. remove: function () {
  352. this.transformControls.detach();
  353. this.el.sceneEl.removeObject3D('control-' + this.el.id);
  354. },
  355. // tick: function (t) {
  356. // // this.transformControls.update();
  357. // }
  358. });
  359. AFRAME.registerComponent('cursor-listener', {
  360. init: function () {
  361. let self = this;
  362. this.el.addEventListener('click', function (evt) {
  363. console.log('I was clicked at: ', evt.detail.intersection.point);
  364. //let cursorID = 'cursor-avatar-' + vwf_view.kernel.moniker();
  365. if (evt.detail.cursorEl.id.includes(vwf_view.kernel.moniker())) {
  366. vwf_view.kernel.fireEvent(evt.detail.intersection.object.el.id, "clickEvent", [evt.detail.intersection.point]);
  367. }
  368. //vwf_view.kernel.fireEvent(evt.detail.target.id, "clickEvent")
  369. });
  370. // this.el.addEventListener('mousedown', function (evt) {
  371. // console.log('mousedown at: ', evt.detail.intersection.point);
  372. // if (evt.detail.cursorEl.id.includes(vwf_view.kernel.moniker())) {
  373. // let point = evt.detail.intersection.point;
  374. // // let locPoint = new THREE.Vector3();
  375. // // locPoint.copy(evt.detail.intersection.point);
  376. // // self.el.object3D.parent.worldToLocal(locPoint);
  377. // // let point = AFRAME.utils.coordinates.stringify(locPoint);
  378. // //vwf_view.kernel.callMethod('mouse-'+vwf_view.kernel.moniker(), "showHandSelection", [point]);
  379. // vwf_view.kernel.fireEvent(evt.detail.intersection.object.el.id, "mousedownEvent", [point]);
  380. // }
  381. // })
  382. // this.el.addEventListener('mouseup', function (evt) {
  383. // let intersection = evt.detail.intersection;
  384. // if(intersection)
  385. // {
  386. // console.log('mouseup at: ', evt.detail.intersection.point);
  387. // vwf_view.kernel.fireEvent(evt.detail.intersection.object.el.id, "mouseupEvent", [evt.detail.intersection.point]);
  388. // } else {
  389. // console.log('mouseup');
  390. // //vwf_view.kernel.fireEvent(evt.detail.intersection.object.el.id, "mouseupEvent", []);
  391. // }
  392. // //vwf_view.kernel.callMethod('mouse-'+vwf_view.kernel.moniker(), "resetHandSelection", []);
  393. // })
  394. }
  395. });
  396. AFRAME.registerComponent('aabb-collider-listener', {
  397. // If the target collidable object is moving, set <a-entity data-aabb-collider-dynamic> on the target. By default, collidable objects are presumed to be static for performance purposes.
  398. init: function () {
  399. // let self = this;
  400. // this.me = vwf_view.kernel.moniker();
  401. this.el.addEventListener('hitstart', function (evt) {
  402. vwf_view.kernel.fireEvent(evt.target.id, "hitstartEvent");
  403. })
  404. this.el.addEventListener('hitend', function (evt) {
  405. vwf_view.kernel.fireEvent(evt.target.id, "hitendEvent");
  406. })
  407. }
  408. });
  409. AFRAME.registerComponent('raycaster-listener', {
  410. init: function () {
  411. let self = this;
  412. this.intersected = false;
  413. this.casters = {}
  414. this.me = vwf_view.kernel.moniker();
  415. this.driver = vwf.views["/drivers/view/aframe"];
  416. this.el.addEventListener('raycaster-intersected', function (evt) {
  417. if (evt.detail.el.nodeName == 'A-CURSOR') {
  418. //console.log('CURSOR was intersected at: ', evt.detail.intersection.point);
  419. } else {
  420. if (self.intersected) {
  421. } else {
  422. let point = evt.detail.getIntersection(evt.target).point;
  423. //console.log('I was intersected at: ', evt.target, ' point: ', point);//evt.detail.getIntersection().point);
  424. vwf_view.kernel.fireEvent(evt.target.id, "intersectEvent", [point]);
  425. //vwf.callMethod(evt.target.id, "intersectEventMethod", [point]);
  426. }
  427. self.casters[evt.target.id] = evt.target;
  428. self.intersected = true;
  429. }
  430. });
  431. this.el.addEventListener('raycaster-intersected-cleared', function (evt) {
  432. if (evt.detail.el.nodeName == 'A-CURSOR') {
  433. //console.log('CURSOR was intersected at: ', evt.detail.intersection.point);
  434. } else {
  435. if (self.intersected) {
  436. //console.log('Clear intersection');
  437. if (Object.entries(self.casters).length == 1 && (self.casters[evt.target.id] !== undefined)) {
  438. vwf_view.kernel.fireEvent(evt.target.id, "clearIntersectEvent")
  439. //vwf.callMethod(evt.target.id, "clearIntersectEventMethod", [])
  440. }
  441. delete self.casters[evt.target.id]
  442. } else { }
  443. self.intersected = false;
  444. }
  445. });
  446. }
  447. });
  448. AFRAME.registerComponent('envmap', {
  449. /**
  450. * Creates a new THREE.ShaderMaterial using the two shaders defined
  451. * in vertex.glsl and fragment.glsl.
  452. */
  453. init: function () {
  454. const data = this.data;
  455. //this.applyToMesh();
  456. this.el.addEventListener('model-loaded', () => this.applyToMesh());
  457. },
  458. /**
  459. * Update the ShaderMaterial when component data changes.
  460. */
  461. update: function () {
  462. },
  463. getEnvMap: function () {
  464. var path = './assets/textures/skybox2/';
  465. var format = '.jpg';
  466. var urls = [
  467. path + 'px' + format, path + 'nx' + format,
  468. path + 'py' + format, path + 'ny' + format,
  469. path + 'pz' + format, path + 'nz' + format
  470. ];
  471. envMap = new THREE.CubeTextureLoader().load(urls);
  472. envMap.format = THREE.RGBFormat;
  473. return envMap;
  474. },
  475. /**
  476. * Apply the material to the current entity.
  477. */
  478. applyToMesh: function () {
  479. const mesh = this.el.getObject3D('mesh');
  480. //var scene = mesh;
  481. var envMap = this.getEnvMap();
  482. mesh.traverse(function (node) {
  483. if (node.material) {
  484. node.material.side = THREE.BackSide;
  485. node.material.needsUpdate = true;
  486. //side = THREE.DoubleSide; break;
  487. }
  488. });
  489. mesh.traverse(function (node) {
  490. if (node.material && (node.material.isMeshStandardMaterial ||
  491. (node.material.isShaderMaterial && node.material.envMap !== undefined))) {
  492. node.material.envMap = envMap;
  493. node.material.needsUpdate = true;
  494. }
  495. });
  496. // const mesh = this.el.getObject3D('mesh');
  497. // if (mesh) {
  498. // mesh.material = this.material;
  499. // }
  500. },
  501. /**
  502. * On each frame, update the 'time' uniform in the shaders.
  503. */
  504. // tick: function (t) {
  505. // }
  506. })
  507. //https://threejs.org/examples/webgl_shaders_sky.html
  508. AFRAME.registerComponent('skyshader', {
  509. makeSun: function () {
  510. let sunSphere = new THREE.Mesh(
  511. new THREE.SphereBufferGeometry(20000, 16, 8),
  512. new THREE.MeshBasicMaterial({ color: 0xffffff })
  513. );
  514. sunSphere.position.y = - 700000;
  515. sunSphere.visible = true;
  516. let scene = this.el.sceneEl;
  517. this.el.sceneEl.setObject3D('sun', sunSphere);
  518. },
  519. init: function () {
  520. //let sunSphereEl = document.querySelector('a-scene').querySelector('#sun');
  521. //this.sunSphere = sunSphereEl.object3D;
  522. this.makeSun();
  523. this.sunSphere = this.el.sceneEl.getObject3D('sun');
  524. this.sky = new THREE.Sky();
  525. let scene = this.el.sceneEl;
  526. let effectController = {
  527. turbidity: 5,
  528. rayleigh: 2,
  529. mieCoefficient: 0.005,
  530. mieDirectionalG: 0.8,
  531. luminance: 1,
  532. inclination: 0, // elevation / inclination
  533. azimuth: 0.25, // Facing front,
  534. sun: ! true
  535. };
  536. let uniforms = this.sky.uniforms;
  537. uniforms.turbidity.value = effectController.turbidity;
  538. uniforms.rayleigh.value = effectController.rayleigh;
  539. uniforms.luminance.value = effectController.luminance;
  540. uniforms.mieCoefficient.value = effectController.mieCoefficient;
  541. uniforms.mieDirectionalG.value = effectController.mieDirectionalG;
  542. this.el.setObject3D('mesh', this.sky.mesh);
  543. let distance = 400000;
  544. var theta = Math.PI * (effectController.inclination - 0.5);
  545. var phi = 2 * Math.PI * (effectController.azimuth - 0.5);
  546. this.sunSphere.position.x = distance * Math.cos(phi);
  547. this.sunSphere.position.y = distance * Math.sin(phi) * Math.sin(theta);
  548. this.sunSphere.position.z = distance * Math.sin(phi) * Math.cos(theta);
  549. this.sunSphere.visible = effectController.sun;
  550. this.sky.uniforms.sunPosition.value.copy(this.sunSphere.position);
  551. },
  552. update: function () {
  553. },
  554. // tick: function (t) {
  555. // }
  556. })
  557. AFRAME.registerComponent('sun', {
  558. init: function () {
  559. this.sunSphere = new THREE.Mesh(
  560. new THREE.SphereBufferGeometry(20000, 16, 8),
  561. new THREE.MeshBasicMaterial({ color: 0xffffff })
  562. );
  563. this.sunSphere.position.y = - 700000;
  564. this.sunSphere.visible = true;
  565. this.el.setObject3D('mesh', this.sunSphere);
  566. },
  567. update: function () {
  568. },
  569. // tick: function (t) {
  570. // }
  571. })
  572. AFRAME.registerComponent('gearvrcontrol', {
  573. init: function () {
  574. var self = this;
  575. var controllerID = 'gearvr-' + vwf_view.kernel.moniker();
  576. this.el.addEventListener('triggerdown', function (event) {
  577. if(!self.xrcontroller){
  578. self.xrcontroller = document.querySelector('#'+ self.controllerID);
  579. }
  580. let intersection = self.xrcontroller.components.raycaster.intersections[0];
  581. let point = intersection ? intersection.point : null;
  582. let elID = intersection ? intersection.object.el.id : null;
  583. if(point) console.log('Point to: ', point);
  584. vwf_view.kernel.callMethod(controllerID, "triggerdown", [point, elID]);
  585. });
  586. this.el.addEventListener('triggerup', function (event) {
  587. vwf_view.kernel.callMethod(controllerID, "triggerup", [point, elID]);
  588. });
  589. //X-buttorn Pressed
  590. this.el.addEventListener('buttondown', function (e) { //xbuttondown
  591. //Start pointing position to teleport
  592. //let buttonID = e.detail.id;
  593. //let avatarID = 'avatar-' + vwf_view.kernel.moniker();
  594. //vwf_view.kernel.callMethod(vwf.application(), "createPrimitive", ['text', buttonID, 'debug', null, avatarID]);
  595. if(e.detail.id == 2){
  596. this.emit('teleportstart');
  597. }
  598. });
  599. //X-buttorn Released
  600. this.el.addEventListener('buttonup', function (e) { //xbuttonup
  601. //Jump to pointed position
  602. if(e.detail.id == 2){
  603. this.emit('teleportend');
  604. }
  605. });
  606. },
  607. update: function () {
  608. },
  609. // tick: function (t) {
  610. // }
  611. })
  612. AFRAME.registerComponent('xrcontroller', {
  613. schema: {
  614. hand: { default: 'right' }
  615. },
  616. update: function (old) {
  617. this.hand = this.data.hand;
  618. },
  619. init: function () {
  620. var self = this;
  621. this.hand = this.data.hand;
  622. this.controllerID = 'xrcontroller-' + this.hand + '-' + vwf_view.kernel.moniker();
  623. this.el.addEventListener('triggerdown', function (event) { //pointdown 'triggerdown'
  624. if(!self.xrcontroller){
  625. self.xrcontroller = document.querySelector('#'+ self.controllerID);
  626. }
  627. let intersection = self.xrcontroller.components.raycaster.intersections[0];
  628. let point = intersection ? intersection.point : null;
  629. let elID = intersection ? intersection.object.el.id : null;
  630. if(point) console.log('Point to: ', point);
  631. vwf_view.kernel.callMethod(self.controllerID, "triggerdown", [point, elID]);
  632. //this.emit('teleportstart');
  633. });
  634. this.el.addEventListener('triggerup', function (event) { //pointup 'triggerup'
  635. if(!self.xrcontroller){
  636. self.xrcontroller = document.querySelector('#'+ self.controllerID);
  637. }
  638. let intersection = self.xrcontroller.components.raycaster.intersections[0];
  639. let point = intersection ? intersection.point : null;
  640. let elID = intersection ? intersection.object.el.id : null;
  641. if(point) console.log('Point to: ', point);
  642. vwf_view.kernel.callMethod(self.controllerID, "triggerup", [point, elID]);
  643. //this.emit('teleportend');
  644. });
  645. //X-buttorn Pressed
  646. this.el.addEventListener('xbuttondown', function (e) { //xbuttondown
  647. //Start pointing position to teleport
  648. console.log('TELEPORT START: ', e);
  649. this.emit('teleportstart');
  650. });
  651. //X-buttorn Released
  652. this.el.addEventListener('xbuttonup', function (e) { //xbuttonup
  653. //Jump to pointed position
  654. console.log('TELEPORT END: ', e);
  655. this.emit('teleportend');
  656. });
  657. this.el.addEventListener('teleported', function (e) { //xbuttonup
  658. //Teleported
  659. console.log('TELEPORTED: ', e);
  660. });
  661. },
  662. // tick: function (t) {
  663. // }
  664. })
  665. AFRAME.registerComponent('wmrvrcontrol', {
  666. schema: {
  667. hand: { default: 'right' }
  668. },
  669. update: function (old) {
  670. this.hand = this.data.hand;
  671. },
  672. init: function () {
  673. var self = this;
  674. this.hand = this.data.hand;
  675. this.controllerID = 'wmrvr-' + this.hand + '-' + vwf_view.kernel.moniker();
  676. //this.gearel = document.querySelector('#gearvrcontrol');
  677. this.el.addEventListener('triggerdown', function (event) { //pointdown 'triggerdown'
  678. vwf_view.kernel.callMethod(self.controllerID, "triggerdown", []);
  679. });
  680. this.el.addEventListener('triggerup', function (event) { //pointup 'triggerup'
  681. vwf_view.kernel.callMethod(self.controllerID, "triggerup", []);
  682. });
  683. },
  684. // tick: function (t) {
  685. // }
  686. })
  687. AFRAME.registerComponent('streamsound', {
  688. schema: {
  689. positional: { default: true }
  690. },
  691. init: function () {
  692. var self = this;
  693. let driver = vwf.views["/drivers/view/webrtc"];
  694. this.listener = null;
  695. this.stream = null;
  696. if (!this.sound) {
  697. this.setupSound();
  698. }
  699. if (driver) {
  700. //let avatarID = 'avatar-' + vwf.moniker();
  701. let avatarID = this.el.id.slice(0, 27); //avatar-0RtnYBBTBU84OCNcAAFY
  702. let client = driver.state.clients[avatarID];
  703. if (client) {
  704. if (client.connection) {
  705. this.stream = client.connection.stream;
  706. if (this.stream) {
  707. this.audioEl = new Audio();
  708. this.audioEl.srcObject = this.stream;
  709. this.sound.setNodeSource(this.sound.context.createMediaStreamSource(this.stream));
  710. }
  711. }
  712. }
  713. }
  714. },
  715. setupSound: function () {
  716. var el = this.el;
  717. var sceneEl = el.sceneEl;
  718. if (this.sound) {
  719. el.removeObject3D(this.attrName);
  720. }
  721. if (!sceneEl.audioListener) {
  722. sceneEl.audioListener = new THREE.AudioListener();
  723. sceneEl.camera && sceneEl.camera.add(sceneEl.audioListener);
  724. sceneEl.addEventListener('camera-set-active', function (evt) {
  725. evt.detail.cameraEl.getObject3D('camera').add(sceneEl.audioListener);
  726. });
  727. }
  728. this.listener = sceneEl.audioListener;
  729. this.sound = this.data.positional
  730. ? new THREE.PositionalAudio(this.listener)
  731. : new THREE.Audio(this.listener);
  732. el.setObject3D(this.attrName, this.sound);
  733. },
  734. remove: function () {
  735. if (!this.sound) return;
  736. this.el.removeObject3D(this.attrName);
  737. if (this.stream) {
  738. this.sound.disconnect();
  739. }
  740. },
  741. update: function (old) {
  742. },
  743. // tick: function (t) {
  744. // }
  745. })
  746. AFRAME.registerComponent('viewoffset', {
  747. // fullWidth:
  748. // fullHeight:
  749. // xoffset:
  750. // yoffset:
  751. // width:
  752. // height:
  753. schema: {
  754. fullWidth: { default: window.innerWidth },
  755. fullHeight: { default: window.innerHeight },
  756. xoffset: { default: window.innerWidth / 2 },
  757. yoffset: { default: window.innerHeight / 2 },
  758. width: { default: window.innerWidth },
  759. height: { default: window.innerHeight }
  760. },
  761. init: function () {
  762. var self = this;
  763. this.el.sceneEl.addEventListener('loaded', setOffset);
  764. function setOffset() {
  765. this.setNewOffset();
  766. }
  767. },
  768. update: function (old) {
  769. this.fullWidth = this.data.fullWidth;
  770. this.fullHeight = this.data.fullHeight;
  771. this.xoffset = this.data.xoffset;
  772. this.yoffset = this.data.yoffset;
  773. this.width = this.data.width;
  774. this.height = this.data.height;
  775. //console.log(this.data);
  776. this.setNewOffset();
  777. },
  778. setNewOffset: function () {
  779. this.el.object3DMap.camera.setViewOffset(
  780. this.data.fullWidth,
  781. this.data.fullHeight,
  782. this.data.xoffset,
  783. this.data.yoffset,
  784. this.data.width,
  785. this.data.height)
  786. },
  787. // tick: function (t) {
  788. // }
  789. })
  790. AFRAME.registerComponent("virtual-gamepad-controls", {
  791. schema: {},
  792. init() {
  793. this.onEnterVr = this.onEnterVr.bind(this);
  794. this.onExitVr = this.onExitVr.bind(this);
  795. this.onFirstInteraction = this.onFirstInteraction.bind(this);
  796. this.onMoveJoystickChanged = this.onMoveJoystickChanged.bind(this);
  797. this.onMoveJoystickEnd = this.onMoveJoystickEnd.bind(this);
  798. // this.onLookJoystickChanged = this.onLookJoystickChanged.bind(this);
  799. // this.onLookJoystickEnd = this.onLookJoystickEnd.bind(this);
  800. this.mockJoystickContainer = document.createElement("div");
  801. this.mockJoystickContainer.classList.add('mockJoystickContainer');
  802. const leftMock = document.createElement("div");
  803. leftMock.classList.add('mockJoystick');
  804. const leftMockSmall = document.createElement("div");
  805. leftMockSmall.classList.add('mockJoystick', 'inner');
  806. leftMock.appendChild(leftMockSmall);
  807. this.mockJoystickContainer.appendChild(leftMock);
  808. // const rightMock = document.createElement("div");
  809. // rightMock.classList.add('mockJoystick');
  810. // const rightMockSmall = document.createElement("div");
  811. // rightMockSmall.classList.add('mockJoystick', 'inner');
  812. // rightMock.appendChild(rightMockSmall);
  813. // this.mockJoystickContainer.appendChild(rightMock);
  814. document.body.appendChild(this.mockJoystickContainer);
  815. // Setup gamepad elements
  816. const leftTouchZone = document.createElement("div");
  817. leftTouchZone.classList.add('touchZone', 'left');
  818. document.body.appendChild(leftTouchZone);
  819. this.leftTouchZone = leftTouchZone;
  820. this.leftStick = nipplejs.create({
  821. zone: this.leftTouchZone,
  822. color: "white",
  823. fadeTime: 0
  824. });
  825. this.leftStick.on("start", this.onFirstInteraction);
  826. this.leftStick.on("move", this.onMoveJoystickChanged);
  827. this.leftStick.on("end", this.onMoveJoystickEnd);
  828. // const rightTouchZone = document.createElement("div");
  829. // rightTouchZone.classList.add('touchZone', 'right');
  830. // document.body.appendChild(rightTouchZone);
  831. // this.rightTouchZone = rightTouchZone;
  832. // this.rightStick = nipplejs.create({
  833. // zone: this.rightTouchZone,
  834. // color: "white",
  835. // fadeTime: 0
  836. // });
  837. // this.rightStick.on("start", this.onFirstInteraction);
  838. // this.rightStick.on("move", this.onLookJoystickChanged);
  839. // this.rightStick.on("end", this.onLookJoystickEnd);
  840. this.inVr = false;
  841. this.moving = false;
  842. this.rotating = false;
  843. this.moveEvent = {
  844. axis: [0, 0]
  845. };
  846. // this.rotateYEvent = {
  847. // value: 0
  848. // };
  849. // this.rotateXEvent = {
  850. // value: 0
  851. // };
  852. this.el.sceneEl.addEventListener("enter-vr", this.onEnterVr);
  853. this.el.sceneEl.addEventListener("exit-vr", this.onExitVr);
  854. },
  855. onFirstInteraction() {
  856. this.leftStick.off("start", this.onFirstInteraction);
  857. //this.rightStick.off("start", this.onFirstInteraction);
  858. document.body.removeChild(this.mockJoystickContainer);
  859. },
  860. onMoveJoystickChanged(event, joystick) {
  861. const angle = joystick.angle.radian;
  862. const force = joystick.force < 1 ? joystick.force : 1;
  863. const moveStrength = 1.85;
  864. const x = Math.cos(angle) * force * moveStrength;
  865. const z = Math.sin(angle) * force * moveStrength;
  866. this.moving = true;
  867. this.moveEvent.axis[0] = x;
  868. this.moveEvent.axis[1] = z;
  869. },
  870. onMoveJoystickEnd() {
  871. this.moving = false;
  872. this.moveEvent.axis[0] = 0;
  873. this.moveEvent.axis[1] = 0;
  874. this.el.emit("move", this.moveEvent);
  875. },
  876. onLookJoystickChanged(event, joystick) {
  877. // Set pitch and yaw angles on right stick move
  878. const angle = joystick.angle.radian;
  879. const force = joystick.force < 1 ? joystick.force : 1;
  880. const turnStrength = 0.5;
  881. this.rotating = true;
  882. this.rotateYEvent.value = Math.cos(angle) * force * turnStrength;
  883. this.rotateXEvent.value = Math.sin(angle) * force * turnStrength;
  884. },
  885. onLookJoystickEnd() {
  886. this.rotating = false;
  887. this.rotateYEvent.value = 0;
  888. this.rotateXEvent.value = 0;
  889. this.el.emit("rotateY", this.rotateYEvent);
  890. this.el.emit("rotateX", this.rotateXEvent);
  891. },
  892. tick() {
  893. if (!this.inVr) {
  894. if (this.moving) {
  895. this.el.emit("move", this.moveEvent);
  896. }
  897. // if (this.rotating) {
  898. // this.el.emit("rotateY", this.rotateYEvent);
  899. // this.el.emit("rotateX", this.rotateXEvent);
  900. // }
  901. }
  902. },
  903. onEnterVr() {
  904. // Hide the joystick controls
  905. this.inVr = true;
  906. this.leftTouchZone.style.display = "none";
  907. // this.rightTouchZone.style.display = "none";
  908. },
  909. onExitVr() {
  910. // Show the joystick controls
  911. this.inVr = false;
  912. this.leftTouchZone.style.display = "block";
  913. // this.rightTouchZone.style.display = "block";
  914. },
  915. remove() {
  916. this.el.sceneEl.removeEventListener("entervr", this.onEnterVr);
  917. this.el.sceneEl.removeEventListener("exitvr", this.onExitVr);
  918. if (document.getElementsByClassName('mockJoystickContainer').length > 0){
  919. document.body.removeChild(this.mockJoystickContainer);
  920. }
  921. document.body.removeChild(this.leftTouchZone);
  922. // document.body.removeChild(this.rightTouchZone);
  923. }
  924. });
  925. ////ARJS///
  926. //////////////////////////////////////////////////////////////////////////////
  927. // arjs-anchor
  928. //////////////////////////////////////////////////////////////////////////////
  929. AFRAME.registerComponent('arjs-anchor', {
  930. dependencies: ['arjs', 'artoolkit'],
  931. schema: {
  932. preset: {
  933. type: 'string',
  934. },
  935. markerhelpers: { // IIF preset === 'area'
  936. type: 'boolean',
  937. default: false,
  938. },
  939. // controls parameters
  940. size: {
  941. type: 'number',
  942. default: 1
  943. },
  944. type: {
  945. type: 'string',
  946. },
  947. patternUrl: {
  948. type: 'string',
  949. },
  950. barcodeValue: {
  951. type: 'number'
  952. },
  953. changeMatrixMode: {
  954. type: 'string',
  955. default: 'modelViewMatrix',
  956. },
  957. minConfidence: {
  958. type: 'number',
  959. default: 0.6,
  960. },
  961. smooth: {
  962. type: 'boolean',
  963. default: false,
  964. },
  965. smoothCount: {
  966. type: 'number',
  967. default: 5,
  968. },
  969. smoothTolerance: {
  970. type: 'number',
  971. default: 0.01,
  972. },
  973. smoothThreshold: {
  974. type: 'number',
  975. default: 2,
  976. },
  977. },
  978. init: function () {
  979. var _this = this
  980. // get arjsSystem
  981. var arjsSystem = this.el.sceneEl.systems.arjs || this.el.sceneEl.systems.artoolkit
  982. //////////////////////////////////////////////////////////////////////////////
  983. // Code Separator
  984. //////////////////////////////////////////////////////////////////////////////
  985. _this.isReady = false
  986. _this._arAnchor = null
  987. //LiveCoding.space fix for editor mode
  988. if(arjsSystem) {
  989. // honor object visibility
  990. if (_this.data.changeMatrixMode === 'modelViewMatrix') {
  991. _this.el.object3D.visible = false
  992. } else if (_this.data.changeMatrixMode === 'cameraTransformMatrix') {
  993. _this.el.sceneEl.object3D.visible = false
  994. } else console.assert(false)
  995. // trick to wait until arjsSystem is isReady
  996. var startedAt = Date.now()
  997. var timerId = setInterval(function () {
  998. // wait until the system is isReady
  999. if (arjsSystem.isReady === false) return
  1000. clearInterval(timerId)
  1001. //////////////////////////////////////////////////////////////////////////////
  1002. // update arProfile
  1003. //////////////////////////////////////////////////////////////////////////////
  1004. var arProfile = arjsSystem._arProfile
  1005. // arProfile.changeMatrixMode('modelViewMatrix')
  1006. arProfile.changeMatrixMode(_this.data.changeMatrixMode)
  1007. // honor this.data.preset
  1008. var markerParameters = Object.assign({}, arProfile.defaultMarkerParameters)
  1009. if (_this.data.preset === 'hiro') {
  1010. markerParameters.type = 'pattern'
  1011. markerParameters.patternUrl = THREEx.ArToolkitContext.baseURL + 'examples/marker-training/examples/pattern-files/pattern-hiro.patt'
  1012. markerParameters.markersAreaEnabled = false
  1013. } else if (_this.data.preset === 'kanji') {
  1014. markerParameters.type = 'pattern'
  1015. markerParameters.patternUrl = THREEx.ArToolkitContext.baseURL + 'examples/marker-training/examples/pattern-files/pattern-kanji.patt'
  1016. markerParameters.markersAreaEnabled = false
  1017. } else if (_this.data.preset === 'area') {
  1018. markerParameters.type = 'barcode'
  1019. markerParameters.barcodeValue = 1001
  1020. markerParameters.markersAreaEnabled = true
  1021. } else if (_this.data.type === 'barcode') {
  1022. markerParameters = {
  1023. type: _this.data.type,
  1024. changeMatrixMode: 'modelViewMatrix',
  1025. barcodeValue: _this.data.barcodeValue,
  1026. markersAreaEnabled: false
  1027. }
  1028. } else if (_this.data.type === 'pattern') {
  1029. markerParameters.type = _this.data.type
  1030. markerParameters.patternUrl = _this.data.patternUrl;
  1031. markerParameters.markersAreaEnabled = false
  1032. }
  1033. markerParameters.smooth = _this.data.smooth;
  1034. markerParameters.smoothCount = _this.data.smoothCount;
  1035. markerParameters.smoothTolerance = _this.data.smoothTolerance;
  1036. markerParameters.smoothThreshold = _this.data.smoothThreshold;
  1037. //////////////////////////////////////////////////////////////////////////////
  1038. // create arAnchor
  1039. //////////////////////////////////////////////////////////////////////////////
  1040. var arSession = arjsSystem._arSession
  1041. var arAnchor = _this._arAnchor = new ARjs.Anchor(arSession, markerParameters)
  1042. // it is now considered isReady
  1043. _this.isReady = true
  1044. //////////////////////////////////////////////////////////////////////////////
  1045. // honor .debugUIEnabled
  1046. //////////////////////////////////////////////////////////////////////////////
  1047. if (arjsSystem.data.debugUIEnabled) {
  1048. // get or create containerElement
  1049. var containerElement = document.querySelector('#arjsDebugUIContainer')
  1050. if (containerElement === null) {
  1051. containerElement = document.createElement('div')
  1052. containerElement.id = 'arjsDebugUIContainer'
  1053. containerElement.setAttribute('style', 'position: fixed; bottom: 10px; width:100%; text-align: center; z-index: 1; color: grey;')
  1054. document.body.appendChild(containerElement)
  1055. }
  1056. // create anchorDebugUI
  1057. var anchorDebugUI = new ARjs.AnchorDebugUI(arAnchor)
  1058. containerElement.appendChild(anchorDebugUI.domElement)
  1059. }
  1060. }, 1000 / 60)
  1061. }
  1062. },
  1063. remove: function () {
  1064. },
  1065. update: function () {
  1066. },
  1067. tick: function () {
  1068. var _this = this
  1069. // if not yet isReady, do nothing
  1070. if (this.isReady === false) return
  1071. //////////////////////////////////////////////////////////////////////////////
  1072. // update arAnchor
  1073. //////////////////////////////////////////////////////////////////////////////
  1074. var arjsSystem = this.el.sceneEl.systems.arjs || this.el.sceneEl.systems.artoolkit
  1075. this._arAnchor.update()
  1076. //////////////////////////////////////////////////////////////////////////////
  1077. // honor pose
  1078. //////////////////////////////////////////////////////////////////////////////
  1079. var arWorldRoot = this._arAnchor.object3d
  1080. arWorldRoot.updateMatrixWorld(true)
  1081. arWorldRoot.matrixWorld.decompose(this.el.object3D.position, this.el.object3D.quaternion, this.el.object3D.scale)
  1082. //////////////////////////////////////////////////////////////////////////////
  1083. // honor visibility
  1084. //////////////////////////////////////////////////////////////////////////////
  1085. if (_this._arAnchor.parameters.changeMatrixMode === 'modelViewMatrix') {
  1086. var wasVisible = _this.el.object3D.visible
  1087. _this.el.object3D.visible = this._arAnchor.object3d.visible
  1088. } else if (_this._arAnchor.parameters.changeMatrixMode === 'cameraTransformMatrix') {
  1089. var wasVisible = _this.el.sceneEl.object3D.visible
  1090. _this.el.sceneEl.object3D.visible = this._arAnchor.object3d.visible
  1091. } else console.assert(false)
  1092. // emit markerFound markerLost
  1093. if (_this._arAnchor.object3d.visible === true && wasVisible === false) {
  1094. _this.el.emit('markerFound')
  1095. } else if (_this._arAnchor.object3d.visible === false && wasVisible === true) {
  1096. _this.el.emit('markerLost')
  1097. }
  1098. }
  1099. })
  1100. //////////////////////////////////////////////////////////////////////////////
  1101. // define some primitives shortcuts
  1102. //////////////////////////////////////////////////////////////////////////////
  1103. AFRAME.registerPrimitive('a-anchor', AFRAME.utils.extendDeep({}, AFRAME.primitives.getMeshMixin(), {
  1104. defaultComponents: {
  1105. 'arjs-anchor': {},
  1106. 'arjs-hit-testing': {},
  1107. },
  1108. mappings: {
  1109. 'type': 'arjs-anchor.type',
  1110. 'size': 'arjs-anchor.size',
  1111. 'url': 'arjs-anchor.patternUrl',
  1112. 'value': 'arjs-anchor.barcodeValue',
  1113. 'preset': 'arjs-anchor.preset',
  1114. 'min-confidence': 'arjs-anchor.minConfidence',
  1115. 'marker-helpers': 'arjs-anchor.markerhelpers',
  1116. 'smooth': 'arjs-anchor.smooth',
  1117. 'smooth-count': 'arjs-anchor.smoothCount',
  1118. 'smooth-tolerance': 'arjs-anchor.smoothTolerance',
  1119. 'smooth-threshold': 'arjs-anchor.smoothThreshold',
  1120. 'hit-testing-render-debug': 'arjs-hit-testing.renderDebug',
  1121. 'hit-testing-enabled': 'arjs-hit-testing.enabled',
  1122. }
  1123. }))
  1124. AFRAME.registerPrimitive('a-camera-static', AFRAME.utils.extendDeep({}, AFRAME.primitives.getMeshMixin(), {
  1125. defaultComponents: {
  1126. 'camera': {},
  1127. },
  1128. mappings: {
  1129. }
  1130. }))
  1131. //////////////////////////////////////////////////////////////////////////////
  1132. // backward compatibility
  1133. //////////////////////////////////////////////////////////////////////////////
  1134. // FIXME
  1135. AFRAME.registerPrimitive('a-marker', AFRAME.utils.extendDeep({}, AFRAME.primitives.getMeshMixin(), {
  1136. defaultComponents: {
  1137. 'arjs-anchor': {},
  1138. 'arjs-hit-testing': {},
  1139. },
  1140. mappings: {
  1141. 'type': 'arjs-anchor.type',
  1142. 'size': 'arjs-anchor.size',
  1143. 'url': 'arjs-anchor.patternUrl',
  1144. 'value': 'arjs-anchor.barcodeValue',
  1145. 'preset': 'arjs-anchor.preset',
  1146. 'min-confidence': 'arjs-anchor.minConfidence',
  1147. 'marker-helpers': 'arjs-anchor.markerhelpers',
  1148. 'smooth': 'arjs-anchor.smooth',
  1149. 'smooth-count': 'arjs-anchor.smoothCount',
  1150. 'smooth-tolerance': 'arjs-anchor.smoothTolerance',
  1151. 'smooth-threshold': 'arjs-anchor.smoothThreshold',
  1152. 'hit-testing-render-debug': 'arjs-hit-testing.renderDebug',
  1153. 'hit-testing-enabled': 'arjs-hit-testing.enabled',
  1154. }
  1155. }))
  1156. AFRAME.registerPrimitive('a-marker-camera', AFRAME.utils.extendDeep({}, AFRAME.primitives.getMeshMixin(), {
  1157. defaultComponents: {
  1158. 'arjs-anchor': {
  1159. changeMatrixMode: 'cameraTransformMatrix'
  1160. },
  1161. 'camera': {},
  1162. },
  1163. mappings: {
  1164. 'type': 'arjs-anchor.type',
  1165. 'size': 'arjs-anchor.size',
  1166. 'url': 'arjs-anchor.patternUrl',
  1167. 'value': 'arjs-anchor.barcodeValue',
  1168. 'preset': 'arjs-anchor.preset',
  1169. 'min-confidence': 'arjs-anchor.minConfidence',
  1170. 'marker-helpers': 'arjs-anchor.markerhelpers',
  1171. }
  1172. }))
  1173. //////////////////////////////////////////////////////////////////////////////
  1174. // arjs-hit-testing
  1175. //////////////////////////////////////////////////////////////////////////////
  1176. AFRAME.registerComponent('arjs-hit-testing', {
  1177. dependencies: ['arjs', 'artoolkit'],
  1178. schema: {
  1179. enabled : {
  1180. type: 'boolean',
  1181. default: false,
  1182. },
  1183. renderDebug : {
  1184. type: 'boolean',
  1185. default: false,
  1186. },
  1187. },
  1188. init: function () {
  1189. var _this = this
  1190. var arjsSystem = this.el.sceneEl.systems.arjs || this.el.sceneEl.systems.artoolkit
  1191. // TODO make it work on cameraTransformMatrix too
  1192. //
  1193. _this.isReady = false
  1194. _this._arAnchor = null
  1195. _this._arHitTesting = null
  1196. //LiveCdoing.space fix for editor mode
  1197. if(arjsSystem) {
  1198. // trick to wait until arjsSystem is isReady
  1199. var startedAt = Date.now()
  1200. var timerId = setInterval(function(){
  1201. var anchorEl = _this.el
  1202. var anchorComponent = anchorEl.components['arjs-anchor']
  1203. // wait until anchorComponent is isReady
  1204. if( anchorComponent === undefined || anchorComponent.isReady === false ) return
  1205. clearInterval(timerId)
  1206. //////////////////////////////////////////////////////////////////////////////
  1207. // create arAnchor
  1208. //////////////////////////////////////////////////////////////////////////////
  1209. var arAnchor = anchorComponent._arAnchor
  1210. var arSession = arjsSystem._arSession
  1211. var renderer = arSession.parameters.renderer
  1212. var hitTesting = _this._arHitTesting = new ARjs.HitTesting(arSession)
  1213. hitTesting.enabled = _this.data.enabled
  1214. _this.isReady = true
  1215. }, 1000/60)
  1216. }
  1217. },
  1218. remove : function(){
  1219. },
  1220. update: function () {
  1221. },
  1222. tick: function(){
  1223. var _this = this
  1224. // if not yet isReady, do nothing
  1225. if( this.isReady === false ) return
  1226. var arjsSystem = this.el.sceneEl.systems.arjs || this.el.sceneEl.systems.artoolkit
  1227. var arSession = arjsSystem._arSession
  1228. var anchorEl = _this.el
  1229. var anchorComponent = anchorEl.components['arjs-anchor']
  1230. var arAnchor = anchorComponent._arAnchor
  1231. var hitTesting = this._arHitTesting
  1232. var camera = arSession.parameters.camera
  1233. // console.log(camera.position)
  1234. hitTesting.update(camera, arAnchor.object3d, arAnchor.parameters.changeMatrixMode)
  1235. }
  1236. });
  1237. ///////////////END ARJS/////////
  1238. ///MIRROR//
  1239. THREE.ShaderLib[ 'mirror' ] = {
  1240. uniforms: {
  1241. "mirrorColor": { value: new THREE.Color( 0x7F7F7F ) },
  1242. "mirrorSampler": { value: null },
  1243. "textureMatrix" : { value: new THREE.Matrix4() }
  1244. },
  1245. vertexShader: [
  1246. "uniform mat4 textureMatrix;",
  1247. "varying vec4 mirrorCoord;",
  1248. "void main() {",
  1249. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1250. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  1251. "mirrorCoord = textureMatrix * worldPosition;",
  1252. "gl_Position = projectionMatrix * mvPosition;",
  1253. "}"
  1254. ].join( "\n" ),
  1255. fragmentShader: [
  1256. "uniform vec3 mirrorColor;",
  1257. "uniform sampler2D mirrorSampler;",
  1258. "varying vec4 mirrorCoord;",
  1259. "float blendOverlay(float base, float blend) {",
  1260. "return( base < 0.5 ? ( 2.0 * base * blend ) : (1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );",
  1261. "}",
  1262. "void main() {",
  1263. "vec4 color = texture2DProj(mirrorSampler, mirrorCoord);",
  1264. "color = vec4(blendOverlay(mirrorColor.r, color.r), blendOverlay(mirrorColor.g, color.g), blendOverlay(mirrorColor.b, color.b), 1.0);",
  1265. "gl_FragColor = color;",
  1266. "}"
  1267. ].join( "\n" )
  1268. };
  1269. THREE.Mirror = function ( renderer, camera, options ) {
  1270. THREE.Object3D.call( this );
  1271. this.name = 'mirror_' + this.id;
  1272. options = options || {};
  1273. this.matrixNeedsUpdate = true;
  1274. var width = options.textureWidth !== undefined ? options.textureWidth : 512;
  1275. var height = options.textureHeight !== undefined ? options.textureHeight : 512;
  1276. this.clipBias = options.clipBias !== undefined ? options.clipBias : 0.0;
  1277. var mirrorColor = options.color !== undefined ? new THREE.Color( options.color ) : new THREE.Color( 0x7F7F7F );
  1278. this.renderer = renderer;
  1279. this.mirrorPlane = new THREE.Plane();
  1280. this.normal = new THREE.Vector3( 0, 0, 1 );
  1281. this.mirrorWorldPosition = new THREE.Vector3();
  1282. this.cameraWorldPosition = new THREE.Vector3();
  1283. this.rotationMatrix = new THREE.Matrix4();
  1284. this.lookAtPosition = new THREE.Vector3( 0, 0, - 1 );
  1285. this.clipPlane = new THREE.Vector4();
  1286. // For debug only, show the normal and plane of the mirror
  1287. var debugMode = options.debugMode !== undefined ? options.debugMode : false;
  1288. if ( debugMode ) {
  1289. var arrow = new THREE.ArrowHelper( new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 0, 0, 0 ), 10, 0xffff80 );
  1290. var planeGeometry = new THREE.Geometry();
  1291. planeGeometry.vertices.push( new THREE.Vector3( - 10, - 10, 0 ) );
  1292. planeGeometry.vertices.push( new THREE.Vector3( 10, - 10, 0 ) );
  1293. planeGeometry.vertices.push( new THREE.Vector3( 10, 10, 0 ) );
  1294. planeGeometry.vertices.push( new THREE.Vector3( - 10, 10, 0 ) );
  1295. planeGeometry.vertices.push( planeGeometry.vertices[ 0 ] );
  1296. var plane = new THREE.Line( planeGeometry, new THREE.LineBasicMaterial( { color: 0xffff80 } ) );
  1297. this.add( arrow );
  1298. this.add( plane );
  1299. }
  1300. if ( camera instanceof THREE.PerspectiveCamera ) {
  1301. this.camera = camera;
  1302. } else {
  1303. this.camera = new THREE.PerspectiveCamera();
  1304. console.log( this.name + ': camera is not a Perspective Camera!' );
  1305. }
  1306. this.textureMatrix = new THREE.Matrix4();
  1307. this.mirrorCamera = this.camera.clone();
  1308. this.mirrorCamera.matrixAutoUpdate = true;
  1309. var parameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
  1310. //this.currentRenderTarget = this.renderer.getRenderTarget();
  1311. this.renderTarget = new THREE.WebGLRenderTarget( width, height, parameters );
  1312. this.renderTarget2 = new THREE.WebGLRenderTarget( width, height, parameters );
  1313. var mirrorShader = THREE.ShaderLib[ "mirror" ];
  1314. var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );
  1315. this.material = new THREE.ShaderMaterial( {
  1316. fragmentShader: mirrorShader.fragmentShader,
  1317. vertexShader: mirrorShader.vertexShader,
  1318. uniforms: mirrorUniforms
  1319. } );
  1320. this.material.uniforms.mirrorSampler.value = this.renderTarget.texture;
  1321. this.material.uniforms.mirrorColor.value = mirrorColor;
  1322. this.material.uniforms.textureMatrix.value = this.textureMatrix;
  1323. if ( ! THREE.Math.isPowerOfTwo( width ) || ! THREE.Math.isPowerOfTwo( height ) ) {
  1324. this.renderTarget.texture.generateMipmaps = false;
  1325. this.renderTarget2.texture.generateMipmaps = false;
  1326. }
  1327. this.updateTextureMatrix();
  1328. this.render();
  1329. };
  1330. THREE.Mirror.prototype = Object.create( THREE.Object3D.prototype );
  1331. THREE.Mirror.prototype.constructor = THREE.Mirror;
  1332. THREE.Mirror.prototype.renderWithMirror = function ( otherMirror, aScene ) {
  1333. // update the mirror matrix to mirror the current view
  1334. this.updateTextureMatrix();
  1335. this.matrixNeedsUpdate = false;
  1336. // set the camera of the other mirror so the mirrored view is the reference view
  1337. var tempCamera = otherMirror.camera;
  1338. otherMirror.camera = this.mirrorCamera;
  1339. // render the other mirror in temp texture
  1340. otherMirror.renderTemp(aScene);
  1341. otherMirror.material.uniforms.mirrorSampler.value = otherMirror.renderTarget2.texture;
  1342. // render the current mirror
  1343. this.render(aScene);
  1344. this.matrixNeedsUpdate = true;
  1345. // restore material and camera of other mirror
  1346. otherMirror.material.uniforms.mirrorSampler.value = otherMirror.renderTarget.texture;
  1347. otherMirror.camera = tempCamera;
  1348. // restore texture matrix of other mirror
  1349. otherMirror.updateTextureMatrix();
  1350. };
  1351. THREE.Mirror.prototype.updateTextureMatrix = function () {
  1352. this.updateMatrixWorld();
  1353. this.camera.updateMatrixWorld();
  1354. this.mirrorWorldPosition.setFromMatrixPosition( this.matrixWorld );
  1355. this.cameraWorldPosition.setFromMatrixPosition( this.camera.matrixWorld );
  1356. this.rotationMatrix.extractRotation( this.matrixWorld );
  1357. this.normal.set( 0, 0, 1 );
  1358. this.normal.applyMatrix4( this.rotationMatrix );
  1359. var view = this.mirrorWorldPosition.clone().sub( this.cameraWorldPosition );
  1360. view.reflect( this.normal ).negate();
  1361. view.add( this.mirrorWorldPosition );
  1362. this.rotationMatrix.extractRotation( this.camera.matrixWorld );
  1363. this.lookAtPosition.set( 0, 0, - 1 );
  1364. this.lookAtPosition.applyMatrix4( this.rotationMatrix );
  1365. this.lookAtPosition.add( this.cameraWorldPosition );
  1366. var target = this.mirrorWorldPosition.clone().sub( this.lookAtPosition );
  1367. target.reflect( this.normal ).negate();
  1368. target.add( this.mirrorWorldPosition );
  1369. this.up.set( 0, - 1, 0 );
  1370. this.up.applyMatrix4( this.rotationMatrix );
  1371. this.up.reflect( this.normal ).negate();
  1372. this.mirrorCamera.position.copy( view );
  1373. this.mirrorCamera.up = this.up;
  1374. this.mirrorCamera.lookAt( target );
  1375. this.mirrorCamera.updateProjectionMatrix();
  1376. this.mirrorCamera.updateMatrixWorld();
  1377. this.mirrorCamera.matrixWorldInverse.getInverse( this.mirrorCamera.matrixWorld );
  1378. // Update the texture matrix
  1379. this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5,
  1380. 0.0, 0.5, 0.0, 0.5,
  1381. 0.0, 0.0, 0.5, 0.5,
  1382. 0.0, 0.0, 0.0, 1.0 );
  1383. this.textureMatrix.multiply( this.mirrorCamera.projectionMatrix );
  1384. this.textureMatrix.multiply( this.mirrorCamera.matrixWorldInverse );
  1385. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  1386. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  1387. this.mirrorPlane.setFromNormalAndCoplanarPoint( this.normal, this.mirrorWorldPosition );
  1388. this.mirrorPlane.applyMatrix4( this.mirrorCamera.matrixWorldInverse );
  1389. this.clipPlane.set( this.mirrorPlane.normal.x, this.mirrorPlane.normal.y, this.mirrorPlane.normal.z, this.mirrorPlane.constant );
  1390. var q = new THREE.Vector4();
  1391. var projectionMatrix = this.mirrorCamera.projectionMatrix;
  1392. q.x = ( Math.sign( this.clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  1393. q.y = ( Math.sign( this.clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  1394. q.z = - 1.0;
  1395. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  1396. // Calculate the scaled plane vector
  1397. var c = new THREE.Vector4();
  1398. c = this.clipPlane.multiplyScalar( 2.0 / this.clipPlane.dot( q ) );
  1399. // Replacing the third row of the projection matrix
  1400. projectionMatrix.elements[ 2 ] = c.x;
  1401. projectionMatrix.elements[ 6 ] = c.y;
  1402. projectionMatrix.elements[ 10 ] = c.z + 1.0 - this.clipBias;
  1403. projectionMatrix.elements[ 14 ] = c.w;
  1404. };
  1405. THREE.Mirror.prototype.render = function (aScene) {
  1406. if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
  1407. this.matrixNeedsUpdate = true;
  1408. // Render the mirrored view of the current scene into the target texture
  1409. //var scene = aScene //this;
  1410. // while ( scene.parent !== null ) {
  1411. // scene = scene.parent;
  1412. // }
  1413. //this.renderer.setRenderTarget( null );
  1414. if ( aScene !== undefined) //&& scene instanceof THREE.Scene )
  1415. {
  1416. // We can't render ourself to ourself
  1417. var visible = this.material.visible;
  1418. this.material.visible = false;
  1419. this.renderer.clear();
  1420. this.renderer.setRenderTarget( this.renderTarget);
  1421. this.renderer.render( aScene.object3D, this.mirrorCamera);
  1422. this.renderer.setRenderTarget(null);
  1423. //this.renderer.render( scene, this.mirrorCamera, this.renderTarget, true );
  1424. this.material.visible = visible;
  1425. }
  1426. };
  1427. THREE.Mirror.prototype.renderTemp = function (aScene) {
  1428. if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
  1429. this.matrixNeedsUpdate = true;
  1430. // Render the mirrored view of the current scene into the target texture
  1431. // var scene = this;
  1432. // while ( scene.parent !== null ) {
  1433. // scene = scene.parent;
  1434. // }
  1435. if ( aScene !== undefined) //&& scene instanceof THREE.Scene ) {
  1436. {
  1437. this.renderer.clear();
  1438. this.renderer.setRenderTarget( this.renderTarget2);
  1439. this.renderer.render( aScene.object3D, this.mirrorCamera );
  1440. this.renderer.setRenderTarget( null );
  1441. //this.renderer.render( scene, this.mirrorCamera, this.renderTarget2, true );
  1442. }
  1443. };
  1444. AFRAME.registerComponent('mirror', {
  1445. schema:{
  1446. renderothermirror:{default:true},
  1447. camera:{default: 'avatarControl'}
  1448. },
  1449. init: function () {
  1450. var scene = this.el.sceneEl;
  1451. this.cameraID = this.data.camera;
  1452. scene.addEventListener('render-target-loaded',this.OnRenderLoaded()); //this.OnRenderLoaded.bind(this)
  1453. },
  1454. // update: function (old) {
  1455. // this.cameraID = this.data.camera;
  1456. // this.OnRenderLoaded();
  1457. // },
  1458. OnRenderLoaded: function()
  1459. {
  1460. var mirrorObj = this.el.getOrCreateObject3D('mesh',THREE.Mesh);
  1461. // var cameraEl = document.querySelector('a-entity[camera]')
  1462. // if(!cameraEl)
  1463. // {
  1464. // cameraEl = document.querySelector('a-camera');
  1465. // }
  1466. // var camera = cameraEl.components.camera.camera;
  1467. let cameraEl = document.querySelector("[id='" + this.cameraID + "']")
  1468. if (cameraEl){
  1469. let camera = cameraEl.getObject3D('camera'); //document.querySelector('#avatarControl').getObject3D('camera');
  1470. var scene = this.el.sceneEl;
  1471. // this.renderer = new THREE.WebGLRenderer({
  1472. // antialias: true,
  1473. // });
  1474. this.renderer = scene.renderer;
  1475. this.mirror = new THREE.Mirror( this.renderer, camera, { clipBias: 0.003, textureWidth: window.innerWidth, textureHeight: window.innerHeight, color: 0x777777, debugMode: false} );
  1476. mirrorObj.material = this.mirror.material;
  1477. //mirrorObj.children = [];
  1478. mirrorObj.add(this.mirror);
  1479. }
  1480. //As of A-Frame tick (behaviours) priority issue need to place mirror tick onto upper tick()
  1481. this.el.sceneEl.components['scene-utils'].mirrors[this.el.id] = this;
  1482. },
  1483. remove: function () {
  1484. delete this.el.sceneEl.components['scene-utils'].mirrors[this.el.id]
  1485. },
  1486. mirrorTick: function () {
  1487. // //this.mirror.render();
  1488. if(!this.data.renderothermirror)
  1489. {
  1490. this.mirror.render(this.el.sceneEl);
  1491. }
  1492. else
  1493. {
  1494. var mirrors = [];
  1495. var mirrorEls = document.querySelectorAll("[mirror]");
  1496. for(var i=0;i<mirrorEls.length;i++)
  1497. {
  1498. if(mirrorEls[i]!=this.el)
  1499. {
  1500. mirrors.push(mirrorEls[i].components.mirror.mirror);
  1501. }
  1502. }
  1503. if(mirrors.length === 0)
  1504. {
  1505. this.mirror.render(this.el.sceneEl);
  1506. }
  1507. for(var i = 0; i<mirrors.length;i++)
  1508. {
  1509. this.mirror.renderWithMirror(mirrors[i], this.el.sceneEl);
  1510. }
  1511. }
  1512. },
  1513. // tick: function () {
  1514. // }
  1515. });