From 45b0241f996182dff706942f8394ecd599e82276 Mon Sep 17 00:00:00 2001 From: skyun Date: Fri, 20 Oct 2023 04:59:44 +0800 Subject: [PATCH] Auto Commit --- src/threejs-utils/threejs-manager.js | 79 ++++++++------ src/threejs-utils/threejs-tools.js | 23 ++-- src/views/light-option.vue | 157 ++++++++++++++++++++++++--- src/views/lights.vue | 14 ++- 4 files changed, 210 insertions(+), 63 deletions(-) diff --git a/src/threejs-utils/threejs-manager.js b/src/threejs-utils/threejs-manager.js index a5aa4c9..a1dba55 100644 --- a/src/threejs-utils/threejs-manager.js +++ b/src/threejs-utils/threejs-manager.js @@ -1,46 +1,43 @@ -import { onMounted } from 'vue' +import { ref, reactive, toRef, watch, onMounted } from 'vue' import * as THREE from 'three' import { OrbitControls } from 'three/addons/controls/OrbitControls.js' import * as TT from './threejs-tools' export default class ThreejsManager { - constructor(container) { - // init scene camera + constructor() { + this.animateFuncs = {} + this.lights = reactive([]) + } + addScene() { this.scene = new THREE.Scene() - - // init renderer + } + addRenderer() { this.renderer = new THREE.WebGLRenderer() - - this.animateFuncs = {} - const animate = () => { - requestAnimationFrame(animate) - Object.values(this.animateFuncs).forEach(animFunc => animFunc()) - this.renderer.render(this.scene, this.camera) + } + startRender(container) { + this.renderer.rendering = true + container.value.appendChild(this.renderer.domElement) + this.renderer.setSize(container.value.clientWidth, container.value.clientHeight) + this.camera = new THREE.PerspectiveCamera(60, container.value.clientWidth / container.value.clientHeight, 0.1, 1000) + this.camera.position.set(0, 10, 10) + this.controls = new OrbitControls(this.camera, this.renderer.domElement); + this.animate() + } + animate() { + if (!this.renderer.rendering) { + return } - - onMounted(() => { - container.value.appendChild(this.renderer.domElement) - this.renderer.setSize(container.value.clientWidth, container.value.clientHeight) - - this.camera = new THREE.PerspectiveCamera(60, container.value.clientWidth / container.value.clientHeight, 0.1, 1000) - this.camera.position.set(0, 10, 10) - this.controls = new OrbitControls(this.camera, this.renderer.domElement); - - animate() - }) - - this.initDefaultScene() + requestAnimationFrame(this.animate.bind(this)) + Object.values(this.animateFuncs).forEach(animFunc => animFunc()) + this.renderer.render(this.scene, this.camera) } + initDefaultScene() { this.scene.add(new THREE.GridHelper(100, 100, 0x660000, 0x004400)) this.addPlane() this.addCube() - this.lightHelpers = [] - this.lightHelpers.push(this.addAmbientLight()) - this.lightHelpers.push(this.addDirectLight()) - this.lightHelpers.push(this.addPointLight()) - this.lightHelpers.push(this.addSpotLight()) + this.addAmbientLight() } onAnimate(name, animFunc) { this.animateFuncs[name] = animFunc @@ -61,15 +58,31 @@ export default class ThreejsManager { return cube } addAmbientLight() { - return TT.addLight(this.scene, THREE.AmbientLight, [0xffffff, .1]) + const light = TT.createLight(THREE.AmbientLight, [0xffffff, .1]) + this.scene.add(light) + this.lights.push(light) + light.name = light.type + `-` + this.lights.length + return light } addDirectLight() { - return TT.addLight(this.scene, THREE.DirectionalLight, [0xff0000, 1], [-4, 5, -3]) + const light = TT.createLight(THREE.DirectionalLight, [0xff0000, 1], [-4, 5, -3]) + this.scene.add(light) + this.lights.push(light) + light.name = light.type + `-` + this.lights.length + return light } addPointLight() { - return TT.addLight(this.scene, THREE.PointLight, [0x00ff00, 50], [3, 3, -2], .5) + const light = TT.createLight(THREE.PointLight, [0x00ff00, 50, 10], [3, 3, -2]) + this.scene.add(light) + this.lights.push(light) + light.name = light.type + `-` + this.lights.length + return light } addSpotLight() { - return TT.addLight(this.scene, THREE.SpotLight, [0x0000ff, 1000, 7, .2, .5], [-1, 6, 1]) + const light = TT.createLight(THREE.SpotLight, [0x0000ff, 1000, 7, .2, .5], [-1, 6, 1]) + this.scene.add(light) + this.lights.push(light) + light.name = light.type + `-` + this.lights.length + return light } } diff --git a/src/threejs-utils/threejs-tools.js b/src/threejs-utils/threejs-tools.js index 3cbb195..c15d3a1 100644 --- a/src/threejs-utils/threejs-tools.js +++ b/src/threejs-utils/threejs-tools.js @@ -20,25 +20,22 @@ export function createCube() { return cube } -export function createLight(lightConstuctor, lightArgs = [0xffffff, 1], lightPos = [0, 0, 1], ...helperArgs) { +export function createLight(lightConstuctor, lightArgs = [0xffffff, 1], lightPos = [0, 0, 1]) { const light = new lightConstuctor(...lightArgs) light.position.set(...lightPos) + return light +} + +export function createLightHelper(light, ...helperArgs) { let helperType - if (lightConstuctor === THREE.AmbientLight) { - } else if (lightConstuctor === THREE.DirectionalLight) { + if (light.type === 'AmbientLight') { + } else if (light.type === 'DirectionalLight') { helperType = THREE.DirectionalLightHelper - } else if (lightConstuctor === THREE.PointLight) { + } else if (light.type === 'PointLight') { helperType = THREE.PointLightHelper - } else if (lightConstuctor === THREE.SpotLight) { + } else if (light.type === 'SpotLight') { helperType = THREE.SpotLightHelper } - const helper = helperType && new helperType(light, ...helperArgs) || {light, isMockHelper: true} + const helper = !!helperType && new helperType(light, ...helperArgs) || null return helper } - -export function addLight(scene, ...createArgs) { - const helper = createLight(...createArgs) - scene.add(helper.light) - helper.isMockHelper || scene.add(helper) - return helper -} \ No newline at end of file diff --git a/src/views/light-option.vue b/src/views/light-option.vue index af25c54..7bad72d 100644 --- a/src/views/light-option.vue +++ b/src/views/light-option.vue @@ -1,39 +1,166 @@ \ No newline at end of file diff --git a/src/views/lights.vue b/src/views/lights.vue index d77eabf..b8bb318 100644 --- a/src/views/lights.vue +++ b/src/views/lights.vue @@ -1,8 +1,7 @@ @@ -14,6 +13,17 @@ import LightOption from './light-option.vue' const container = ref(null) const tm = new ThreeManager(container) +tm.addScene() +tm.addRenderer() +onMounted(() => { + tm.startRender(container) +}) + +tm.initDefaultScene() + +tm.addDirectLight() +tm.addPointLight() +tm.addSpotLight() -- GitLab