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 2021-03-06 19:22:01 +01:00
parent 33d4ae5e4c
commit 7e0dd5b2ea
19 changed files with 76 additions and 73 deletions

View File

@ -1,5 +1,5 @@
import { Group } from 'three'; import { Group } from 'three';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
export default { export default {
inject: { inject: {
@ -18,23 +18,30 @@ export default {
}; };
}, },
created() { created() {
if (!this.$parent) {
console.error('Missing parent');
} else if (!this.$parent.add || !this.$parent.remove) {
}
this.parent = this.group ? this.group : this.scene; this.parent = this.group ? this.group : this.scene;
this.group = new Group(); this.group = new Group();
useBindProp(this, 'position', this.group.position); bindProp(this, 'position', this.group.position);
useBindProp(this, 'rotation', this.group.rotation); bindProp(this, 'rotation', this.group.rotation);
useBindProp(this, 'scale', this.group.scale); bindProp(this, 'scale', this.group.scale);
this.parent.add(this.group); this.parent.add(this.group);
}, },
unmounted() { unmounted() {
this.parent.remove(this.group); if (this.$parent) this.$parent.remove(this.group);
},
methods: {
add(o) { this.group.add(o); },
remove(o) { this.group.remove(o); },
}, },
render() { render() {
if (this.$slots.default) { return this.$slots.default ? this.$slots.default() : [];
return this.$slots.default();
}
return [];
}, },
__hmrId: 'Group', __hmrId: 'Group',
}; };

View File

