Skip to content

Commit

Permalink
feat: vc-primitive-tileset added customShader props
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyaoji committed Jan 29, 2022
1 parent bff1a70 commit 7cbfa80
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
56 changes: 54 additions & 2 deletions packages/components/primitives/tileset/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ExtractPropTypes, PropType } from 'vue'
import { createCommentVNode, defineComponent, getCurrentInstance } from 'vue'
import type { VcComponentInternalInstance } from '@vue-cesium/utils/types'
import type { VcComponentInternalInstance, VcComponentPublicInstance } from '@vue-cesium/utils/types'
import { usePrimitives } from '@vue-cesium/composables'
import {
show,
Expand Down Expand Up @@ -182,7 +182,19 @@ export const tilesetPrimitiveProps = {
},
customShader: {
type: Object as PropType<Cesium.CustomShader>
}
},
properties: {
type: Array as PropType<
Array<{
key: string
keyValue: any
propertyName: string
propertyValue: any
}>
>
},
fragmentShader: String,
replaceFS: Boolean
}
export default defineComponent({
name: 'VcPrimitiveTileset',
Expand All @@ -193,6 +205,46 @@ export default defineComponent({
const instance = getCurrentInstance() as VcComponentInternalInstance
instance.cesiumClass = 'Cesium3DTileset'
usePrimitives(props, ctx, instance)
;(instance.proxy as VcComponentPublicInstance).createPromise.then(obj => {
const tileset = obj.cesiumObject as Cesium.Cesium3DTileset
instance.removeCallbacks.push(tileset.tileVisible.addEventListener(updateTile))
console.log(tileset)
})

const updateTile = (tile: Cesium.Cesium3DTile) => {
const content = tile.content
const model = (content as any)._model
// sets properties
for (let i = 0; i < content.featuresLength; i++) {
const feature = content.getFeature(i)
if (props.properties && props.properties.length) {
props.properties.forEach(property => {
if (feature.hasProperty(property['key']) && feature.getProperty(property['key']) === property['keyValue']) {
feature.setProperty(property['propertyName'], property['propertyValue'])
}
})
}
}
// sets fragmentShader
if (props.fragmentShader && model && model._sourcePrograms && model._rendererResources) {
Object.keys(model._sourcePrograms).forEach(key => {
const program = model._sourcePrograms[key]
const sourceShaders = model._rendererResources.sourceShaders
if (props.replaceFS) {
sourceShaders[program.fragmentShader] = props.fragmentShader
} else {
const oldFS = sourceShaders[program.fragmentShader]
sourceShaders[program.fragmentShader] = oldFS.replace(
'gl_FragColor = vec4(color, 1.0);\n}',
`gl_FragColor = vec4(color, 1.0);
${props.fragmentShader}\n}
`
)
}
})
model._shouldRegenerateShaders = true
}
}
return () => createCommentVNode(kebabCase(instance.proxy?.$options.name || ''))
}
})
Expand Down
4 changes: 4 additions & 0 deletions packages/composables/use-common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function (props, { emit }, vcInstance: VcComponentInternalInstanc

// state
vcInstance.alreadyListening = []
vcInstance.removeCallbacks = []
let unwatchFns: Array<WatchStopHandle> = []
vcInstance.mounted = false
const vcMitt: Emitter<VcMittEvents> = mitt()
Expand Down Expand Up @@ -124,6 +125,9 @@ export default function (props, { emit }, vcInstance: VcComponentInternalInstanc
setPropsWatcher(false)
vcInstance.cesiumObject = undefined
vcInstance.mounted = false
vcInstance.removeCallbacks.forEach(removeCallback => {
removeCallback()
})
emit('destroyed', vcInstance)
logger.debug(`${vcInstance.cesiumClass}---unmounted`)

Expand Down
2 changes: 2 additions & 0 deletions packages/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ interface VcComponentInternalInstance extends ComponentInternalInstance {
unmount?(): Promise<boolean | undefined>
children: Array<VcComponentInternalInstance>
alreadyListening: string[]
removeCallbacks: Array<AnyFunction<any>>
// third
earth?: AnyObject
map?: AnyObject
Expand Down Expand Up @@ -534,6 +535,7 @@ export type VcPrimitive =
| Cesium.PointPrimitive
| Cesium.Primitive
| Cesium.Model
| Cesium.Cesium3DTileset

export type VcPrimitiveCollection =
| Cesium.BillboardCollection
Expand Down

0 comments on commit 7cbfa80

Please sign in to comment.