three.js如何实现简易3D机房?(二)模型加载的过渡动画

接上一篇: 

three.js如何实现简易3D机房?(一)基础准备-下:http://t.csdnimg.cn/TTI1P

目录

六、自定义过渡动画

1.过渡动画组件 

2.模型加载时使用


完整源码地址:computerRoom-demo: 3d机房源码

不完美的地方还有很多,多多包涵~

根据模型大小,可以自定义模型加载的过渡动画效果,由于公司和本人设备配置有限,我这个模型才26M,打开都要至少加载一分钟。。。(大家配置高、网速快的当我没说)

六、自定义过渡动画

1.过渡动画组件 

在component/loading.vue文件中

<!--
 * @Description: 模型加载过渡动画
 * @FilePath: \hk-computerRoom-Security\src\views\home\component\loading.vue
 * @Date: 2024-02-02 15:29:56
 * @LastEditTime: 2024-02-02 15:55:12
-->
<template>
	<div id="loading-mark" v-if="props.loading">
		<div class="loading-box">
			<div class="loading">
				<img src="../../../assets/images/home/threeD/loading.svg" />
				<div class="progress-txt">
					当前模型已加载 <b>{{ props.percentage }}</b> %
				</div>
				<div class="loading-txt">模型文件首次加载时间较长请耐心等待.....</div>
			</div>
		</div>
	</div>
</template>
<script setup lang="ts">
const props = defineProps({
	percentage: {
		type: Number,
		default: 0,
	},
	loading: {
		type: Boolean,
		default: false,
	},
});
</script>
<style scoped lang="scss">
#loading-mark {
	position: absolute;
	width: 100%;
	height: 100%;
	z-index: 2000;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;

	.loading-box {
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		transition: opacity 0.3s;
		background-color: rgb(0 0 0 / 64%);

		.loading {
			width: 320px;
			height: 120px;
			text-align: center;
		}
		.progress-txt {
			font-size: 18px;
			font-weight: bold;
			color: #327cec;
			b {
				color: #9dadba;
			}
		}
		.loading-txt {
			margin-top: 10px;
			text-align: center;
			font-size: 14px;
			color: #327cec;
			font-weight: bold;
		}
	}
}
</style>
2.模型加载时使用

在index.vue中(css代码就不展示了哈)

在模型加载函数中,计算出模型实时加载进度即可,关键代码如下:

完整代码: 

// 导入模型
const importModel = () => {
	loader.load(
		'model/room1.glb',
		(gltf: any) => {
			model = gltf.scene;
			model.rotation.set(0, -Math.PI / 12, 0);
			model.position.set(0, -5, 0);
			model.traverse(function (child: any) {
				if (child.isMesh) {
					// 1.去掉文字
					if (child.name == 'Text') child.visible = false;
					// 2.修复设备颜色偏暗的问题
					if (child.parent.name.includes('AU')) {
						child.frustumCulled = false;
						//模型阴影
						child.castShadow = true;
						//模型自发光
						child.material.emissive = child.material.color;
						child.material.emissive.setHSL(1, 0, 0.35);
						child.material.emissiveMap = child.material.map;
					}
				}
			});
			// create3DDialog();
			scene.add(model);
		},
		(xhr: any) => {
			// 计算加载进度百分比
			state.progress = Math.floor((xhr.loaded / xhr.total) * 100);
			if (state.progress == 100) state.loading = false;
		},
		// 模型加载错误
		(error: any) => {
			console.log(error, 'error');
		}
	);
};

接下一篇:

three.js如何实现简易3D机房?(三)显示信息弹框/标签:http://t.csdnimg.cn/sXtjv

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Three.js是一个基于WebGL的JavaScript库,用于创建和显示3D图形。要实现3D机房效果,可以使用Three.js来创建和渲染机房模型,并添光照、材质和动画效果。 以下是使用Three.js实现3D机房效果的一般步骤: 1. 引入Three.js库和其他必要的依赖库: ```html <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script> ``` 2. 创建场景(Scene)和相机(Camera): ```javascript var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); ``` 3. 创建渲染器(Renderer)并设置其大小: ```javascript var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); ``` 4. 创建几何体(Geometry)和材质(Material): ```javascript var geometry = new THREE.BoxGeometry(1, 1, 1); var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); var cube = new THREE.Mesh(geometry, material); scene.add(cube); ``` 5. 添光源(Light): ```javascript var light = new THREE.PointLight(0xffffff, 1, 100); light.position.set(0, 0, 10); scene.add(light); ``` 6. 设置相机位置并渲染场景: ```javascript camera.position.z = 5; function animate() { requestAnimationFrame(animate); cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render(scene, camera); } animate(); ``` 通过上述步骤,你可以使用Three.js创建一个简单的3D机房效果。你可以根据自己的需求进一步添模型、调整材质和光照等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值