diff --git a/src/mol-geo/geometry/isosurface/isosurface.ts b/src/mol-geo/geometry/isosurface/isosurface.ts index 14a415f3d0931809ecce03b475146b40ed9a33f1..c55b9388dac18d67012f9bab0c7aaa837012a3f8 100644 --- a/src/mol-geo/geometry/isosurface/isosurface.ts +++ b/src/mol-geo/geometry/isosurface/isosurface.ts @@ -32,23 +32,22 @@ export interface Isosurface { readonly groupCount: ValueCell<number>, readonly geoTextureDim: ValueCell<Vec2>, - readonly vertexTexture: ValueCell<Texture>, + /** texture has vertex positions in XYZ and group id in W */ + readonly vertexGroupTexture: ValueCell<Texture>, readonly normalTexture: ValueCell<Texture>, - readonly groupTexture: ValueCell<Texture>, readonly boundingSphere: ValueCell<Sphere3D>, } export namespace Isosurface { - export function create(vertexCount: number, groupCount: number, vertexTexture: Texture, normalTexture: Texture, groupTexture: Texture, boundingSphere: Sphere3D, isosurface?: Isosurface): Isosurface { - const { width, height } = vertexTexture + export function create(vertexCount: number, groupCount: number, vertexGroupTexture: Texture, normalTexture: Texture, boundingSphere: Sphere3D, isosurface?: Isosurface): Isosurface { + const { width, height } = vertexGroupTexture if (isosurface) { ValueCell.update(isosurface.vertexCount, vertexCount) ValueCell.update(isosurface.groupCount, groupCount) ValueCell.update(isosurface.geoTextureDim, Vec2.set(isosurface.geoTextureDim.ref.value, width, height)) - ValueCell.update(isosurface.vertexTexture, vertexTexture) + ValueCell.update(isosurface.vertexGroupTexture, vertexGroupTexture) ValueCell.update(isosurface.normalTexture, normalTexture) - ValueCell.update(isosurface.groupTexture, groupTexture) ValueCell.update(isosurface.boundingSphere, boundingSphere) return isosurface } else { @@ -57,9 +56,8 @@ export namespace Isosurface { vertexCount: ValueCell.create(vertexCount), groupCount: ValueCell.create(groupCount), geoTextureDim: ValueCell.create(Vec2.create(width, height)), - vertexTexture: ValueCell.create(vertexTexture), + vertexGroupTexture: ValueCell.create(vertexGroupTexture), normalTexture: ValueCell.create(normalTexture), - groupTexture: ValueCell.create(groupTexture), boundingSphere: ValueCell.create(boundingSphere), } } @@ -101,11 +99,10 @@ export namespace Isosurface { return { uGeoTexDim: isosurface.geoTextureDim, - tPosition: isosurface.vertexTexture, + tPositionGroup: isosurface.vertexGroupTexture, tNormal: isosurface.normalTexture, - tGroup: isosurface.groupTexture, - // aGroup is used as a triangle index here and the group id is retirieved from the tGroup texture + // aGroup is used as a vertex index here and the group id is retirieved from tPositionGroup aGroup: ValueCell.create(fillSerial(new Float32Array(isosurface.vertexCount.ref.value))), boundingSphere: ValueCell.create(transformBoundingSphere), invariantBoundingSphere: isosurface.boundingSphere, diff --git a/src/mol-gl/compute/marching-cubes/isosurface.ts b/src/mol-gl/compute/marching-cubes/isosurface.ts index fc6a4bc271b01596d3674409096fbd928d728337..dcc4f3d3ee50d3688971e3b353e1cf04336ca939 100644 --- a/src/mol-gl/compute/marching-cubes/isosurface.ts +++ b/src/mol-gl/compute/marching-cubes/isosurface.ts @@ -13,7 +13,7 @@ import { ShaderCode } from 'mol-gl/shader-code'; import { ValueCell } from 'mol-util'; import { GLRenderingContext } from 'mol-gl/webgl/compat'; import { Vec3, Vec2, Mat4 } from 'mol-math/linear-algebra'; -import { QuadSchema, QuadValues } from '../util'; +import { QuadSchema, QuadValues, readTexture } from '../util'; import { HistogramPyramid } from '../histogram-pyramid/reduction'; import { getTriIndices } from './tables'; @@ -87,15 +87,12 @@ export function createIsosurfaceBuffers(ctx: WebGLContext, activeVoxelsBase: Tex const framebuffer = framebufferCache.get(FramebufferName).value framebuffer.bind() - const vertexTexture = createTexture(ctx, 'image-float32', 'rgba', 'float', 'nearest') - vertexTexture.define(pyramidTex.width, pyramidTex.height) + const vertexGroupTexture = createTexture(ctx, 'image-float32', 'rgba', 'float', 'nearest') + vertexGroupTexture.define(pyramidTex.width, pyramidTex.height) const normalTexture = createTexture(ctx, 'image-float32', 'rgba', 'float', 'nearest') normalTexture.define(pyramidTex.width, pyramidTex.height) - const groupTexture = createTexture(ctx, 'image-float32', 'rgba', 'float', 'nearest') - groupTexture.define(pyramidTex.width, pyramidTex.height) - // const infoTex = createTexture(ctx, 'image-float32', 'rgba', 'float', 'nearest') // infoTex.define(pyramidTex.width, pyramidTex.height) @@ -115,7 +112,7 @@ export function createIsosurfaceBuffers(ctx: WebGLContext, activeVoxelsBase: Tex pr.update() pr.use() - vertexTexture.attachFramebuffer(framebuffer, 0) + vertexGroupTexture.attachFramebuffer(framebuffer, 0) normalTexture.attachFramebuffer(framebuffer, 1) // infoTex.attachFramebuffer(framebuffer, 1) // pointTexA.attachFramebuffer(framebuffer, 2) @@ -141,6 +138,9 @@ export function createIsosurfaceBuffers(ctx: WebGLContext, activeVoxelsBase: Tex pr.render() gl.disable(gl.SCISSOR_TEST) + const vgt = readTexture(ctx, vertexGroupTexture, pyramidTex.width, height) + console.log('vertexGroupTexture', vgt.array.subarray(0, 4 * count)) + // const vt = readTexture(ctx, verticesTex, pyramidTex.width, height) // console.log('vt', vt) // const vertices = new Float32Array(3 * compacted.count) @@ -178,5 +178,5 @@ export function createIsosurfaceBuffers(ctx: WebGLContext, activeVoxelsBase: Tex // console.log('valuesA', valuesA) // console.log('valuesB', valuesB) - return { vertexTexture, normalTexture, groupTexture, vertexCount: count } + return { vertexGroupTexture, normalTexture, vertexCount: count } } \ No newline at end of file diff --git a/src/mol-gl/renderable/isosurface.ts b/src/mol-gl/renderable/isosurface.ts index 05b083d86044fd5c7f164285ce7edf8336128c77..7ae65dd30e05782beaa2d49414411ad590233915 100644 --- a/src/mol-gl/renderable/isosurface.ts +++ b/src/mol-gl/renderable/isosurface.ts @@ -15,9 +15,9 @@ export const IsosurfaceSchema = { ...BaseSchema, uGeoTexDim: UniformSpec('v2'), - tPosition: TextureSpec('texture', 'rgba', 'float', 'nearest'), + /** texture has vertex positions in XYZ and group id in W */ + tPositionGroup: TextureSpec('texture', 'rgba', 'float', 'nearest'), tNormal: TextureSpec('texture', 'rgba', 'float', 'nearest'), - tGroup: TextureSpec('texture', 'rgba', 'float', 'nearest'), dFlatShaded: DefineSpec('boolean'), dDoubleSided: DefineSpec('boolean'), diff --git a/src/mol-gl/shader/chunks/assign-group.glsl b/src/mol-gl/shader/chunks/assign-group.glsl index 6da1b8503a563afe8c89e79cfb9cc4afa0b4a033..9a797bc402bcbf9ade19590f03382a57cf1d88c3 100644 --- a/src/mol-gl/shader/chunks/assign-group.glsl +++ b/src/mol-gl/shader/chunks/assign-group.glsl @@ -1,6 +1,6 @@ #ifdef dGeoTexture - // aGroup is used as a triangle index here and the group id is retirieved from the tGroup texture - float group = readFromTexture(tGroup, aGroup, uGeoTexDim).a; + // aGroup is used as a vertex index here and the group id is retirieved from tPositionGroup + float group = readFromTexture(tPositionGroup, aGroup, uGeoTexDim).w; #else float group = aGroup; #endif \ No newline at end of file diff --git a/src/mol-gl/shader/chunks/assign-position.glsl b/src/mol-gl/shader/chunks/assign-position.glsl index e696bd4f5e756389980ffbf5823a5901a59c0094..50440d786ecc1685bd37313a9e96f015c27873b0 100644 --- a/src/mol-gl/shader/chunks/assign-position.glsl +++ b/src/mol-gl/shader/chunks/assign-position.glsl @@ -1,6 +1,6 @@ mat4 modelView = uView * uModel * aTransform; #ifdef dGeoTexture - vec3 position = readFromTexture(tPosition, aGroup, uGeoTexDim).xyz; + vec3 position = readFromTexture(tPositionGroup, aGroup, uGeoTexDim).xyz; #else vec3 position = aPosition; #endif diff --git a/src/mol-gl/shader/marching-cubes/isosurface.frag b/src/mol-gl/shader/marching-cubes/isosurface.frag index 28dc849fda53d2cf9050cb6ca8cd00510699bfac..23939b15d1848b422d5530a1bed7eb9eb1477897 100644 --- a/src/mol-gl/shader/marching-cubes/isosurface.frag +++ b/src/mol-gl/shader/marching-cubes/isosurface.frag @@ -56,6 +56,8 @@ vec4 voxel(vec3 pos) { return texture3dFrom2dNearest(tVolumeData, pos, uGridDim, uGridTexDim.xy); } +#pragma glslify: decodeFloatRGB = require(../utils/decode-float-rgb.glsl) + void main(void) { // get 1D index float vI = dot(floor(uSize * vCoordinate), vec2(1.0, uSize)); @@ -176,8 +178,8 @@ void main(void) { // b0 = floor(b0 + 0.5); // b1 = floor(b1 + 0.5); - float d0 = voxel(b0 / uGridDim); - float d1 = voxel(b1 / uGridDim); + vec4 d0 = voxel(b0 / uGridDim); + vec4 d1 = voxel(b1 / uGridDim); float v0 = d0.a; float v1 = d1.a; @@ -185,6 +187,7 @@ void main(void) { float t = (uIsoValue - v0) / (v0 - v1); // t = -0.5; gl_FragColor.xyz = b0 + t * (b0 - b1); + gl_FragColor.w = decodeFloatRGB(d0.rgb); // group id // normals from gradients vec3 n0 = -normalize(vec3( diff --git a/src/mol-gl/shader/mesh.vert b/src/mol-gl/shader/mesh.vert index 5ae6e3d9c16b6edcd16241cdb1fdb6956d79d1d1..d49bba6012ca5deb366c5a648a4b8906bce4221b 100644 --- a/src/mol-gl/shader/mesh.vert +++ b/src/mol-gl/shader/mesh.vert @@ -12,8 +12,7 @@ precision highp int; #ifdef dGeoTexture uniform vec2 uGeoTexDim; - uniform sampler2D tPosition; - uniform sampler2D tGroup; + uniform sampler2D tPositionGroup; #else attribute vec3 aPosition; #endif diff --git a/src/tests/browser/marching-cubes.ts b/src/tests/browser/marching-cubes.ts index b819370eb0c108fa91bfd7b8ebd9bbb27ff254e4..a40c0ab8052765a470874b0cc55e67c8fd5b7925 100644 --- a/src/tests/browser/marching-cubes.ts +++ b/src/tests/browser/marching-cubes.ts @@ -39,20 +39,20 @@ canvas3d.animate() async function init() { const { webgl } = canvas3d - // const position: PositionData = { - // x: [0, 2], - // y: [0, 2], - // z: [0, 2], - // indices: OrderedSet.ofSortedArray([0, 1]), - // } - // const box = Box3D.create(Vec3.create(-1, -1, -1), Vec3.create(3, 3, 3)) const position: PositionData = { - x: [0], - y: [0], - z: [0], - indices: OrderedSet.ofSortedArray([0]), + x: [0, 2], + y: [0, 2], + z: [0, 2], + indices: OrderedSet.ofSortedArray([0, 1]), } - const box = Box3D.create(Vec3.create(-1, -1, -1), Vec3.create(1, 1, 1)) + const box = Box3D.create(Vec3.create(-1, -1, -1), Vec3.create(3, 3, 3)) + // const position: PositionData = { + // x: [0], + // y: [0], + // z: [0], + // indices: OrderedSet.ofSortedArray([0]), + // } + // const box = Box3D.create(Vec3.create(-1, -1, -1), Vec3.create(1, 1, 1)) const radius = () => 1.4 const props = { resolution: 0.1, @@ -110,7 +110,7 @@ async function init() { const mcBoundingSphere = Sphere3D.zero() Sphere3D.addVec3(mcBoundingSphere, mcBoundingSphere, densityTextureData.gridDimension) console.log('mcBoundingSphere', mcBoundingSphere, densityTextureData.gridDimension) - const mcIsosurface = Isosurface.create(gv.vertexCount, 1, gv.vertexTexture, gv.normalTexture, gv.groupTexture, mcBoundingSphere) + const mcIsosurface = Isosurface.create(gv.vertexCount, 1, gv.vertexGroupTexture, gv.normalTexture, mcBoundingSphere) const mcIsoSurfaceProps = { doubleSided: true, flatShaded: false, alpha: 1.0 } const mcIsoSurfaceValues = Isosurface.Utils.createValuesSimple(mcIsosurface, mcIsoSurfaceProps, Color(0x112299), 1) // console.log('mcIsoSurfaceValues', mcIsoSurfaceValues)