Line data Source code
1 : import * as THREE from 'three'
2 : import { PLYLoader } from 'three/examples/jsm/loaders/PLYLoader'
3 :
4 : import { isPLYAsciiFormat } from './loader/ply'
5 :
6 : import type {
7 : ModelAdapter,
8 : ModelAdapterCapabilities,
9 : ModelLoadContext,
10 : ModelLoadResult
11 : } from './ModelAdapter'
12 : import type { Load3dHost, MaterialMode } from './interfaces'
13 : import { FastPLYLoader } from './loader/FastPLYLoader'
14 :
15 24 : const POINT_CLOUD_CAPABILITIES: ModelAdapterCapabilities = {
16 : fitToViewer: true,
17 : requiresMaterialRebuild: true,
18 : gizmoTransform: false,
19 : lighting: true,
20 : exportable: true,
21 : materialModes: ['original', 'pointCloud', 'normal', 'wireframe'],
22 : fitTargetSize: 5
23 : }
24 :
25 24 : export class PointCloudModelAdapter implements ModelAdapter {
26 : readonly kind = 'pointCloud' as const
27 : readonly extensions = ['ply'] as const
28 : readonly capabilities = POINT_CLOUD_CAPABILITIES
29 :
30 : private readonly plyLoader = new PLYLoader()
31 : private readonly fastPlyLoader = new FastPLYLoader()
32 :
33 27 : constructor(private readonly getPLYEngine: Load3dHost['getPLYEngine']) {}
34 :
35 : async load(
36 : ctx: ModelLoadContext,
37 : path: string,
38 : _filename: string,
39 : fetchBytes: () => Promise<ArrayBuffer>
40 : ): Promise<ModelLoadResult | null> {
41 0 : const arrayBuffer = await fetchBytes()
42 : const isASCII = isPLYAsciiFormat(arrayBuffer)
43 :
44 0 : const plyGeometry =
45 : isASCII && this.getPLYEngine() === 'fastply'
46 : ? this.fastPlyLoader.parse(arrayBuffer)
47 : : (this.plyLoader.setPath(path), this.plyLoader.parse(arrayBuffer))
48 :
49 0 : ctx.setOriginalModel(plyGeometry)
50 0 : if (!plyGeometry.hasAttribute('normal')) {
51 0 : plyGeometry.computeVertexNormals()
52 : }
53 :
54 0 : const hasVertexColors = plyGeometry.hasAttribute('color')
55 0 : const hasFaces = (plyGeometry.index?.count ?? 0) > 0
56 :
57 : const object =
58 : ctx.materialMode === 'pointCloud' || !hasFaces
59 : ? buildPointsGroup(ctx, plyGeometry, hasVertexColors)
60 : : buildMeshGroup(ctx, plyGeometry, hasVertexColors)
61 :
62 : const capabilities = hasFaces
63 : ? POINT_CLOUD_CAPABILITIES
64 : : { ...POINT_CLOUD_CAPABILITIES, materialModes: ['pointCloud'] as const }
65 :
66 0 : return { object, capabilities }
67 : }
68 : }
69 :
70 0 : function buildPointsGroup(
71 : ctx: ModelLoadContext,
72 : geometry: THREE.BufferGeometry,
73 : hasVertexColors: boolean
74 : ): THREE.Group {
75 0 : geometry.computeBoundingSphere()
76 0 : if (geometry.boundingSphere) {
77 0 : const { center, radius } = geometry.boundingSphere
78 0 : geometry.translate(-center.x, -center.y, -center.z)
79 0 : if (radius > 0) {
80 0 : const scale = 1.0 / radius
81 0 : geometry.scale(scale, scale, scale)
82 : }
83 : }
84 :
85 0 : const pointMaterial = hasVertexColors
86 : ? new THREE.PointsMaterial({
87 : size: 0.005,
88 : vertexColors: true,
89 : sizeAttenuation: true
90 : })
91 : : new THREE.PointsMaterial({
92 : size: 0.005,
93 : color: 0xcccccc,
94 : sizeAttenuation: true
95 : })
96 :
97 0 : const points = new THREE.Points(geometry, pointMaterial)
98 0 : ctx.registerOriginalMaterial(points as unknown as THREE.Mesh, pointMaterial)
99 :
100 0 : const group = new THREE.Group()
101 0 : group.add(points)
102 0 : return group
103 : }
104 :
105 0 : function buildMeshGroup(
106 : ctx: ModelLoadContext,
107 : geometry: THREE.BufferGeometry,
108 : hasVertexColors: boolean
109 : ): THREE.Group {
110 0 : const material = hasVertexColors
111 : ? new THREE.MeshStandardMaterial({
112 : vertexColors: true,
113 : metalness: 0.0,
114 : roughness: 0.5,
115 : side: THREE.DoubleSide
116 : })
117 : : ctx.standardMaterial.clone()
118 :
119 0 : if (!hasVertexColors && material instanceof THREE.MeshStandardMaterial) {
120 0 : material.side = THREE.DoubleSide
121 : }
122 :
123 0 : const mesh = new THREE.Mesh(geometry, material)
124 0 : ctx.registerOriginalMaterial(mesh, material)
125 :
126 0 : const group = new THREE.Group()
127 0 : group.add(mesh)
128 0 : return group
129 : }
130 :
131 0 : export function buildPointCloudForMaterialMode(
132 : originalGeometry: THREE.BufferGeometry,
133 : mode: MaterialMode,
134 : standardMaterial: THREE.MeshStandardMaterial,
135 : originalMaterials: WeakMap<THREE.Mesh, THREE.Material | THREE.Material[]>
136 : ): THREE.Group {
137 0 : const geometry = originalGeometry.clone()
138 0 : const hasVertexColors = geometry.hasAttribute('color')
139 :
140 0 : const ctx: ModelLoadContext = {
141 : setOriginalModel: () => {},
142 : registerOriginalMaterial: (mesh, material) =>
143 : originalMaterials.set(mesh, material),
144 : standardMaterial,
145 : materialMode: mode
146 : }
147 :
148 0 : if (mode === 'pointCloud') {
149 0 : return buildPointsGroup(ctx, geometry, hasVertexColors)
150 : }
151 :
152 0 : const group = buildMeshGroup(ctx, geometry, hasVertexColors)
153 :
154 0 : if (mode === 'normal' || mode === 'wireframe') {
155 0 : const mesh = group.children[0] as THREE.Mesh
156 0 : mesh.material =
157 : mode === 'normal'
158 : ? new THREE.MeshNormalMaterial({
159 : flatShading: false,
160 : side: THREE.DoubleSide
161 : })
162 : : new THREE.MeshBasicMaterial({
163 : color: 0xffffff,
164 : wireframe: true
165 : })
166 : }
167 :
168 0 : return group
169 : }
|