Skip to content

Commit

Permalink
Add options for the color of highlighted models
Browse files Browse the repository at this point in the history
  • Loading branch information
myshov committed Jun 29, 2023
1 parent 17ca784 commit ad631bd
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
3 changes: 3 additions & 0 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ async function start() {
fontSize: 14,
},
},
hoverHighlight: {
intencity: 0.1,
},
});

const defaultState = {
Expand Down
4 changes: 4 additions & 0 deletions src/defaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export const defaultOptions: Required<PluginOptions> = {
color: '#ffffff',
intencity: 3,
},
hoverHighlight: {
color: '#ffffff',
intencity: 0.0,
},
dracoScriptsUrl: 'https://unpkg.com/@2gis/mapgl-gltf@^1/dist/libs/draco/',
modelsBaseUrl: '',
modelsLoadStrategy: 'waitAll',
Expand Down
10 changes: 8 additions & 2 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader';
import { degToRad } from './utils/common';
import { concatUrl, isAbsoluteUrl } from './utils/url';
import { mapPointFromLngLat, geoToMapDistance } from './utils/geo';
import { defaultOptions } from './defaultOptions';

import type { ModelOptions } from './types/plugin';
import type { ModelOptions, HightlightOptions } from './types/plugin';

interface LoaderOptions {
dracoScriptsUrl: string;
Expand All @@ -16,6 +17,7 @@ interface LoaderOptions {
export class Loader extends GLTFLoader {
private options: LoaderOptions;
private models = new Map<string, THREE.Object3D>();
private hoverParams = defaultOptions.hoverHighlight;

constructor(options: LoaderOptions) {
super();
Expand Down Expand Up @@ -90,7 +92,7 @@ export class Loader extends GLTFLoader {
map: obj.material.map,
});
obj.material = newMaterial;
obj.material.emissive = new THREE.Color('#ffffff');
obj.material.emissive = new THREE.Color(this.hoverParams.color);
obj.material.emissiveIntensity = 0.0;
}
});
Expand Down Expand Up @@ -121,4 +123,8 @@ export class Loader extends GLTFLoader {
public getModels() {
return this.models;
}

public setHoverParams(color: HightlightOptions) {
this.hoverParams = color;
}
}
1 change: 1 addition & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class GltfPlugin extends Evented<GltfPluginEventTable> {
modelsBaseUrl: this.options.modelsBaseUrl,
dracoScriptsUrl: this.options.dracoScriptsUrl,
});
this.loader.setHoverParams(this.options.hoverHighlight);
this.models = this.loader.getModels();

this.poiGroups = new PoiGroups(this.map, this.options.poiConfig);
Expand Down
11 changes: 9 additions & 2 deletions src/realtyScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class RealtyScene {
private activePoiGroupIds: Id[] = [];
private container: HTMLElement;
private buildingFacadeIds: Id[] = [];
// this field is needed when the hightlited
// this field is needed when the highlighted
// model is placed under the floors' control
private prevHoveredModelId: Id | null = null;

Expand Down Expand Up @@ -437,6 +437,13 @@ export class RealtyScene {
}

public toggleHighlightModel(modelId: Id) {
// skip toggle if user is using default emissiveIntensity
// that means that model won't be hovered
const { intencity } = this.options.hoverHighlight;
if (intencity === 0) {
return;
}

const model = this.models.get(String(modelId));

if (model === undefined) {
Expand All @@ -450,7 +457,7 @@ export class RealtyScene {
obj.material.emissiveIntensity = 0.0;
shouldUnsetFlag = true;
} else {
obj.material.emissiveIntensity = 0.25;
obj.material.emissiveIntensity = intencity;
}
}
});
Expand Down
30 changes: 25 additions & 5 deletions src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export type ColorRepresentation = ColorModelString | HexColorString | number;
*/
export interface AmbientLightOptions {
/**
* Numeric value of the RGB component of the color.
* Default is 0xffffff
* Color of the ambient light.
* @default '#ffffff'
*/
color: ColorRepresentation;
/**
* Numeric value of the light's strength/intensity.
* Default is 1
* @default 3
*/
intencity: number;
}
Expand Down Expand Up @@ -60,6 +60,23 @@ export interface ControlOptions {
position: ControlPosition;
}

/**
* Options for the highlight color of hovered models
*/
export interface HightlightOptions {
/**
* Color of the hover
* @default '#ffffff'
*/
color?: ColorRepresentation;
/**
* Intensity of the color on the hover
* in the range from 0 to 1
* @default 0.0
*/
intencity: number;
}

/**
* Options for the plugin
*/
Expand Down Expand Up @@ -95,11 +112,14 @@ export interface PluginOptions {
*/
secondary?: PoiConfigGranular;
};

/**
* Floors control
* Settings for floors' control
*/
floorsControl?: ControlOptions;
/**
* Settings of the highlighted models
*/
hoverHighlight?: HightlightOptions;
}

/**
Expand Down

0 comments on commit ad631bd

Please sign in to comment.