浏览代码

add torus primitive and fix render tests

Alexander Rose 4 年之前
父节点
当前提交
93df548cfe

+ 1 - 0
CHANGELOG.md

@@ -12,6 +12,7 @@ Note that since we don't clearly distinguish between a public and private interf
 - Fix SSAO bugs when used with ``Canvas3D`` viewport.
 - Support for  full pausing (no draw) rendering: ``Canvas3D.pause(true)``.
 - Add `MeshBuilder.addMesh`.
+- Add `Torus` primitive.
 
 ## [v2.0.4] - 2021-04-20
 

+ 1 - 1
src/mol-geo/geometry/geometry.ts

@@ -55,7 +55,7 @@ export interface GeometryUtils<G extends Geometry, P extends PD.Params = Geometr
     createValuesSimple(geometry: G, props: Partial<PD.Values<P>>, colorValue: Color, sizeValue: number, transform?: TransformData): V
     updateValues(values: V, props: PD.Values<P>): void
     updateBoundingSphere(values: V, geometry: G): void
-    createRenderableState(props: Partial<PD.Values<P>>): RenderableState
+    createRenderableState(props: PD.Values<P>): RenderableState
     updateRenderableState(state: RenderableState, props: PD.Values<P>): void
     createPositionIterator(geometry: G, transform: TransformData): LocationIterator
 }

+ 78 - 0
src/mol-geo/primitive/torus.ts

@@ -0,0 +1,78 @@
+/**
+ * Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
+ */
+
+// adapted from three.js, MIT License Copyright 2010-2021 three.js authors
+
+import { Vec3 } from '../../mol-math/linear-algebra';
+import { Primitive } from './primitive';
+
+export const DefaultTorusProps = {
+    radius: 1,
+    tube: 0.4,
+    radialSegments: 8,
+    tubularSegments: 6,
+    arc: Math.PI * 2,
+};
+export type TorusProps = Partial<typeof DefaultTorusProps>
+
+export function Torus(props?: TorusProps): Primitive {
+    const { radius, tube, radialSegments, tubularSegments, arc } = { ...DefaultTorusProps, ...props };
+
+    // buffers
+    const indices: number[] = [];
+    const vertices: number[] = [];
+    const normals: number[] = [];
+
+    // helper variables
+    const center = Vec3();
+    const vertex = Vec3();
+    const normal = Vec3();
+
+    // generate vertices and normals
+    for (let j = 0; j <= radialSegments; ++j) {
+        for (let i = 0; i <= tubularSegments; ++i) {
+            const u = i / tubularSegments * arc;
+            const v = j / radialSegments * Math.PI * 2;
+
+            // vertex
+            Vec3.set(
+                vertex,
+                (radius + tube * Math.cos(v)) * Math.cos(u),
+                (radius + tube * Math.cos(v)) * Math.sin(u),
+                tube * Math.sin(v)
+            );
+            vertices.push(...vertex);
+
+            // normal
+            Vec3.set(center, radius * Math.cos(u), radius * Math.sin(u), 0 );
+            Vec3.sub(normal, vertex, center);
+            Vec3.normalize(normal, normal);
+            normals.push(...normal);
+        }
+    }
+
+    // generate indices
+    for (let j = 1; j <= radialSegments; ++j) {
+        for (let i = 1; i <= tubularSegments; ++i) {
+
+            // indices
+            const a = (tubularSegments + 1) * j + i - 1;
+            const b = (tubularSegments + 1) * (j - 1) + i - 1;
+            const c = (tubularSegments + 1) * (j - 1) + i;
+            const d = (tubularSegments + 1) * j + i;
+
+            // faces
+            indices.push(a, b, d);
+            indices.push(b, c, d);
+        }
+    }
+
+    return {
+        vertices: new Float32Array(vertices),
+        normals: new Float32Array(normals),
+        indices: new Uint32Array(indices)
+    };
+}

+ 12 - 2
src/tests/browser/marching-cubes.ts

