From ff2da62e4b71802efc13ee61e41e7c380e22afc4 Mon Sep 17 00:00:00 2001 From: Kevin Levron Date: Sat, 3 Oct 2020 21:26:12 +0200 Subject: [PATCH] add refraction mesh --- src/meshes/RefractionMesh.js | 46 ++++++++++++++++++++++++++++++++++++ src/meshes/index.js | 1 + src/plugin.js | 1 + 3 files changed, 48 insertions(+) create mode 100644 src/meshes/RefractionMesh.js diff --git a/src/meshes/RefractionMesh.js b/src/meshes/RefractionMesh.js new file mode 100644 index 0000000..1181fca --- /dev/null +++ b/src/meshes/RefractionMesh.js @@ -0,0 +1,46 @@ +import { + CubeCamera, + CubeRefractionMapping, + LinearMipmapLinearFilter, + RGBFormat, + WebGLCubeRenderTarget, +} from 'three'; +// import { watch } from 'vue'; +import Mesh from './Mesh.js'; +import useBindProp from '../use/useBindProp.js'; + +export default { + extends: Mesh, + props: { + cubeRTSize: { type: Number, default: 512 }, + cubeCameraNear: { type: Number, default: 0.1 }, + cubeCameraFar: { type: Number, default: 2000 }, + cubeRTAutoUpdate: Boolean, + }, + mounted() { + this.initMirrorMesh(); + if (this.cubeRTAutoUpdate) this.three.onBeforeRender(this.updateCubeRT); + else this.rendererComponent.onMounted(this.updateCubeRT); + }, + unmounted() { + this.three.offBeforeRender(this.updateCubeRT); + }, + methods: { + initMirrorMesh() { + const cubeRT = new WebGLCubeRenderTarget(this.cubeRTSize, { mapping: CubeRefractionMapping, format: RGBFormat, generateMipmaps: true, minFilter: LinearMipmapLinearFilter }); + this.cubeCamera = new CubeCamera(this.cubeCameraNear, this.cubeCameraFar, cubeRT); + useBindProp(this, 'position', this.cubeCamera.position); + this.scene.add(this.cubeCamera); + + this.material.envMap = cubeRT.texture; + this.material.refractionRatio = 0.95; + this.material.needsUpdate = true; + }, + updateCubeRT() { + this.mesh.visible = false; + this.cubeCamera.update(this.three.renderer, this.scene); + this.mesh.visible = true; + }, + }, + __hmrId: 'RefractionMesh', +}; diff --git a/src/meshes/index.js b/src/meshes/index.js index bf9f113..d984563 100644 --- a/src/meshes/index.js +++ b/src/meshes/index.js @@ -22,4 +22,5 @@ export { default as Gem } from './Gem.js'; export { default as Image } from './Image.js'; export { default as InstancedMesh } from './InstancedMesh.js'; export { default as MirrorMesh } from './MirrorMesh.js'; +export { default as RefractionMesh } from './RefractionMesh.js'; export { default as Sprite } from './Sprite.js'; diff --git a/src/plugin.js b/src/plugin.js index 44c0af3..f4605f4 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -60,6 +60,7 @@ export const TroisJSVuePlugin = { 'Image', 'InstancedMesh', 'MirrorMesh', + 'RefractionMesh', 'Sprite', 'BokehPass',