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
@@ -409,11 +414,15 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
409414 publicAPI . updateUBO = ( ) => {
410415 const actor = model . WebGPUActor . getRenderable ( ) ;
411416 const ppty = actor . getProperty ( ) ;
417+ const clippingPlanesMTime = model . renderable . getClippingPlanesMTime ( ) ;
418+ const activeCamera = model . WebGPURenderer . getRenderable ( ) . getActiveCamera ( ) ;
412419 const utime = model . UBO . getSendTime ( ) ;
413420 if (
414421 publicAPI . getMTime ( ) <= utime &&
415422 ppty . getMTime ( ) <= utime &&
416- model . renderable . getMTime ( ) <= utime
423+ model . renderable . getMTime ( ) <= utime &&
424+ clippingPlanesMTime <= utime &&
425+ activeCamera . getMTime ( ) <= utime
417426 ) {
418427 return ;
419428 }
@@ -474,6 +483,24 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
474483 model . UBO . setValue ( 'LineWidth' , ppty . getLineWidth ( ) ) ;
475484 model . UBO . setValue ( 'Opacity' , ppty . getOpacity ( ) ) ;
476485 model . UBO . setValue ( 'PropID' , model . WebGPUActor . getPropID ( ) ) ;
486+ model . UBO . setValue ( 'NumClipPlanes' , 0 ) ;
487+
488+ if ( ! model . is2D && model . useRendererMatrix ) {
489+ const webGPUCamera = model . WebGPURenderer . getViewNodeFor ( activeCamera ) ;
490+ const cameraKeyMats = webGPUCamera . getKeyMatrices ( model . WebGPURenderer ) ;
491+ const numClipPlanes = getClippingPlaneEquationsInCoords (
492+ model . renderable ,
493+ cameraKeyMats . wcvc ,
494+ model . clipPlanes
495+ ) ;
496+ model . UBO . setValue ( 'NumClipPlanes' , numClipPlanes ) ;
497+
498+ if ( numClipPlanes > 0 ) {
499+ for ( let i = 0 ; i < numClipPlanes ; i ++ ) {
500+ model . UBO . setArray ( `ClipPlane${ i } ` , model . clipPlanes [ i ] ) ;
501+ }
502+ }
503+ }
477504
478505 // Only send if needed
479506 model . UBO . sendIfNeeded ( model . WebGPURenderWindow . getDevice ( ) ) ;
@@ -550,6 +577,19 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
550577 ] ) . result ;
551578
552579 vDesc . setCode ( code ) ;
580+
581+ const fDesc = pipeline . getShaderDescription ( 'fragment' ) ;
582+ code = fDesc . getCode ( ) ;
583+ const clipPlaneChecks = getClipPlaneShaderChecks ( {
584+ countName : 'mapperUBO.NumClipPlanes' ,
585+ planePrefix : 'mapperUBO.ClipPlane' ,
586+ positionName : 'input.vertexVC' ,
587+ } ) ;
588+ code = vtkWebGPUShaderCache . substitute ( code , '//VTK::Position::Impl' , [
589+ ...clipPlaneChecks ,
590+ '//VTK::Position::Impl' ,
591+ ] ) . result ;
592+ fDesc . setCode ( code ) ;
553593 } ;
554594 model . shaderReplacements . set (
555595 'replaceShaderPosition' ,
@@ -1129,7 +1169,8 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
11291169 // --- Texture Coordinates ---
11301170 let tcoords = null ;
11311171 if (
1132- model . renderable . getInterpolateScalarsBeforeMapping ?. ( ) &&
1172+ ( model . renderable . getAreScalarsMappedFromCells ( ) ||
1173+ model . renderable . getInterpolateScalarsBeforeMapping ?. ( ) ) &&
11331174 model . renderable . getColorCoordinates ( )
11341175 ) {
11351176 tcoords = model . renderable . getColorCoordinates ( ) ;
@@ -1363,7 +1404,6 @@ export function extend(publicAPI, model, initiaLalues = {}) {
13631404 model . vertexShaderTemplate = vtkWebGPUPolyDataVS ;
13641405
13651406 model . _tmpMat3 = mat3 . identity ( new Float64Array ( 9 ) ) ;
1366- model . _tmpMat4 = mat4 . identity ( new Float64Array ( 16 ) ) ;
13671407
13681408 // UBO
13691409 model . UBO = vtkWebGPUUniformBuffer . newInstance ( { label : 'mapperUBO' } ) ;
@@ -1391,6 +1431,8 @@ export function extend(publicAPI, model, initiaLalues = {}) {
13911431 model . UBO . addEntry ( 'ClipNear' , 'f32' ) ;
13921432 model . UBO . addEntry ( 'ClipFar' , 'f32' ) ;
13931433 model . UBO . addEntry ( 'Time' , 'u32' ) ;
1434+ addClipPlaneEntries ( model . UBO , 'ClipPlane' ) ;
1435+ model . UBO . addEntry ( 'NumClipPlanes' , 'u32' ) ;
13941436
13951437 // Build VTK API
13961438 macro . setGet ( publicAPI , model , [
@@ -1403,6 +1445,9 @@ export function extend(publicAPI, model, initiaLalues = {}) {
14031445 ] ) ;
14041446
14051447 model . textures = [ ] ;
1448+ model . clipPlanes = Array . from ( { length : MAX_CLIPPING_PLANES } , ( ) => [
1449+ 0.0 , 0.0 , 0.0 , 0.0 ,
1450+ ] ) ;
14061451
14071452 // Object methods
14081453 vtkWebGPUCellArrayMapper ( publicAPI , model ) ;
0 commit comments