three.js 修改模型的材质属性 并结合gsap材质颜色修改动画
import * as THREE from 'three'
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 { GUI } from "three/addons/libs/lil-gui.module.min.js"
import gsap from 'gsap'const box = document.getElementById('box')const scene = new THREE.Scene()const camera = new THREE.PerspectiveCamera(75, box.clientWidth / box.clientHeight, 0.1, 1000)camera.position.set(1, 2, 2)const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })renderer.setSize(box.clientWidth, box.clientHeight)box.appendChild(renderer.domElement)const controls = new OrbitControls(camera, renderer.domElement)controls.enableDamping = truewindow.onresize = () => {renderer.setSize(box.clientWidth, box.clientHeight)camera.aspect = box.clientWidth / box.clientHeightcamera.updateProjectionMatrix()}animate()function animate() {requestAnimationFrame(animate)controls.update()renderer.render(scene, camera)}scene.add(new THREE.AmbientLight(0xffffff, 0.2))const pointLight = new THREE.PointLight(0xffffff, 1.5, 0, 2)pointLight.position.set(5, 5, 5)scene.add(pointLight)const directionalLight = new THREE.DirectionalLight(0xffffff, 2)directionalLight.position.set(-5, 5, -5)scene.add(directionalLight)let group// 加载模型 gltf/ glb draco解码器
const loader = new GLTFLoader()loader.setDRACOLoader(new DRACOLoader().setDecoderPath(`https://file.threehub.cn/` + 'js/three/draco/'))loader.load(`https://threehub.cn/` + '/files/model/car.glb',gltf => {group = gltf.scenescene.add(group)changeMaterial()})// 文件地址
const urls = [0, 1, 2, 3, 4, 5].map(k => (`https://file.threehub.cn/` + 'files/sky/skyBox0/' + (k + 1) + '.png'));const textureCube = new THREE.CubeTextureLoader().load(urls);function changeMaterial() {let materialif (!group) returngroup.traverse(child => {if (child.isMesh && child.name === '网格138_3') {material = child.materialmaterial.envMap = textureCube}})const folder = new GUI()folder.add(material, 'wireframe').name('线框').listen()folder.add(material, 'transparent').name('透明').listen()folder.add(material, 'opacity').min(0).max(1).name('透明度').listen()folder.addColor({ color: material.color.clone() }, 'color').name('颜色').listen().onChange(c => {gsap.to(material.color, { ...c, duration: 1.5 })})folder.add(material, 'metalness').min(0).max(1).name('金属度').listen()folder.add(material, 'roughness').min(0).max(1).name('粗糙度').listen()}/*** 名称: 材质修改动画* 作者: 优雅永不过时 https://github.com/z2586300277
*/