1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00

improve poc

This commit is contained in:
Kevin Levron 2020-09-14 22:53:30 +02:00
parent b177aa4341
commit fb5fcfdb6c
7 changed files with 77 additions and 59 deletions

View File

@ -1,14 +1,14 @@
<template> <template>
<Renderer ref="renderer"> <Renderer ref="renderer" :orbit-ctrl="{ enableDamping: true, dampingFactor: 0.05 }">
<PerspectiveCamera :position="{ z: 100 }"></PerspectiveCamera> <PerspectiveCamera :position="{ z: 100 }"></PerspectiveCamera>
<PhongMaterial name="mat1" color="#ff0000"></PhongMaterial> <PhongMaterial id="material1" color="#ff0000"></PhongMaterial>
<LambertMaterial name="mat2" color="#0000ff"></LambertMaterial> <LambertMaterial id="material2" color="#0000ff"></LambertMaterial>
<Scene> <Scene id="scene1">
<PointLight :position="{ x: 0, y: 50, z: 50 }"></PointLight> <PointLight :position="{ x: 0, y: 50, z: 50 }"></PointLight>
<Box ref="box" :size="10" :rotation="{ x: 0.5, y: 0.25 }" material="mat1"></Box> <Box ref="box" :size="10" :rotation="{ y: Math.PI / 4 }" material="material1"></Box>
<Sphere ref="sphere" :radius="10" :position="{ x: 50 }" material="mat2"></Sphere> <Sphere ref="sphere" :radius="10" :position="{ x: 50 }" material="material2"></Sphere>
</Scene> </Scene>
</Renderer> </Renderer>
</template> </template>
@ -28,5 +28,13 @@ export default {
Box, Sphere, Box, Sphere,
LambertMaterial, PhongMaterial, LambertMaterial, PhongMaterial,
}, },
mounted() {
const renderer = this.$refs.renderer;
const box = this.$refs.box.mesh;
renderer.onBeforeRender(() => {
box.rotation.x += 0.01;
});
},
}; };
</script> </script>

View File

