1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00
This commit is contained in:
Kevin Levron 2020-10-03 14:41:08 +02:00
parent bfc9edba0f
commit 9ba757228c
10 changed files with 102 additions and 27 deletions

View File

@ -28,10 +28,11 @@ export default {
}; };
}, },
mounted() { mounted() {
const startTime = Date.now(); this.startTime = Date.now();
this.three.onBeforeRender(() => { this.three.onBeforeRender(this.updateTime);
this.uTime.value = (Date.now() - startTime) * this.timeCoef; },
}); unmounted() {
this.three.offBeforeRender(this.updateTime);
}, },
methods: { methods: {
createGeometry() { createGeometry() {
@ -80,6 +81,9 @@ export default {
this.materialShader = shader; this.materialShader = shader;
}; };
}, },
updateTime() {
this.uTime.value = (Date.now() - this.startTime) * this.timeCoef;
},
}, },
__hmrId: 'NoisyImage', __hmrId: 'NoisyImage',
}; };

View File

@ -23,10 +23,11 @@ export default {
}, },
mounted() { mounted() {
this.updateMaterial(); this.updateMaterial();
const startTime = Date.now(); this.startTime = Date.now();
this.three.onBeforeRender(() => { this.three.onBeforeRender(this.updateTime);
this.uTime.value = (Date.now() - startTime) * this.timeCoef; },
}); unmounted() {
this.three.offBeforeRender(this.updateTime);
}, },
methods: { methods: {
updateMaterial() { updateMaterial() {
@ -55,6 +56,9 @@ export default {
}; };
this.material.needsupdate = true; this.material.needsupdate = true;
}, },
updateTime() {
this.uTime.value = (Date.now() - this.startTime) * this.timeCoef;
},
}, },
__hmrId: 'NoisyPlane', __hmrId: 'NoisyPlane',
}; };

View File

@ -27,10 +27,11 @@ export default {
mounted() { mounted() {
this.updateMaterial(); this.updateMaterial();
const startTime = Date.now(); this.startTime = Date.now();
this.three.onBeforeRender(() => { this.three.onBeforeRender(this.updateTime);
this.uTime.value = (Date.now() - startTime) * this.timeCoef; },
}); unmounted() {
this.three.offBeforeRender(this.updateTime);
}, },
methods: { methods: {
updateMaterial() { updateMaterial() {
@ -59,6 +60,9 @@ export default {
}; };
this.material.needsupdate = true; this.material.needsupdate = true;
}, },
updateTime() {
this.uTime.value = (Date.now() - this.startTime) * this.timeCoef;
},
}, },
__hmrId: 'NoisySphere', __hmrId: 'NoisySphere',
}; };

View File

@ -24,10 +24,11 @@ export default {
mounted() { mounted() {
this.updateMaterial(); this.updateMaterial();
const startTime = Date.now(); this.startTime = Date.now();
this.three.onBeforeRender(() => { this.three.onBeforeRender(this.updateTime);
this.uTime.value = (Date.now() - startTime) * this.timeCoef; },
}); unmounted() {
this.three.offBeforeRender(this.updateTime);
}, },
methods: { methods: {
updateMaterial() { updateMaterial() {
@ -56,6 +57,9 @@ export default {
}; };
this.material.needsupdate = true; this.material.needsupdate = true;
}, },
updateTime() {
this.uTime.value = (Date.now() - this.startTime) * this.timeCoef;
},
}, },
__hmrId: 'NoisyText', __hmrId: 'NoisyText',
}; };

View File

