1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
| <script setup> import { ref, onMounted, onBeforeUnmount } from 'vue'; import { useRouter } from 'vue-router' import * as THREE from 'three' import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js' import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js' import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js' import * as GeometryCompressionUtils from 'three/addons/utils/GeometryCompressionUtils.js'; import gsap from 'gsap' import JSZip from 'jszip' import JSZipUtils from 'jszip-utils'
const router = useRouter() const width = window.innerWidth, height = window.innerHeight; const threeContainer = ref(null); const guiContainer = ref(null);
// 车辆外观 let wheelsCar, carBody, frontCar, hoodCar, backCar, glassCar; const bodyMaterial = new THREE.MeshPhysicalMaterial({ color: 0xff00ff, roughness: 0.5, // 粗糙度 metalness: 1, // 金属度 clearcoat: 1, clearcoatRoughness: 0 // 清漆 });
const wheelsCarMaterial = new THREE.MeshPhysicalMaterial({ color: 0x000000, roughness: 0.01, // 粗糙度 metalness: 0.1, // 金属度 transmission: 1 })
const glassCarMaterial = new THREE.MeshPhysicalMaterial({ color: 0xffffff, transmission: 1, roughness: 0, // 粗糙度 metalness: 0, // 金属度 transparent: true, });
// 创建场景 const scene = new THREE.Scene();
// init WebGL渲染器 const renderer = new THREE.WebGLRenderer({ antialias: true, // 抗锯齿 }); renderer.setSize( width, height );
// 设置色调映射 // renderer.toneMapping = THREE.ACESFilmicToneMapping; // renderer.toneMappingExposure = 1; // 色调映射曝光 // renderer.outputEncoding = THREE.sRGBEncoding;
// init 相机 const camera = new THREE.PerspectiveCamera( 75, width / height, 0.1, 1000 ); camera.position.set( 0, 2, 6 ); camera.updateProjectionMatrix(); // 更新相机投影矩阵
// init 轨道控制器 const controls = new OrbitControls(camera, renderer.domElement) controls.enableDamping = true; // 启用阻尼(惯性)
// 坐标辅助 // const axesHelper = new THREE.AxesHelper( 5 ); // scene.add(axesHelper); // init 加载器gltf const gltfLoader = new GLTFLoader(); // init 加载器draco const dracoLoader = new DRACOLoader(); gltfLoader.setDRACOLoader(dracoLoader) dracoLoader.setDecoderPath('/draco/gltf/');
gltfLoader.load('/su8-pipeline.glb', (gltf) => { gltf.scene.traverse(function (child) { const dreamCar = gltf.scene; dreamCar.traverse( child => { if ( child.isMesh ) { // console.log( child.name ) // if ( child.name !== 'Object_45' ) { // 车身 // child.visible = false; // } // 车轮 // if ( child.name === 'Object_56' ) { // wheelsCar = child; // wheelsCar.material = wheelsCarMaterial; // }
// 玻璃 if ( child.name === 'Object_21' || child.name === 'Object_40' || child.name === 'Object_49' || child.name === 'Object_36') { glassCar = child; glassCar.material = glassCarMaterial; }
// 车身 if ( child.name === 'Object_18' || child.name === 'Object_32' || child.name === 'Object_39' || child.name === 'Object_31' || child.name === 'Object_52' || child.name === 'Object_45' ) { carBody = child; carBody.material = bodyMaterial; }
// Object_21:前后档玻璃 Object_45: 主驾门 Object_36:主驾车窗玻璃 Object_39:主驾后门 Object_40:主驾后门玻璃 // Object_32: 副驾门 Object_49:副驾门玻璃 Object_52:副驾后门 Object_53:副驾后门玻璃 Object_52:后挡风玻璃 if ( child.name === 'Object_56' ) { wheelsCar = child; } // if ( child.name === 'Object_19' ) { // frontCar = child; // } } }) scene.add(dreamCar); }); }); // const zipFile = await fetch('/su7.zip').then(response => response.arrayBuffer()); // 获取zip文件的二进制数据 // const new_zip = new JSZip(); // 实例化jszip // JSZipUtils.getBinaryContent("/su7.zip", function (err, data) { // if (err) { // throw err; // or handle err // }
// new_zip.loadAsync(data).then(function (res) { // let fileList = res.files; // for (let key in fileList) { // console.log(key) // new_zip // .file(key) // .async("arraybuffer") // .then((content) => { // // Blob构造文件地址,通过url加载模型 // let blob = new Blob([content]); // let modelUrl = URL.createObjectURL(blob); // console.log(modelUrl); // gltfLoader.load(modelUrl, (gltf) => { // gltf.scene.traverse(function (child) { // const dreamCar = gltf.scene; // dreamCar.traverse( child => { // if ( child.isMesh ) { // console.log( child.name ) // // if ( child.name !== 'Object_45' ) { // 车身 // // child.visible = false; // // } // // 车轮 // if ( child.name === 'Object_56' ) { // wheelsCar = child; // wheelsCar.material = wheelsCarMaterial; // }
// // 玻璃 // if ( child.name === 'Object_21' // || child.name === 'Object_40' // || child.name === 'Object_49' // || child.name === 'Object_36') { // glassCar = child; // glassCar.material = glassCarMaterial; // }
// // 车身 // if ( child.name === 'Object_18' // || child.name === 'Object_32' // || child.name === 'Object_39' // || child.name === 'Object_31' // || child.name === 'Object_52' // || child.name === 'Object_45' // ) { // carBody = child; // carBody.material = bodyMaterial; // }
// // Object_21:前后档玻璃 Object_45: 主驾门 Object_36:主驾车窗玻璃 Object_39:主驾后门 Object_40:主驾后门玻璃 // // Object_32: 副驾门 Object_49:副驾门玻璃 Object_52:副驾后门 Object_53:副驾后门玻璃 Object_52:后挡风玻璃 // if ( child.name === 'Object_56' ) { // wheelsCar = child; // } // // if ( child.name === 'Object_19' ) { // // frontCar = child; // // } // } // }) // scene.add(dreamCar); // }); // }); // }); // } // }) // })
// 添加光源 前、后、左、右、顶 const light1 = new THREE.DirectionalLight( 0xffffff, 1 ); light1.position.set( 0, 0, 10 ); scene.add( light1 ); const light2 = new THREE.DirectionalLight( 0xffffff, 1 ); light2.position.set( 0, 0, -10 ); scene.add( light2 ); const light3 = new THREE.DirectionalLight( 0xffffff, 1 ); light3.position.set( 0, 10, 0 ); scene.add( light3 ); const light4 = new THREE.DirectionalLight( 0xffffff, 1 ); light4.position.set( 10, 0, 0 ); scene.add( light4 ); const light5 = new THREE.DirectionalLight( 0xffffff, 1 ); light5.position.set( -10, 0, 0 ); scene.add( light5 ); const light6 = new THREE.DirectionalLight( 0xffffff, 1 ); light6.position.set( 10, 5, 0 ); scene.add( light6 );
const render = () => { controls.update(); renderer.render( scene, camera ); requestAnimationFrame( render ); }
renderer.setClearColor('#000'); scene.background = new THREE.Color('#ccc'); scene.environment = new THREE.Color('#ccc');
// 添加网格地面 const gridHelper = new THREE.GridHelper( 10, 10 ); gridHelper.material.opacity = 0.5; gridHelper.material.transparent = true; scene.add( gridHelper );
onMounted(() => { render(); document.body.appendChild( renderer.domElement ); // 将渲染器添加到DOM中 // init renderer`s backgroundColor })
onBeforeUnmount(() => { memoryClean(); // gui.destroy(); // 销毁GUI实例 })
const memoryClean = () =>{ const meshes = []; scene.traverse(function (object) { object.isMesh && meshes.push(object); }); for (let i = 0; i < meshes.length; i++) { const mesh = meshes[i]; mesh.material.dispose(); mesh.geometry.dispose(); scene.remove(mesh); } }
const back = () => { router.go(-1) } </script>
<template> <div class="btn-group"> <button class="back-btn" @click="back">返回</button> </div> <div ref="threeContainer" class="threeContainer"></div> <div ref="guiContainer"></div> </template>
|