1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 12:22:03 +08:00
trois/src/effects/TiltShiftPass.js

54 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-10-07 02:57:00 +08:00
import { Vector2 } from 'three';
import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';
import { watch } from 'vue';
import EffectPass from './EffectPass.js';
import TiltShift from '../shaders/TiltShift.js';
import useBindPropValue from '../use/useBindPropValue.js';
export default {
extends: EffectPass,
props: {
blurRadius: { type: Number, default: 10 },
gradientRadius: { type: Number, default: 100 },
start: { type: Object, default: { x: 0, y: 100 } },
end: { type: Object, default: { x: 10, y: 100 } },
},
mounted() {
this.pass = new ShaderPass(TiltShift);
this.passes.push(this.pass);
this.pass1 = new ShaderPass(TiltShift);
this.passes.push(this.pass1);
const uniforms = this.uniforms = this.pass.uniforms;
const uniforms1 = this.uniforms1 = this.pass1.uniforms;
uniforms1.blurRadius = uniforms.blurRadius;
uniforms1.gradientRadius = uniforms.gradientRadius;
uniforms1.start = uniforms.start;
uniforms1.end = uniforms.end;
uniforms1.texSize = uniforms.texSize;
useBindPropValue(this, 'blurRadius', uniforms.blurRadius);
useBindPropValue(this, 'gradientRadius', uniforms.gradientRadius);
this.updateFocusLine();
['start', 'end'].forEach(p => {
watch(() => this[p], this.updateFocusLine);
});
this.pass.setSize = (width, height) => {
uniforms.texSize.value.set(width, height);
};
},
methods: {
updateFocusLine() {
this.uniforms.start.value.copy(this.start);
this.uniforms.end.value.copy(this.end);
const dv = new Vector2().copy(this.end).sub(this.start).normalize();
this.uniforms.delta.value.copy(dv);
this.uniforms1.delta.value.set(-dv.y, dv.x);
},
},
__hmrId: 'TiltShiftPass',
};