美文网首页
three.js - Basic scene

three.js - Basic scene

作者: 闪电西兰花 | 来源:发表于2023-08-20 10:56 被阅读0次
  • three.js is the javascript library
  • 4 elements:
    • a scene that will contain objects(scene likes a container), scene译为场景,可以理解为电影布景,在布景中一般会有演员、灯光、摄像机等
    • some objects
    • a camera
    • a renderer
  • Mesh
    • combination of a geometry(the shape) and a material(how it looks)
  • Camera
    • not visible
    • server as point of view when doing a render
    • can have multiple and switch between them different types
    • 垂直视野,视场(Field of View)
    • the aspect ratio 纵横比, the width of the render divided by the height of the render
  • Renderer
    • render the scene from the camera point of view
    • result drawn into a canvas
    • a canvas is a HTML element in which you can draw stuff
    • three.js will use WebGL to draw the render inside this canvas
// main.js 

  // 导入three.js
  import * as THREE from 'three'

  /**
   * 1. 创建场景
  **/
  const scene = new THREE.Scene()

  /**
   * 2. 创建透视相机(近大远小,相机只渲染场景内的东西)
  **/
  const camera = new THREE.PerspectiveCamera(
    45, // 视角
    window.innerWidth / window.innerHeight, // 视椎体长宽比
    0.1, // 近端面
    1000 // 远端面
  )
  camera.position.z = 6 // 相机在z轴的位置

  /**
   * 3. 创建渲染器
  **/
  const renderer = new THREE.WebGLRenderer()
  renderer.setSize(window.innerWidth, window.innerHeight)
  document.body.appendChild(renderer.domElement) // 在body上添加渲染器,domElement指向canvas

  /*
    * 4. 创建几何体
  */
  const geometry = new THREE.BoxGeometry(1, 1, 1) // 几何体
  const material = new THREE.MeshBasicMaterial({ // 几何体材质
    color: 0x00ff00
  })
  const cube = new THREE.Mesh(geometry, material) // 网格
  scene.add(cube)

  /*
    * 5. 渲染
  */
  function animate () {
    requestAnimationFrame(animate)
    cube.rotation.x += 0.01
    cube.rotation.y += 0.01
    renderer.render(scene, camera)
  }
  animate()

相关文章

  • 3d中遇到的问题以及解决办法

    1、加载three.js报错TypeError: THREE.Scene is not a constructor...

  • threeJs基础文档

    three.js基础结构 目录 一个three.js项目至少需要的东西有—— [ ] scene:场景 [ ] c...

  • 15three.js加载obj模型

    three.js加载obj模型 varscene =newTHREE.Scene(); varloader =ne...

  • 读一个WebGL引擎的渲染流程

    材料: three.js 0.95.0 三要素:Camera Scene WebGLRenderer 1、Came...

  • 基本概念

    three.js基本概念## 一个典型的Three.js程序至少要包括渲染器(Renderer)、场景(Scene...

  • three.js笔记

    three.js 专业名词 场景(Scene)、相机(camera)和渲染器(WebGLRenderer)rend...

  • three 简单模板

    Three.js的核心五步就是: 1.设置three.js渲染器2.设置摄像机camera3.设置场景scene4...

  • three.js

    three.js 使用ES6标准 三大组建 场景(scene)、相机(camera)和渲染器(renderer)。...

  • Three.js与计算机图形学(三)

    Three.js中的3d场景(scene)为无限大的,我们当然不可能去获取无限大的信息,在Three.js中封装了...

  • 1.three.js世界中的四大要素

    一、三大组件 在Three.js中,要渲染物体到网页中,我们需要3个组建:场景(scene)、相机(camera)...

网友评论

      本文标题:three.js - Basic scene

      本文链接:https://www.haomeiwen.com/subject/gxyludtx.html