Browse Source

wip, mc group id

Alexander Rose 6 years ago
parent
commit
0d69130cf5

+ 8 - 11
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,

+ 8 - 8
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 }
 }

+ 2 - 2
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'),

+ 2 - 2
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

+ 1 - 1
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

+ 5 - 2
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(

+ 1 - 2
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

+ 13 - 13
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)