threeJS-RFM模型

2024-01-31 13:40
文章标签 模型 threejs rfm

本文主要是介绍threeJS-RFM模型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

threeJS-RFM模型

threeJS官方文档-three.js docs

PMF模型效果展示:

RFM模型的绘制

绘制流程:

  • 获取容器,创建基本类

  • 创建三维场景

  • 设置观察点

  • 设置渲染分辨率,避免绘图模糊

  • 绘制立方体和组

  • 绘制坐标轴

  • 绘制坐标轴文字及立方体描述文字

  • 立方体响应式,保证立方体不被拉伸和块状化

  • 整个组设置X方向的旋转并开启自动旋转

1.获取容器,创建基本类

  //容器this.container = this.$refs.container; //表示2D vector(二维向量)的类this.mouse = new THREE.Vector2(); //这个类用于进行光线投射。 光线投射用于进行鼠标拾取(在三维空间中计算出鼠标移过了什么物体)。this.raycaster = new THREE.Raycaster();//获取挂载DOM相对于视窗的位置集合const boundRect = this.container.getBoundingClientRect();this.boundRect = boundRect;

2.创建三维场景

this.scene = this.initScene(); //构建一个三维场景initScene() {//构建一个三维场景const scene = new THREE.Scene();return scene;
}

3.设置观察点

this.camera = this.initCamera(boundRect); //设置观察点initCamera(boundingBox) {
//创建一个透视摄像机
const camera = new THREE.PerspectiveCamera(15, boundingBox.width / boundingBox.height, 0.1, 1000); //设置 摄像机视锥体垂直视野角度、长宽比、近端面、远端面
camera.position.set(0, 0.5, 62);
return camera;
}

4.设置渲染分辨率,避免绘图模糊。

this.renderer = this.initRenderer(boundRect); //设置渲染分辨率
​initRenderer(boundingBox) {//设置渲染分辨率const renderer = new THREE.WebGLRenderer({alpha: true});//设置设备宽高renderer.setSize(boundingBox.width, boundingBox.height); //设置设备像素比。避免绘图模糊renderer.setPixelRatio(window.devicePixelRatio < 2 ? 2 : window.devicePixelRatio); renderer.setClearColor(0xeeeeee, 0.0); // 设置场景背景色this.container.appendChild(renderer.domElement);renderer.render(this.scene, this.camera); //渲染场景return renderer;}

5.绘制立方体。注意创建一个组 将立方体、坐标轴等都放入这个组,方便整体的操作。

 this.group = new THREE.Group(); //创建一个组 将立方体 坐标轴等都放入这个组 方便整体的操作const offset = 1.25;const posList = [//把8个立方体进行 x y z轴三个方向的偏移{ x: -offset, y: offset, z: offset },{ x: offset, y: offset, z: offset },{ x: -offset, y: offset, z: -offset },{ x: offset, y: offset, z: -offset },{ x: -offset, y: -offset, z: offset },{ x: offset, y: -offset, z: offset },{ x: -offset, y: -offset, z: -offset },{ x: offset, y: -offset, z: -offset }];let dataset= [{pos: posList[0], //坐标color: 'rgba(255,217,81,1)', //立方体颜色borderColor: 'rgb(250, 223, 127)', //边框颜色name: '重要价值用户-1' //name和code},{pos: posList[1],color: 'rgba(71,210,235,1)',borderColor: 'rgb(97, 203, 221)',name: '重要保持用户-3',code: 3},{pos: posList[2],color: 'rgba(137,220,87,1)',borderColor: 'rgb(160, 221, 123)',name: '重要发展用户-2'},{pos: posList[3],color: 'rgb(197, 103, 235)',borderColor: 'rgb(153, 81, 204)',name: '重要挽留用户-4'},{pos: posList[4],color: 'rgb(123,128,250)',borderColor: 'rgb(123,128,250)',name: '一般价值用户-5'},{pos: posList[5],color: 'rgb(248, 95, 87)',borderColor: 'rgb(253, 155, 150)',name: '一般保持用户-7'},{pos: posList[6],color: 'rgb(250, 134, 45)',borderColor: 'rgb(245, 162, 99)',name: '一般发展用户-6'},{pos: posList[7],color: 'rgba(36,128,223,1)',borderColor: 'rgb(92, 156, 221)',name: '一般挽留用户-8'}]this.dataset.forEach(item => {const cube = this.createBox(item.pos, item.color, item.name); //创建立方体this.group.add(cube);/* //BoxHelper用于图形化地展示对象世界轴心对齐的包围盒的辅助对象。此处即为立方体的线框let border = new THREE.BoxHelper(cube, item.borderColor);this.group.add(border); */});
​
​
createBox(pos, color, name) {//创建立方体const geometry = new THREE.BoxGeometry(2.2, 2.2, 2.2); //创建立体几何体const material = this.meshBasicMaterial({ color: color, name: name }); //决定了mesh模型中三角形的外观显示const cube = new THREE.Mesh(geometry, material); //基于以三角形为polygon mesh(多边形网格)的物体的类pos && cube.position.set(pos.x, pos.y, pos.z);return cube;},meshBasicMaterial(option) {//材质-基础网格材质const material = new THREE.MeshBasicMaterial({// wireframe: true, // 显示骨架transparent: true,opacity: 0.35,...option});return material;}

6.绘制坐标轴

  //坐标轴辅助this.setCoordinates(new THREE.Vector3(0, 10, 0));this.setCoordinates(new THREE.Vector3(0, -10, 0));this.setCoordinates(new THREE.Vector3(10, 0, 0));this.setCoordinates(new THREE.Vector3(-10, 0, 0));this.setCoordinates(new THREE.Vector3(0, 0, 10));this.setCoordinates(new THREE.Vector3(0, 0, -10));
​setCoordinates(dir) {//坐标轴dir.normalize(); //将该三维向量转化为单位向量 ; 基于箭头原点的方向var origin = new THREE.Vector3(0, 0, 0); //箭头的原点var length = 3.2; //箭头的长度var hex = 0x748097; //定义的16进制颜色值var headLength = 0.2; //箭头头部(锥体)的长度var headWidth = 0.2; //箭头头部(锥体)的宽度//三维箭头对象var arrowHelper = new THREE.ArrowHelper(dir, origin, length, hex, headLength, headWidth);this.group.add(arrowHelper);this.scene.add(this.group);},

7.绘制坐标轴文字及立方体描述文字

let textureData=[//坐标轴文字{pos: { x: 0, y: 3.4, z: 0 },dataURL: require('@/assets/img/personas/M+.svg'),width: { x: 0.6, y: 0.5, z: 0 }},{pos: { x: 0, y: -3.4, z: 0 },dataURL: require('@/assets/img/personas/M-.svg'),width: { x: 0.6, y: 0.5, z: 0 }}.......//模块文字{pos: { x: -2, y: 3, z: 2.5 },dataURL: require('@/assets/img/personas/RFMText1.svg'),width: { x: 1.1, y: 0.48, z: 0 }},{pos: { x: 2, y: 3, z: 2.5 },dataURL: require('@/assets/img/personas/RFMText2.svg'),width: { x: 1.1, y: 0.48, z: 0 }}.......]//绘制坐标轴文字及立方体描述文字this.textureData.forEach(item => {this.getTextureData(item.dataURL, item.pos, item.width);});​getTextureData(dataURL, pos, width) {//文字描述let texture = new THREE.TextureLoader().load(dataURL); //加载文件const geometry = new THREE.BoxGeometry(width.x, width.y, width.z);const material = new THREE.MeshBasicMaterial({transparent: true,opacity: 1,map: texture});let rect = new THREE.Mesh(geometry, material);
​pos && rect.position.set(pos.x, pos.y, pos.z);this.group.add(rect);this.render();}
​render() {//渲染场景this.renderer.render(this.scene, this.camera);},

8.立方体响应式。检查渲染器的canvas尺寸是不是和canvas的显示尺寸不一样 如果不一样就设置它 可保证立方体不被拉伸和块状化

 //立方体响应式if (this.resizeRendererToDisplaySize(this.renderer)) {const canvas = this.renderer.domElement;this.camera.aspect = canvas.clientWidth / canvas.clientHeight;//更新相机投影矩阵,必须在参数发生变化后调用this.camera.updateProjectionMatrix();}​resizeRendererToDisplaySize(renderer) {const canvas = renderer.domElement;const width = canvas.clientWidth;const height = canvas.clientHeight;const needResize = canvas.width !== width || canvas.height !== height;if (needResize) {renderer.setSize(width, height, false);}return needResize;}

9.整个组设置X方向的旋转并开启自动旋转。

自动旋转详见下面的事件模块

//整个组设置X方向的旋转this.group.rotation.set(0.2, 0, 0);//自动旋转this.animate(this.scene, this.camera, this.renderer, this.group);

RFM模型的事件

交互描述:

  • 立方体点击高亮该立方体。
  • 立方体可自动旋转。鼠标hover到图层时停止自动旋转,鼠标离开图层时开启自动旋转。
  • 记录鼠标按下到放开移动的距离并让立方体沿Y轴旋转相应的角度。

1.挂载事件

  this.container = this.$refs.container; //容器 let content = document.getElementsByClassName('app-wrapper')[0]; //最外层domthis.container.addEventListener('mousemove', this.mousemoveToCanvas, false);this.container.addEventListener('mouseleave', this.leaveToCanvas, false);this.container.addEventListener('click', this.coordsToCanvas, false);this.container.addEventListener('mousedown', this.onMouseDown, false);this.container.addEventListener('mouseup', this.onMouseup, false);content.addEventListener('mouseup', this.onMouseup, false);

2.立方体点击高亮。如果想hover高亮的话用这个方法也可实现。

// 将鼠标位置归一化为设备坐标。x 和 y 方向的取值范围是 (-1 to +1)const boundRect = this.container.getBoundingClientRect();this.mouse.x = ((evt.clientX - boundRect.left) / boundRect.width) * 2 - 1;this.mouse.y = (-(evt.clientY - boundRect.top) / boundRect.height) * 2 + 1;this.raycaster.setFromCamera(this.mouse, this.camera);var intersects = this.raycaster.intersectObjects(this.scene.children[0].children, false);this.voluntarily = false; //关闭自动旋转if (intersects.filter(item => item.object.type === 'Mesh')[0] && intersects.filter(item => item.object.type === 'Mesh')[0].object.material.name) {//所有立方体设置透明度为0.35this.scene.children[0].children.filter(item => item.type === 'Mesh').slice(0, 8).forEach(item => {item.material.opacity && (item.material.opacity = 0.35); });//当前hover或点击的立方体高亮intersects.filter(item => item.object.type === 'Mesh')[0].object.material.opacity = 0.7; this.render();} return this.mouse;
}

3.立方体可自动旋转。鼠标hover到图层时停止自动旋转,鼠标离开图层时开启自动旋转。

//自动旋转this.animate(this.scene, this.camera, this.renderer, this.group);animate(scene, camera, renderer, graphic) {//自动旋转 -沿Y轴旋转const _this = this;const step = function() {if (_this.voluntarily) {graphic.rotation.y -= 0.003;_this.group.rotation.y -= 0.003;}requestAnimationFrame(step);renderer.render(scene, camera);};step(scene, camera, graphic);},//hover停止自转mousemoveToCanvas() {this.voluntarily = false;}//鼠标离开图层时 开启自转leaveToCanvas() {this.setAllOpacity();this.voluntarily = true;} 

4.记录鼠标按下到放开移动的距离并让立方体沿Y轴旋转相应的角度。

 onMouseDown(event) {event.preventDefault();this.mouseDown = true;this.mouseX = event.clientX; //出发事件时的鼠标指针的水平坐标var rotateStart;rotateStart = new THREE.Vector2();rotateStart.set(event.clientX, event.clientY);document.addEventListener('mousemove', this.onMouseMove, false);},onMouseup(event) {this.mouseDown = false;document.removeEventListener('mousemove', this.onMouseMove);},onMouseMove(event, opt) {if (!this.mouseDown) {return;}//记录鼠标按下到放开移动的距离 并让立方体沿Y轴旋转相应的角度var deltaX = event.clientX - this.mouseX;this.mouseX = event.clientX;this.rotateScene(deltaX);},//设置模型旋转速度rotateScene(deltaX) {var deg = -deltaX / 279;//deg 设置模型旋转的弧度this.group.rotation.y -= deg;this.render();}

这篇关于threeJS-RFM模型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/663960

相关文章

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

Spring AI Alibaba接入大模型时的依赖问题小结

《SpringAIAlibaba接入大模型时的依赖问题小结》文章介绍了如何在pom.xml文件中配置SpringAIAlibaba依赖,并提供了一个示例pom.xml文件,同时,建议将Maven仓... 目录(一)pom.XML文件:(二)application.yml配置文件(一)pom.xml文件:首

如何在本地部署 DeepSeek Janus Pro 文生图大模型

《如何在本地部署DeepSeekJanusPro文生图大模型》DeepSeekJanusPro模型在本地成功部署,支持图片理解和文生图功能,通过Gradio界面进行交互,展示了其强大的多模态处... 目录什么是 Janus Pro1. 安装 conda2. 创建 python 虚拟环境3. 克隆 janus

本地私有化部署DeepSeek模型的详细教程

《本地私有化部署DeepSeek模型的详细教程》DeepSeek模型是一种强大的语言模型,本地私有化部署可以让用户在自己的环境中安全、高效地使用该模型,避免数据传输到外部带来的安全风险,同时也能根据自... 目录一、引言二、环境准备(一)硬件要求(二)软件要求(三)创建虚拟环境三、安装依赖库四、获取 Dee