import { mat3, mat4 } from 'gl-matrix'; import { ObjectType } from 'vtk.js/Sources/Rendering/OpenGL/BufferObject/Constants';
import * as macro from 'vtk.js/Sources/macros';
import vtkBufferObject from 'vtk.js/Sources/Rendering/OpenGL/BufferObject'; import vtkStickMapperVS from 'vtk.js/Sources/Rendering/OpenGL/glsl/vtkStickMapperVS.glsl'; import vtkPolyDataFS from 'vtk.js/Sources/Rendering/OpenGL/glsl/vtkPolyDataFS.glsl';
import vtkShaderProgram from 'vtk.js/Sources/Rendering/OpenGL/ShaderProgram'; import vtkOpenGLPolyDataMapper from 'vtk.js/Sources/Rendering/OpenGL/PolyDataMapper';
import { registerOverride } from 'vtk.js/Sources/Rendering/OpenGL/ViewNodeFactory';
const { vtkErrorMacro } = macro;
function vtkOpenGLStickMapper(publicAPI, model) { model.classHierarchy.push('vtkOpenGLStickMapper');
const superClass = { ...publicAPI };
publicAPI.getShaderTemplate = (shaders, ren, actor) => { shaders.Vertex = vtkStickMapperVS; shaders.Fragment = vtkPolyDataFS; shaders.Geometry = ''; };
publicAPI.replaceShaderValues = (shaders, ren, actor) => { let VSSource = shaders.Vertex; let FSSource = shaders.Fragment;
VSSource = vtkShaderProgram.substitute(VSSource, '//VTK::Camera::Dec', [ 'uniform mat4 VCPCMatrix;\n', 'uniform mat4 MCVCMatrix;', ]).result;
FSSource = vtkShaderProgram.substitute( FSSource, '//VTK::PositionVC::Dec', 'varying vec4 vertexVCVSOutput;' ).result;
FSSource = vtkShaderProgram.substitute( FSSource, '//VTK::PositionVC::Impl', ' vec4 vertexVC = vertexVCVSOutput;\n' ).result;
const replacement = [ 'uniform int cameraParallel;\n', 'varying float radiusVCVSOutput;\n', 'varying vec3 orientVCVSOutput;\n', 'varying float lengthVCVSOutput;\n', 'varying vec3 centerVCVSOutput;\n', 'uniform mat4 VCPCMatrix;\n', ]; FSSource = vtkShaderProgram.substitute( FSSource, '//VTK::Normal::Dec', replacement ).result;
let fragString = ''; if (model.context.getExtension('EXT_frag_depth')) { fragString = ' gl_FragDepthEXT = (pos.z / pos.w + 1.0) / 2.0;\n'; } if (model._openGLRenderWindow.getWebgl2()) { fragString = 'gl_FragDepth = (pos.z / pos.w + 1.0) / 2.0;\n'; } FSSource = vtkShaderProgram.substitute(FSSource, '//VTK::Depth::Impl', [ ' vec3 EyePos;\n', ' vec3 EyeDir;\n', ' if (cameraParallel != 0) {\n', ' EyePos = vec3(vertexVC.x, vertexVC.y, vertexVC.z + 3.0*radiusVCVSOutput);\n', ' EyeDir = vec3(0.0,0.0,-1.0); }\n', ' else {\n', ' EyeDir = vertexVC.xyz;\n', ' EyePos = vec3(0.0,0.0,0.0);\n', ' float lengthED = length(EyeDir);\n', ' EyeDir = normalize(EyeDir);\n', ' if (lengthED > radiusVCVSOutput*3.0) {\n', ' EyePos = vertexVC.xyz - EyeDir*3.0*radiusVCVSOutput; }\n', ' }\n',
' EyePos = EyePos - centerVCVSOutput;\n',
' vec3 base1;\n', ' if (abs(orientVCVSOutput.z) < 0.99) {\n', ' base1 = normalize(cross(orientVCVSOutput,vec3(0.0,0.0,1.0))); }\n', ' else {\n', ' base1 = normalize(cross(orientVCVSOutput,vec3(0.0,1.0,0.0))); }\n', ' vec3 base2 = cross(orientVCVSOutput,base1);\n', ' EyePos = vec3(dot(EyePos,base1),dot(EyePos,base2),dot(EyePos,orientVCVSOutput));\n', ' EyeDir = vec3(dot(EyeDir,base1),dot(EyeDir,base2),dot(EyeDir,orientVCVSOutput));\n',
' EyePos = EyePos/radiusVCVSOutput;\n',
' float a = EyeDir.x*EyeDir.x + EyeDir.y*EyeDir.y;\n', ' float b = 2.0*(EyePos.x*EyeDir.x + EyePos.y*EyeDir.y);\n', ' float c = EyePos.x*EyePos.x + EyePos.y*EyePos.y - 1.0;\n', ' float d = b*b - 4.0*a*c;\n', ' vec3 normalVCVSOutput = vec3(0.0,0.0,1.0);\n', ' if (d < 0.0) { discard; }\n', ' else {\n', ' float t = (-b - sqrt(d))/(2.0*a);\n', ' float tz = EyePos.z + t*EyeDir.z;\n', ' vec3 iPoint = EyePos + t*EyeDir;\n', ' if (abs(iPoint.z)*radiusVCVSOutput > lengthVCVSOutput*0.5) {\n', ' float t2 = (-b + sqrt(d))/(2.0*a);\n', ' float tz2 = EyePos.z + t2*EyeDir.z;\n', ' if (tz2*radiusVCVSOutput > lengthVCVSOutput*0.5 || tz*radiusVCVSOutput < -0.5*lengthVCVSOutput) { discard; }\n', ' else {\n', ' normalVCVSOutput = orientVCVSOutput;\n', ' float t3 = (lengthVCVSOutput*0.5/radiusVCVSOutput - EyePos.z)/EyeDir.z;\n', ' iPoint = EyePos + t3*EyeDir;\n', ' vertexVC.xyz = radiusVCVSOutput*(iPoint.x*base1 + iPoint.y*base2 + iPoint.z*orientVCVSOutput) + centerVCVSOutput;\n', ' }\n', ' }\n', ' else {\n', ' normalVCVSOutput = iPoint.x*base1 + iPoint.y*base2;\n', ' vertexVC.xyz = radiusVCVSOutput*(normalVCVSOutput + iPoint.z*orientVCVSOutput) + centerVCVSOutput;\n', ' }\n', ' }\n',
' vec4 pos = VCPCMatrix * vertexVC;\n', fragString, ]).result;
FSSource = vtkShaderProgram.substitute( FSSource, '//VTK::Normal::Impl', '' ).result;
if (model.haveSeenDepthRequest) { FSSource = vtkShaderProgram.substitute(FSSource, '//VTK::ZBuffer::Impl', [ 'if (depthRequest == 1) {', 'float computedZ = (pos.z / pos.w + 1.0) / 2.0;', 'float iz = floor(computedZ * 65535.0 + 0.1);', 'float rf = floor(iz/256.0)/255.0;', 'float gf = mod(iz,256.0)/255.0;', 'gl_FragData[0] = vec4(rf, gf, 0.0, 1.0); }', ]).result; }
shaders.Vertex = VSSource; shaders.Fragment = FSSource;
superClass.replaceShaderValues(shaders, ren, actor); };
publicAPI.setMapperShaderParameters = (cellBO, ren, actor) => { if ( cellBO.getCABO().getElementCount() && (model.VBOBuildTime > cellBO.getAttributeUpdateTime().getMTime() || cellBO.getShaderSourceTime().getMTime() > cellBO.getAttributeUpdateTime().getMTime()) ) { if (cellBO.getProgram().isAttributeUsed('orientMC')) { if ( !cellBO.getVAO().addAttributeArray( cellBO.getProgram(), cellBO.getCABO(), 'orientMC', 12, cellBO.getCABO().getStride(), model.context.FLOAT, 3, false ) ) { vtkErrorMacro("Error setting 'orientMC' in shader VAO."); } } if (cellBO.getProgram().isAttributeUsed('offsetMC')) { if ( !cellBO .getVAO() .addAttributeArray( cellBO.getProgram(), cellBO.getCABO().getColorBO(), 'offsetMC', 0, cellBO.getCABO().getColorBOStride(), model.context.UNSIGNED_BYTE, 3, true ) ) { vtkErrorMacro("Error setting 'offsetMC' in shader VAO."); } } if (cellBO.getProgram().isAttributeUsed('radiusMC')) { if ( !cellBO.getVAO().addAttributeArray( cellBO.getProgram(), cellBO.getCABO(), 'radiusMC', 24, cellBO.getCABO().getStride(), model.context.FLOAT, 1, false ) ) { vtkErrorMacro("Error setting 'radiusMC' in shader VAO."); } } }
superClass.setMapperShaderParameters(cellBO, ren, actor); };
publicAPI.setCameraShaderParameters = (cellBO, ren, actor) => { const program = cellBO.getProgram();
const cam = ren.getActiveCamera(); const keyMats = model.openGLCamera.getKeyMatrices(ren);
if (program.isUniformUsed('VCPCMatrix')) { program.setUniformMatrix('VCPCMatrix', keyMats.vcpc); }
if (!actor.getIsIdentity()) { const actMats = model.openGLActor.getKeyMatrices(); if (program.isUniformUsed('MCVCMatrix')) { const tmp4 = new Float64Array(16); mat4.multiply(tmp4, keyMats.wcvc, actMats.mcwc); program.setUniformMatrix('MCVCMatrix', tmp4); } if (program.isUniformUsed('normalMatrix')) { const anorms = new Float64Array(9); mat3.multiply(anorms, keyMats.normalMatrix, actMats.normalMatrix); program.setUniformMatrix3x3('normalMatrix', anorms); } } else { if (program.isUniformUsed('MCVCMatrix')) { program.setUniformMatrix('MCVCMatrix', keyMats.wcvc); } if (program.isUniformUsed('normalMatrix')) { program.setUniformMatrix3x3('normalMatrix', keyMats.normalMatrix); } }
if (program.isUniformUsed('cameraParallel')) { cellBO .getProgram() .setUniformi('cameraParallel', cam.getParallelProjection()); } };
publicAPI.getOpenGLMode = (rep, type) => model.context.TRIANGLES;
publicAPI.buildBufferObjects = (ren, actor) => { const poly = model.currentInput;
if (poly === null) { return; }
model.renderable.mapScalars(poly, 1.0); const c = model.renderable.getColorMapColors();
const vbo = model.primitives[model.primTypes.Tris].getCABO();
const pointData = poly.getPointData(); const points = poly.getPoints(); const numPoints = points.getNumberOfPoints(); const pointArray = points.getData(); let pointSize = 3;
pointSize += 4;
let colorData = null; let colorComponents = 0; vbo.setColorBOStride(4);
if (!vbo.getColorBO()) { vbo.setColorBO(vtkBufferObject.newInstance()); } vbo.getColorBO().setOpenGLRenderWindow(model._openGLRenderWindow); if (c) { colorComponents = c.getNumberOfComponents(); vbo.setColorOffset(4); colorData = c.getData(); vbo.setColorBOStride(8); } vbo.setColorComponents(colorComponents);
vbo.setStride(pointSize * 4);
const packedVBO = new Float32Array(pointSize * numPoints * 12); const packedUCVBO = new Uint8Array(12 * numPoints * (colorData ? 8 : 4));
let scales = null; let orientationArray = null; if ( model.renderable.getScaleArray() != null && pointData.hasArray(model.renderable.getScaleArray()) ) { scales = pointData.getArray(model.renderable.getScaleArray()).getData(); }
if ( model.renderable.getOrientationArray() != null && pointData.hasArray(model.renderable.getOrientationArray()) ) { orientationArray = pointData .getArray(model.renderable.getOrientationArray()) .getData(); } else { vtkErrorMacro([ 'Error setting orientationArray.\n', 'You have to specify the stick orientation', ]); }
const verticesArray = [ 0, 1, 3, 0, 3, 2, 2, 3, 5, 2, 5, 4, ];
let pointIdx = 0; let colorIdx = 0; let vboIdx = 0; let ucIdx = 0;
for (let i = 0; i < numPoints; ++i) { let length = model.renderable.getLength(); let radius = model.renderable.getRadius(); if (scales) { length = scales[i * 2]; radius = scales[i * 2 + 1]; }
for (let j = 0; j < verticesArray.length; ++j) { pointIdx = i * 3; packedVBO[vboIdx++] = pointArray[pointIdx++]; packedVBO[vboIdx++] = pointArray[pointIdx++]; packedVBO[vboIdx++] = pointArray[pointIdx++]; pointIdx = i * 3; packedVBO[vboIdx++] = orientationArray[pointIdx++] * length; packedVBO[vboIdx++] = orientationArray[pointIdx++] * length; packedVBO[vboIdx++] = orientationArray[pointIdx++] * length; packedVBO[vboIdx++] = radius;
packedUCVBO[ucIdx++] = 255 * (verticesArray[j] % 2); packedUCVBO[ucIdx++] = verticesArray[j] >= 4 ? 255 : 0; packedUCVBO[ucIdx++] = verticesArray[j] >= 2 ? 255 : 0; packedUCVBO[ucIdx++] = 255;
colorIdx = i * colorComponents; if (colorData) { packedUCVBO[ucIdx++] = colorData[colorIdx]; packedUCVBO[ucIdx++] = colorData[colorIdx + 1]; packedUCVBO[ucIdx++] = colorData[colorIdx + 2]; packedUCVBO[ucIdx++] = colorData[colorIdx + 3]; } } } vbo.setElementCount(vboIdx / pointSize); vbo.upload(packedVBO, ObjectType.ARRAY_BUFFER); vbo.getColorBO().upload(packedUCVBO, ObjectType.ARRAY_BUFFER); model.VBOBuildTime.modified(); }; }
const DEFAULT_VALUES = {};
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
vtkOpenGLPolyDataMapper.extend(publicAPI, model, initialValues);
vtkOpenGLStickMapper(publicAPI, model); }
export const newInstance = macro.newInstance(extend, 'vtkOpenGLStickMapper');
export default { newInstance, extend };
registerOverride('vtkStickMapper', newInstance);
|