-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature-isosuface-heatmap' into main
- Loading branch information
Showing
15 changed files
with
755 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
packages/three-coverage-heatmap/src/IsoSurface/UniformSampler3D.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import * as THREE from "three"; | ||
import HeatmapTextureMaterial from "../Material/HeatmapTextureMaterial"; | ||
|
||
export default class UniformSampler3D { | ||
constructor(samplesY, samplesXZ, scale) { | ||
this._samplesXZ = samplesXZ; | ||
this._texturesPerAxis = Math.ceil(Math.sqrt(samplesY)); | ||
const offset = [-scale[0] / 2, 0, -scale[2] / 2]; | ||
this._renderMesh = this._createRenderMesh(samplesY, scale, offset); | ||
this._points = this._createVisulizePoints( | ||
samplesY, | ||
samplesXZ, | ||
scale, | ||
offset | ||
); | ||
|
||
const resolution = this._texturesPerAxis * samplesXZ; | ||
this._renderTarget = new THREE.WebGLRenderTarget(resolution, resolution); | ||
|
||
this.object = new THREE.Group(); | ||
this.object.add(this._points); | ||
} | ||
|
||
_createVisulizePoints(samplesY, samplesXZ, scale, offset) { | ||
const geometry = new THREE.BufferGeometry(); | ||
|
||
const vertices = ((size, scale, offset) => | ||
new Array(size[1]) | ||
.fill(0) | ||
.flatMap((_, y) => | ||
new Array(size[2] * size[0]) | ||
.fill(0) | ||
.map((_, index) => [ | ||
index % size[0], | ||
y, | ||
Math.floor(index / size[0]), | ||
]) | ||
) | ||
.map((data) => data.map((value, index) => value / size[index])) | ||
.map((data) => | ||
data.map((v, index) => v * scale[index] + offset[index]) | ||
))([samplesXZ, samplesY, samplesXZ], scale, offset); | ||
|
||
geometry.setAttribute( | ||
"position", | ||
new THREE.BufferAttribute(new Float32Array(vertices.flat()), 3) | ||
); | ||
const material = new THREE.PointsMaterial({ vertexColors: true }); | ||
material.size = 0.1; | ||
const points = new THREE.Points(geometry, material); | ||
|
||
return points; | ||
} | ||
|
||
_createRenderMesh(samplesY, scale, offset) { | ||
const coords = new Array(samplesY).fill(0).map((_, index) => { | ||
const y = index / samplesY; | ||
return [ | ||
[1, y, 1], | ||
[0, y, 1], | ||
[1, y, 0], | ||
[1, y, 0], | ||
[0, y, 1], | ||
[0, y, 0], | ||
[1, y, 1], | ||
[1, y, 0], | ||
[0, y, 1], | ||
[1, y, 0], | ||
[0, y, 0], | ||
[0, y, 1], | ||
]; | ||
}); | ||
|
||
const vertices = coords | ||
.flat() | ||
.flatMap((data) => | ||
data.map((v, index) => v * scale[index] + offset[index]) | ||
); | ||
|
||
const texturesPerRow = Math.ceil(Math.sqrt(samplesY)); | ||
const layerSize = 1 / texturesPerRow; | ||
const uvs = coords.flatMap((data, index) => { | ||
const xOffset = index % texturesPerRow; | ||
const yOffset = Math.floor(index / texturesPerRow); | ||
const uv = data.flatMap(([x, _, z]) => [ | ||
x / texturesPerRow + xOffset * layerSize, | ||
z / texturesPerRow + yOffset * layerSize, | ||
]); | ||
return uv; | ||
}); | ||
|
||
const geometry = new THREE.BufferGeometry(); | ||
const material = new HeatmapTextureMaterial(); | ||
geometry.setAttribute( | ||
"position", | ||
new THREE.BufferAttribute(new Float32Array(vertices), 3) | ||
); | ||
geometry.setAttribute( | ||
"uv", | ||
new THREE.BufferAttribute(new Float32Array(uvs), 2) | ||
); | ||
|
||
const mesh = new THREE.Mesh(geometry, material); | ||
const scene = new THREE.Scene(); | ||
scene.add(mesh); | ||
|
||
return { object: scene, material }; | ||
} | ||
|
||
sample(renderer) { | ||
renderer.setRenderTarget(this._renderTarget); | ||
renderer.render(this._renderMesh.object, new THREE.Camera()); | ||
renderer.setRenderTarget(null); | ||
|
||
const colors = new Array(this._texturesPerAxis ** 2) | ||
.fill(0) | ||
.reduce((prev, _, index) => { | ||
const pixels = new Uint8Array(4 * this._samplesXZ * this._samplesXZ); | ||
const xOffset = (index % this._texturesPerAxis) * this._samplesXZ; | ||
const yOffset = | ||
Math.floor(index / this._texturesPerAxis) * this._samplesXZ; | ||
renderer.readRenderTargetPixels( | ||
this._renderTarget, | ||
xOffset, | ||
yOffset, | ||
this._samplesXZ, | ||
this._samplesXZ, | ||
pixels | ||
); | ||
|
||
return [...prev, ...pixels]; | ||
}, []); | ||
|
||
this._points.geometry.setAttribute( | ||
"color", | ||
new THREE.BufferAttribute( | ||
new Float32Array(colors.map((color) => color / 255)), | ||
4 | ||
) | ||
); | ||
return colors; | ||
} | ||
|
||
setUniforms(data) { | ||
this._renderMesh.material.setUniforms(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as THREE from "three"; | ||
import { MarchingCubes } from "three/addons/objects/MarchingCubes.js"; | ||
|
||
class IsoSurface extends MarchingCubes { | ||
constructor(samplesY, samplesXZ, scale) { | ||
const material = new THREE.MeshBasicMaterial({ | ||
color: "red", | ||
side: THREE.DoubleSide, | ||
opacity: 0.5, | ||
transparent: true, | ||
// depthTest: false, | ||
}); | ||
super(samplesXZ, material, true, true, 100000); | ||
this._samplesY = samplesY; | ||
this._samplesXZ = samplesXZ; | ||
this._material = material; | ||
|
||
this.position.set(0, scale[1] / 2, 0); | ||
this.scale.set(scale[0] / 2, scale[1] / 2, scale[2] / 2); | ||
} | ||
|
||
setIsoValue(value) { | ||
this.isolation = value * 255; | ||
|
||
const color = new THREE.Color(); | ||
5; | ||
color.setHSL(value, 1, 0.5); | ||
this.material.color = color; | ||
this.update(); | ||
} | ||
|
||
updateFromColors(colors) { | ||
this.reset(); | ||
|
||
new Array(this._samplesXZ) | ||
.fill(0) | ||
.flatMap((_, y) => | ||
new Array(this._samplesXZ ** 2) | ||
.fill(0) | ||
.map((_, xz) => [ | ||
xz % this._samplesXZ, | ||
y, | ||
Math.floor(xz / this._samplesXZ), | ||
]) | ||
) | ||
.forEach(([x, y, z]) => { | ||
const index = | ||
Math.floor((y / this._samplesXZ) * this._samplesY) * | ||
this._samplesXZ ** 2 + | ||
z * this._samplesXZ + | ||
x; | ||
this.setCell(x, y, z, colors[index * 4]); | ||
}); | ||
|
||
this.update(); | ||
} | ||
} | ||
|
||
export default IsoSurface; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.