Skip to content

Commit

Permalink
move renderer out of colorHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
meganrm committed Dec 4, 2023
1 parent c9acaf9 commit 690f03b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
27 changes: 17 additions & 10 deletions src/visGeometry/ColorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { map, round, isEqual } from "lodash";
import { Color } from "three";
import SimulariumRenderer from "./rendering/SimulariumRenderer";

export function convertColorStringToNumber(color: number | string): number {
if (typeof color !== "string") {
Expand All @@ -27,13 +26,12 @@ export const checkHexColor = (color: string): string => {
class ColorHandler {
public idColorMapping: Map<number, number>;
private colorsData: Float32Array;
private renderer: SimulariumRenderer;

constructor(colors: (number | string)[], renderer: SimulariumRenderer) {
constructor() {
this.idColorMapping = new Map<number, number>();
this.colorsData = this.setColorArray(colors);
this.renderer = renderer;
this.renderer.updateColors(colors.length, this.colorsData);
// will be set by setColorArray, but need to initialize
// so that typescript doesn't complain
this.colorsData = new Float32Array(this.numberOfColors * 4);
}

private get numberOfColors(): number {
Expand Down Expand Up @@ -146,7 +144,6 @@ class ColorHandler {
const newColorData = new Float32Array(newArray.length);
newColorData.set(newArray);
this.colorsData = newColorData;
this.renderer.updateColors(this.numberOfColors, this.colorsData);
return this.convertDataColorIndexToId(newColorDataIndex);
}

Expand All @@ -158,9 +155,15 @@ class ColorHandler {
return this.addNewColor(color);
}

public updateColorArray(colors: (number | string)[]): void {
public updateColorArray(colors: (number | string)[]): {
colorArray: Float32Array;
numberOfColors: number;
} {
this.colorsData = this.setColorArray(colors);
this.renderer.updateColors(colors.length, this.colorsData);
return {
colorArray: this.colorsData,
numberOfColors: this.numberOfColors,
};
}

public clearColorMapping(): void {
Expand All @@ -170,7 +173,7 @@ class ColorHandler {
public setColorForAgentTypes(
agentTypes: number[],
color: string | number
): void {
): { colorArray: Float32Array; numberOfColors: number } {
const colorString = convertColorNumberToString(color);
const colorId = this.getColorId(colorString);
/**
Expand All @@ -181,6 +184,10 @@ class ColorHandler {
agentTypes.forEach((id) => {
this.setColorForAgentType(id, colorId);
});
return {
colorArray: this.colorsData,
numberOfColors: this.numberOfColors,
};
}

public getColorForAgentType(agentType: number): Color {
Expand Down
17 changes: 14 additions & 3 deletions src/visGeometry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class VisGeometry {
this.backgroundColor = DEFAULT_BACKGROUND_COLOR;
this.pathEndColor = this.backgroundColor.clone();
this.renderer.setBackgroundColor(this.backgroundColor);
this.colorHandler = new ColorHandler([], this.renderer);
this.colorHandler = new ColorHandler();
// Set up scene

this.scene = new Scene();
Expand Down Expand Up @@ -1075,15 +1075,26 @@ class VisGeometry {
}

public createMaterials(colors: (number | string)[]): void {
this.colorHandler.updateColorArray(colors);
const newColorData = this.colorHandler.updateColorArray(colors);
this.renderer.updateColors(
newColorData.numberOfColors,
newColorData.colorArray
);
this.setAgentColors();
}

public applyColorToAgents(
agentIds: number[],
color: string | number
): void {
this.colorHandler.setColorForAgentTypes(agentIds, color);
const newColorData = this.colorHandler.setColorForAgentTypes(
agentIds,
color
);
this.renderer.updateColors(
newColorData.numberOfColors,
newColorData.colorArray
);
this.updateScene(this.currentSceneAgents);
}

Expand Down

0 comments on commit 690f03b

Please sign in to comment.