diff --git a/packages/frontend-2/lib/viewer/pipelines/snow/SnowPipeline.ts b/packages/frontend-2/lib/viewer/pipelines/snow/SnowPipeline.ts index 54ba6e1288..b062714db4 100644 --- a/packages/frontend-2/lib/viewer/pipelines/snow/SnowPipeline.ts +++ b/packages/frontend-2/lib/viewer/pipelines/snow/SnowPipeline.ts @@ -1,7 +1,8 @@ import type { SpeckleRenderer, MeshBatch, - SpeckleStandardMaterial + SpeckleStandardMaterial, + SpeckleMesh } from '@speckle/viewer' import { ProgressivePipeline, @@ -19,12 +20,13 @@ import { AssetType } from '@speckle/viewer' import SnowMaterial from './SnowMaterial' -import type SpeckleMesh from '@speckle/viewer/dist/modules/objects/SpeckleMesh' import { RepeatWrapping, NearestFilter } from 'three' import snowTex from './snow.png' import { SnowFallPass } from './SnowFallPass' export class SnowPipeline extends ProgressivePipeline { + private batchSnowMaterials: { [id: string]: SnowMaterial } = {} + constructor(speckleRenderer: SpeckleRenderer) { super(speckleRenderer) @@ -135,14 +137,26 @@ export class SnowPipeline extends ProgressivePipeline { GeometryType.MESH ) - for (let k = 0; k < batches.length; k++) { - const batchRenderable: SpeckleMesh = batches[k].renderObject as SpeckleMesh - const batchMaterial: SpeckleStandardMaterial = batches[k] - .batchMaterial as SpeckleStandardMaterial - const snowMaterial = new SnowMaterial({}, ['USE_RTE']) - snowMaterial.copy(batchMaterial) - snowMaterial.normalMap = snowTexture - batchRenderable.setOverrideBatchMaterial(snowMaterial) + this.onBeforePipelineRender = () => { + for (let k = 0; k < batches.length; k++) { + const batchRenderable: SpeckleMesh = batches[k].renderObject as SpeckleMesh + const batchMaterial: SpeckleStandardMaterial = batches[k] + .batchMaterial as SpeckleStandardMaterial + if (!this.batchSnowMaterials[batches[k].id]) { + const snowMaterial = new SnowMaterial({}, ['USE_RTE']) + snowMaterial.copy(batchMaterial) + snowMaterial.normalMap = snowTexture + this.batchSnowMaterials[batches[k].id] = snowMaterial + } + batchRenderable.setOverrideBatchMaterial(this.batchSnowMaterials[batches[k].id]) + } + } + + this.onAfterPipelineRender = () => { + for (let k = 0; k < batches.length; k++) { + const batchRenderable: SpeckleMesh = batches[k].renderObject as SpeckleMesh + batchRenderable.restoreBatchMaterial() + } } } } diff --git a/packages/viewer-sandbox/src/Pipelines/Snow/SnowPipeline.ts b/packages/viewer-sandbox/src/Pipelines/Snow/SnowPipeline.ts index c305080b0f..a005ccc9d9 100644 --- a/packages/viewer-sandbox/src/Pipelines/Snow/SnowPipeline.ts +++ b/packages/viewer-sandbox/src/Pipelines/Snow/SnowPipeline.ts @@ -23,6 +23,8 @@ import snowTex from '../../../assets/snow.png' import { SnowFallPass } from './SnowFallPass' export class SnowPipeline extends ProgressivePipeline { + private batchSnowMaterials: { [id: string]: SnowMaterial } = {} + constructor(speckleRenderer: SpeckleRenderer) { super(speckleRenderer) @@ -133,14 +135,26 @@ export class SnowPipeline extends ProgressivePipeline { GeometryType.MESH ) - for (let k = 0; k < batches.length; k++) { - const batchRenderable: SpeckleMesh = batches[k].renderObject as SpeckleMesh - const batchMaterial: SpeckleStandardMaterial = batches[k] - .batchMaterial as SpeckleStandardMaterial - const snowMaterial = new SnowMaterial({}, ['USE_RTE']) - snowMaterial.copy(batchMaterial) - snowMaterial.normalMap = snowTexture - batchRenderable.setOverrideBatchMaterial(snowMaterial) + this.onBeforePipelineRender = () => { + for (let k = 0; k < batches.length; k++) { + const batchRenderable: SpeckleMesh = batches[k].renderObject as SpeckleMesh + const batchMaterial: SpeckleStandardMaterial = batches[k] + .batchMaterial as SpeckleStandardMaterial + if (!this.batchSnowMaterials[batches[k].id]) { + const snowMaterial = new SnowMaterial({}, ['USE_RTE']) + snowMaterial.copy(batchMaterial) + snowMaterial.normalMap = snowTexture + this.batchSnowMaterials[batches[k].id] = snowMaterial + } + batchRenderable.setOverrideBatchMaterial(this.batchSnowMaterials[batches[k].id]) + } + } + + this.onAfterPipelineRender = () => { + for (let k = 0; k < batches.length; k++) { + const batchRenderable: SpeckleMesh = batches[k].renderObject as SpeckleMesh + batchRenderable.restoreBatchMaterial() + } } } } diff --git a/packages/viewer/src/index.ts b/packages/viewer/src/index.ts index 8f200bd815..6d9a8bd5c9 100644 --- a/packages/viewer/src/index.ts +++ b/packages/viewer/src/index.ts @@ -138,6 +138,8 @@ import { SpeckleWebGLRenderer } from './modules/objects/SpeckleWebGLRenderer.js' import { InstancedMeshBatch } from './modules/batching/InstancedMeshBatch.js' import { ViewModeEvent, ViewModeEventPayload } from './modules/extensions/ViewModes.js' import { BasitPipeline } from './modules/pipeline/Pipelines/BasitViewPipeline.js' +import SpeckleMesh from './modules/objects/SpeckleMesh.js' +import SpeckleInstancedMesh from './modules/objects/SpeckleInstancedMesh.js' export { Viewer, @@ -235,7 +237,9 @@ export { NOT_INTERSECTED, INTERSECTED, CONTAINED, - ViewModeEvent + ViewModeEvent, + SpeckleMesh, + SpeckleInstancedMesh } export type {