Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added remaining color types for PNTS #391

Merged
merged 7 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/three/PNTSLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
BufferAttribute,
DefaultLoadingManager,
Vector3,
Color,
} from 'three';
import { rgb565torgb } from '../utilities/rgb565torgb.js';

const DRACO_ATTRIBUTE_MAP = {
RGB: 'color',
Expand Down Expand Up @@ -81,6 +83,9 @@ export class PNTSLoader extends PNTSLoaderBase {
const POINTS_LENGTH = featureTable.getData( 'POINTS_LENGTH' );
const POSITION = featureTable.getData( 'POSITION', POINTS_LENGTH, 'FLOAT', 'VEC3' );
const RGB = featureTable.getData( 'RGB', POINTS_LENGTH, 'UNSIGNED_BYTE', 'VEC3' );
const RGBA = featureTable.getData( 'RGBA', POINTS_LENGTH, 'UNSIGNED_BYTE', 'VEC4' );
const RGB565 = featureTable.getData( 'RGB565', POINTS_LENGTH, 'UNSIGNED_SHORT', 'SCALAR' );
const CONSTANT_RGBA = featureTable.getData( 'CONSTANT_RGBA', POINTS_LENGTH, 'UNSIGNED_BYTE', 'VEC4' );
const POSITION_QUANTIZED = featureTable.getData( 'POSITION_QUANTIZED', POINTS_LENGTH, 'UNSIGNED_SHORT', 'VEC3' );
const QUANTIZED_VOLUME_SCALE = featureTable.getData( 'QUANTIZED_VOLUME_SCALE', POINTS_LENGTH, 'FLOAT', 'VEC3' );
const QUANTIZED_VOLUME_OFFSET = featureTable.getData( 'QUANTIZED_VOLUME_OFFSET', POINTS_LENGTH, 'FLOAT', 'VEC3' );
Expand Down Expand Up @@ -111,20 +116,48 @@ export class PNTSLoader extends PNTSLoaderBase {

}

if ( RGB !== null ) {
if ( RGBA !== null ) {

geometry.setAttribute( 'color', new BufferAttribute( RGBA, 4, true ) );
material.vertexColors = true;
material.transparent = true;

} else if ( RGB !== null ) {

geometry.setAttribute( 'color', new BufferAttribute( RGB, 3, true ) );
material.vertexColors = true;

} else if ( RGB565 !== null ) {

const color = new Uint8Array( POINTS_LENGTH * 3 );
for ( let i = 0; i < POINTS_LENGTH; i ++ ) {

const rgbColor = rgb565torgb( RGB565[ i ] );

for ( let j = 0; j < 3; j ++ ) {

const index = 3 * i + j;
color[ index ] = rgbColor[ j ];

}

}
geometry.setAttribute( 'color', new BufferAttribute( color, 3, true ) );
material.vertexColors = true;

} else if ( CONSTANT_RGBA !== null ) {

const color = new Color( `rgb(${CONSTANT_RGBA[ 0 ]}, ${CONSTANT_RGBA[ 1 ]}, ${CONSTANT_RGBA[ 2 ]})` );
material.color = color;
material.opacity = CONSTANT_RGBA[ 3 ] / 255;
material.transparent = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only be set to true if the opacity is < 1.0


}

}

[
'CONSTANT_RGBA',
'BATCH_LENGTH',
'RGBA',
'RGB565',
'NORMAL',
'NORMAL_OCT16P',
].forEach( ( feature ) => {
Expand Down
11 changes: 11 additions & 0 deletions src/utilities/rgb565torgb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function rgb565torgb( rgb565 ) {

let r = ( rgb565 & 0xF800 ) >> 11;
let g = ( rgb565 & 0x07E0 ) >> 5;
let b = rgb565 & 0x001F;
r = ( r << 3 ) | ( r >> 2 ); // Scale up red component
g = ( g << 2 ) | ( g >> 4 ); // Scale up green component
b = ( b << 3 ) | ( b >> 2 ); // Scale up blue component
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These all seem like magic numbers. Does this 565 unpacking implementation come from somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I updated the 565 unpacking to a much cleaner one I found over here link, the previous one was taken from a arduino forum.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks! Can we add a comment with a link to that page - then I'll test it and get this merged!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok 👍

return [ r, g, b ];

}