three.js实现三维爆炸效果

server/2024/9/23 9:35:59/

主要是split函数

javascript"><template><div class="app"><div class="btns"><button @click="split">{{ isSplit ? "组合" : "分解" }}</button></div><div ref="canvesRef" class="canvas-wrap"></div></div>
</template><script setup>
import { ref, onMounted } from "vue";
import * as THREE from "three";
import TWEEN from "@tweenjs/tween.js";import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
const canvesRef = ref(null);
const canvasWidth = window.innerWidth;
const canvasHeight = window.innerHeight;
let scene;
let camera;
let renderer;
let axesHelper;
let cameraControls;
let model;
let isSplit = ref(false);
init();
render();
function init() {// 场景scene = new THREE.Scene();// 相机camera = new THREE.PerspectiveCamera(45,canvasWidth / canvasHeight,1,10000);camera.position.set(0, 0, 700);camera.lookAt(0, 0, 0);// 模型addModel();// const ambient = new THREE.AmbientLight(0xffffff, 10000);// scene.add(ambient);const light1 = new THREE.PointLight(0xffffff, 3000000);light1.position.set(500, 500, 500);scene.add(light1);const light2 = new THREE.PointLight(0xffffff, 3000000);light2.position.set(-500, -500, -500);scene.add(light2);// 坐标辅助对象// axesHelper = new THREE.AxesHelper(200);// scene.add(axesHelper);// console.log(scene);// 渲染器//antialias - 是否执行抗锯齿。默认为false.renderer = new THREE.WebGLRenderer({alpha: true,});renderer.setSize(canvasWidth, canvasHeight);// 相机轨道控制器cameraControls = new OrbitControls(camera, renderer.domElement);
}function addModel() {// const geometry = new THREE.BoxGeometry(100, 100, 100);// const material = new THREE.MeshLambertMaterial({ color: 0x00ff00 });// const cube = new THREE.Mesh(geometry, material);// cube.position.set(300, 0, 0);// scene.add(cube);new GLTFLoader().load("../src/model/bdbs.glb",function (gltf) {model = gltf.scene.children[0];console.log(model);scene.add(model);},function () {console.log("加载中");},function (error) {console.error(error);});
}
function render() {renderer.render(scene, camera);requestAnimationFrame(render);
}
function split() {isSplit.value = !isSplit.value;if (isSplit.value) {// 模型世界中心var modelWorldCenter = new THREE.Vector3(0, 0, 0); //.addVectors(box.max,box.min).multiplyScalar(0.5);//模型中心坐标// 定义盒子var childBox = new THREE.Box3();model.traverse(function (child) {if (child.isMesh) {childBox.setFromObject(child);var childCenter = new THREE.Vector3().addVectors(childBox.max, childBox.min).multiplyScalar(0.5);if (isNaN(childCenter.x)) return;child.childCenter = new THREE.Vector3().subVectors(childCenter, modelWorldCenter).normalize();if (!child.isMesh || !child.childCenter) return;// 爆炸公式:更改mesh位置var p = new THREE.Vector3().copy(child.childCenter).multiplyScalar(100);child.oldPosition = child.position.clone();child.position.copy(p);}});} else {model.traverse(function (child) {if (child.isMesh) {child.position.copy(child.oldPosition);}});}
}
onMounted(() => {canvesRef.value.appendChild(renderer.domElement);
});
</script><style lang="scss" scoped>
.app {position: relative;background-color: #00253f;.btns {position: absolute;top: 50;left: 50;}
}
</style>

http://www.ppmy.cn/server/36902.html

相关文章

Vitis HLS 学习笔记--Schedule Viewer 调度查看器

目录 1. 简介 2. Schedule Viewer详解 2.1 视图说明 2.1.1 Operation\Control Step 2.1.2 周期关系图 2.1.3 Schedule Viewer 菜单栏 2.1.4 属性视图 2.2 内容说明 2.2.1 实参&#xff08;b&#xff09;解释 2.2.2 实参&#xff08;a&#xff09;解释 2.2.3 变量&am…

商米-android-使用NFC读IC卡,身份证云解和IC卡同时兼容

商米介绍地址&#xff1a;https://www.sunmi.com/ 商米是一个提供手持PDA的一个很好的解决方案厂商&#xff0c; 也有其他的一些桌面设备。 其中商米提供的软件服务中&#xff0c;比较特别的是 身份证云解功能。 此处重点说明一下&#xff0c;身份证云解功能。 以往市面上的身…

一键转换,MP4视频变为MP3音频,只需这一行代码!

想要将珍藏的视频配乐提取出来&#xff1f;想把喜欢的电影原声变成音频&#xff1f;现在&#xff0c;只需一行代码&#xff0c;就能轻松将MP4视频转换为MP3音频&#xff01; 这篇文章将带你一步步完成转换&#xff0c;并详细解释每一步的操作&#xff0c;即使你是新手也能轻松…

93. 复原 IP 地址

93. 复原 IP 地址 题目描述: 有效 IP 地址 正好由四个整数&#xff08;每个整数位于 0 到 255 之间组成&#xff0c;且不能含有前导 0&#xff09;&#xff0c;整数之间用 . 分隔。 例如&#xff1a;"0.1.2.201" 和 "192.168.1.1" 是 有效 IP 地址&…

贪心算法应用例题

最优装载问题 #include <stdio.h> #include <algorithm>//排序int main() {int data[] { 8,20,5,80,3,420,14,330,70 };//物体重量int max 500;//船容最大总重量int count sizeof(data) / sizeof(data[0]);//物体数量std::sort(data, data count);//排序,排完数…

大宋咨询消费者需求研究问卷如何设计

设计消费者需求研究问卷需要考虑清楚研究目标、问题和目标受众的特点。一个良好的问卷设计能够确保收集到准确、有用的消费者反馈。以下大宋咨询是设计消费者需求研究问卷的一些建议&#xff1a; 1. 确定研究目标和问题&#xff1a; 在设计问卷之前&#xff0c;明确你希望从问…

06.Git远程仓库

Git远程仓库 #仓库种类&#xff0c;举例说明 github gitlab gitee #以这个仓库为例子操作登录码云 https://gitee.com/projects/new 创建仓库 选择ssh方式 需要配置ssh公钥 在系统上获取公钥输入命令&#xff1a;ssh-keygen 查看文件&#xff0c;复制公钥信息内…

如何做好一个活动策划?

活动策划的关键要素是什么&#xff1f; 首先&#xff0c;要明确一个概念:做活动就是走钢丝&#xff0c;没有保险的高空走钢丝!因为&#xff0c;活动没有“彩排”&#xff0c;只有现场"直播”! 无论什么类型的活动&#xff0c;人数是50人还是2000人&#xff0c;也不论预算…