赞
踩
在引入多个模型时,需要通过按钮在一个页面中展示多个不同的模型
需要设置点击按钮,在这里按钮不做过多阐述:
function gltfModel1() {
loader.load("../../../../public/工厂.glb", function (gltf) {
scene.add(gltf.scene)
})
}
function gltfModel2() {
// glb
loader.load("../../../../public/aaa.glb", function (gltf) { //gltf加载成功后返回一个对象
scene.add(gltf.scene)
})
}
const SpanActive = (index, val) => {
spanIndex.value = index
clearnModel()
document.getElementById('my-three')?.appendChild(renderer.domElement)
if (index === 0) {
gltfModel1()
} else {
gltfModel2()
}
}
// 模型切换清除上个模型
function clearnModel() {
if (scene !== null && scene.children.length > 3) {
scene.children = []
composer.removePass(outlinePass);
// 必须要清空当前div下的canvas不然canvas会继续叠加
const domDiv = document.getElementById('my-three')
if (domDiv !== null) {
domDiv.removeChild(domDiv.firstChild)
}
init()
renderModel()
render()
}
}
<template> <div> <div class="butCheck nc-flex-wrap"> <div class="btnPad" v-for="(span, index) in spanText" :key="index" :class="{ stopTextActive: spanIndex === index }" @click="SpanActive(index,span)"> {{ span.type }} </div> </div> <div id="my-three"></div> </div> </template> <script setup> import {ref, onMounted} from 'vue' import * as THREE from 'three' import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls' import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader'; //gltf const {proxy} = getCurrentInstance(); onMounted(() => { document.getElementById('my-three')?.appendChild(renderer.domElement) init() renderModel() gltfModel1() render() }) // 按钮 const spanText = ref([ {type: '模型1', value: 'one'}, {type: '模型2', value: 'two'}, ]) const spanIndex = ref(0) const SpanActive = (index, val) => { spanIndex.value = index clearnModel() document.getElementById('my-three')?.appendChild(renderer.domElement) if (index === 0) { gltfModel1() } else { gltfModel2() } } const width = window.innerWidth, height = window.innerHeight; const scene = new THREE.Scene(); const renderer = new THREE.WebGLRenderer() const loader = new GLTFLoader(); const camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000); const controls = new OrbitControls(camera, renderer.domElement) function init() { //光源 const ambient = new THREE.AmbientLight(0xffffff, 0.4); scene.add(ambient); const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); directionalLight.position.set(400, 200, 300); scene.add(directionalLight); //设置相机位置 camera.position.set(300, 300, 300); //设置相机方向 camera.lookAt(0, 0, 0); //辅助坐标轴 const axesHelper = new THREE.AxesHelper(200);//参数200标示坐标系大小,可以根据场景大小去设置 scene.add(axesHelper); scene.background = new THREE.Color(0xeaeaea); renderer.setPixelRatio(window.devicePixelRatio); renderer.antialias = true; } function gltfModel1() { loader.load("../../../../public/工厂.glb", function (gltf) { scene.add(gltf.scene) }) } function gltfModel2() { // glb loader.load("../../../../public/aaa.glb", function (gltf) { scene.add(gltf.scene) }) } function renderModel() { //渲染 renderer.setSize(width, height)//设置渲染区尺寸 renderer.render(scene, camera)//执行渲染操作、指定场景、相机作为参数 renderer.toneMapping = THREE.ACESFilmicToneMapping; // 设置曝光度 renderer.toneMappingExposure = 1.5; // 适当调整曝光度 controls.minPolarAngle = Math.PI / 4; // 最小极角为 45 度 controls.maxPolarAngle = Math.PI / 2; // 最大极角为 90 度 } function render() { renderer.render(scene, camera); controls.update() requestAnimationFrame(render); } // 模型切换清除上个模型 function clearnModel() { if (scene !== null && scene.children.length > 3) { scene.children = [] composer.removePass(outlinePass); // 必须要清空当前div下的canvas不然canvas会继续叠加 const domDiv = document.getElementById('my-three') if (domDiv !== null) { domDiv.removeChild(domDiv.firstChild) } init() renderModel() render() } } </script> <style scoped lang="scss"> .butCheck { position: absolute; width: 100%; padding: 2%; .btnPad { padding: 10px 18px; text-align: center; min-width: 100px; color: white; border: 1px solid black; cursor: pointer; margin-left: 20px; } .stopTextActive { background-color: #58b1fd; border: 1px solid #58b1fd; } } </style>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。