当前位置: 首页 > news >正文

ThreeJS-缩放、旋转(四)

代码:

<template>

  <div id="three_div">

  </div>

</template>

  <script>

import * as THREE from "three";

import {OrbitControls } from 'three/examples/jsm/controls/OrbitControls'

export default {

  name: "HOME",

  components: {

    // vueQr,

    // glHome,

  },

  data() {

    return {

    };

  },

  mounted() {

    //使用控制器控制3D拖动旋转OrbitControls

    //控制3D物体移动

    //1.创建场景

    const scene = new THREE.Scene();

    console.log(scene)

   

    //2.创建相机

    const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);

    //设置相机位置

    camera.position.set(0,0,10);

    //将相机添加到场景

    scene.add(camera);

    //添加物体

    //创建几何体

    const cubeGeometry = new THREE.BoxGeometry(2,2,2);

    const cubeMaterial = new THREE.MeshBasicMaterial({color: 0xffff00});

    //根据几何体和材质创建物体

    const mesh  = new THREE.Mesh(cubeGeometry, cubeMaterial);

    //将物体加入到场景

    scene.add(mesh);

    //添加坐标轴辅助器

    const axesHepler = new THREE.AxesHelper(5)

    scene.add(axesHepler)

    //初始化渲染器

    const  render = new THREE.WebGLRenderer();

    //设置渲染器的尺寸

    render.setSize(window.innerWidth, window.innerHeight);

    //使用渲染器,通过相机将场景渲染进来

    //创建轨道控制器,可以拖动,控制的是摄像头

    const controls = new OrbitControls(camera, render.domElement);

    //将webgl渲染的canvas内容添加到body上

    document.getElementById('three_div').appendChild(render.domElement);

    //修改3D位置

    mesh.position.set(0, 0, 0);

   // render.render(scene, camera);

    //渲染下一帧的时候就会调用回调函数

    let i=1;

    //缩放

    mesh.scale.x = 5;

    let renderFun = ()=>{

      //位移

      mesh.position.x += 0.02*i;

      //旋转吧

      mesh.rotation.x += 0.01;

     // mesh.scale.x += 0.02*i;

      if(mesh.position.x >= 5){

        i = -1;

      }

      if(mesh.position.x <= 0){

        i = 1;

      }

      console.log(1)

      //需要重新绘制canvas画布

      render.render(scene, camera);

      //监听屏幕刷新(60HZ,120HZ),每次刷新触发一次requestAnimationFrame回调函数

      //但是requestAnimationFrame的回调函数注册生命只有一次,因此需要循环注册,才能达到一直调用的效果

      window.requestAnimationFrame(renderFun);

    }

   // window.requestAnimationFrame(renderFun);

    renderFun();


 

  },

   methods: {

  },

};

</script>

<style scoped lang="scss">

</style>

 效果:

http://www.lryc.cn/news/45549.html

相关文章:

  • 数据更新 | CnOpenData法拍房数据
  • 【Spring从成神到升仙系列 五】从根上剖析 Spring 循环依赖
  • 设计模式之代理模式(C++)
  • c++11 标准模板(STL)(std::unordered_multimap)(三)
  • Linux进程控制-2
  • 快速排序算法
  • 中华好诗词大学季第二季(四)
  • 分布式系统容灾部署方案
  • Python 爬虫性能相关总结
  • Baumer工业相机堡盟工业相机如何设置网口的IP地址(工业相机连接的网口设置IP地址步骤)
  • Android MediaCodec设置H264 Profile到High
  • QT之QSysInfo(查看电脑信息)
  • 中国塑料编织袋产业竞争状况及投资前景预测报告2023-2029年
  • 从头用脚分析FFmpeg源码 - av_read_frame
  • 第17章_触发器
  • 3956. 截断数组
  • React Labs: 我们最近在做什么——2023 年 3 月
  • 文件系统设计详解
  • 好看~立马启动python实现美女通通下
  • Git 安装设置
  • Python-闭包
  • Gitlab中Pipeline语法四
  • Go语言精修(尚硅谷笔记)第五章
  • 三、MySQL 高级(DML 增删改)
  • 面向AI编程的本质是什么?
  • 深入浅出——深度学习训练中的warmup
  • 你知道如何用C语言将格式化数据和字符串相互转换吗?
  • 免费一键生成原创文章-原创文章批量生成
  • 【数据库管理】④重做日志Redo Log
  • 5-python文件操作