@ -1,5 +1,4 @@
import { PerspectiveCamera, Vector3 } from 'three'; import { PerspectiveCamera } from 'three';
import { setFromProp } from '../tools.js'; import { setFromProp } from '../tools.js';
export default { export default {
@ -10,10 +9,6 @@ export default {
default: 50, default: 50,
}, },
position: Object, position: Object,
// position: {
// type: Object,
// default: new Vector3(),
// },
}, },
created() { created() {
const camera = new PerspectiveCamera(this.fov); const camera = new PerspectiveCamera(this.fov);

View File

@ -7,30 +7,25 @@
<script> <script>
import useThree from './useThree'; import useThree from './useThree';
const animateCallbacks = [];
export function useAnimate(callback) {
animateCallbacks.push(callback);
};
export default { export default {
props: { props: {
antialias: {
type: Boolean,
default: true,
},
alpha: { alpha: {
type: Boolean, type: Boolean,
default: false, default: false,
}, },
animate: { orbitCtrl: {
type: Function, default: false,
}, },
}, },
data() {
return {
raf: true,
};
},
setup(props) { setup(props) {
return { return {
three: useThree(), three: useThree(),
beforeRender: [],
raf: true,
}; };
}, },
provide() { provide() {
@ -39,25 +34,30 @@ export default {
}; };
}, },
mounted() { mounted() {
// console.log('Renderer mounted'); const params = {
this.three.init({
canvas: this.$refs.canvas, canvas: this.$refs.canvas,
}); antialias: this.antialias,
alpha: this.alpha,
orbit_ctrl: this.orbitCtrl,
};
this._animate(); if (this.three.init(params)) {
this.animate();
};
}, },
beforeUnmount() { beforeUnmount() {
// console.log('Renderer beforeUnmount');
// animateCallbacks.splice(0);
this.raf = false; this.raf = false;
this.beforeRender.splice(0);
this.three.dispose(); this.three.dispose();
}, },
methods: { methods: {
_animate() { onBeforeRender(callback) {
if (this.raf) requestAnimationFrame(this._animate); this.beforeRender.push(callback);
// if (this.animate) this.animate(); },
animateCallbacks.forEach(c => c()); animate() {
if (this.three.scene) this.three.render(this.three.scene); if (this.raf) requestAnimationFrame(this.animate);
this.beforeRender.forEach(c => c());
this.three.render();
}, },
}, },
}; };

View File

@ -1,12 +1,14 @@
import { Scene } from 'three'; import { Scene } from 'three';
export default { export default {
emits: ['scene-ready'], props: {
inject: ['three'], id: String,
setup (props) { },
setup () {
const scene = new Scene(); const scene = new Scene();
return { scene }; return { scene };
}, },
inject: ['three'],
provide() { provide() {
return { return {
scene: this.scene, scene: this.scene,
@ -14,7 +16,6 @@ export default {
}, },
mounted() { mounted() {
this.three.scene = this.scene; this.three.scene = this.scene;
this.$emit('scene-ready');
}, },
render() { render() {
return this.$slots.default(); return this.$slots.default();

View File

@ -18,9 +18,7 @@ export default function useThree() {
canvas: null, canvas: null,
antialias: true, antialias: true,
alpha: false, alpha: false,
camera_fov: 50, orbit_ctrl: false,
camera_pos: new Vector3(0, 0, 100),
camera_ctrl: false,
mouse_move: false, mouse_move: false,
mouse_raycast: false, mouse_raycast: false,
window_resize: true, window_resize: true,
@ -46,6 +44,7 @@ export default function useThree() {
camera: null, camera: null,
cameraCtrl: null, cameraCtrl: null,
materials: {}, materials: {},
scene: null,
size, size,
mouse, mouseV3, mouse, mouseV3,
init, init,
@ -66,17 +65,24 @@ export default function useThree() {
obj.renderer = new WebGLRenderer({ canvas: conf.canvas, antialias: conf.antialias, alpha: conf.alpha }); obj.renderer = new WebGLRenderer({ canvas: conf.canvas, antialias: conf.antialias, alpha: conf.alpha });
// obj.camera = new PerspectiveCamera(conf.camera_fov); if (!obj.scene) {
// obj.camera.position.copy(conf.camera_pos); console.error('Missing Scene');
return;
}
// if (conf.camera_ctrl) { if (!obj.camera) {
// obj.cameraCtrl = new OrbitControls(obj.camera, obj.renderer.domElement); console.error('Missing Camera');
// if (conf.camera_ctrl instanceof Object) { return;
// for (const [key, value] of Object.entries(conf.camera_ctrl)) { }
// obj.cameraCtrl[key] = value;
// } if (conf.orbit_ctrl) {
// } obj.orbitCtrl = new OrbitControls(obj.camera, obj.renderer.domElement);
// } if (conf.orbit_ctrl instanceof Object) {
for (const [key, value] of Object.entries(conf.orbit_ctrl)) {
obj.orbitCtrl[key] = value;
}
}
}
if (conf.window_resize) { if (conf.window_resize) {
onResize(); onResize();
@ -94,9 +100,9 @@ export default function useThree() {
/** /**
* default render * default render
*/ */
function render(scene) { function render() {
if (obj.cameraCtrl) obj.cameraCtrl.update(); if (obj.orbitCtrl) obj.orbitCtrl.update();
obj.renderer.render(scene, obj.camera); obj.renderer.render(obj.scene, obj.camera);
} }
/** /**

View File

@ -1,14 +1,14 @@
export default { export default {
inject: ['three'], inject: ['three'],
props: { props: {
name: String, id: String,
color: { color: {
type: String, type: String,
default: '#ffffff', default: '#ffffff',
}, },
}, },
mounted() { mounted() {
this.three.materials[this.name] = this.material; this.three.materials[this.id] = this.material;
}, },
render() { render() {
return []; return [];

View File

@ -8,8 +8,16 @@ export default {
extends: Mesh, extends: Mesh,
props: { props: {
radius: Number, radius: Number,
widthSegments: {
type: Number,
default: 12,
},
heightSegments: {
type: Number,
default: 12,
},
}, },
created() { created() {
this.geometry = new SphereBufferGeometry(this.radius, 32, 32); this.geometry = new SphereBufferGeometry(this.radius, this.widthSegments, this.heightSegments);
}, },
}; };