@ -1,6 +1,6 @@
import { OrthographicCamera, Vector3 } from 'three'; import { OrthographicCamera, Vector3 } from 'three';
import { watch } from 'vue'; import { watch } from 'vue';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
export default { export default {
inject: ['three'], inject: ['three'],
@ -16,7 +16,7 @@ export default {
}, },
created() { created() {
this.camera = new OrthographicCamera(this.left, this.right, this.top, this.bottom, this.near, this.far); this.camera = new OrthographicCamera(this.left, this.right, this.top, this.bottom, this.near, this.far);
useBindProp(this, 'position', this.camera.position); bindProp(this, 'position', this.camera.position);
['left', 'right', 'top', 'bottom', 'near', 'far', 'zoom'].forEach(p => { ['left', 'right', 'top', 'bottom', 'near', 'far', 'zoom'].forEach(p => {
watch(() => this[p], () => { watch(() => this[p], () => {

View File

@ -1,6 +1,6 @@
import { PerspectiveCamera, Vector3 } from 'three'; import { PerspectiveCamera, Vector3 } from 'three';
import { watch } from 'vue'; import { watch } from 'vue';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
export default { export default {
inject: ['three'], inject: ['three'],
@ -14,7 +14,7 @@ export default {
}, },
created() { created() {
this.camera = new PerspectiveCamera(this.fov, this.aspect, this.near, this.far); this.camera = new PerspectiveCamera(this.fov, this.aspect, this.near, this.far);
useBindProp(this, 'position', this.camera.position); bindProp(this, 'position', this.camera.position);
if (this.lookAt) this.camera.lookAt(this.lookAt.x, this.lookAt.y, this.lookAt.z); if (this.lookAt) this.camera.lookAt(this.lookAt.x, this.lookAt.y, this.lookAt.z);
watch(() => this.lookAt, (v) => { this.camera.lookAt(v.x, v.y, v.z); }, { deep: true }); watch(() => this.lookAt, (v) => { this.camera.lookAt(v.x, v.y, v.z); }, { deep: true });

View File

@ -47,7 +47,8 @@ export default {
}; };
if (this.three.init(params)) { if (this.three.init(params)) {
this.three.renderer.shadowMap.enabled = this.shadow; this.renderer = this.three.renderer;
this.renderer.shadowMap.enabled = this.shadow;
if (this.three.composer) this.animateC(); if (this.three.composer) this.animateC();
else this.animate(); else this.animate();
}; };

View File

@ -24,12 +24,8 @@ export default {
} }
}, },
methods: { methods: {
// add(o) { add(o) { this.scene.add(o); },
// this.scene.add(o); remove(o) { this.scene.remove(o); },
// },
// remove(o) {
// this.scene.remove(o);
// },
}, },
render() { render() {
if (this.$slots.default) { if (this.$slots.default) {

View File

@ -3,7 +3,7 @@ import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';
import { watch } from 'vue'; import { watch } from 'vue';
import EffectPass from './EffectPass.js'; import EffectPass from './EffectPass.js';
import TiltShift from '../shaders/TiltShift.js'; import TiltShift from '../shaders/TiltShift.js';
import useBindPropValue from '../use/useBindPropValue.js'; import { bindPropValue } from '../tools.js';
export default { export default {
extends: EffectPass, extends: EffectPass,
@ -28,8 +28,8 @@ export default {
uniforms1.end = uniforms.end; uniforms1.end = uniforms.end;
uniforms1.texSize = uniforms.texSize; uniforms1.texSize = uniforms.texSize;
useBindPropValue(this, 'blurRadius', uniforms.blurRadius); bindPropValue(this, 'blurRadius', uniforms.blurRadius);
useBindPropValue(this, 'gradientRadius', uniforms.gradientRadius); bindPropValue(this, 'gradientRadius', uniforms.gradientRadius);
this.updateFocusLine(); this.updateFocusLine();
['start', 'end'].forEach(p => { ['start', 'end'].forEach(p => {

View File

@ -1,8 +1,7 @@
import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js'; import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';
import EffectPass from './EffectPass.js'; import EffectPass from './EffectPass.js';
import ZoomBlur from '../shaders/ZoomBlur.js'; import ZoomBlur from '../shaders/ZoomBlur.js';
import useBindProp from '../use/useBindProp.js'; import { bindProp, bindPropValue } from '../tools.js';
import useBindPropValue from '../use/useBindPropValue.js';
export default { export default {
extends: EffectPass, extends: EffectPass,
@ -15,8 +14,8 @@ export default {
this.passes.push(this.pass); this.passes.push(this.pass);
const uniforms = this.uniforms = this.pass.uniforms; const uniforms = this.uniforms = this.pass.uniforms;
useBindProp(this, 'center', uniforms.center.value); bindProp(this, 'center', uniforms.center.value);
useBindPropValue(this, 'strength', uniforms.strength); bindPropValue(this, 'strength', uniforms.strength);
}, },
__hmrId: 'ZoomBlurPass', __hmrId: 'ZoomBlurPass',
}; };

View File

@ -1,7 +1,7 @@
import { Color } from 'three'; import { Color } from 'three';
import { watch } from 'vue'; import { watch } from 'vue';
import { setFromProp } from '../tools.js'; import { setFromProp } from '../tools.js';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
export default { export default {
inject: { inject: {
@ -30,10 +30,10 @@ export default {
this.parent = this.group ? this.group : this.scene; this.parent = this.group ? this.group : this.scene;
}, },
mounted() { mounted() {
useBindProp(this, 'position', this.light.position); bindProp(this, 'position', this.light.position);
if (this.light.target) { if (this.light.target) {
useBindProp(this, 'target', this.light.target.position); bindProp(this, 'target', this.light.target.position);
} }
if (this.light.shadow) { if (this.light.shadow) {

View File

@ -1,7 +1,7 @@
import { MeshStandardMaterial, Vector2 } from 'three'; import { MeshStandardMaterial, Vector2 } from 'three';
import { watch } from 'vue'; import { watch } from 'vue';
import { propsValues } from '../tools.js'; import { propsValues } from '../tools.js';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
import Material from './Material'; import Material from './Material';
const props = { const props = {
@ -39,7 +39,7 @@ export default {
} }
}); });
}); });
useBindProp(this, 'normalScale', this.material.normalScale); bindProp(this, 'normalScale', this.material.normalScale);
}, },
}, },
__hmrId: 'StandardMaterial', __hmrId: 'StandardMaterial',

View File

@ -9,7 +9,7 @@ import {
} from 'three'; } from 'three';
// import { watch } from 'vue'; // import { watch } from 'vue';
import Mesh from '../meshes/Mesh.js'; import Mesh from '../meshes/Mesh.js';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
export default { export default {
extends: Mesh, extends: Mesh,
@ -33,7 +33,7 @@ export default {
initGem() { initGem() {
const cubeRT = new WebGLCubeRenderTarget(this.cubeRTSize, { format: RGBFormat, generateMipmaps: true, minFilter: LinearMipmapLinearFilter }); const cubeRT = new WebGLCubeRenderTarget(this.cubeRTSize, { format: RGBFormat, generateMipmaps: true, minFilter: LinearMipmapLinearFilter });
this.cubeCamera = new CubeCamera(this.cubeCameraNear, this.cubeCameraFar, cubeRT); this.cubeCamera = new CubeCamera(this.cubeCameraNear, this.cubeCameraFar, cubeRT);
useBindProp(this, 'position', this.cubeCamera.position); bindProp(this, 'position', this.cubeCamera.position);
this.parent.add(this.cubeCamera); this.parent.add(this.cubeCamera);
this.material.side = FrontSide; this.material.side = FrontSide;
@ -55,9 +55,9 @@ export default {
this.meshBack = new TMesh(this.geometry, this.materialBack); this.meshBack = new TMesh(this.geometry, this.materialBack);
useBindProp(this, 'position', this.meshBack.position); bindProp(this, 'position', this.meshBack.position);
useBindProp(this, 'rotation', this.meshBack.rotation); bindProp(this, 'rotation', this.meshBack.rotation);
useBindProp(this, 'scale', this.meshBack.scale); bindProp(this, 'scale', this.meshBack.scale);
this.parent.add(this.meshBack); this.parent.add(this.meshBack);
}, },
updateCubeRT() { updateCubeRT() {

View File

@ -1,6 +1,6 @@
import { InstancedMesh } from 'three'; import { InstancedMesh } from 'three';
import { watch } from 'vue'; import { watch } from 'vue';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
export default { export default {
inject: { inject: {
@ -42,9 +42,9 @@ export default {
this.mesh = new InstancedMesh(this.geometry, this.material, this.count); this.mesh = new InstancedMesh(this.geometry, this.material, this.count);
useBindProp(this, 'position', this.mesh.position); bindProp(this, 'position', this.mesh.position);
useBindProp(this, 'rotation', this.mesh.rotation); bindProp(this, 'rotation', this.mesh.rotation);
useBindProp(this, 'scale', this.mesh.scale); bindProp(this, 'scale', this.mesh.scale);
['castShadow', 'receiveShadow'].forEach(p => { ['castShadow', 'receiveShadow'].forEach(p => {
this.mesh[p] = this[p]; this.mesh[p] = this[p];

View File

@ -1,6 +1,6 @@
import { Mesh } from 'three'; import { Mesh } from 'three';
import { watch } from 'vue'; import { watch } from 'vue';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
export default { export default {
inject: { inject: {
@ -63,9 +63,9 @@ export default {
this.$emit('ready'); this.$emit('ready');
}, },
bindProps() { bindProps() {
useBindProp(this, 'position', this.mesh.position); bindProp(this, 'position', this.mesh.position);
useBindProp(this, 'rotation', this.mesh.rotation); bindProp(this, 'rotation', this.mesh.rotation);
useBindProp(this, 'scale', this.mesh.scale); bindProp(this, 'scale', this.mesh.scale);
['castShadow', 'receiveShadow'].forEach(p => { ['castShadow', 'receiveShadow'].forEach(p => {
this.mesh[p] = this[p]; this.mesh[p] = this[p];

View File

@ -6,7 +6,7 @@ import {
} from 'three'; } from 'three';
// import { watch } from 'vue'; // import { watch } from 'vue';
import Mesh from './Mesh.js'; import Mesh from './Mesh.js';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
export default { export default {
extends: Mesh, extends: Mesh,

View File

@ -7,7 +7,7 @@ import {
} from 'three'; } from 'three';
// import { watch } from 'vue'; // import { watch } from 'vue';
import Mesh from './Mesh.js'; import Mesh from './Mesh.js';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
export default { export default {
extends: Mesh, extends: Mesh,
@ -31,7 +31,7 @@ export default {
initMirrorMesh() { initMirrorMesh() {
const cubeRT = new WebGLCubeRenderTarget(this.cubeRTSize, { mapping: CubeRefractionMapping, format: RGBFormat, generateMipmaps: true, minFilter: LinearMipmapLinearFilter }); const cubeRT = new WebGLCubeRenderTarget(this.cubeRTSize, { mapping: CubeRefractionMapping, format: RGBFormat, generateMipmaps: true, minFilter: LinearMipmapLinearFilter });
this.cubeCamera = new CubeCamera(this.cubeCameraNear, this.cubeCameraFar, cubeRT); this.cubeCamera = new CubeCamera(this.cubeCameraNear, this.cubeCameraFar, cubeRT);
useBindProp(this, 'position', this.cubeCamera.position); bindProp(this, 'position', this.cubeCamera.position);
this.parent.add(this.cubeCamera); this.parent.add(this.cubeCamera);
this.material.envMap = cubeRT.texture; this.material.envMap = cubeRT.texture;

View File

@ -1,5 +1,5 @@
import { Sprite, SpriteMaterial, TextureLoader } from 'three'; import { Sprite, SpriteMaterial, TextureLoader } from 'three';
import useBindProp from '../use/useBindProp.js'; import { bindProp } from '../tools.js';
export default { export default {
emits: ['ready', 'loaded'], emits: ['ready', 'loaded'],
@ -21,8 +21,8 @@ export default {
this.material = new SpriteMaterial({ map: this.texture }); this.material = new SpriteMaterial({ map: this.texture });
this.sprite = new Sprite(this.material); this.sprite = new Sprite(this.material);
this.geometry = this.sprite.geometry; this.geometry = this.sprite.geometry;
useBindProp(this, 'position', this.sprite.position); bindProp(this, 'position', this.sprite.position);
useBindProp(this, 'scale', this.sprite.scale); bindProp(this, 'scale', this.sprite.scale);
this.parent.add(this.sprite); this.parent.add(this.sprite);
this.$emit('ready'); this.$emit('ready');

View File

@ -1,3 +1,4 @@
import { createApp as _createApp } from 'vue';
import * as TROIS from './index.js'; import * as TROIS from './index.js';
export const TroisJSVuePlugin = { export const TroisJSVuePlugin = {
@ -90,3 +91,7 @@ export const TroisJSVuePlugin = {
}); });
}, },
}; };
export function createApp(params) {
return _createApp(params).use(TroisJSVuePlugin);
};

View File

@ -1,3 +1,5 @@
import { toRef, watch } from 'vue';
export function setFromProp(o, prop) { export function setFromProp(o, prop) {
if (prop instanceof Object) { if (prop instanceof Object) {
Object.entries(prop).forEach(([key, value]) => { Object.entries(prop).forEach(([key, value]) => {
@ -6,6 +8,21 @@ export function setFromProp(o, prop) {
} }
}; };
export function bindProp(comp, prop, object) {
const ref = toRef(comp, prop);
setFromProp(object, ref.value);
watch(ref, () => {
setFromProp(object, ref.value);
}, { deep: true });
};
export function bindPropValue(src, srcProp, dst, dstProp = 'value') {
dst[dstProp] = src[srcProp];
watch(() => src[srcProp], (value) => {
dst[dstProp] = value;
});
};
export function propsValues(props, exclude) { export function propsValues(props, exclude) {
const values = {}; const values = {};
Object.entries(props).forEach(([key, value]) => { Object.entries(props).forEach(([key, value]) => {

View File

@ -1,12 +0,0 @@
import { toRef, watch } from 'vue';
import { setFromProp } from '../tools.js';
export default function useBindProp(comp, prop, object) {
if (comp[prop]) {
const ref = toRef(comp, prop);
setFromProp(object, ref.value);
watch(ref, () => {
setFromProp(object, ref.value);
}, { deep: true });
}
};

View File

@ -1,10 +0,0 @@
import { watch } from 'vue';
export default function useBindPropValue(src, srcProp, dst, dstProp = 'value') {
if (src[srcProp]) {
dst[dstProp] = src[srcProp];
watch(() => src[srcProp], (value) => {
dst[dstProp] = value;
});
}
};