three.js 3D Banner实战

2024-04-24 18:20
文章标签 实战 js 3d banner three

本文主要是介绍three.js 3D Banner实战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需求场景

        1、加载glb模型

        2、设置背景

        3、让第一个模型转动,并调整模型的转动速度

        4、设置模型的宽度。

遇到的问题

        1、为什么加载的模型不能转动?

        加载的glb模型需要加入到goup里面才能转动。

代码实施

<script setup>
import * as THREE from 'three'
import Stat from 'three/examples/jsm/libs/stats.module'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
// 引入gltf模型加载库GLTFLoader.js
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';import * as dat from 'dat.gui'
import { onMounted, ref } from 'vue';let w = 500
let h = 500
const stat = new Stat()//Scene
const scene = new THREE.Scene()//Camera
const camera = new THREE.PerspectiveCamera(75, w / h, 0.1, 100)
camera.position.set(0, 0.5, 3)
camera.lookAt(0, 0, 0)//渲染
const renderer = new THREE.WebGLRenderer()
const orbitControls = new OrbitControls(camera, renderer.domElement)
const loader = new GLTFLoader();
const TextureLoader = new THREE.TextureLoader()const clock = new THREE.Clock()const containerRef = ref()//组合
const group = new THREE.Group()//
const groupEarth = new THREE.Group()//正方形 地球
const geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5)
//材质
const material = new THREE.MeshBasicMaterial({ color: 0xff0000 })
const earth = new THREE.Mesh(geometry, material)
groupEarth.position.y = 0.5
groupEarth.add(earth)
scene.add(earth)// group.add(group2)let glbs//模型2  模型可以加入group,然后给group动画 可达鸭自转,同时围绕着地球转
loader.load( 'src/assets/file/kedaya.glb', function ( gltf ) {console.log('kedaya',gltf)gltf.scene.position.y = 0.5glbs = gltf.scenegroupEarth.add(glbs)scene.add(groupEarth)
})
const zuqiu = new THREE.Group()//模型3  模型可以加入group,然后给group动画 可达鸭自转,同时围绕着地球转
loader.load( 'src/assets/file/zuqiu.glb', function ( gltf ) {console.log('zuqiu',gltf)gltf.scene.position.y = 1zuqiu.add(gltf.scene)scene.add(zuqiu)
})// //模型3
const geometry2 = new THREE.BoxGeometry(1, 1, 1)
const material2 = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
const month = new THREE.Mesh(geometry2, material2)
month.position.y = 1
group.add(month)const geometry3 = new THREE.BoxGeometry(0.2, 0.2, 0.2)
const material3 = new THREE.MeshBasicMaterial({ color: 0x0000ff })
const cube3 = new THREE.Mesh(geometry3, material3)
cube3.position.y = 0.4
groupEarth.add(cube3)scene.add(group)//添加背景图
//多个面贴多张图片
const texture1 = TextureLoader.load('src/assets/img/girl/1.jpg')
const texture2 = TextureLoader.load('src/assets/img/girl/2.jpg')
const texture3 = TextureLoader.load('src/assets/img/girl/3.jpg')
const texture4 = TextureLoader.load('src/assets/img/girl/4.jpg')
const texture5 = TextureLoader.load('src/assets/img/girl/5.jpg')
const texture6 = TextureLoader.load('src/assets/img/girl/6.jpg')//设置背景
scene.background = texture2onMounted(() => {renderer.setSize(w, h)renderer.setClearColor(0x95e4e8) //设置背景色//解决加载gltf格式模型纹理贴图和原图不一样问题renderer.outputEncoding = THREE.sRGBEncoding;containerRef.value.appendChild(renderer.domElement)// containerRef.value.appendChild(stat.dom)tick()
})function tick() {const time = clock.getElapsedTime()//地球自转  绕着z轴旋转earth.rotation.z = time//将模型加入一个组合里面,就可以自己转动了groupEarth.rotation.y = time//可达鸭围绕着groupEarth.rotation.z = time//month.rotation.z = timegroup.rotation.z = time*5requestAnimationFrame(tick)renderer.render(scene, camera)stat.update()orbitControls.update()
}</script><template><main ref="containerRef" class="threeBox"></main>
</template>
<style scoped lang="less">
.threeBox{width: 500px;height: 500px;margin: 0 auto;margin-top: 200px;
}
</style>

这篇关于three.js 3D Banner实战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/932491

相关文章

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Pandas使用SQLite3实战

《Pandas使用SQLite3实战》本文主要介绍了Pandas使用SQLite3实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1 环境准备2 从 SQLite3VlfrWQzgt 读取数据到 DataFrame基础用法:读

Python实战之屏幕录制功能的实现

《Python实战之屏幕录制功能的实现》屏幕录制,即屏幕捕获,是指将计算机屏幕上的活动记录下来,生成视频文件,本文主要为大家介绍了如何使用Python实现这一功能,希望对大家有所帮助... 目录屏幕录制原理图像捕获音频捕获编码压缩输出保存完整的屏幕录制工具高级功能实时预览增加水印多平台支持屏幕录制原理屏幕

最新Spring Security实战教程之Spring Security安全框架指南

《最新SpringSecurity实战教程之SpringSecurity安全框架指南》SpringSecurity是Spring生态系统中的核心组件,提供认证、授权和防护机制,以保护应用免受各种安... 目录前言什么是Spring Security?同类框架对比Spring Security典型应用场景传统

最新Spring Security实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)

《最新SpringSecurity实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)》本章节介绍了如何通过SpringSecurity实现从配置自定义登录页面、表单登录处理逻辑的配置,并简单模拟... 目录前言改造准备开始登录页改造自定义用户名密码登陆成功失败跳转问题自定义登出前后端分离适配方案结语前言

OpenManus本地部署实战亲测有效完全免费(最新推荐)

《OpenManus本地部署实战亲测有效完全免费(最新推荐)》文章介绍了如何在本地部署OpenManus大语言模型,包括环境搭建、LLM编程接口配置和测试步骤,本文给大家讲解的非常详细,感兴趣的朋友一... 目录1.概况2.环境搭建2.1安装miniconda或者anaconda2.2 LLM编程接口配置2

基于Canvas的Html5多时区动态时钟实战代码

《基于Canvas的Html5多时区动态时钟实战代码》:本文主要介绍了如何使用Canvas在HTML5上实现一个多时区动态时钟的web展示,通过Canvas的API,可以绘制出6个不同城市的时钟,并且这些时钟可以动态转动,每个时钟上都会标注出对应的24小时制时间,详细内容请阅读本文,希望能对你有所帮助...