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-09-19 11:24:03 +02:00
parent b6a8727c3e
commit f4734ea224
8 changed files with 63 additions and 7 deletions

View File

@ -166,6 +166,7 @@ export default function useThree() {
obj.mouse_move_element.removeEventListener('mouseleave', onMouseleave); obj.mouse_move_element.removeEventListener('mouseleave', onMouseleave);
} }
if (obj.orbitCtrl) obj.orbitCtrl.dispose(); if (obj.orbitCtrl) obj.orbitCtrl.dispose();
this.renderer.dispose();
} }
/** /**

View File

@ -0,0 +1,9 @@
import { DirectionalLight } from 'three';
import Light from './Light.js';
export default {
extends: Light,
created() {
this.light = new DirectionalLight(this.color, this.intensity);
},
};

View File

@ -1,3 +1,4 @@
export { default as AmbientLight } from './AmbientLight.js'; export { default as AmbientLight } from './AmbientLight.js';
export { default as DirectionalLight } from './DirectionalLight.js';
export { default as PointLight } from './PointLight.js'; export { default as PointLight } from './PointLight.js';
export { default as SpotLight } from './SpotLight.js'; export { default as SpotLight } from './SpotLight.js';

25
src/meshes/Image.js Normal file
View File

@ -0,0 +1,25 @@
import { DoubleSide, MeshBasicMaterial, PlaneBufferGeometry, TextureLoader } from 'three';
import Mesh from './Mesh.js';
export default {
extends: Mesh,
props: {
src: String,
width: {
type: Number,
default: 1,
},
height: {
type: Number,
default: 1,
},
},
created() {
this.geometry = new PlaneBufferGeometry(this.width, this.height, 1, 1);
this.material = new MeshBasicMaterial({ map: new TextureLoader().load(this.src), side: DoubleSide });
},
unmounted() {
// this material is not mounted, it won't be auto dispose
this.material.dispose();
},
};

View File

@ -4,7 +4,7 @@ import { setFromProp } from '../tools.js';
export default { export default {
inject: ['three', 'scene'], inject: ['three', 'scene'],
props: { props: {
material: String, materialId: String,
count: Number, count: Number,
position: Object, position: Object,
castShadow: { castShadow: {
@ -32,7 +32,7 @@ export default {
} }
}, },
mounted() { mounted() {
this.mesh = new InstancedMesh(this.conf.geometry, this.three.materials[this.material], this.count); this.mesh = new InstancedMesh(this.conf.geometry, this.three.materials[this.materialId], this.count);
setFromProp(this.mesh.position, this.position); setFromProp(this.mesh.position, this.position);
this.mesh.castShadow = this.castShadow; this.mesh.castShadow = this.castShadow;
this.mesh.receiveShadow = this.receiveShadow; this.mesh.receiveShadow = this.receiveShadow;

View File

@ -1,3 +1,4 @@
import { watch } from 'vue';
import { Mesh } from 'three'; import { Mesh } from 'three';
import { setFromProp } from '../tools.js'; import { setFromProp } from '../tools.js';
@ -5,7 +6,7 @@ export default {
inject: ['three', 'scene'], inject: ['three', 'scene'],
emits: ['ready'], emits: ['ready'],
props: { props: {
material: String, materialId: String,
position: Object, position: Object,
rotation: Object, rotation: Object,
scale: Object, scale: Object,
@ -26,14 +27,29 @@ export default {
}, },
methods: { methods: {
initMesh() { initMesh() {
this.mesh = new Mesh(this.geometry, this.three.materials[this.material]); if (!this.material && this.materialId) {
this.material = this.three.materials[this.materialId];
}
this.mesh = new Mesh(this.geometry, this.material);
this.updateMesh();
this.scene.add(this.mesh);
this.addWatchers();
this.$emit('ready');
},
addWatchers() {
['position', 'rotation', 'scale'].forEach(p => {
watch(() => this[p], () => {
setFromProp(this.mesh[p], this[p]);
}, { deep: true });
});
},
updateMesh() {
setFromProp(this.mesh.position, this.position); setFromProp(this.mesh.position, this.position);
setFromProp(this.mesh.rotation, this.rotation); setFromProp(this.mesh.rotation, this.rotation);
setFromProp(this.mesh.scale, this.scale); setFromProp(this.mesh.scale, this.scale);
this.mesh.castShadow = this.castShadow; this.mesh.castShadow = this.castShadow;
this.mesh.receiveShadow = this.receiveShadow; this.mesh.receiveShadow = this.receiveShadow;
this.scene.add(this.mesh);
this.$emit('ready');
}, },
}, },
render() { render() {

View File

@ -1,4 +1,5 @@
export { default as Box } from './Box.js'; export { default as Box } from './Box.js';
export { default as Image } from './Image.js';
export { default as Plane } from './Plane.js'; export { default as Plane } from './Plane.js';
export { default as Sphere } from './Sphere.js'; export { default as Sphere } from './Sphere.js';
export { default as Text } from './Text.js'; export { default as Text } from './Text.js';

View File

@ -12,6 +12,7 @@ export const TroisJSVuePlugin = {
'SphereGeometry', 'SphereGeometry',
'AmbientLight', 'AmbientLight',
'DirectionalLight',
'PointLight', 'PointLight',
'SpotLight', 'SpotLight',
@ -24,11 +25,13 @@ export const TroisJSVuePlugin = {
'SubSurfaceMaterial', 'SubSurfaceMaterial',
'Box', 'Box',
'InstancedMesh', 'Image',
'Plane', 'Plane',
'Sphere', 'Sphere',
'Text', 'Text',
'InstancedMesh',
'BokehPass', 'BokehPass',
'EffectComposer', 'EffectComposer',
'RenderPass', 'RenderPass',