Three.js - 网格(八)

网格

  • 表示基于以三角形组合成的几何体的类。
  • three.js中几何体是不能直接渲染的。在three.js有一种类型物体,这种类型放入场景中才能直接渲染图形。网格(Mesh)是这种类型中的一种。

创建使用

  • 构造参数new THREE.Mesh( geometry, material )
  1. geometry 几何体实例。
  2. material 一个材质(material)或多个材质(material),多个材质对应几何体的各个面。
      // 立体几何
      const boxWidth = 6
      const boxHeight = 6
      const boxDepth = 6
      const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth)

      const loader = new THREE.TextureLoader()
      const texture = loader.load(
        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.16pic.com%2F00%2F07%2F46%2F16pic_746871_b.jpg'
      )

      // 基础材质
      const material = new THREE.MeshBasicMaterial({
        map: texture
      })

      // 网格
      const mesh = new THREE.Mesh(geometry, material)
      mesh.position.x = 5
      scene.add(mesh)
  1. 把定义好的几何体和材质传入,实例化网格。在放入场景中就实例化完成。

image.png

位置、缩放、旋转

  • 因为网格(Mesh)的基类是.Object3D。因此包含scale、rotation、position三个属性,设置网站在场景中的位置。
  1. .position网格相对于父级坐标的位置。
  mesh.position.x = x
  mesh.position.y = y
  mesh.position.z = z
  1. .rotation 围绕x、y、z轴旋转的弧度,需注意是弧度值
    mesh.rotation.x = x
    mesh.rotation.y = y
    mesh.rotation.z = z
  1. .scalex、y、z轴缩放的大小。
    mesh.scale.x = x
    mesh.scale.y = y
    mesh.scale.z = z

使用多个材质

      const loader = new THREE.TextureLoader()
      const texture = loader.load( 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.16pic.com%2F00%2F07%2F46%2F16pic_746871_b.jpg'
      )
      const texture2 = loader.load(
        'https://img2.baidu.com/it/u=363752184,2041904643&fm=253&fmt=auto&app=138&f=JPEG?w=747&h=500'
      )
      // 基础材质
      const material = new THREE.MeshBasicMaterial({
        map: texture
      })
      const material2 = new THREE.MeshBasicMaterial({
        map: texture2
      })
      // 网格
      const mesh = new THREE.Mesh(geometry, [material, material, material, material, material, material2])

image.png

  1. 通过网格的第二个参数,传入多个材质就能实现。
  2. 并不是所有的几何体类型都支持多种材质,立方体可以使用6种材料,每个面一个。圆锥体可以使用2种材料,一种用于底部,一种用于侧面。

网格组

  • 在物体类中有一个组(Group)对象。使用.add()方法将网格加入到组。用于同时操作网格组在场景中的坐标。
    const group = new THREE.Group()
    group.add(sphere)
    group.add(cube)
    scene.add(group)
  1. 在使用了组后。我们修改组的位置、缩放、旋转,是会同步到子对象的,他们被视为一个整体。当我们单独修改网格对象时,它的位置、缩放、旋转,都是相对于其父对象所在位置上进行变化。
  2. 我们通常说的,全局坐标就是场景的坐标,相对坐标是其父对象的坐标。
      // 网格
      const mesh = new THREE.Mesh(geometry, material)
      // 相对坐标 x 移动5
      mesh.position.x = 5

      const mesh2 = new THREE.Mesh(geometry, material)
      // 相对坐标 z 移动-10
      mesh2.position.z = -10

      const group = new THREE.Group()
      group.add(mesh)
      group.add(mesh2)

      // 全局坐标x 移动10
      group.position.x = 10
      scene.add(group)

1.gif

  1. 这里能清晰的看到子网格的位移都是在父坐标基础上改变的。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 three.js 中创建网格地板,可以使用 `Mesh` 类。首先,需要创建一个网格地板的几何体,可以使用 `PlaneGeometry` 类。然后,可以使用一个材质(如 `MeshBasicMaterial` 或 `MeshLambertMaterial`)来创建网格地板的外观。最后,将几何体和材质作为参数传递给 `Mesh` 构造函数,并将新创建的网格地板添加到场景中。 例如: ``` // 创建网格地板的几何体 const geometry = new THREE.PlaneGeometry(width, height, widthSegments, heightSegments); // 创建网格地板的材质 const material = new THREE.MeshBasicMaterial({ color: 0xffffff }); // 创建网格地板 const mesh = new THREE.Mesh(geometry, material); // 将网格地板添加到场景中 scene.add(mesh); ``` 其中,`width` 和 `height` 分别指定网格地板的宽度和高度,`widthSegments` 和 `heightSegments` 分别指定沿网格地板宽度和高度方向的网格数。 ### 回答2: 在three.js中创建网格地板需要经历以下步骤: 1. 首先,我们需要导入three.js库及其他必要的依赖文件。 ``` import * as THREE from 'https://cdn.skypack.dev/[email protected]/build/three.module.js'; ``` 2. 创建场景、相机和渲染器。 ``` const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); ``` 3. 创建网格地板的材质和几何体。 ``` const floorGeometry = new THREE.PlaneGeometry(10, 10, 10, 10); const floorMaterial = new THREE.MeshBasicMaterial({ color: 0x808080, wireframe: true }); ``` 4. 创建网格对象并将材质应用于它。 ``` const floor = new THREE.Mesh(floorGeometry, floorMaterial); scene.add(floor); ``` 5. 设置相机的位置和方向。 ``` camera.position.z = 5; camera.lookAt(floor.position); ``` 6. 创建动画循环函数,并在其中渲染场景。 ``` function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate(); ``` 整个过程中,我们先创建了场景、相机和渲染器,然后定义了网格地板的材质和几何体。接下来,我们使用这些材质和几何体创建一个网格对象,并将它添加到场景中。然后,我们设置相机的位置和方向,以确保地板在可视范围内。最后,我们通过创建动画循环函数来持续渲染场景,使得地板可以旋转或进行其他动画效果。 这样,我们就成功地使用three.js创建了一个简单的网格地板。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值