嗨,我是小路。今天主要和大家分享的主题是“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 / 2
scene.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>
三、注意事项
注意球体的设置,和贴图的位置防止,其余的都可以查看参考的文章。
都看到这里了,记得【点赞】+【关注】哟。
参考文章: