首页 > 编程语言 >#yyds干货盘点#【愚公系列】2022年12月 微信小程序-three.js绘制多维旋转正方体

#yyds干货盘点#【愚公系列】2022年12月 微信小程序-three.js绘制多维旋转正方体

时间:2022-12-23 20:32:03浏览次数:77  
标签:正方体 yyds canvas 12 INTERSECTED object THREE random Math

(文章目录)


前言

Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场景,包括了摄影机、光影、材质等各种对象。

一个典型的 Three.js 程序至少要包括渲染器(Renderer)、场景(Scene)、照相机(Camera),以及你在场景中创建的物体。

Three.js相关文档:http://docs.thingjs.com/ 在这里插入图片描述

一、Three.js的使用

安装第三方包:npm i --save threejs-miniprogram

1.多维旋转正方体的绘制

<view style="display:inline-block;">
  <button size="mini" type="primary" class="btn" data-action="Walking" bindtap="play">走</button>
  <button size="mini" type="primary" class="btn"  data-action="WalkJump" bindtap="play">跳</button>
  <button size="mini" type="primary" class="btn"  data-action="Sitting" bindtap="play">坐</button>
  <button size="mini" type="primary" class="btn"  data-action="Standing" bindtap="play">站</button>
</view>
<canvas
  type="webgl"
  id="webgl"
  style="width: 100%; height: 450px;"
  bindtouchstart="touchStart"
  bindtouchmove="touchMove"
  bindtouchend="touchEnd"
></canvas>
import { createScopedThreejs } from 'threejs-miniprogram'

const { renderCubes } = require('../../../lib/test-cases/cubes')

const app = getApp()

Page({
  data: {},
  onl oad: function () {
    wx.createSelectorQuery()
      .select('#webgl')
      .node()
      .exec((res) => {
        const canvas = res[0].node
        this.canvas = canvas
        const THREE = createScopedThreejs(canvas)
        
        renderCubes(canvas, THREE)
      })
  },
  play(e){
    let action = e.currentTarget.dataset.action
    this.fadeToAction(action)
  },
  touchStart(e) {
    this.canvas.dispatchTouchEvent({...e, type:'touchstart'})
  },
  touchMove(e) {
    this.canvas.dispatchTouchEvent({...e, type:'touchmove'})
  },
  touchEnd(e) {
    this.canvas.dispatchTouchEvent({...e, type:'touchend'})
  }
})

二、多维旋转正方体相关js文件

export function renderCubes(canvas, THREE) {
  var container, stats;
  var camera, scene, raycaster, renderer;
  var mouse = new THREE.Vector2(), INTERSECTED;
  var radius = 100, theta = 0;
  init();
  animate();
  function init() {
    camera = new THREE.PerspectiveCamera(70, canvas.width / canvas.height, 1, 10000);
    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xf0f0f0);
    var light = new THREE.DirectionalLight(0xffffff, 1);
    light.position.set(1, 1, 1).normalize();
    scene.add(light);
    var geometry = new THREE.BoxBufferGeometry(20, 20, 20);
    for (var i = 0; i < 2000; i++) {
      var object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff }));
      object.position.x = Math.random() * 800 - 400;
      object.position.y = Math.random() * 800 - 400;
      object.position.z = Math.random() * 800 - 400;
      object.rotation.x = Math.random() * 2 * Math.PI;
      object.rotation.y = Math.random() * 2 * Math.PI;
      object.rotation.z = Math.random() * 2 * Math.PI;
      object.scale.x = Math.random() + 0.5;
      object.scale.y = Math.random() + 0.5;
      object.scale.z = Math.random() + 0.5;
      scene.add(object);
    }
    raycaster = new THREE.Raycaster();
    renderer = new THREE.WebGLRenderer();
    renderer.setPixelRatio(wx.getSystemInfoSync().pixelRatio);
    renderer.setSize(canvas.width, canvas.height);
  }
  function animate() {
    canvas.requestAnimationFrame(animate);
    render();
  }
  function render() {
    theta += 0.5;
    camera.position.x = radius * Math.sin(THREE.Math.degToRad(theta));
    camera.position.y = radius * Math.sin(THREE.Math.degToRad(theta));
    camera.position.z = radius * Math.cos(THREE.Math.degToRad(theta));
    camera.lookAt(scene.position);
    camera.updateMatrixWorld();
    // find intersections
    raycaster.setFromCamera(mouse, camera);
    var intersects = raycaster.intersectObjects(scene.children);
    if (intersects.length > 0) {
      if (INTERSECTED != intersects[0].object) {
        if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);
        INTERSECTED = intersects[0].object;
        INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
        INTERSECTED.material.emissive.setHex(0xff0000);
      }
    } else {
      if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);
      INTERSECTED = null;
    }
    renderer.render(scene, camera);
  }
}

三、效果图

在这里插入图片描述

四、总结

three.js画一个图形主要经历如下八个步骤:

  • 1.创建透视相机
  • 2.创建场景
  • 3.创建光源
  • 4.构造辅助网格
  • 5.创建加载器,加载模型文件
  • 6.创建渲染器,渲染场景
  • 7.创建控制器
  • 8.循环渲染场景

标签:正方体,yyds,canvas,12,INTERSECTED,object,THREE,random,Math
From: https://blog.51cto.com/u_15437432/5966304

相关文章

  • #yyds干货盘点# LeetCode程序员面试金典:检查平衡性
    题目:实现一个函数,检查二叉树是否平衡。在这个问题中,平衡树的定义如下:任意一个节点,其两棵子树的高度差不超过1。示例1:给定二叉树[3,9,20,null,null,15,7]  3 /\......
  • #yyds干货盘点# 名企真题专题:微信红包
    1.简述:描述春节期间小明使用微信收到很多个红包,非常开心。在查看领取红包记录时发现,某个红包金额出现的次数超过了红包总数的一半。请帮小明找到该红包金额。写出具体算法思......
  • CSS-显示方块-2022-12-23
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><style>#box{width:300px;height:30......
  • CSS-父类边框塌陷问题-2022-12-23
    1、增加DIV空的但不建议2、在父元素中设置高度3、使用OVERFLOW 宁愿用hidden,不要用scroll滚动条,看上去很怪异4、在父类后添加一个伪类:写法稍微复杂一点,但是推荐......
  • Wallys/IPQ4018 12V-56VDCPassive 24V-56V/2*MMCX 27dbm Support MU-MIMO /industrial
    MT7915/MT7975/IPQ6000/IPQ6018/IPQ6010/IPQ4019/IPQ4029/ipq4018/IPQ4028/IPQ8072/IPQ8072A/IPQ8074/IPQ8074A/IPQ9074/QCN9074/QCN9072/QCN9024/IPQ5018/AR9223/QCA9880/......
  • CCS-盒子模型-2022-12-23
    Margin外边距Padding内边距Border边框<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>box</title><style>/*常见操作,初始化*/......
  • 【12.17-12.23】博客精彩回顾
    一、优秀文章推荐1.​​Java基本语法(上):变量与运算符.md​​2.​​构建一个应用程序,用于在基于内存的数据库中存储POJO(普通旧Java对象)​​3.​​Go语言的并发编程​​4......
  • CSS-背景-2022-12-23
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><style>div{width:1000px;height:700px;border:......
  • SpringBoot2.x系列教程12--SpringBoot自动配置原理探析
    SpringBoot系列教程12--SpringBoot自动配置原理探析作者:一一哥一.SpringBoot自动配置概述1.概述在Spring4.x之后,提供了一个按条件配置Bean的功能,并且结合“习惯优于配置”......
  • Acwing 12. 背包问题求具体方案
    Acwing12.背包问题求具体方案01背包问题,但是要求输出字典序最小的方案数。思路:状态转移方程:\(f[i][j]=max(f[i-1][j],f[i-1][j-v[i]]+w[i])\)求具体......