1
1
2
- import { Color3 , Mesh , StandardMaterial , VertexBuffer , VertexData } from "@babylonjs/core" ;
2
+ import * as BABYLON from "@babylonjs/core" ;
3
3
import { Context } from "../context" ;
4
4
import { GeometryHelper } from "../geometry-helper" ;
5
5
import * as Inputs from "../inputs/inputs" ;
@@ -25,21 +25,21 @@ export class Point {
25
25
* @drawable false
26
26
* @ignore true
27
27
*/
28
- drawPoint ( inputs : Inputs . Point . DrawPointDto ) : Mesh {
28
+ drawPoint ( inputs : Inputs . Point . DrawPointDto ) : BABYLON . Mesh {
29
29
const vectorPoints = [ inputs . point ] ;
30
30
31
- let colours ;
31
+ let colorsHex : string [ ] = [ ] ;
32
32
if ( Array . isArray ( inputs . colours ) ) {
33
- colours = inputs . colours . map ( colour => Color3 . FromHexString ( colour ) ) ;
33
+ colorsHex = inputs . colours ;
34
34
} else {
35
- colours = [ Color3 . FromHexString ( inputs . colours ) ] ;
35
+ colorsHex = [ inputs . colours ] ;
36
36
}
37
- const { positions, colors } = this . setUpPositionsAndColours ( vectorPoints , colours ) ;
37
+ // const { positions, colors } = this.setUpPositionsAndColours(vectorPoints, colours);
38
38
if ( inputs . pointMesh && inputs . updatable ) {
39
- this . updatePoints ( inputs . pointMesh , inputs . opacity , inputs . size , positions , colors ) ;
39
+ this . updatePointsInstances ( inputs . pointMesh , vectorPoints ) ;
40
40
} else {
41
- inputs . pointMesh = this . createNewMesh (
42
- `poinsMesh${ Math . random ( ) } ` , positions , colors , inputs . opacity , inputs . size , inputs . updatable
41
+ inputs . pointMesh = this . createPointSpheresMesh (
42
+ `poinsMesh${ Math . random ( ) } ` , vectorPoints , colorsHex , inputs . opacity , inputs . size , inputs . updatable
43
43
) ;
44
44
}
45
45
return inputs . pointMesh ;
@@ -54,28 +54,29 @@ export class Point {
54
54
* @drawable false
55
55
* @ignore true
56
56
*/
57
- drawPoints ( inputs : Inputs . Point . DrawPointsDto ) : Mesh {
57
+ drawPoints ( inputs : Inputs . Point . DrawPointsDto ) : BABYLON . Mesh {
58
58
const vectorPoints = inputs . points ;
59
-
60
- let colours ;
59
+ let coloursHex : string [ ] = [ ] ;
61
60
if ( Array . isArray ( inputs . colours ) ) {
62
- colours = inputs . colours . map ( colour => Color3 . FromHexString ( colour ) ) ;
61
+ coloursHex = inputs . colours ;
62
+ if ( coloursHex . length === 1 ) {
63
+ coloursHex = inputs . points . map ( ( ) => coloursHex [ 0 ] ) ;
64
+ }
63
65
} else {
64
- colours = [ Color3 . FromHexString ( inputs . colours ) ] ;
66
+ coloursHex = inputs . points . map ( ( ) => inputs . colours as string ) ;
65
67
}
66
- const { positions, colors } = this . setUpPositionsAndColours ( vectorPoints , colours ) ;
67
68
if ( inputs . pointsMesh && inputs . updatable ) {
68
- if ( inputs . pointsMesh . getTotalVertices ( ) === vectorPoints . length ) {
69
- this . updatePoints ( inputs . pointsMesh , inputs . opacity , inputs . size , positions , colors ) ;
69
+ if ( inputs . pointsMesh . getChildMeshes ( ) . length === vectorPoints . length ) {
70
+ this . updatePointsInstances ( inputs . pointsMesh , vectorPoints ) ;
70
71
} else {
71
72
inputs . pointsMesh . dispose ( ) ;
72
- inputs . pointsMesh = this . createNewMesh (
73
- `pointsMesh${ Math . random ( ) } ` , positions , colors , inputs . opacity , inputs . size , inputs . updatable
73
+ inputs . pointsMesh = this . createPointSpheresMesh (
74
+ `pointsMesh${ Math . random ( ) } ` , vectorPoints , coloursHex , inputs . opacity , inputs . size , inputs . updatable
74
75
) ;
75
76
}
76
77
} else {
77
- inputs . pointsMesh = this . createNewMesh (
78
- `pointsMesh${ Math . random ( ) } ` , positions , colors , inputs . opacity , inputs . size , inputs . updatable
78
+ inputs . pointsMesh = this . createPointSpheresMesh (
79
+ `pointsMesh${ Math . random ( ) } ` , vectorPoints , coloursHex , inputs . opacity , inputs . size , inputs . updatable
79
80
) ;
80
81
}
81
82
return inputs . pointsMesh ;
@@ -90,7 +91,7 @@ export class Point {
90
91
* @drawable false
91
92
* @ignore true
92
93
*/
93
- drawPointsAsync ( inputs : Inputs . Point . DrawPointsDto ) : Promise < Mesh > {
94
+ drawPointsAsync ( inputs : Inputs . Point . DrawPointsDto ) : Promise < BABYLON . Mesh > {
94
95
return Promise . resolve ( this . drawPoints ( inputs ) ) ;
95
96
}
96
97
@@ -351,38 +352,63 @@ export class Point {
351
352
return { index : closestPointIndex + 1 , distance, point } ;
352
353
}
353
354
354
- private createNewMesh (
355
- meshName : string , positions : number [ ] , colors : number [ ] , opacity : number , size : number , updatable : boolean ) : Mesh {
355
+ private createPointSpheresMesh (
356
+ meshName : string , positions : Base . Point3 [ ] , colors : string [ ] , opacity : number , size : number , updatable : boolean ) : BABYLON . Mesh {
356
357
357
- const vertexData = new VertexData ( ) ;
358
+ const positionsModel = positions . map ( ( pos , index ) => {
359
+ return {
360
+ position : pos ,
361
+ color : colors [ index ] ,
362
+ index
363
+ } ;
364
+ } ) ;
358
365
359
- vertexData . positions = positions ;
360
- vertexData . colors = colors ;
366
+ const colorSet = Array . from ( new Set ( colors ) ) ;
367
+ const materialSet = colorSet . map ( ( colour , index ) => {
361
368
362
- const pointsMesh = new Mesh ( meshName , this . context . scene ) ;
363
- vertexData . applyToMesh ( pointsMesh , updatable ) ;
369
+ const mat = new BABYLON . StandardMaterial ( `mat${ Math . random ( ) } ` , this . context . scene ) ;
364
370
365
- const mat = new StandardMaterial ( `mat${ Math . random ( ) } ` , this . context . scene ) ;
371
+ mat . disableLighting = true ;
372
+ mat . emissiveColor = BABYLON . Color3 . FromHexString ( colour ) ;
373
+ mat . alpha = opacity ;
366
374
367
- mat . emissiveColor = new Color3 ( 1 , 1 , 1 ) ;
368
- mat . disableLighting = true ;
369
- mat . pointsCloud = true ;
370
- mat . alpha = opacity ;
371
- mat . pointSize = size ;
375
+ const positions = positionsModel . filter ( s => s . color === colour ) ;
372
376
373
- pointsMesh . material = mat ;
377
+ return { hex : colorSet , material : mat , positions } ;
378
+ } ) ;
379
+
380
+ const pointsMesh = new BABYLON . Mesh ( meshName , this . context . scene ) ;
381
+
382
+ materialSet . forEach ( ms => {
383
+ const sphereOriginal = BABYLON . MeshBuilder . CreateSphere ( `sphere${ Math . random ( ) } ` , { diameter : size , segments : 6 , updatable } , this . context . scene ) ;
384
+ sphereOriginal . material = ms . material ;
385
+ sphereOriginal . isVisible = false ;
386
+ ms . positions . forEach ( ( pos , index ) => {
387
+ const instance = sphereOriginal . createInstance ( `sphere-${ index } -${ Math . random ( ) } ` ) ;
388
+ instance . position = new BABYLON . Vector3 ( pos . position [ 0 ] , pos . position [ 1 ] , pos . position [ 2 ] ) ;
389
+ instance . metadata = { index : pos . index } ;
390
+ instance . parent = pointsMesh ;
391
+ instance . isVisible = true ;
392
+ } ) ;
393
+ } ) ;
374
394
375
395
return pointsMesh ;
376
396
}
377
397
378
- private updatePoints ( mesh : Mesh , opacity : number , size : number , positions : any [ ] , colors : any [ ] ) : void {
379
- mesh . updateVerticesData ( VertexBuffer . PositionKind , positions ) ;
380
- mesh . updateVerticesData ( VertexBuffer . ColorKind , colors ) ;
381
- mesh . material . alpha = opacity ;
382
- mesh . material . pointSize = size ;
398
+ private updatePointsInstances ( mesh : BABYLON . Mesh , positions : any [ ] ) : void {
399
+
400
+ const children = mesh . getChildMeshes ( ) ;
401
+ const po = { } ;
402
+ positions . forEach ( ( pos , index ) => {
403
+ po [ index ] = new BABYLON . Vector3 ( pos [ 0 ] , pos [ 1 ] , pos [ 2 ] ) ;
404
+ } ) ;
405
+
406
+ children . forEach ( ( child : BABYLON . InstancedMesh ) => {
407
+ child . position = po [ child . metadata . index ] ;
408
+ } ) ;
383
409
}
384
410
385
- private setUpPositionsAndColours ( vectorPoints : number [ ] [ ] , colours : Color3 [ ] ) : { positions , colors } {
411
+ private setUpPositionsAndColours ( vectorPoints : number [ ] [ ] , colours : BABYLON . Color3 [ ] ) : { positions , colors } {
386
412
const positions = [ ] ;
387
413
const colors = [ ] ;
388
414
0 commit comments