Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 67 additions & 6 deletions src/volume_rendering/volume_render_layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
import { ChunkState } from "#src/chunk_manager/base.js";
import { ChunkRenderLayerFrontend } from "#src/chunk_manager/frontend.js";
import type { CoordinateSpace } from "#src/coordinate_transform.js";
import type { VisibleLayerInfo } from "#src/layer/index.js";
import type { PickState, VisibleLayerInfo } from "#src/layer/index.js";
import type { PerspectivePanel } from "#src/perspective_view/panel.js";
import type {
PerspectiveViewReadyRenderContext,
PerspectiveViewRenderContext,
} from "#src/perspective_view/render_layer.js";
import { PerspectiveViewRenderLayer } from "#src/perspective_view/render_layer.js";
import type { RenderLayerTransformOrError } from "#src/render_coordinate_transform.js";
import {
getChunkPositionFromCombinedGlobalLocalPositions,
type RenderLayerTransformOrError,
} from "#src/render_coordinate_transform.js";
import type { RenderScaleHistogram } from "#src/render_scale_statistics.js";
import {
numRenderScaleHistogramBins,
Expand Down Expand Up @@ -222,6 +225,9 @@ export class VolumeRenderingRenderLayer extends PerspectiveViewRenderLayer {
private modeOverride: TrackableVolumeRenderingModeValue;
private vertexIdHelper: VertexIdHelper;
private dataHistogramSpecifications: HistogramSpecifications;
private inverseModelViewProjection: mat4;
private tempChunkPosition: Float32Array;
private currentTransformedSources: TransformedVolumeSource[] | null = null;

private shaderGetter: ParameterizedContextDependentShaderGetter<
{ emitter: ShaderModule; chunkFormat: ChunkFormat; wireFrame: boolean },
Expand Down Expand Up @@ -257,7 +263,9 @@ export class VolumeRenderingRenderLayer extends PerspectiveViewRenderLayer {
super();
this.gain = options.gain;
this.multiscaleSource = options.multiscaleSource;
this.tempChunkPosition = new Float32Array(options.multiscaleSource.rank);
this.transform = options.transform;
this.inverseModelViewProjection = mat4.create();
this.channelCoordinateSpace = options.channelCoordinateSpace;
this.shaderControlState = options.shaderControlState;
this.localPosition = options.localPosition;
Expand Down Expand Up @@ -367,7 +375,7 @@ void emitRGBA(vec4 rgba) {
glsl_handleMaxProjectionUpdate = `
float newIntensity = getIntensity();
bool intensityChanged = newIntensity > savedIntensity;
savedIntensity = intensityChanged ? newIntensity : savedIntensity;
savedIntensity = intensityChanged ? newIntensity : savedIntensity;
savedDepth = intensityChanged ? depthAtRayPosition : savedDepth;
outputColor = intensityChanged ? newColor : outputColor;
emit(outputColor, savedDepth, savedIntensity, uPickId);
Expand Down Expand Up @@ -657,6 +665,13 @@ outputValue = vec4(1.0, 1.0, 1.0, 1.0);
this.transform.changed.add(this.redrawNeeded.dispatch),
);
this.registerDisposer(this.mode.changed.add(this.redrawNeeded.dispatch));
this.registerDisposer(
this.mode.changed.add(() => {
if (this.mode.value === VolumeRenderingModes.OFF) {
this.currentTransformedSources = null;
}
}),
);
this.registerDisposer(
this.shaderControlState.fragmentMain.changed.add(
this.redrawNeeded.dispatch,
Expand Down Expand Up @@ -738,9 +753,10 @@ outputValue = vec4(1.0, 1.0, 1.0, 1.0);
VolumeRenderingAttachmentState
>,
) {
if (!renderContext.emitColor) return;
const allSources = attachment.state!.sources.value;
if (allSources.length === 0) return;
this.currentTransformedSources =
allSources.length === 0 ? null : allSources[0];
if (!renderContext.emitColor || allSources.length === 0) return;
let curPhysicalSpacing = 0;
let curOptimalSamples = 0;
let curHistogramInformation: HistogramInformation = {
Expand Down Expand Up @@ -928,9 +944,9 @@ outputValue = vec4(1.0, 1.0, 1.0, 1.0);
projectionParameters.viewProjectionMat,
chunkLayout.transform,
);
const { inverseModelViewProjection } = this;
const clippingPlanes = tempVisibleVolumetricClippingPlanes;
getFrustrumPlanes(clippingPlanes, modelViewProjection);
const inverseModelViewProjection = mat4.create();
mat4.invert(inverseModelViewProjection, modelViewProjection);
const { near, far, adjustedNear, adjustedFar } =
getVolumeRenderingNearFarBounds(
Expand Down Expand Up @@ -1263,6 +1279,51 @@ outputValue = vec4(1.0, 1.0, 1.0, 1.0);
restoreDrawingBuffersAndState();
}
}
// The pick ID encodes no spatial data; return null so UserLayer.getValueAt
// falls through to getValueAt() which looks up the voxel value from the depth-derived position if in max or min mode.
transformPickedValue(_pickState: PickState) {
return null;
}

getValueAt(globalPosition: Float32Array) {
const { currentTransformedSources, tempChunkPosition } = this;
if (currentTransformedSources === null) return null;
const rank = tempChunkPosition.length;
for (const {
source,
chunkTransform,
lowerClipBound,
upperClipBound,
} of currentTransformedSources) {
if (
!getChunkPositionFromCombinedGlobalLocalPositions(
tempChunkPosition,
globalPosition,
this.localPosition.value,
chunkTransform.layerRank,
chunkTransform.combinedGlobalLocalToChunkTransform,
)
) {
continue;
}
let outOfBounds = false;
for (let i = 0; i < rank; ++i) {
if (
tempChunkPosition[i] < lowerClipBound[i] ||
tempChunkPosition[i] >= upperClipBound[i]
) {
outOfBounds = true;
break;
}
}
if (outOfBounds) continue;
const result = source.getValueAt(tempChunkPosition, chunkTransform);
if (result != null) {
return result;
}
}
return null;
}

private bindDepthBufferTexture(
renderContext: PerspectiveViewRenderContext,
Expand Down
Loading