首页 > 其他分享 >three.js简单实践

three.js简单实践

时间:2024-03-06 16:02:13浏览次数:20  
标签:container 创建 实践 value three new js THREE properties

1.引入

yarn add three

2.vue页面引入

  <div id="container"></div>

import * as THREE from 'three' import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' import { createMultiMaterialObject } from 'three/examples/jsm/utils/SceneUtils.js'

  <style lang="less" scoped>   #container {     width: 100vw;     height: 400px;   }   </style>

3.初始化方法



  data() {
    return {
      properties: {
        width: {
          name: 'width',
          value: 0.5,
          min: 0,
          max: 1,
          step: 0.01
        },
        height: {
          name: 'height',
          value: 0.5,
          min: 0,
          max: 1,
          step: 0.01
        },
        depth: {
          name: 'depth',
          value: 0.5,
          min: 0,
          max: 1,
          step: 0.01
        },
        widthSegments: {
          name: 'widthments',
          value: 8,
          min: 0,
          max: 40,
          step: 1
        },
        heightSegments: {
          name: 'heightments',
          value: 8,
          min: 0,
          max: 40,
          step: 1
        },
        depthSegments: {
          name: 'depthments',
          value: 8,
          min: 0,
          max: 40,
          step: 1
        }
      },  
      camera: null,
      scene: null,
      renderer: null,
      mesh: null
    }
  },


mounted() { this.initThree() }, methods: { initThree() { this.createScene() // 创建场景 this.createMesh() // 创建网格模型 this.createLight() // 创建光源 this.createCamera() // 创建相机 this.createRender() // 创建渲染器 this.createControls() // 创建控件对象 this.render() // 渲染 }, }
    // 创建场景
    createScene() {
      this.scene = new THREE.Scene()
    },
    // 创建网格模型
    createMesh() {
      // //创建图形
      let geometry = new THREE.BoxGeometry(
        this.properties.width.value, 
        this.properties.height.value, 
        this.properties.depth.value,
        Math.round(this.properties.widthSegments.value),
        Math.round(this.properties.heightSegments.value),
        Math.round(this.properties.depthSegments.value)
      );
      // 创建材质
      const meshMaterial = new THREE.MeshNormalMaterial({
        side: THREE.DoubleSide
      })
      const wireFrameMat = new THREE.MeshBasicMaterial({ wireframe: true })

      // 添加组合材质
      this.mesh = createMultiMaterialObject(geometry, [
        meshMaterial,
        wireFrameMat
      ])
      this.scene.add(this.mesh);
    },
    // 创建光源
    createLight() {

    },
    // 创建相机
    createCamera() {
      let container = document.getElementById('container');
      this.camera = new THREE.PerspectiveCamera(70, container.clientWidth/container.clientHeight, 0.01, 10);
      this.camera.position.z = 1;
    },
    // 创建渲染器
    createRender() {
      let container = document.getElementById('container');
      this.renderer = new THREE.WebGLRenderer({antialias: true});
      //setSize 设置大小
      this.renderer.setSize(container.clientWidth, container.clientHeight);
      container.appendChild(this.renderer.domElement);
    },
    // 创建控件对象
    createControls() {
      this.controls = new OrbitControls(this.camera, this.renderer.domElement)
    },
    // 渲染
    render() {
      this.updateFun()
      this.renderer.render(this.scene, this.camera)
      requestAnimationFrame(this.render)
    },
    // 更新属性
    updateFun() {
      const tempRotationY = this.mesh.rotation.y
      this.scene.remove(this.mesh)
      this.createMesh()
      this.mesh.rotation.y += tempRotationY + 0.01
    },

 

标签:container,创建,实践,value,three,new,js,THREE,properties
From: https://www.cnblogs.com/ZJTL/p/18056813

相关文章

  • Three.js_解决谍影闪烁重影模型的方法
    renderer=newTHREE.WebGLRenderer({antialias:true,logarithmicDepthBuffer:true,});logarithmicDepthBuffer,官方解释:是否使用对数深度缓存。如果要在单个场景中处理巨大的比例差异,就有必要使用,默认是false。使用了会带来额外的开销,但是效果会变好.原因......
  • 2024-03-06 Module '"@nestjs/platform-express"' has no exported member 'Expr
    问题描述:nestjs后端开发,遇到跨域问题,打算用express来配合处理,结果引入express的一个模块失败。app.modules.tsimport{MiddlewareConsumer,Module,NestModule}from'@nestjs/common';import{AppController}from'./app.controller';import{AppService}from'.......
  • 2024-03-06 NestJs学习日志之跨域
    新建一个跨域中间件(如:cors.middleware.ts),并把它导入到项目根目录的app.modules.ts里面使用。中间件代码如下:import{Injectable,NestMiddleware}from"@nestjs/common";@Injectable()//跨域中间件exportclassCorsMiddlewareimplementsNestMiddleware{use(r......
  • c++中nlohmann json的基本使用教程
    摘自:https://www.jb51.net/article/261677.htm 一.json.hpp库下载及安装1.1开源地址及引入方法nlohmannjson的开源项目地址,其中有对json使用方法的详细说明:https://github.com/nlohmann/json#serialization–deserialization对于我们项目中要使用nlohmannjson工具,只......
  • 2024-03-05 NestJs学习日志之新建nest项目,运行启动命令nest start报错:Could not find
    如题,低级错误。具体报错:CouldnotfindTypeScriptconfigurationfile"tsconfig.json".Please,ensurethatyouarerunningthiscommandintheappropriatedirectory(insideNestworkspace)找不到TypeScript配置文件“tsconfig.json”。请确保您在适当的目录(Nest工作......
  • 在.NET程序中整合微软的Playwright,使用 Playwright 的最佳实践和技巧
    Playwright是一个由Microsoft开发的开源工具,用于自动化Web浏览器的测试和操作。它提供了一种跨浏览器、跨平台的自动化解决方案,可以在Chromium、Firefox和WebKit(Safari)等多种浏览器上进行测试和操作。本篇随笔介绍Playwright的一些特点,以及能够完成的工作,并总结一些使用P......
  • NewtonJson
    Newtonsoft.Json.xml<membername="M:Newtonsoft.Json.JsonConvert.SerializeObject(System.Object)"><summary>SerializesthespecifiedobjecttoaJSONstring.</summary><param......
  • JS字符串、数组 常用方法
    字符串字符串增:1、+拼接2、concat()例:leta='hello'  letb=a.concat('word')  console.log(b) // "helloworld" 字符串删:1、slice(star,end)  从原始字符串中提取一个子字符串,并返回一个新的字符串,而不改变原字符串。start(必需):起始位置。如果是正数,则......
  • ems-jsp 职工列表功能
    1.思路简单的一个数据库查询所有,将数据放入list列表,通过spring提供的model传入到前端页面。2.代码controller:/**员工列表**/@RequestMapping("list")publicStringlistEmployee(HttpServletRequestrequest,Modelmodel){List<Employ......
  • ems-jsp 添加职工功能
    1.思路接受前端的表单,将数据存入数据库。2.代码:controller:/**添加员工信息**/@RequestMapping("add")publicStringaddEmployee(Employeeemployee){log.debug("员工名称:{}",employee.getName());log.debug("员工工资:{}",......