PCL

12/2/2020 threejspcl点云

# 效果

本示例由 29.4 万点阵数据绘制

# 代码

html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../three/build/three.min.js"></script>
  <script src="../three/examples/js/controls/FirstPersonControls.js"></script>
  <script src="../three/examples/js/controls/TrackballControls.js"></script>
  <script src="../three/examples/js/controls/TransformControls.js"></script>
  <script src="../three/examples/js/controls/OrbitControls.js"></script>
  <script src="../three/examples/js/libs/stats.min.js"></script>
  <script src="../three/examples/js/controls/TrackballControls.js"></script>
  <style>
    html,
    body,
    .three-box {
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
    }

    .three-box {
      /* background-color: #eee; */
    }
  </style>
</head>

<body>
  <div class="three-box" id='box' data-lll='xxxx'></div>
</body>
<script src='./data.js'></script>
<script src='./t_pcl.js'></script>
<script src="./index.js"></script>

</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
js
const tpcl = new Tpcl(document.getElementById('box'))
const data = pclData.filter(([x, y, z]) => x || y || z)
tpcl.updateData(data)

let curdata = 1
function update() {
  curdata++
  curdata % 2 === 0 ? tpcl.updateData(data) : tpcl.updateData([])
  requestAnimationFrame(update)
}

update()
1
2
3
4
5
6
7
8
9
10
11
t_pcl



class Tpcl {
  constructor(el, maxPoints = 300000) {
    this.$el = el
    this.$maxPoints = maxPoints
    this.init()
  }
  init() {
    const { offsetWidth, offsetHeight } = this.$el
    const scene = new THREE.Scene();
    scene.background = new THREE.Color(0x000000);
    // scene.fog = new THREE.Fog(0x050505, 2000, 3500);
    const camera = new THREE.PerspectiveCamera(75, offsetWidth / offsetHeight, 0.1, 8000);
    const renderer = new THREE.WebGLRenderer();
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(offsetWidth, offsetHeight);
    // 设置相机的位置
    camera.position.z = 3000;
    camera.position.x = 0;
    camera.position.y = 0;
    this.$el.appendChild(renderer.domElement);

    this.$threeObj = {
      scene,
      camera,
      renderer,

    }
    this.color = new THREE.Color();
    this.initPoints()
    // 三种控制器
    this.initOrbi()
    // this.initTrac()
    // this.initTracBall()
    this.render()
    // this.initStates()

  }
  initStates() {
    const stats = new Stats();
    this.$el.appendChild(stats.dom);
    this.stats = stats
  }
  initOrbi() {
    const { camera, renderer } = this.$threeObj
    const controls = new THREE.OrbitControls(camera, renderer.domElement);
    controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
    controls.dampingFactor = 0.05;

    controls.screenSpacePanning = false;

    controls.minDistance = 100;
    controls.maxDistance = 5000;

    controls.maxPolarAngle = Math.PI / 2;
    this.controls = controls
  }
  initTrac() {
    const { camera, renderer } = this.$threeObj
    const controls = new THREE.TrackballControls(camera, renderer.domElement);

    controls.rotateSpeed = 1.0;
    controls.zoomSpeed = 1.2;
    controls.panSpeed = 0.8;
    this.controls = controls
  }
  initFirst() {
    let clock = new THREE.Clock();
    let controls = new THREE.FirstPersonControls(this.$threeObj.camera);
    controls.enabled = true;
    controls.autoForward = true;
    controls.lookSpeed = 0.02; //鼠标移动查看的速度
    controls.movementSpeed = 10; //相机移动速度
    controls.noFly = false;
    controls.constrainVertical = true; //约束垂直
    controls.verticalMin = 1.0;
    controls.verticalMax = 2.0;
    controls.lon = 0; //进入初始视角x轴的角度
    controls.lat = 0; //初始视角进入后y轴的角度
    this.controls = controls
    this.clock = clock
  }
  initTracBall() {
    const { scene, camera, renderer } = this.$threeObj
    const controls = new THREE.TrackballControls(camera, renderer.domElement);

    // controls.rotateSpeed = 1;
    // controls.zoomSpeed = 1;
    // controls.panSpeed = 1;

    // controls.keys = [65, 83, 68];
    this.controls = controls
  }
  initPoints() {
    //! 更新BufferGeometries,最重要的是理解你不能调整 buffers 大小(这种操作开销很大,相当于创建了个新的geometry)。 但你可以更新 buffers的内容。
    const positions = new Float32Array(this.$maxPoints * 3)
    const colors = new Float32Array(this.$maxPoints * 3)
    const geometry = new THREE.BufferGeometry();
    geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3))
    geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3))
    const material = new THREE.PointsMaterial({ size: 1, vertexColors: true });
    this.pointesMaterial = material
    let points = new THREE.Points(geometry, material);
    this.points = points
    this.pointesMaterial = material
    this.pointesGeometry = geometry
    this.$threeObj.scene.add(points);
  }
  updateData(arr) {
    //! 更新BufferGeometries,最重要的是理解你不能调整 buffers 大小(这种操作开销很大,相当于创建了个新的geometry)。 但你可以更新 buffers的内容。
    // const positions = this.points.geometry.attributes.array
    // const colors = this.points.geometry.attributes.color.array
    const positions = this.points.geometry.attributes.position
    const colors = this.points.geometry.attributes.color
    arr.forEach(([x, y, z, b, g, r], index) => {
      positions.setXYZ(index, x, -y, z)
      colors.setXYZ(index, r / 255, g / 255, b / 255)
      // const start = index * 3
      // positions[start] = x
      // positions[start + 1] = -y
      // positions[start + 2] = z
      // colors[start] = r / 255
      // colors[start + 1] = g / 255
      // colors[start + 2] = b / 255
    })
    this.points.geometry.setDrawRange(0, arr.length)
    this.points.geometry.attributes.position.needsUpdate = true
    this.points.geometry.attributes.color.needsUpdate = true
  }
  destory() {
    const { pointesMaterial, pointesGeometry, requestId, $el, stats, $threeObj, controls } = this

    requestId && cancelAnimationFrame(requestId)
    pointesMaterial && pointesMaterial.dispose()
    pointesGeometry && pointesGeometry.dispose()
    $el && stats && $el.removeChild(stats.dom)
    $el && $threeObj && $el.removeChild($threeObj.renderer.domElement)
    controls && controls.dispose()
  }
  resize() {
    const { scene, camera, renderer } = this.$threeObj
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
  }
  render() {
    const { controls, clock, stats } = this
    const { scene, camera, renderer } = this.$threeObj
    // controls.update(clock.getDelta());
    controls && controls.update();
    stats && stats.update();
    renderer.render(scene, camera);
    this.requestId = requestAnimationFrame(() => {
      this.render()
    })
  }
}


function setbdColor(b, g, r) {
  document.body.style.border = `10px solid rgb(${r},${g},${b})`
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
pclData
// x y z b g r
const pclData = [
  ...
  [-170.494, -102.207, 1287, 9, 20, 12],
  [-168.57, -102.207, 1287, 14, 22, 15],
  [-166.646, -102.207, 1287, 17, 21, 16],
  [-164.723, -102.207, 1287, 18, 26, 19],
  [-162.799, -102.207, 1287, 43, 54, 45],
  [-160.875, -102.207, 1287, 70, 82, 76],
  [-158.952, -102.207, 1287, 58, 74, 75],
  [-157.028, -102.207, 1287, 60, 77, 78],
  [-155.104, -102.207, 1287, 61, 77, 86],
  ...
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Last Updated: 12/3/2020, 4:59:24 PM