aframe-refraction-system
Version:
Realtime envMap creation using a ThreeCubeCamera attached to the camera entity
1 lines • 2.58 kB
JavaScript
!function(e){var t={};function r(i){if(t[i])return t[i].exports;var a=t[i]={i:i,l:!1,exports:{}};return e[i].call(a.exports,a,a.exports,r),a.l=!0,a.exports}r.m=e,r.c=t,r.d=function(e,t,i){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:i})},r.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t){if("undefined"==typeof AFRAME)throw new Error("Component attempted to register before AFRAME was available.");AFRAME.registerSystem("refraction-component",{schema:{near:{type:"number",default:1},far:{type:"number",default:200},resolution:{type:"number",default:256},tickrate:{type:"number",default:10}},init:function(){this.entities=[],this.refractionCamera=null,this.el.addEventListener("camera-set-active",e=>this.getCameraPosition())},getCameraPosition:function(){this.camera=document.querySelector("[camera]"),this.camera||(this.camera=document.querySelector("a-camera")),this.cameraPos=this.camera?this.camera.getAttribute("position"):null},throttledTick:function(e,t){if(this.refractionCamera){this.cameraPos&&this.refractionCamera.position.set(this.cameraPos.x,this.cameraPos.y,this.cameraPos.z),this.refractionCamera.update(AFRAME.scenes[0].renderer,this.el.sceneEl.object3D);for(let e=0;e<this.entities.length;e++)this.entities[e].components["refraction-component"].updateMaterial()}},getTexture(){return this.refractionCamera.renderTarget.texture},update:function(){let e=this.data;this.refractionCamera&&delete this.refractionCamera,this.tick=AFRAME.utils.throttleTick(this.throttledTick,e.tickrate,this),this.refractionCamera=new THREE.CubeCamera(e.near,e.far,e.resolution),this.refractionCamera.renderTarget.texture.mapping=THREE.CubeRefractionMapping,this.el.object3D.add(this.refractionCamera)},remove:function(){this.refractionCamera&&(delete this.refractionCamera,this.el.object3D.remove(this.refractionCamera))},registerMe:function(e){this.entities.push(e)},unregisterMe:function(e){var t=this.entities.indexOf(e);this.entities.splice(t,1)}}),AFRAME.registerComponent("refraction-component",{init:function(){this.system.registerMe(this.el)},updateMaterial:function(){this.mesh=this.el.getObject3D("mesh"),this.mesh&&(this.mesh.material.visible=!1,this.mesh.material.envMap=this.system.getTexture(),this.mesh.material.visible=!0)},remove:function(){this.system.unregisterMe(this.el),this.mesh.material.envMap=null}})}]);