threejs案例一——画线

2 篇文章 0 订阅
2 篇文章 0 订阅
案例一
1. 效果

在这里插入图片描述

2. 知识点
  • new THREE.LineBasicMaterial
  • new THREE.Vector3(-10, 0, 0)
  • new THREE.BufferGeometry().setFromPoints(points)
    渲染=场景(几何体+材质)+相机(属性+位置)
3. 代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>画线</title>
    <style>
      body {
        margin: 0;
        overflow: hidden;
        /* 隐藏body窗口区域滚动条 */
      }
    </style>
    <!--引入three.js三维引擎-->
    <!-- <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>-->
    <script src="three.js-master/three.js-master/build/three.js"></script>
    <!-- <script src="http://www.yanhuangxueyuan.com/threejs/build/three.js"></script> -->
  </head>

  <body>
    <script>
    //1. 创建场景
      const scene = new THREE.Scene();
      //2. 添加一个材质,对于线条来说,我们能使用的材质只有LineBasicMaterial 或者 LineDashedMaterial。
      const material = new THREE.LineBasicMaterial({ color: 0x0eeeff });
//3.创建点组
      const points = [];
      points.push(new THREE.Vector3(-10, 0, 0));
      points.push(new THREE.Vector3(0, 10, 0));
      points.push(new THREE.Vector3(10, 0, 0));
//4.根据点组生成集合体
      const geometry = new THREE.BufferGeometry().setFromPoints(points);
      //5. 将集合体与材质放到场景中
      const line = new THREE.Line(geometry, material);
      scene.add(line);

      const renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      const camera = new THREE.PerspectiveCamera(
        45,
        window.innerWidth / window.innerHeight,
        1,
        500
      );
      camera.position.set(0, 0, 100);
      camera.lookAt(0, 0, 0);

//对相机、场景进行渲染
      renderer.render(scene, camera);
    </script>
  </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值