three.js vue3封装贴图循环偏移案例

news/2024/9/23 9:26:25/

以上是代码实现效果。本篇文章主要讲述vue3对代码的封装,以及如何让图片循环播放。

封装主要分三步:

1、封装实例化模块;2、封装渲染;3、偏移量操作。

具体的代码如下:

javascript"><script setup>
import * as THREE from 'three'import {  onMounted, ref } from 'vue';
const containerRef = ref()
let camera, scene, renderer, mesh, texture = null, geometry, material, loader, para = { repeat: 1, wrap: 1, offsetX: 0, offsetY: 0 }, gui, parameter;
let width = 500,height = 500
const initData = () => {scene = new THREE.Scene();camera = new THREE.PerspectiveCamera(70, width / height, 1, 1000);camera.position.z = 400;renderer = new THREE.WebGLRenderer({alpha:true});renderer.setSize(width, height);containerRef.value.appendChild(renderer.domElement);geometry = new THREE.PlaneGeometry(500, 300, 1, 1);//贴图loader = new THREE.TextureLoader()loader.load('src/assets/img/girl/7.png',function (text) {texture = text;material = new THREE.MeshBasicMaterial({map: texture});mesh = new THREE.Mesh(geometry, material);scene.add(mesh);},function (xhr) {console.log((xhr.loaded / xhr.total * 100) + '% loaded');},function (xhr) {console.log('An error happened');});
}//玄幻渲染
const animate =  ()=> {change();requestAnimationFrame(animate);renderer.render(scene, camera);
}function change() {if (texture != null) {//贴图在滚动texture.repeat.x = texture.repeat.y = para.repeat;//x轴偏移texture.offset.x += 0.01;//y轴偏移// texture.offset.y += 0.01;if (para.wrap == 1) {texture.wrapS = texture.wrapT = THREE.RepeatWrapping;} else if (para.wrap == 2) {texture.wrapS = texture.wrapT = THREE.ClampToEdgeWrapping;} else if (para.wrap == 3) {texture.wrapS = texture.wrapT = THREE.MirroredRepeatWrapping;}texture.needsUpdate = true;}
}onMounted(() => {initData()animate();
})</script><template><main class="containBox"><div ref="containerRef" class="threeBox"></div></main>
</template>
<style  lang="less" scoped>
.containBox {background-color: #95e4e8;min-width: 100vw;.threeBox {width: 500px;height: 500px;margin: 0 auto;margin-top: 200px;}
}
</style>

爬坑是一场没有终点的旅程,只有不断前行,才能看到更广阔的风景


http://www.ppmy.cn/news/1442184.html

相关文章

Unity打开Android文件管理器并加载文件

1、在AssetStore商店中加入免费插件 2、调用代码 3、使用UnityWebRequest加载路径数据

10 SQL进阶 -- 综合练习题 -- 10道经典SQL题目,配套数据与解答

1. 创建表结构和导入数据 1.1 新建数据库 1.2 执行建表语句 点击下方链接直接下载创建数据表脚本:http://tianchi-media.oss-cn-beijing.aliyuncs.com/dragonball/SQL/create_table.sql 执行建表语句执行成功查看创建的表1.3 导入数据 点击下方链接直接下载插入数据脚本:htt…

pymilvus执行多向量搜索

pymilvus执行多向量搜索 从 Milvus 2.4 开始&#xff0c;引入了多向量支持和混合搜索框架&#xff0c;单个collection可以支持10个向量字段。不同的向量字段可以表示不同的方面、不同的embedding模型甚至表征同一实体的不同数据模态。该功能在综合搜索场景中特别有用&#xff…

ElasticSearch自动补全

一、拼音分词器&#xff1a; 当用户在搜索框输入字符时&#xff0c;我们应该提示出与该字符有关的搜索项&#xff0c;如图&#xff1a; 这种根据用户输入的字母&#xff0c;提示完整词条的功能&#xff0c;就是自动补全了。 GET /_analyze {"text":"我爱螺蛳粉…

Java try catch 应该在 for 循环里面还是外面?(面试)

时间上&#xff0c; 其实算是无差别。内存上&#xff0c; 如果没出异常&#xff0c;其实也是无差别。 但是如果出现了异常&#xff0c; 那就要注意了。 一、try catch 在 for 循环外面 public static void tryOutside() { try { for (int count 1; count < 5; count) …

kotlin 编写一个简单的天气预报app (七)使用material design

一、优化思路 对之前的天气预报的app进行了优化&#xff0c;原先的天气预报程序逻辑是这样的。 使用text和button组合了一个输入城市&#xff0c;并请求openweathermap对应数据&#xff0c;并显示的功能。 但是搜索城市的时候&#xff0c;可能会有错误&#xff0c;比如大小写…

代码随想录算法训练营第三十七天| LeetCode738.单调递增的数字

LeetCode 738 单调递增的数字 题目链接&#xff1a;738. 单调递增的数字 - 力扣&#xff08;LeetCode&#xff09; 【解题思路】 需要采用从后往前的遍历 如果发现该数字的前一位比后一位大&#xff0c;那么前一位就做-1处理&#xff0c;后一位变为9 注意&#xff1a;千万不…

Axure RP 9中文激活版:专业原型设计工具mac/win

Axure RP 9是一款由美国Axure Software Solution公司开发的专业原型设计工具。它凭借强大的交互功能和丰富的设计素材&#xff0c;为产品经理、UI设计师、交互设计师等用户提供了高效、便捷的原型设计体验。 Axure RP 9支持快速创建线框图、流程图、原型和规格说明文档&#xf…