当前位置: 首页 > 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博客

相关文章:

  • WEB安全--RCE--webshell bypass2
  • Dagster Pipes系列-2:增强外部脚本与Dagster的交互能力
  • 【Python】Python常用数据类型判断方法详解
  • Node.js中那些常用的进程通信方式
  • 4.MySQL全量、增量备份与恢复
  • 使用本地部署的 LLaMA 3 模型进行中文对话生成
  • 【WPF】Opacity 属性的使用
  • uniapp|实现手机通讯录、首字母快捷导航功能、多端兼容(H5、微信小程序、APP)
  • WebSocket的原理及QT示例
  • MIT XV6 - 1.6 Lab: Xv6 and Unix utilities -uptime
  • 内存泄漏系列专题分析之十一:高通相机CamX ION/dmabuf内存管理机制Camx ImageBuffer原理
  • 新手安装java所有工具(jdk、idea,Maven,数据库)
  • 迁移 Visual Studio Code 设置和扩展到 VSCodium
  • FFmpeg 与 C++ 构建音视频处理全链路实战(一)—— 环境配置与视频解封装
  • Linux异步通知机制详解
  • 存算一体芯片对传统GPU架构的挑战:在GNN训练中的颠覆性实验
  • labview硬件采集
  • AD22 快速定义PCB板框与DXF导入定义
  • MFC listctrl修改背景颜色
  • 项目全栈实战-基于智能体、工作流、API模块化Docker集成的创业分析平台
  • 水豚“豆包”出逃已40天,扬州茱萸湾景区追加悬赏
  • 27岁杨阳拟任苏木镇党委副职,系2020年内蒙古自治区选调生
  • 言短意长|西湖大学首次“走出西湖”
  • 湛江霞山通报渔船火灾:起火船舶共8艘,无人员伤亡或被困
  • 1至4月我国汽车产销量首次双超千万辆
  • “影像上海”中的自媒体影像特展:无论何时,影像都需要空间