@@ -105,7 +105,12 @@ async function init() {
 
     const mcBoundingSphere = Sphere3D.fromBox3D(Sphere3D(), densityTextureData.bbox);
     const mcIsosurface = TextureMesh.create(gv.vertexCount, 1, gv.vertexTexture, gv.groupTexture, gv.normalTexture, mcBoundingSphere);
-    const mcIsoSurfaceProps = { doubleSided: true, flatShaded: true, alpha: 1.0 };
+    const mcIsoSurfaceProps = {
+        ...PD.getDefaultValues(TextureMesh.Params),
+        doubleSided: true,
+        flatShaded: true,
+        alpha: 1.0
+    };
     const mcIsoSurfaceValues = TextureMesh.Utils.createValuesSimple(mcIsosurface, mcIsoSurfaceProps, Color(0x112299), 1);
     // console.log('mcIsoSurfaceValues', mcIsoSurfaceValues)
     const mcIsoSurfaceState = TextureMesh.Utils.createRenderableState(mcIsoSurfaceProps);
@@ -133,7 +138,12 @@ async function init() {
     console.timeEnd('cpu mc');
     console.log('surface', surface);
     Mesh.transform(surface, densityData.transform);
-    const meshProps = { doubleSided: true, flatShaded: false, alpha: 1.0 };
+    const meshProps = {
+        ...PD.getDefaultValues(Mesh.Params),
+        doubleSided: true,
+        flatShaded: false,
+        alpha: 1.0
+    };
     const meshValues = Mesh.Utils.createValuesSimple(surface, meshProps, Color(0x995511), 1);
     const meshState = Mesh.Utils.createRenderableState(meshProps);
     const meshRenderObject = createRenderObject('mesh', meshValues, meshState, -1);

+ 4 - 2
src/tests/browser/render-lines.ts

@@ -14,6 +14,7 @@ import { Lines } from '../../mol-geo/geometry/lines/lines';
 import { Color } from '../../mol-util/color';
 import { createRenderObject } from '../../mol-gl/render-object';
 import { Representation } from '../../mol-repr/representation';
+import { ParamDefinition } from '../../mol-util/param-definition';
 
 const parent = document.getElementById('app')!;
 parent.style.width = '100%';
@@ -33,8 +34,9 @@ function linesRepr() {
     linesBuilder.addCage(t, dodecahedronCage, 0);
     const lines = linesBuilder.getLines();
 
-    const values = Lines.Utils.createValuesSimple(lines, {}, Color(0xFF0000), 3);
-    const state = Lines.Utils.createRenderableState({});
+    const props = ParamDefinition.getDefaultValues(Lines.Utils.Params);
+    const values = Lines.Utils.createValuesSimple(lines, props, Color(0xFF0000), 3);
+    const state = Lines.Utils.createRenderableState(props);
     const renderObject = createRenderObject('lines', values, state, -1);
     const repr = Representation.fromRenderObject('cage-lines', renderObject);
     return repr;

+ 14 - 5
src/tests/browser/render-mesh.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2019-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
@@ -15,6 +15,8 @@ import { Mesh } from '../../mol-geo/geometry/mesh/mesh';
 import { Color } from '../../mol-util/color';
 import { createRenderObject } from '../../mol-gl/render-object';
 import { Representation } from '../../mol-repr/representation';
+import { Torus } from '../../mol-geo/primitive/torus';
+import { ParamDefinition } from '../../mol-util/param-definition';
 
 const parent = document.getElementById('app')!;
 parent.style.width = '100%';
@@ -31,17 +33,24 @@ function meshRepr() {
     const builderState = MeshBuilder.createState();
 
     const t = Mat4.identity();
-    MeshBuilder.addCage(builderState, t, HexagonalPrismCage(), 0.005, 2, 20);
+    Mat4.scaleUniformly(t, t, 10);
+    MeshBuilder.addCage(builderState, t, HexagonalPrismCage(), 0.05, 2, 20);
 
     const t2 = Mat4.identity();
-    Mat4.scaleUniformly(t2, t2, 0.1);
+    Mat4.scaleUniformly(t2, t2, 1);
     MeshBuilder.addPrimitive(builderState, t2, SpikedBall(3));
 
+    const t3 = Mat4.identity();
+    Mat4.scaleUniformly(t3, t3, 8);
+    MeshBuilder.addPrimitive(builderState, t3, Torus({ tubularSegments: 64, radialSegments: 32, tube: 0.1 }));
+
     const mesh = MeshBuilder.getMesh(builderState);
 
-    const values = Mesh.Utils.createValuesSimple(mesh, {}, Color(0xFF0000), 1);
-    const state = Mesh.Utils.createRenderableState({});
+    const props = ParamDefinition.getDefaultValues(Mesh.Utils.Params);
+    const values = Mesh.Utils.createValuesSimple(mesh, props, Color(0xFF4433), 1);
+    const state = Mesh.Utils.createRenderableState(props);
     const renderObject = createRenderObject('mesh', values, state, -1);
+    console.log('mesh', renderObject);
     const repr = Representation.fromRenderObject('mesh', renderObject);
     return repr;
 }

+ 1 - 0
src/tests/browser/render-shape.ts

@@ -111,6 +111,7 @@ const repr = ShapeRepresentation(getShape, Mesh.Utils);
 export async function init() {
     // Create shape from myData and add to canvas3d
     await repr.createOrUpdate({}, myData).run((p: Progress) => console.log(Progress.format(p)));
+    console.log('shape', repr);
     canvas3d.add(repr);
     canvas3d.requestCameraReset();
 

+ 3 - 1
src/tests/browser/render-spheres.ts

@@ -12,6 +12,7 @@ import { Spheres } from '../../mol-geo/geometry/spheres/spheres';
 import { Color } from '../../mol-util/color';
 import { createRenderObject } from '../../mol-gl/render-object';
 import { Representation } from '../../mol-repr/representation';
+import { ParamDefinition } from '../../mol-util/param-definition';
 
 const parent = document.getElementById('app')!;
 parent.style.width = '100%';
@@ -31,8 +32,9 @@ function spheresRepr() {
     spheresBuilder.add(-4, 1, 0, 0);
     const spheres = spheresBuilder.getSpheres();
 
+    const props = ParamDefinition.getDefaultValues(Spheres.Utils.Params);
     const values = Spheres.Utils.createValuesSimple(spheres, {}, Color(0xFF0000), 1);
-    const state = Spheres.Utils.createRenderableState({});
+    const state = Spheres.Utils.createRenderableState(props);
     const renderObject = createRenderObject('spheres', values, state, -1);
     console.log(renderObject);
     const repr = Representation.fromRenderObject('spheres', renderObject);

+ 4 - 3
src/tests/browser/render-text.ts

@@ -8,7 +8,7 @@ import './index.html';
 import { Canvas3D, Canvas3DContext } from '../../mol-canvas3d/canvas3d';
 import { TextBuilder } from '../../mol-geo/geometry/text/text-builder';
 import { Text } from '../../mol-geo/geometry/text/text';
-import { ParamDefinition as PD } from '../../mol-util/param-definition';
+import { ParamDefinition, ParamDefinition as PD } from '../../mol-util/param-definition';
 import { Color } from '../../mol-util/color';
 import { Representation } from '../../mol-repr/representation';
 import { SpheresBuilder } from '../../mol-geo/geometry/spheres/spheres-builder';
@@ -64,8 +64,9 @@ function spheresRepr() {
     spheresBuilder.add(-4, 1, 0, 0);
     const spheres = spheresBuilder.getSpheres();
 
-    const values = Spheres.Utils.createValuesSimple(spheres, {}, Color(0xFF0000), 0.2);
-    const state = Spheres.Utils.createRenderableState({});
+    const props = ParamDefinition.getDefaultValues(Spheres.Utils.Params);
+    const values = Spheres.Utils.createValuesSimple(spheres, props, Color(0xFF0000), 0.2);
+    const state = Spheres.Utils.createRenderableState(props);
     const renderObject = createRenderObject('spheres', values, state, -1);
     console.log('spheres', renderObject);
     const repr = Representation.fromRenderObject('spheres', renderObject);