@ -34,9 +34,9 @@ export default function useThree() {
}; };
// handlers // handlers
const afterInitHandlers = []; let afterInitHandlers = [];
const afterResizeHandlers = []; let afterResizeHandlers = [];
const beforeRenderHandlers = []; let beforeRenderHandlers = [];
// mouse tracking // mouse tracking
const mouse = new Vector2(); const mouse = new Vector2();
@ -60,8 +60,8 @@ export default function useThree() {
renderC, renderC,
setSize, setSize,
onAfterInit, onAfterInit,
onAfterResize, onAfterResize, offAfterResize,
onBeforeRender, onBeforeRender, offBeforeRender,
}; };
/** /**
@ -119,26 +119,40 @@ export default function useThree() {
}; };
/** /**
* add after init handler * add after init callback
*/ */
function onAfterInit(callback) { function onAfterInit(callback) {
afterInitHandlers.push(callback); afterInitHandlers.push(callback);
} }
/** /**
* add after resize handler * add after resize callback
*/ */
function onAfterResize(callback) { function onAfterResize(callback) {
afterResizeHandlers.push(callback); afterResizeHandlers.push(callback);
} }
/** /**
* add before render handler * remove after resize callback
*/
function offAfterResize(callback) {
afterResizeHandlers = afterResizeHandlers.filter(c => c !== callback);
}
/**
* add before render callback
*/ */
function onBeforeRender(callback) { function onBeforeRender(callback) {
beforeRenderHandlers.push(callback); beforeRenderHandlers.push(callback);
} }
/**
* remove before render callback
*/
function offBeforeRender(callback) {
beforeRenderHandlers = beforeRenderHandlers.filter(c => c !== callback);
}
/** /**
* default render * default render
*/ */
@ -161,7 +175,7 @@ export default function useThree() {
* remove listeners * remove listeners
*/ */
function dispose() { function dispose() {
beforeRenderHandlers.splice(0); beforeRenderHandlers = [];
window.removeEventListener('resize', onResize); window.removeEventListener('resize', onResize);
if (obj.mouse_move_element) { if (obj.mouse_move_element) {
obj.mouse_move_element.removeEventListener('mousemove', onMousemove); obj.mouse_move_element.removeEventListener('mousemove', onMousemove);

43
src/meshes/MirrorMesh.js Normal file
View File

@ -0,0 +1,43 @@
import {
CubeCamera,
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 },
},
mounted() {
this.initMirrorMesh();
this.three.onBeforeRender(this.upateCubeRT);
},
unmounted() {
this.three.offBeforeRender(this.upateCubeRT);
},
methods: {
initMirrorMesh() {
const cubeRT = new WebGLCubeRenderTarget(this.cubeRTSize, { 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.needsUpdate = true;
},
upateCubeRT() {
this.mesh.visible = false;
this.cubeCamera.update(this.three.renderer, this.scene);
this.mesh.visible = true;
console.log('upateCubeRT');
},
},
__hmrId: 'MirrorMesh',
};

View File

@ -5,7 +5,7 @@ import Mesh from './Mesh.js';
export default { export default {
extends: Mesh, extends: Mesh,
props: { props: {
radius: { type: Number, default: 1 }, radius: { type: Number, default: 0.5 },
tube: { type: Number, default: 0.4 }, tube: { type: Number, default: 0.4 },
radialSegments: { type: Number, default: 8 }, radialSegments: { type: Number, default: 8 },
tubularSegments: { type: Number, default: 6 }, tubularSegments: { type: Number, default: 6 },

View File

@ -5,7 +5,7 @@ import Mesh from './Mesh.js';
export default { export default {
extends: Mesh, extends: Mesh,
props: { props: {
radius: { type: Number, default: 1 }, radius: { type: Number, default: 0.5 },
tube: { type: Number, default: 0.4 }, tube: { type: Number, default: 0.4 },
radialSegments: { type: Number, default: 64 }, radialSegments: { type: Number, default: 64 },
tubularSegments: { type: Number, default: 8 }, tubularSegments: { type: Number, default: 8 },

View File

@ -20,4 +20,5 @@ export { default as Tube } from './Tube.js';
export { default as Image } from './Image.js'; export { default as Image } from './Image.js';
export { default as InstancedMesh } from './InstancedMesh.js'; export { default as InstancedMesh } from './InstancedMesh.js';
export { default as MirrorMesh } from './MirrorMesh.js';
export { default as Sprite } from './Sprite.js'; export { default as Sprite } from './Sprite.js';

View File

@ -58,6 +58,7 @@ export const TroisJSVuePlugin = {
'Image', 'Image',
'InstancedMesh', 'InstancedMesh',
'MirrorMesh',
'Sprite', 'Sprite',
'BokehPass', 'BokehPass',