Three.js 鬼屋教程

发布时间:2026/9/25 11:46:47

Three.js 鬼屋教程
鬼屋 ·Ghost House· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么OrbitControls 相机轨道交互场景雾效增强纵深requestAnimationFrame渲染循环与resize自适应效果说明本案例演示鬼屋效果基于 WebGL 实现「鬼屋」可视化效果附完整可运行源码核心用到 OrbitControls、场景雾效增强纵深。建议先打开文首在线案例查看动态画面再对照下方源码逐步理解。核心概念Scene / Camera / WebGLRenderer构成最小渲染闭环大场景可开logarithmicDepthBuffer缓解 Z-fighting。OrbitControls提供轨道旋转/缩放开启enableDamping后需在 animate 中controls.update()。阅读下方完整源码时建议从init/load/animate三条主线入手再深入 shader 与工具函数。实现步骤搭建 Scene、PerspectiveCamera、WebGLRenderer挂载 canvas 并处理resize创建 OrbitControls及 Raycaster 等交互控件若源码包含在requestAnimationFrame循环中更新状态并 renderCesium 为viewer.render或自动渲染代码要点import * as THREE from three;import { OrbitControls } from three/examples/jsm/controls/OrbitControls.js; import { GUI } from three/addons/libs/lil-gui.module.min.jsconst getRandomIntInclusive function (min, max) { const minCeiled Math.ceil(min); const maxFloored Math.floor(max); return Math.floor(Math.random() * (maxFloored - minCeiled 1) minCeiled); // 包含最小值和最大值 }const setAoMapEnable function (mesh) { mesh.geometry.setAttribute(uv2, new THREE.Float32BufferAttribute( mesh.geometry.attributes.uv.array, 2 ) ) }const scene new THREE.Scene();const loaderManager new THREE.LoadingManager(); const textureLoader new THREE.TextureLoader(loaderManager); const door_colorTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/door/color.jpg, () { door_colorTexture.colorSpace THREE.SRGBColorSpace }); const door_lphaTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/door/alpha.jpg); const door_ambientOcclusionTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/door/ambientOcclusion.jpg); const door_heightTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/door/height.jpg); const door_metalnessTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/door/metalness.jpg); const door_normalTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/door/normal.jpg); const door_roughnessTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/door/roughness.jpg);const bricks_colorTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/bricks/color.jpg, () { bricks_colorTexture.colorSpace THREE.SRGBColorSpace }); const bricks_ambientOcclusionTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/bricks/ambientOcclusion.jpg); const bricks_normalTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/bricks/normal.jpg); const bricks_roughnessTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/bricks/roughness.jpg);const grass_colorTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/grass/color.jpg, () { grass_colorTexture.colorSpace THREE.SRGBColorSpace }); const grass_ambientOcclusionTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/grass/ambientOcclusion.jpg); const grass_normalTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/grass/normal.jpg); const grass_roughnessTexture textureLoader.load(FILE_HOST threeExamples/application/haunted_house/grass/roughness.jpg); loaderManager.onLoad function () { console.log(纹理加载完毕) } loaderManager.onError function (e) { console.log(纹理失败, e) } /**Fog*/ const fog new THREE.Fog(#50508b, 1, 15); scene.fog fog;/**Objects*/ const houseObj { houseWidth: 4, houseHeight: 2.5, houseDepth: 4, roofHeight: 0.8, doorSize: 2.2 }const house new THREE.Group(); scene.add(house);//房屋墙壁 const wall new THREE.Mesh( new THREE.BoxGeometry(houseObj.houseWidth, houseObj.houseHeight, houseObj.houseDepth), new THREE.MeshStandardMaterial({ map: bricks_colorTexture, aoMap: bricks_ambientOcclusionTexture, aoMapIntensity: 2, normalMap: bricks_normalTexture, roughnessMap: bricks_roughnessTexture, }) ) setAoMapEnable(wall); wall.position.y houseObj.houseHeight * 0.5 house.add(wall)const roof new THREE.Mesh( new THREE.ConeGeometry( Math.sqrt(houseObj.houseWidthhouseObj.houseWidth houseObj.houseDepthhouseObj.houseDepth) * 0.5, houseObj.roofHeight, 4), new THREE.MeshStandardMaterial( { color: #b35f45 } ) ) roof.rotation.y Math.PI / 4; roof.position.y houseObj.houseHeight 0.5 * houseObj.roofHeight roof.geometry.scale(1.5, 1.5, 1.5) console.log(roof, roof); house.add(roof);// 门 const door new THREE.Mesh( new THREE.PlaneGeometry(houseObj.doorSize, houseObj.doorSize, 100, 100), new THREE.MeshStandardMaterial({ map: door_colorTexture, transparent: true, alphaMap: door_lphaTexture, aoMap: door_ambientOcclusionTexture, aoMapIntensity: 5, displacementMap: door_heightTexture, displacementScale: 0.1, normalMap: door_normalTexture, metalnessMap: door_metalnessTexture, roughness: door_roughnessTexture }) ) door.geometry.setAttribute(uv2, new THREE.Float32BufferAttribute( door.geometry.attributes.uv.array, 2 ) ) door.position.y 1; door.position.z houseObj.houseDepth * 0.5 0.01; house.add(door);// 灌木丛 const bushes []; const bushGeometry new THREE.SphereGeometry(1, 16, 16) const bushMaterial new THREE.MeshStandardMaterial({ color: #89c854 }) bushes[0] new THREE.Mesh(bushGeometry, bushMaterial) bushes[0].position.set(houseObj.houseWidth * 0.5 0.05, 0.2, houseObj.houseDepth 0.05) bushes[0].scale.set(0.5, 0.5, 0.5);bushes[1] new THREE.Mesh(bushGeometry, bushMaterial) bushes[1].position.set(-(houseObj.houseWidth * 0.5 0.05), 0.2, -(houseObj.houseDepth 0.05)) bushes[1].scale.set(0.8, 0.8, 0.8);bushes[2] new THREE.Mesh(bushGeometry, bushMaterial) bushes[2].position.set(houseObj.houseWidth * 0.5 0.05, 0.2, -(houseObj.houseDepth 0.05)) bushes[2].scale.set(0.3, 0.3, 0.3);bushes[3] new THREE.Mesh(bushGeometry, bushMaterial) bushes[3].position.set(-(houseObj.houseWidth * 0.5 0.05), 0.2, houseObj.houseDepth 0.05) bushes[3].scale.set(0.6, 0.6, 0.6); house.add(...bushes);// 墓碑 const graves new THREE.Group() const gravesGeometry new THREE.BoxGeometry(0.4, 0.8, 0.2) const gravesMaterial new THREE.MeshStandardMaterial({ color: #ccc }) for (let i 0; i 50; i) { const grave new THREE.Mesh(gravesGeometry, gravesMaterial); const angle Math.random()getRandomIntInclusive(0, Math.PI2) const radius getRandomIntInclusive( (Math.max(houseObj.houseDepth, houseObj.houseWidth) * 0.5 3.2), 10 - 0.4 ) // const xMath.cos(angle)*radius; // const yMath.sin(angle)*radius;grave.position.x Math.cos(angle) * radius; grave.position.z Math.sin(angle) * radius; grave.position.y Math.random() * 0.5; grave.rotation.y Math.random() * Math.PI grave.rotation.z (Math.random() - 0.5) * 0.4 grave.castShadow true; graves.add(grave) } scene.add(graves)// 地面 grass_colorTexture.wrapS THREE.RepeatWrapping; grass_colorTexture.wrapT THREE.RepeatWrapping; grass_colorTexture.repeat.set(8, 8);grass_ambientOcclusionTexture.wrapS THREE.RepeatWrapping; grass_ambientOcclusionTexture.wrapT THREE.RepeatWrapping; grass_ambientOcclusionTexture.repeat.set(8, 8);grass_normalTexture.wrapS THREE.RepeatWrapping; grass_normalTexture.wrapT THREE.RepeatWrapping; grass_normalTexture.repeat.set(8, 8);grass_roughnessTexture.wrapS THREE.RepeatWrapping; grass_roughnessTexture.wrapT THREE.RepeatWrapping; grass_roughnessTexture.repeat.set(8, 8); const ground new THREE.Mesh( new THREE.PlaneGeometry(20, 20), new THREE.MeshStandardMaterial({ map: grass_colorTexture, aoMap: grass_ambientOcclusionTexture, aoMapIntensity: 2, normalMap: grass_normalTexture, roughnessMap: grass_roughnessTexture, }) ) setAoMapEnable(ground); ground.rotation.x -Math.PI / 2; ground.position.y 0; scene.add(ground);// 鬼魂 const ghost1 new THREE.PointLight(#ff00ff, 2, 3); scene.add(ghost1); const ghost2 new THREE.PointLight(#00ff00, 2, 3); scene.add(ghost2); const ghost3 new THREE.PointLight(#0000ff, 2, 3); scene.add(ghost3);/**light*/ // Ambient light const ambientLight new THREE.AmbientLight(#b9d5ff, 0.12) scene.add(ambientLight)// Directional light const moonLight new THREE.DirectionalLight(#ffffff, 0.12) moonLight.position.set(4, 5, - 2) scene.add(moonLight)const doorLight new THREE.PointLight(#ff7d46, 1, 7) doorLight.position.set(0, 2.2, 2.7); house.add(doorLight);const gui new GUI({ title: 控制面板, width: 320, closeFolders: true }); gui.add(ambientLight, intensity).name(环境光强度).min(0).max(1).step(0.001); gui.add(moonLight, intensity).name(平行光强度).min(0).max(1).step(0.001); gui.add(moonLight.position, x).name(平行光光源x).min(- 5).max(5).step(0.001); gui.add(moonLight.position, y).name(平行光光源y).min(- 5).max(5).step(0.001); gui.add(moonLight.position, z).name(平行光光源z).min(- 5).max(5).step(0.001); // gui.add(fog.color, z).name(fog).min(- 5).max(5).step(0.001); gui.addColor(fog, color).name(颜色)/**Camera*/ // Base camera const camera new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 100) camera.position.x 4 camera.position.y 2 camera.position.z 5 scene.add(camera);const renderer new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); document.getElementById(box).appendChild(renderer.domElement); renderer.setClearColor(#50508b);/**Shadow*/ renderer.shadowMap.enabled true; renderer.shadowMap.type THREE.PCFSoftShadowMap;moonLight.castShadow true; doorLight.castShadow true; ghost1.castShadow true; ghost2.castShadow true; ghost3.castShadow true;wall.castShadow true; ground.castShadow false; ground.receiveShadow true;doorLight.shadow.mapSize.set(252, 252);doorLight.shadow.camera.far 7;moonLight.shadow.mapSize.set(252, 252);ghost1.shadow.mapSize.set(252, 252);ghost1.shadow.camera.far 7;ghost2.shadow.mapSize.set(252, 252);ghost2.shadow.camera.far 7;ghost3.shadow.mapSize.set(252, 252); ghost3.shadow.camera.far 7;renderer.render(scene, camera); const control new OrbitControls(camera, renderer.domElement);const clock new THREE.Clock(); const tick () { const elapsedTime clock.getElapsedTime() * 0.5; ghost1.position.x Math.cos(elapsedTime) * 4.8; ghost1.position.z Math.sin(elapsedTime) * 4.8; ghost1.position.y Math.sin(elapsedTime * 3);ghost2.position.x Math.cos(-0.8elapsedTime)(8 Math.sin(elapsedTime)); ghost2.position.z Math.sin(-0.8elapsedTime)(8 Math.sin(elapsedTime)); ghost2.position.y Math.sin(elapsedTime * 2);ghost3.position.x Math.cos(-elapsedTime) * 5; ghost3.position.z Math.sin(-elapsedTime) * 5; ghost3.position.y Math.sin(elapsedTime2.5) Math.cos(elapsedTime1.5); control.update(); renderer.render(scene, camera); requestAnimationFrame(tick) } tick();// 尺寸改变重新渲染 window.onresize function () { renderer.setSize(window.innerWidth, window.innerHeight); camera.aspect window.innerWidth / window.innerHeight; renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); camera.updateProjectionMatrix(); }完整源码GitHub小结本文提供鬼屋完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库

相关新闻

Java权限框架选型:Shiro与Sa-Token深度对比与实践指南

Java权限框架选型:Shiro与Sa-Token深度对比与实践指南

2026/9/9 1:25:53

1. 权限框架选型的关键考量因素权限管理作为企业级应用的基石,直接影响系统的安全性和可维护性。在Java生态中,Sa-Token和Shiro是当前最受关注的两大权限框架,但二者的设计理念和适用场景存在显著差异。根据我多年架构经验,选型时…

Three.js 几何合并教程

Three.js 几何合并教程

2026/8/26 13:07:15

几何合并 Geometry Merge ▶ 在线运行案例 案例合集: 三维可视化功能案例(threehub.cn)开源仓库github地址: https://github.com/z2586300277/three-cesium-examples400个案例代码: 网盘链接 你将学到什么 glTF/FBX/OBJ 外部…

终极指南:5分钟掌握免费开源AI图像放大神器Upscayl [特殊字符]

终极指南:5分钟掌握免费开源AI图像放大神器Upscayl [特殊字符]

2026/8/27 14:02:18

终极指南:5分钟掌握免费开源AI图像放大神器Upscayl 🚀 【免费下载链接】upscayl 🆙 Upscayl - #1 Free and Open Source AI Image Upscaler for Linux, MacOS and Windows. 项目地址: https://gitcode.com/GitHub_Trending/up/upscayl …

CANN/GE ACL数据集缓冲区添加函数

CANN/GE ACL数据集缓冲区添加函数

2026/9/25 10:06:33

aclmdlAddDatasetBuffer 【免费下载链接】ge GE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、Te…

用ffmpeg高效批量调整图片尺寸的实战指南

用ffmpeg高效批量调整图片尺寸的实战指南

2026/9/25 9:40:47

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

Transformers 音频特征提取工具库 audio_utils 全解析:从 Mel 刻度换算到对数 Mel 频谱

Transformers 音频特征提取工具库 audio_utils 全解析:从 Mel 刻度换算到对数 Mel 频谱

2026/9/25 10:06:21

Transformers 音频特征提取工具库 audio_utils 全解析:从 Mel 刻度换算到对数 Mel 频谱 【免费下载链接】transformers 🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and mu…

RustFS 多节点集群重启与滚动升级实战:Readiness、Quorum 与 Degraded 模式完全指南

RustFS 多节点集群重启与滚动升级实战:Readiness、Quorum 与 Degraded 模式完全指南

2026/9/25 9:53:52

RustFS 多节点集群重启与滚动升级实战:Readiness、Quorum 与 Degraded 模式完全指南 【免费下载链接】rustfs 🚀2.3x faster than MinIO for 4KB object payloads. RustFS is an open-source, S3-compatible high-performance object storage system sup…

Java Integer缓存揭秘:128陷阱原理、避坑与面试全解

Java Integer缓存揭秘:128陷阱原理、避坑与面试全解

2026/9/25 8:58:17

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

RustFS Scanner 数据用量发布权威性决策:配额准入如何获得可用的权威依据

RustFS Scanner 数据用量发布权威性决策:配额准入如何获得可用的权威依据

2026/9/25 10:00:17

RustFS Scanner 数据用量发布权威性决策:配额准入如何获得可用的权威依据 【免费下载链接】rustfs 🚀2.3x faster than MinIO for 4KB object payloads. RustFS is an open-source, S3-compatible high-performance object storage system supporting mi…

远程协作的工作台整理

远程协作的工作台整理

2026/9/24 16:02:49

远程协作的工作台整理远程协作的核心不是再加一个工具,而是让交接信息足够完整。异步任务要写明目标、输入位置、完成标准和需要决策的人。 工作台的最小配置 将日程、待办、代码和沟通入口收拢到少数固定位置;通知按紧急程度分层。工作台不需要模仿办公…

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能

2026/9/25 9:41:47

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能分类:[AI/大模型]细分主题:AI 增强型 CI/CD 流水线自动化与 GitOps 实践:Agent 工作流、工具调用与任务拆解:从原型到生产的验收清单很多团队在尝试用大…

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场

2026/9/25 4:22:14

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场分类:[工程技术]细分主题:Kubernetes 生产环境运维与排障实战:可复制的项目复盘模板与决策记录大部分团队的事故复盘报告,最后都变成了躺在 Confluence 或钉…