Skip to content

Commit

Permalink
feat(react-components): add default resource styling prop to 3DResour…
Browse files Browse the repository at this point in the history
…ces (#3580)

* feat(react-components): add default resource styling prop to 3DResources

* fix: type import

---------

Co-authored-by: cognite-bulldozer[bot] <51074376+cognite-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
christjt and cognite-bulldozer[bot] authored Aug 14, 2023
1 parent 231f367 commit 6b259d9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
2 changes: 1 addition & 1 deletion react-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cognite/reveal-react-components",
"version": "0.8.0",
"version": "0.9.0",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2023 Cognite AS
*/
import { useRef, type ReactElement, useContext, useState, useEffect } from 'react';
import { type NodeAppearance, type Cognite3DViewer, type PointerEventData } from '@cognite/reveal';
import { type Cognite3DViewer, type PointerEventData } from '@cognite/reveal';
import { ModelsLoadingStateContext } from './ModelsLoadingContext';
import { CadModelContainer, type CadModelStyling } from '../CadModelContainer/CadModelContainer';
import {
Expand All @@ -16,26 +16,16 @@ import {
type AddImageCollection360Options,
type TypedReveal3DModel,
type AddResourceOptions,
type NodeDataResult
type Reveal3DResourcesProps,
type DefaultResourceStyling
} from './types';
import { queryMappedData } from './queryMappedData';
import { useFdmSdk, useSDK } from '../RevealContainer/SDKProvider';
import { useCalculateModelsStyling } from '../../hooks/useCalculateModelsStyling';
import { type DmsUniqueIdentifier } from '../../utilities/FdmSDK';

export type FdmAssetStylingGroup = {
fdmAssetExternalIds: DmsUniqueIdentifier[];
style: { cad: NodeAppearance };
};

export type Reveal3DResourcesProps = {
resources: AddResourceOptions[];
instanceStyling?: FdmAssetStylingGroup[];
onNodeClick?: (node: Promise<NodeDataResult | undefined>) => void;
};

export const Reveal3DResources = ({
resources,
defaultResourceStyling,
instanceStyling,
onNodeClick
}: Reveal3DResourcesProps): ReactElement => {
Expand All @@ -48,7 +38,15 @@ export const Reveal3DResources = ({
const numModelsLoaded = useRef(0);

useEffect(() => {
getTypedModels(resources, viewer).then(setReveal3DModels).catch(console.error);
getTypedModels(resources, viewer)
.then((models) => {
models.forEach((model) => {
setDefaultResourceStyling(model, defaultResourceStyling);
});
return models;
})
.then(setReveal3DModels)
.catch(console.error);
}, [resources, viewer]);

const reveal3DModelsStyling = useCalculateModelsStyling(reveal3DModels, instanceStyling ?? []);
Expand Down Expand Up @@ -155,3 +153,18 @@ async function getTypedModels(
})
);
}

function setDefaultResourceStyling(
model: TypedReveal3DModel,
defaultResourceStyling?: DefaultResourceStyling
): void {
if (model.styling !== undefined || defaultResourceStyling === undefined) {
return;
}

if (model.type === 'cad') {
model.styling = defaultResourceStyling.cad;
} else if (model.type === 'pointcloud') {
model.styling = defaultResourceStyling.pointcloud;
}
}
21 changes: 19 additions & 2 deletions react-components/src/components/Reveal3DResources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type NodeAppearance
} from '@cognite/reveal';
import { type Matrix4 } from 'three';
import { type Source } from '../../utilities/FdmSDK';
import { type DmsUniqueIdentifier, type Source } from '../../utilities/FdmSDK';
import { type Node3D } from '@cognite/sdk/dist/src';

export type AddImageCollection360Options = {
Expand All @@ -21,7 +21,7 @@ export type FdmPropertyType<NodeType> = Record<string, Record<string, NodeType>>
export type AddResourceOptions = AddReveal3DModelOptions | AddImageCollection360Options;

export type AddReveal3DModelOptions = AddModelOptions & { transform?: Matrix4 } & {
styling?: { default: NodeAppearance; mapped: NodeAppearance };
styling?: { default?: NodeAppearance; mapped?: NodeAppearance };
};
export type TypedReveal3DModel = AddReveal3DModelOptions & { type: SupportedModelTypes };

Expand All @@ -31,3 +31,20 @@ export type NodeDataResult = {
cadNode: Node3D;
intersection: CadIntersection;
};

export type FdmAssetStylingGroup = {
fdmAssetExternalIds: DmsUniqueIdentifier[];
style: { cad: NodeAppearance };
};

export type DefaultResourceStyling = {
cad?: { default?: NodeAppearance; mapped?: NodeAppearance };
pointcloud?: { default: NodeAppearance };
};

export type Reveal3DResourcesProps = {
resources: AddResourceOptions[];
defaultResourceStyling?: DefaultResourceStyling;
instanceStyling?: FdmAssetStylingGroup[];
onNodeClick?: (node: Promise<NodeDataResult | undefined>) => void;
};
6 changes: 4 additions & 2 deletions react-components/src/hooks/useCalculateModelsStyling.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*!
* Copyright 2023 Cognite AS
*/
import { type FdmAssetStylingGroup } from '../components/Reveal3DResources/Reveal3DResources';
import { type TypedReveal3DModel } from '../components/Reveal3DResources/types';
import {
type FdmAssetStylingGroup,
type TypedReveal3DModel
} from '../components/Reveal3DResources/types';
import { type PointCloudModelStyling } from '../components/PointCloudContainer/PointCloudContainer';
import {
type NodeStylingGroup,
Expand Down
5 changes: 3 additions & 2 deletions react-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export {
} from './components/CadModelContainer/CadModelContainer';
export {
type Reveal3DResourcesProps,
type FdmAssetStylingGroup
} from './components/Reveal3DResources/Reveal3DResources';
type FdmAssetStylingGroup,
type DefaultResourceStyling
} from './components/Reveal3DResources/types';
export type {
AddImageCollection360Options,
AddResourceOptions,
Expand Down
19 changes: 12 additions & 7 deletions react-components/stories/ModelsStyling.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const meta = {
argTypes: {
resources: {
description: 'Styling of all models',
options: ['RedDefaultGreenMapped', 'GrayDefaultBlueMapped', 'None'],
options: ['RedDefaultGreenMapped', 'GreenDefaultRedMapped', 'None'],
control: {
type: 'radio'
},
Expand All @@ -32,12 +32,12 @@ const meta = {
styling: { default: { color: new Color('red') }, mapped: { color: new Color('green') } }
}
],
GrayDefaultBlueMapped: [
GreenDefaultRedMapped: [
{
...model,
styling: {
default: { color: new Color('#efefef') },
mapped: { color: new Color('#c5cbff') }
default: { color: new Color('green') },
mapped: { color: new Color('red') }
}
}
],
Expand All @@ -63,9 +63,14 @@ export const Main: Story = {
...model
}
],
instanceStyling: []
defaultResourceStyling: {
cad: {
default: { color: new Color('#efefef') },
mapped: { color: new Color('#c5cbff') }
}
}
},
render: ({ resources }) => {
render: ({ resources, defaultResourceStyling }) => {
return (
<RevealContainer
sdk={sdk}
Expand All @@ -76,7 +81,7 @@ export const Main: Story = {
placement: 'topRight'
}
}}>
<Reveal3DResources resources={resources} />
<Reveal3DResources resources={resources} defaultResourceStyling={defaultResourceStyling} />
<CameraController
initialFitCamera={{
to: 'allModels'
Expand Down

0 comments on commit 6b259d9

Please sign in to comment.