Cesium-Examples/examples/threeEx/1.1.16、PBR材质.html
2025-03-19 11:00:22 +08:00

105 lines
3.0 KiB
HTML

<!--********************************************************************
* by jiawanlong
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
* {
margin: 0;
padding: 0;
overflow: hidden;
}
body,
#box {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="box"></div>
<script type="importmap">
{
"imports": {
"three": "./../../libs/three/build/three.module.js",
"three/addons/": "./../../libs/three/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js'
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(5, 5, 5)
// 渲染器
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })
// 环境光
scene.add(new THREE.AmbientLight(0xffffff, 4))
// 参考线
scene.add(new THREE.AxesHelper(1000))
// 加载环境贴图
// 左右上下前后
const textureCube = new THREE.CubeTextureLoader().setPath('./环境贴图/').load(['Left.jpg', 'Right.jpg', 'Up.jpg', 'Down.jpg', 'Front.jpg', 'Back.jpg']);
new THREE.MeshStandardMaterial({
envMap: textureCube, //设置pbr材质环境贴图
metalness: 1, //金属度属性
roughness: 0.5, //粗糙度
clearcoat: 1.0,//物体表面清漆层或者说透明涂层的厚度
clearcoatRoughness: 0.1,//透明涂层表面的粗糙度
envMapIntensity: 1,//环境贴图反射率
})
const loader = new GLTFLoader()
loader.load('./zhanji.glb', (gltf) => {
scene.add(gltf.scene)
gltf.scene.scale.set(0.005, 0.005, 0.005)
// 递归遍历所有模型节点
gltf.scene.traverse(function (obj) {
if (obj.isMesh) {//判断是否是网格模型
obj.material.envMap = textureCube; //设置环境贴图
}
});
}, function (xhr) {
// 加载进度
const percent = xhr.loaded / xhr.total;
console.log(percent)
})
renderer.setSize(box.clientWidth, box.clientHeight)
box.appendChild(renderer.domElement)
new OrbitControls(camera, renderer.domElement)
animate()
function animate() {
requestAnimationFrame(animate)
renderer.render(scene, camera)
}
</script>
</body>
</html>