当前位置: 首页 > news >正文

vue+threeJS 大理石贴图

        嗨,我是小路。今天主要和大家分享的主题是“vue+threeJS 大理石贴图”。        

通过 Vue 3 和 Three.js 实现大理石纹理效果,并将这种技术应用于产品展示、虚拟展览、甚至是互动游戏之中,其潜力无穷。今天主要介绍基础的大理石贴图。

vue+threeJS 大理石贴图示例图

1.下载大理石图片

定义:可以到百度上随意找一张大理石的图片,并将其一部分截图下来。

2.创建球体并贴图

//创建球体
let sphare;
const  createSphare = ()=>{//加载贴图const texture = new THREE.TextureLoader().load("./tietu1.png");const sphareGeometry = new THREE.SphereGeometry(10, 32,32);const sphareMaterial = new THREE.MeshBasicMaterial({ map:texture});sphare = new THREE.Mesh(sphareGeometry, sphareMaterial);//模型上移sphare.position.y = 10;scene.add(sphare);
}

3.设置动画

//渲染
const render = () => {//重复渲染requestAnimationFrame(render);//请求再次执行渲染函数render,渲染下一帧sphare.rotation.x += 0.01 ;//x轴旋转速度sphare.rotation.y += 0.01 ;//y轴旋转速度renderer.render(scene, camera); //执行渲染操作
}

二、实例代码

<template><div class="pageBox"><div class="leftBox" ref="leftRef"></div><div class="rightBox" ref="rightRef" :style="{ background: bgColor }"></div></div></template>
<script setup>
import { onMounted, ref } from 'vue';
import * as THREE from 'three';
// 引入轨道控制器扩展库OrbitControls.js
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';const bgColor = ref("")const leftRef = ref();
const rightRef = ref()
// 定义相机输出画布的尺寸(单位:像素px)
let width = window.innerWidth; //宽度
let height = window.innerHeight; //高度
// 创建3D场景对象Scene
const scene = new THREE.Scene();
//设置背景色
scene.background = new THREE.Color(0xffffff);const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);
camera.position.set(0, 20, 30);//创建一个平面
let plane;
const createPlan = () => {const planeGeometry = new THREE.PlaneGeometry(100, 100);const planMaterial = new THREE.MeshBasicMaterial({color: 0x999999, side: THREE.DoubleSide});plane = new THREE.Mesh(planeGeometry, planMaterial);plane.rotation.x = -Math.PI / 2scene.add(plane);
}
//创建球体
let sphare;
const  createSphare = ()=>{//加载贴图const texture = new THREE.TextureLoader().load("./tietu1.png");const sphareGeometry = new THREE.SphereGeometry(10, 32,32);const sphareMaterial = new THREE.MeshBasicMaterial({ map:texture});sphare = new THREE.Mesh(sphareGeometry, sphareMaterial);//模型上移sphare.position.y = 10;scene.add(sphare);
}
// 创建渲染器对象
const renderer = new THREE.WebGLRenderer();onMounted(() => {initData()//添加相机空间const controls = new OrbitControls(camera, renderer.domElement);// 如果OrbitControls改变了相机参数,重新调用渲染器渲染三维场景controls.addEventListener('change', function () {renderer.render(scene, camera); //执行渲染操作});//监听鼠标、键盘事件renderer.setSize(width, height); //设置three.js渲染区域的尺寸(像素px)//将innerHTML置空,避免append重复添加渲染leftRef.value.innerHTML = ''leftRef.value.append(renderer.domElement);})
const initData = () => {createPlan();createSphare();render();
}//渲染
const render = () => {//重复渲染requestAnimationFrame(render);//请求再次执行渲染函数render,渲染下一帧sphare.rotation.x += 0.01 ;//x轴旋转速度sphare.rotation.y += 0.01 ;//y轴旋转速度renderer.render(scene, camera); //执行渲染操作
}</script>
<style scoped lang="less">
.pageBox {width: 100%;height: 100vh;padding: 0;margin: 0;display: flex;justify-content: space-between;align-items: center;.rightBox {width: 100%;height: 100%;}
}
</style>

三、注意事项

       注意球体的设置,和贴图的位置防止,其余的都可以查看参考的文章。

都看到这里了,记得【点赞】+【关注】哟。

参考文章:

vue3+three 搭建平面上滚动旋转的几何体-CSDN博客

http://www.xdnf.cn/news/415891.html

相关文章:

  • 网络层:ICMP协议
  • Femap许可使用统计与分析
  • 电脑端音乐播放器推荐:提升你的听歌体验!
  • python3数据类型
  • 导轨式直流电表在充电桩中的新突破:DJSF1352-D实战解析
  • 【数据结构与算法】二分查找
  • 嵌入式软件开发常见warning之 warning: implicit declaration of function
  • HTTP协议解析:Session/Cookie机制与HTTPS加密体系的技术演进(一)
  • 专题二:二叉树的深度搜索(求根节点到叶节点数字之和)
  • 构建Android系统UI架构方法论
  • telnetlib关键流程示意图
  • ​​Python大数据分析专项课程​​
  • 解析文件加密软件,文件加密软件是什么?2025最新分享
  • 【深度学习-Day 9】机器学习核心概念入门:监督、无监督与强化学习全解析
  • 如何用vi编辑器的打开,创建和保存文件的基本操作
  • 谈AI/OT 的融合
  • LVGL(lv_switch开关)
  • 【JS逆向基础】前端基础-JS
  • 技术视界 | 具身感知与生成大模型:开启智能新时代(上)
  • 多线程(一)
  • 2025/5/7 心得
  • 进销存ERP系统,Java+Vue,含源码及文档,整合进销存各环节,实现数据精准互通,提升企业运营效能
  • 1.1 文章简介
  • Linux——数据库备份与恢复
  • C++哈希表
  • Fellou智能体调研
  • Python训练营打卡DAY23
  • c++ 类的成员初始化
  • [Windows] PicPick Professional_v7.3.4 中文专业版
  • SpringDataRedis的入门案例,以及RedisTemplate序列化实现