Skip to content

Commit

Permalink
fix(lb-annotation): Fix color render when use webworker
Browse files Browse the repository at this point in the history
  • Loading branch information
lihqi committed Dec 20, 2024
1 parent 1c3ee03 commit 7cd60ef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
3 changes: 3 additions & 0 deletions packages/lb-annotation/src/core/pointCloud/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface IPointCloudAnnotationProps {
checkMode?: boolean;
toolName: THybridToolName;
proxyMode?: boolean;
view?: string;
}

const createEmptyImage = (size: { width: number; height: number }) => {
Expand Down Expand Up @@ -66,6 +67,7 @@ export class PointCloudAnnotation implements IPointCloudAnnotationOperation {
checkMode,
toolName,
proxyMode,
view,
}: IPointCloudAnnotationProps) {
const defaultOrthographic = PointCloudUtils.getDefaultOrthographicParams(size);

Expand All @@ -83,6 +85,7 @@ export class PointCloudAnnotation implements IPointCloudAnnotationOperation {
noAppend: true,
isOrthographicCamera: true,
orthographicParams: defaultOrthographic,
view,
});

if (pcdPath) {
Expand Down
57 changes: 32 additions & 25 deletions packages/lb-annotation/src/core/pointCloud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ interface IProps {
checkMode?: boolean;

hiddenText?: boolean;
view?: string;
}

interface IPipeTypes {
Expand All @@ -83,7 +84,7 @@ export interface IPointCloudDelegate extends IEventBus {
}

const DEFAULT_DISTANCE = 30;
const highlightWorker = new HighlightWorker({ type: 'module' });
let highlightWorker = new HighlightWorker({ type: 'module' });

export class PointCloud extends EventListener {
public renderer: THREE.WebGLRenderer;
Expand Down Expand Up @@ -165,6 +166,8 @@ export class PointCloud extends EventListener {

private highlightColor = 0xffff00;

private view: string;

constructor({
container,
noAppend,
Expand All @@ -175,6 +178,7 @@ export class PointCloud extends EventListener {
isSegment,
checkMode,
hiddenText = false,
view = '',
}: IProps) {
super();
this.container = container;
Expand All @@ -183,7 +187,7 @@ export class PointCloud extends EventListener {
this.config = config;
this.checkMode = checkMode ?? false;
this.hiddenText = hiddenText;

this.view = view;
// TODO: Need to extracted.
if (isOrthographicCamera && orthographicParams) {
this.isOrthographicCamera = true;
Expand Down Expand Up @@ -977,6 +981,10 @@ export class PointCloud extends EventListener {
};

public renderPointCloud(points: THREE.Points, radius?: number) {
this.clearPointCloud();
if (this.workerLoading) {
return;
}
points.name = this.pointCloudObjectName;

/**
Expand Down Expand Up @@ -1052,7 +1060,7 @@ export class PointCloud extends EventListener {
* @param radius Render the range of circle
*/
public loadPCDFile = async (src: string | undefined = this.currentPCDSrc, radius?: number) => {
if (!src) return;
if (!src || this.workerLoading) return;
this.clearPointCloud();
/**
* Clear Img Cache.
Expand Down Expand Up @@ -1162,20 +1170,16 @@ export class PointCloud extends EventListener {

public async handleWebworker(params: any) {
return new Promise((resolve, reject) => {
if (this.workerLoading) {
reject(new Error('highlightWorker called repeatedly, new call discarded'));
return;
}
this.workerLoading = true;
highlightWorker.terminate();
highlightWorker = new HighlightWorker({ type: 'module' });
highlightWorker.postMessage(params);

highlightWorker.onmessage = (e: any) => {
resolve(e.data);
this.workerLoading = false;
highlightWorker.terminate();
};
highlightWorker.onerror = (e: any) => {
reject(e);
this.workerLoading = false;
};
});
}
Expand All @@ -1196,6 +1200,10 @@ export class PointCloud extends EventListener {
resetAreas: [],
},
) {
if (this.workerLoading) {
return;
}
this.workerLoading = true;
const { modifiedBoxIds, resetAreas } = config;
const oldPointCloud = this.scene.getObjectByName(this.pointCloudObjectName) as THREE.Points;
if (!oldPointCloud) {
Expand Down Expand Up @@ -1223,18 +1231,6 @@ export class PointCloud extends EventListener {
this.handleWebworker(params)
.then((res: any) => {
const { color } = res;
let combinedColor = color;
if (modifiedBoxIds.length || resetAreas.length) {
combinedColor = color.map((item: any, index: number) => {
if (item === -1) {
// A magic number is needed here to represent the color used in the last rendering
// involved by packages/lb-annotation/src/core/pointCloud/highlightWorker.js REMAINED_COLOR_FLAG
return oldColor[index];
}
return item;
});
}
const colorAttribute = new THREE.BufferAttribute(combinedColor, 3);
/**
* Need to return;
*
Expand All @@ -1250,9 +1246,18 @@ export class PointCloud extends EventListener {
reject(new Error('Error Path'));
return;
}

// Save the new highlight color.
this.cacheInstance.updateColor(this.highlightPCDSrc, combinedColor);
let combinedColor = color;
if (modifiedBoxIds.length || resetAreas.length) {
combinedColor = color.map((item: any, index: number) => {
if (item === -1) {
// A magic number is needed here to represent the color used in the last rendering
// involved by packages/lb-annotation/src/core/pointCloud/highlightWorker.js REMAINED_COLOR_FLAG
return oldColor[index];
}
return item;
});
}
const colorAttribute = new THREE.BufferAttribute(combinedColor, 3);

// Clear
this.highlightPCDSrc = undefined;
Expand All @@ -1262,9 +1267,11 @@ export class PointCloud extends EventListener {
oldPointCloud.geometry.setAttribute('dimensions', colorAttribute);
oldPointCloud.geometry.attributes.dimensions.needsUpdate = true;
resolve(combinedColor);
this.workerLoading = false;
this.render();
})
.catch((e) => {
this.workerLoading = false;
reject(e);
});
}
Expand Down

0 comments on commit 7cd60ef

Please sign in to comment.