@@ -187,6 +187,7 @@ const useFeature = ({
187187 onComputedFeatureFetch,
188188 shouldUseFeatureIndex,
189189 isTilesetReady,
190+ useExternalStyle,
190191} : {
191192 id ?: string ;
192193 tilesetRef : MutableRefObject < Cesium3DTileset | undefined > ;
@@ -199,6 +200,7 @@ const useFeature = ({
199200 selectedFeatureIdsRef : MutableRefObject < string [ ] > ;
200201 shouldUseFeatureIndex ?: boolean ;
201202 isTilesetReady : boolean ;
203+ useExternalStyle ?: boolean ;
202204} ) => {
203205 const cachedFeaturesRef = useRef < CachedFeature [ ] > ( [ ] ) ;
204206 const cachedCalculatedLayerRef = useRef ( layer ) ;
@@ -220,49 +222,51 @@ const useFeature = ({
220222
221223 const computedFeature = evalFeature ( layer , { ...feature ?. feature , properties } ) ;
222224
223- const style = computedFeature ?. [ "3dtiles" ] ;
225+ if ( ! useExternalStyle ) {
226+ const style = computedFeature ?. [ "3dtiles" ] ;
224227
225- COMMON_STYLE_PROPERTIES . forEach ( ( { name, convert } ) => {
226- const val = convertStyle ( style ?. [ name ] , convert ) ;
227-
228- if ( name === "color" ) {
229- // Reset color to default so that new style could update all.
230- raw . color = DEFAULT_FEATURE_COLOR ;
228+ COMMON_STYLE_PROPERTIES . forEach ( ( { name, convert } ) => {
229+ const val = convertStyle ( style ?. [ name ] , convert ) ;
231230
232- // Apply color from style.
233- if ( val !== undefined ) {
234- raw . color = val ;
231+ if ( name === "color" ) {
232+ // Reset color to default so that new style could update all.
233+ raw . color = DEFAULT_FEATURE_COLOR ;
234+
235+ // Apply color from style.
236+ if ( val !== undefined ) {
237+ raw . color = val ;
238+ }
239+
240+ // Apply color for selected feature.
241+ if ( isFeatureSelected && typeof layer [ "3dtiles" ] ?. selectedFeatureColor === "string" ) {
242+ raw . color = toColor ( layer [ "3dtiles" ] ?. selectedFeatureColor ) ?? val ;
243+ }
244+ } else {
245+ if ( val !== undefined ) {
246+ raw [ name ] = val ;
247+ }
235248 }
249+ } ) ;
236250
237- // Apply color for selected feature.
238- if ( isFeatureSelected && typeof layer [ "3dtiles" ] ?. selectedFeatureColor === "string" ) {
239- raw . color = toColor ( layer [ "3dtiles" ] ?. selectedFeatureColor ) ?? val ;
240- }
241- } else {
242- if ( val !== undefined ) {
243- raw [ name ] = val ;
244- }
251+ if ( raw instanceof Cesium3DTilePointFeature ) {
252+ POINT_STYLE_PROPERTIES . forEach ( ( { name, convert } ) => {
253+ const val = convertStyle ( style ?. [ name ] , convert ) ;
254+ if ( val !== undefined ) {
255+ raw [ name ] = val ;
256+ }
257+ } ) ;
245258 }
246- } ) ;
247-
248- if ( raw instanceof Cesium3DTilePointFeature ) {
249- POINT_STYLE_PROPERTIES . forEach ( ( { name, convert } ) => {
250- const val = convertStyle ( style ?. [ name ] , convert ) ;
251- if ( val !== undefined ) {
252- raw [ name ] = val ;
253- }
254- } ) ;
255- }
256259
257- if ( "style" in raw ) {
258- raw . style = new Cesium3DTileStyle (
259- // TODO: Convert value if it's necessary
260- MODEL_STYLE_PROPERTIES . reduce ( ( res , { name, convert } ) => {
261- const val = convertStyle ( style ?. [ name as keyof typeof style ] , convert ) ;
262- if ( val === undefined ) return res ;
263- return { ...res , [ name ] : val } ;
264- } , { } ) ,
265- ) ;
260+ if ( "style" in raw ) {
261+ raw . style = new Cesium3DTileStyle (
262+ // TODO: Convert value if it's necessary
263+ MODEL_STYLE_PROPERTIES . reduce ( ( res , { name, convert } ) => {
264+ const val = convertStyle ( style ?. [ name as keyof typeof style ] , convert ) ;
265+ if ( val === undefined ) return res ;
266+ return { ...res , [ name ] : val } ;
267+ } , { } ) ,
268+ ) ;
269+ }
266270 }
267271
268272 attachTag ( feature . raw , {
@@ -276,7 +280,7 @@ const useFeature = ({
276280 }
277281 return ;
278282 } ,
279- [ evalFeature , layerId , viewer , shouldUseFeatureIndex , selectedFeatureIdsRef ] ,
283+ [ evalFeature , layerId , viewer , shouldUseFeatureIndex , selectedFeatureIdsRef , useExternalStyle ] ,
280284 ) ;
281285
282286 const handleTilesetLoad = useCallback (
@@ -615,6 +619,7 @@ export const useHooks = ({
615619 selectedFeatureIdsRef,
616620 shouldUseFeatureIndex,
617621 isTilesetReady,
622+ useExternalStyle : ! ! styleUrl ,
618623 } ) ;
619624
620625 const [ terrainHeightEstimate , setTerrainHeightEstimate ] = useState ( 0 ) ;
@@ -730,11 +735,23 @@ export const useHooks = ({
730735 }
731736 ( async ( ) => {
732737 const res = await fetch ( styleUrl ) ;
733- if ( ! res . ok ) return ;
734- setStyle ( new Cesium3DTileStyle ( await res . json ( ) ) ) ;
738+ if ( ! res . ok ) {
739+ console . warn ( "Failed to fetch style from:" , styleUrl ) ;
740+ return ;
741+ }
742+ const styleData = await res . json ( ) ;
743+ const newStyle = new Cesium3DTileStyle ( styleData ) ;
744+ setStyle ( newStyle ) ;
735745 } ) ( ) ;
736746 } , [ styleUrl ] ) ;
737747
748+ // Apply style to tileset when both external style and tileset are ready
749+ useEffect ( ( ) => {
750+ if ( style && tilesetRef . current && isTilesetReady ) {
751+ tilesetRef . current . style = style ;
752+ }
753+ } , [ style , isTilesetReady ] ) ;
754+
738755 const googleMapPhotorealisticResource = useMemo ( ( ) => {
739756 if ( type !== "google-photorealistic" || ! isVisible ) return null ;
740757
0 commit comments