1- import { mat3 , mat4 } from 'gl-matrix' ;
1+ import { mat3 } from 'gl-matrix' ;
22
33import * as macro from 'vtk.js/Sources/macros' ;
44import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper' ;
@@ -11,13 +11,18 @@ import vtkWebGPUShaderCache from 'vtk.js/Sources/Rendering/WebGPU/ShaderCache';
1111import vtkWebGPUUniformBuffer from 'vtk.js/Sources/Rendering/WebGPU/UniformBuffer' ;
1212import vtkWebGPUSimpleMapper from 'vtk.js/Sources/Rendering/WebGPU/SimpleMapper' ;
1313import vtkWebGPUTypes from 'vtk.js/Sources/Rendering/WebGPU/Types' ;
14+ import {
15+ addClipPlaneEntries ,
16+ getClippingPlaneEquationsInCoords ,
17+ getClipPlaneShaderChecks ,
18+ MAX_CLIPPING_PLANES ,
19+ } from 'vtk.js/Sources/Rendering/WebGPU/Helpers/ClippingPlanes' ;
1420
1521const { BufferUsage, PrimitiveTypes } = vtkWebGPUBufferManager ;
1622const { Representation } = vtkProperty ;
1723const { ScalarMode } = vtkMapper ;
1824const { CoordinateSystem } = vtkProp ;
1925const { DisplayLocation } = vtkProperty2D ;
20-
2126const vtkWebGPUPolyDataVS = `
2227//VTK::Renderer::Dec
2328
@@ -432,13 +437,17 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
432437 publicAPI . updateUBO = ( ) => {
433438 const actor = model . WebGPUActor . getRenderable ( ) ;
434439 const ppty = actor . getProperty ( ) ;
440+ const clippingPlanesMTime = model . renderable . getClippingPlanesMTime ( ) ;
441+ const activeCamera = model . WebGPURenderer . getRenderable ( ) . getActiveCamera ( ) ;
435442 const backfaceProperty = actor . getBackfaceProperty ?. ( ) ?? ppty ;
436443 const utime = model . UBO . getSendTime ( ) ;
437444 if (
438445 publicAPI . getMTime ( ) <= utime &&
439446 ppty . getMTime ( ) <= utime &&
440447 backfaceProperty . getMTime ( ) <= utime &&
441- model . renderable . getMTime ( ) <= utime
448+ model . renderable . getMTime ( ) <= utime &&
449+ clippingPlanesMTime <= utime &&
450+ activeCamera . getMTime ( ) <= utime
442451 ) {
443452 return ;
444453 }
@@ -541,6 +550,24 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
541550 model . UBO . setValue ( 'LineWidth' , ppty . getLineWidth ( ) ) ;
542551 model . UBO . setValue ( 'Opacity' , ppty . getOpacity ( ) ) ;
543552 model . UBO . setValue ( 'PropID' , model . WebGPUActor . getPropID ( ) ) ;
553+ model . UBO . setValue ( 'NumClipPlanes' , 0 ) ;
554+
555+ if ( ! model . is2D && model . useRendererMatrix ) {
556+ const webGPUCamera = model . WebGPURenderer . getViewNodeFor ( activeCamera ) ;
557+ const cameraKeyMats = webGPUCamera . getKeyMatrices ( model . WebGPURenderer ) ;
558+ const numClipPlanes = getClippingPlaneEquationsInCoords (
559+ model . renderable ,
560+ cameraKeyMats . wcvc ,
561+ model . clipPlanes
562+ ) ;
563+ model . UBO . setValue ( 'NumClipPlanes' , numClipPlanes ) ;
564+
565+ if ( numClipPlanes > 0 ) {
566+ for ( let i = 0 ; i < numClipPlanes ; i ++ ) {
567+ model . UBO . setArray ( `ClipPlane${ i } ` , model . clipPlanes [ i ] ) ;
568+ }
569+ }
570+ }
544571
545572 // Only send if needed
546573 model . UBO . sendIfNeeded ( model . WebGPURenderWindow . getDevice ( ) ) ;
@@ -635,6 +662,19 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
635662 ] ) . result ;
636663
637664 vDesc . setCode ( code ) ;
665+
666+ const fDesc = pipeline . getShaderDescription ( 'fragment' ) ;
667+ code = fDesc . getCode ( ) ;
668+ const clipPlaneChecks = getClipPlaneShaderChecks ( {
669+ countName : 'mapperUBO.NumClipPlanes' ,
670+ planePrefix : 'mapperUBO.ClipPlane' ,
671+ positionName : 'input.vertexVC' ,
672+ } ) ;
673+ code = vtkWebGPUShaderCache . substitute ( code , '//VTK::Position::Impl' , [
674+ ...clipPlaneChecks ,
675+ '//VTK::Position::Impl' ,
676+ ] ) . result ;
677+ fDesc . setCode ( code ) ;
638678 } ;
639679 model . shaderReplacements . set (
640680 'replaceShaderPosition' ,
@@ -1451,7 +1491,6 @@ export function extend(publicAPI, model, initiaLalues = {}) {
14511491 model . vertexShaderTemplate = vtkWebGPUPolyDataVS ;
14521492
14531493 model . _tmpMat3 = mat3 . identity ( new Float64Array ( 9 ) ) ;
1454- model . _tmpMat4 = mat4 . identity ( new Float64Array ( 16 ) ) ;
14551494
14561495 // UBO
14571496 model . UBO = vtkWebGPUUniformBuffer . newInstance ( { label : 'mapperUBO' } ) ;
@@ -1491,6 +1530,8 @@ export function extend(publicAPI, model, initiaLalues = {}) {
14911530 model . UBO . addEntry ( 'ClipNear' , 'f32' ) ;
14921531 model . UBO . addEntry ( 'ClipFar' , 'f32' ) ;
14931532 model . UBO . addEntry ( 'Time' , 'u32' ) ;
1533+ addClipPlaneEntries ( model . UBO , 'ClipPlane' ) ;
1534+ model . UBO . addEntry ( 'NumClipPlanes' , 'u32' ) ;
14941535
14951536 // Build VTK API
14961537 macro . setGet ( publicAPI , model , [
@@ -1503,6 +1544,9 @@ export function extend(publicAPI, model, initiaLalues = {}) {
15031544 ] ) ;
15041545
15051546 model . textures = [ ] ;
1547+ model . clipPlanes = Array . from ( { length : MAX_CLIPPING_PLANES } , ( ) => [
1548+ 0.0 , 0.0 , 0.0 , 0.0 ,
1549+ ] ) ;
15061550
15071551 // Object methods
15081552 vtkWebGPUCellArrayMapper ( publicAPI , model ) ;
0 commit comments