Actor

Introduction

This class is not intended for general use. Please use the
similarly named class under Rendering/Core. This class is
a WebGL implementation of that generic renderable class in
Rendering Core. This class will automatically get instantiated
and rendered as needed by the OpenGLRenderWindow.

Source

index.js
import { mat3, mat4 } from 'gl-matrix';

import * as macro from 'vtk.js/Sources/macros';
import vtkViewNode from 'vtk.js/Sources/Rendering/SceneGraph/ViewNode';

import { registerOverride } from 'vtk.js/Sources/Rendering/OpenGL/ViewNodeFactory';

// ----------------------------------------------------------------------------
// vtkOpenGLActor methods
// ----------------------------------------------------------------------------

function vtkOpenGLActor(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkOpenGLActor');

// Builds myself.
publicAPI.buildPass = (prepass) => {
if (prepass) {
model._openGLRenderWindow = publicAPI.getLastAncestorOfType(
'vtkOpenGLRenderWindow'
);
model._openGLRenderer =
publicAPI.getFirstAncestorOfType('vtkOpenGLRenderer');
model.context = model._openGLRenderWindow.getContext();
publicAPI.prepareNodes();
publicAPI.addMissingNodes(model.renderable.getTextures());
publicAPI.addMissingNode(model.renderable.getMapper());
publicAPI.removeUnusedNodes();

// we store textures and mapper
model.ogltextures = null;
model.activeTextures = null;
for (let index = 0; index < model.children.length; index++) {
const child = model.children[index];
if (child.isA('vtkOpenGLTexture')) {
if (!model.ogltextures) {
model.ogltextures = [];
}
model.ogltextures.push(child);
} else {
model.oglmapper = child;
}
}
}
};

// render both opaque and translucent actors
publicAPI.traverseZBufferPass = (renderPass) => {
if (
!model.renderable ||
!model.renderable.getNestedVisibility() ||
(model._openGLRenderer.getSelector() &&
!model.renderable.getNestedPickable())
) {
return;
}

publicAPI.apply(renderPass, true);
model.oglmapper.traverse(renderPass);

publicAPI.apply(renderPass, false);
};

// only render opaque actors
publicAPI.traverseOpaqueZBufferPass = (renderPass) =>
publicAPI.traverseOpaquePass(renderPass);

// we draw textures, then mapper, then post pass textures
publicAPI.traverseOpaquePass = (renderPass) => {
if (
!model.renderable ||
!model.renderable.getNestedVisibility() ||
!model.renderable.getIsOpaque() ||
(model._openGLRenderer.getSelector() &&
!model.renderable.getNestedPickable())
) {
return;
}

publicAPI.apply(renderPass, true);

model.oglmapper.traverse(renderPass);

publicAPI.apply(renderPass, false);
};

// we draw textures, then mapper, then post pass textures
publicAPI.traverseTranslucentPass = (renderPass) => {
if (
!model.renderable ||
!model.renderable.getNestedVisibility() ||
model.renderable.getIsOpaque() ||
(model._openGLRenderer.getSelector() &&
!model.renderable.getNestedPickable())
) {
return;
}

publicAPI.apply(renderPass, true);

model.oglmapper.traverse(renderPass);

publicAPI.apply(renderPass, false);
};

publicAPI.activateTextures = () => {
// always traverse textures first, then mapper
if (!model.ogltextures) {
return;
}

model.activeTextures = [];
for (let index = 0; index < model.ogltextures.length; index++) {
const child = model.ogltextures[index];
child.render();
if (child.getHandle()) {
model.activeTextures.push(child);
}
}
};

publicAPI.queryPass = (prepass, renderPass) => {
if (prepass) {
if (!model.renderable || !model.renderable.getVisibility()) {
return;
}
if (model.renderable.getIsOpaque()) {
renderPass.incrementOpaqueActorCount();
} else {
renderPass.incrementTranslucentActorCount();
}
}
};

publicAPI.zBufferPass = (prepass, renderPass) =>
publicAPI.opaquePass(prepass, renderPass);

publicAPI.opaqueZBufferPass = (prepass, renderPass) =>
publicAPI.opaquePass(prepass, renderPass);

publicAPI.opaquePass = (prepass, renderPass) => {
if (prepass) {
model.context.depthMask(true);
publicAPI.activateTextures();
} else if (model.activeTextures) {
for (let index = 0; index < model.activeTextures.length; index++) {
model.activeTextures[index].deactivate();
}
}
};

// Renders myself
publicAPI.translucentPass = (prepass, renderPass) => {
if (prepass) {
model.context.depthMask(
model._openGLRenderer.getSelector() &&
model.renderable.getNestedPickable()
);
publicAPI.activateTextures();
} else if (model.activeTextures) {
for (let index = 0; index < model.activeTextures.length; index++) {
model.activeTextures[index].deactivate();
}
}
};

publicAPI.getKeyMatrices = () => {
// has the actor changed?
if (model.renderable.getMTime() > model.keyMatrixTime.getMTime()) {
model.renderable.computeMatrix();
mat4.copy(model.keyMatrices.mcwc, model.renderable.getMatrix());
mat4.transpose(model.keyMatrices.mcwc, model.keyMatrices.mcwc);

if (model.renderable.getIsIdentity()) {
mat3.identity(model.keyMatrices.normalMatrix);
} else {
mat3.fromMat4(model.keyMatrices.normalMatrix, model.keyMatrices.mcwc);
mat3.invert(
model.keyMatrices.normalMatrix,
model.keyMatrices.normalMatrix
);
mat3.transpose(
model.keyMatrices.normalMatrix,
model.keyMatrices.normalMatrix
);
}
model.keyMatrixTime.modified();
}

return model.keyMatrices;
};
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
context: null,
keyMatrixTime: null,
keyMatrices: null,
activeTextures: null,
};

// ----------------------------------------------------------------------------

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

// Inheritance
vtkViewNode.extend(publicAPI, model, initialValues);

model.keyMatrixTime = {};
macro.obj(model.keyMatrixTime, { mtime: 0 });
model.keyMatrices = {
normalMatrix: mat3.identity(new Float64Array(9)),
mcwc: mat4.identity(new Float64Array(16)),
};

// Build VTK API
macro.setGet(publicAPI, model, ['context']);

macro.get(publicAPI, model, ['activeTextures']);

// Object methods
vtkOpenGLActor(publicAPI, model);
}

// ----------------------------------------------------------------------------

export const newInstance = macro.newInstance(extend);

// ----------------------------------------------------------------------------

export default { newInstance, extend };

// Register ourself to OpenGL backend if imported
registerOverride('vtkActor', newInstance);