|
@@ -17,48 +17,23 @@ import { WebGLContext } from '../../mol-gl/webgl/context';
|
|
import { MeshBuilder } from '../../mol-geo/geometry/mesh/mesh-builder';
|
|
import { MeshBuilder } from '../../mol-geo/geometry/mesh/mesh-builder';
|
|
import { addSphere } from '../../mol-geo/geometry/mesh/builder/sphere';
|
|
import { addSphere } from '../../mol-geo/geometry/mesh/builder/sphere';
|
|
import { addCylinder } from '../../mol-geo/geometry/mesh/builder/cylinder';
|
|
import { addCylinder } from '../../mol-geo/geometry/mesh/builder/cylinder';
|
|
-import { Vec3, Mat3, Mat4 } from '../../mol-math/linear-algebra';
|
|
|
|
|
|
+import { sizeDataFactor } from '../../mol-geo/geometry/size-data';
|
|
|
|
+import { Vec3 } from '../../mol-math/linear-algebra';
|
|
import { RuntimeContext } from '../../mol-task';
|
|
import { RuntimeContext } from '../../mol-task';
|
|
-import { StringBuilder } from '../../mol-util';
|
|
|
|
-import { Color } from '../../mol-util/color/color';
|
|
|
|
import { decodeFloatRGB } from '../../mol-util/float-packing';
|
|
import { decodeFloatRGB } from '../../mol-util/float-packing';
|
|
|
|
+import { RenderObjectExporter, RenderObjectExportData } from './render-object-exporter';
|
|
|
|
|
|
// avoiding namespace lookup improved performance in Chrome (Aug 2020)
|
|
// avoiding namespace lookup improved performance in Chrome (Aug 2020)
|
|
const v3fromArray = Vec3.fromArray;
|
|
const v3fromArray = Vec3.fromArray;
|
|
-const v3transformMat4 = Vec3.transformMat4;
|
|
|
|
-const v3transformMat3 = Vec3.transformMat3;
|
|
|
|
-const mat3directionTransform = Mat3.directionTransform;
|
|
|
|
-
|
|
|
|
-type RenderObjectExportData = {
|
|
|
|
- [k: string]: string | Uint8Array | undefined
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-interface RenderObjectExporter<D extends RenderObjectExportData> {
|
|
|
|
- add(renderObject: GraphicsRenderObject, webgl: WebGLContext, ctx: RuntimeContext): Promise<void> | undefined
|
|
|
|
- getData(): D
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// http://paulbourke.net/dataformats/obj/
|
|
|
|
-// http://paulbourke.net/dataformats/mtl/
|
|
|
|
-
|
|
|
|
-export type ObjData = {
|
|
|
|
- obj: string
|
|
|
|
- mtl: string
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-export class ObjExporter implements RenderObjectExporter<ObjData> {
|
|
|
|
- private obj = StringBuilder.create();
|
|
|
|
- private mtl = StringBuilder.create();
|
|
|
|
- private vertexOffset = 0;
|
|
|
|
- private currentColor: Color | undefined;
|
|
|
|
- private currentAlpha: number | undefined;
|
|
|
|
- private materialSet = new Set<string>();
|
|
|
|
|
|
+
|
|
|
|
+export abstract class MeshExporter<D extends RenderObjectExportData> implements RenderObjectExporter<D> {
|
|
|
|
+ abstract readonly fileExtension: string;
|
|
|
|
|
|
private static getSizeFromTexture(tSize: TextureImage<Uint8Array>, i: number): number {
|
|
private static getSizeFromTexture(tSize: TextureImage<Uint8Array>, i: number): number {
|
|
const r = tSize.array[i * 3];
|
|
const r = tSize.array[i * 3];
|
|
const g = tSize.array[i * 3 + 1];
|
|
const g = tSize.array[i * 3 + 1];
|
|
const b = tSize.array[i * 3 + 2];
|
|
const b = tSize.array[i * 3 + 2];
|
|
- return decodeFloatRGB(r, g, b);
|
|
|
|
|
|
+ return decodeFloatRGB(r, g, b) / sizeDataFactor;
|
|
}
|
|
}
|
|
|
|
|
|
private static getSize(values: BaseValues & SizeValues, instanceIndex: number, group: number): number {
|
|
private static getSize(values: BaseValues & SizeValues, instanceIndex: number, group: number): number {
|
|
@@ -69,20 +44,20 @@ export class ObjExporter implements RenderObjectExporter<ObjData> {
|
|
size = values.uSize.ref.value;
|
|
size = values.uSize.ref.value;
|
|
break;
|
|
break;
|
|
case 'instance':
|
|
case 'instance':
|
|
- size = ObjExporter.getSizeFromTexture(tSize, instanceIndex) / 100;
|
|
|
|
|
|
+ size = MeshExporter.getSizeFromTexture(tSize, instanceIndex);
|
|
break;
|
|
break;
|
|
case 'group':
|
|
case 'group':
|
|
- size = ObjExporter.getSizeFromTexture(tSize, group) / 100;
|
|
|
|
|
|
+ size = MeshExporter.getSizeFromTexture(tSize, group);
|
|
break;
|
|
break;
|
|
case 'groupInstance':
|
|
case 'groupInstance':
|
|
const groupCount = values.uGroupCount.ref.value;
|
|
const groupCount = values.uGroupCount.ref.value;
|
|
- size = ObjExporter.getSizeFromTexture(tSize, instanceIndex * groupCount + group) / 100;
|
|
|
|
|
|
+ size = MeshExporter.getSizeFromTexture(tSize, instanceIndex * groupCount + group);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
return size * values.uSizeFactor.ref.value;
|
|
return size * values.uSizeFactor.ref.value;
|
|
}
|
|
}
|
|
|
|
|
|
- private static getGroup(groups: Float32Array | Uint8Array, i: number): number {
|
|
|
|
|
|
+ protected static getGroup(groups: Float32Array | Uint8Array, i: number): number {
|
|
const i4 = i * 4;
|
|
const i4 = i * 4;
|
|
const r = groups[i4];
|
|
const r = groups[i4];
|
|
const g = groups[i4 + 1];
|
|
const g = groups[i4 + 1];
|
|
@@ -93,131 +68,7 @@ export class ObjExporter implements RenderObjectExporter<ObjData> {
|
|
return decodeFloatRGB(r, g, b);
|
|
return decodeFloatRGB(r, g, b);
|
|
}
|
|
}
|
|
|
|
|
|
- private updateMaterial(color: Color, alpha: number) {
|
|
|
|
- if (this.currentColor === color && this.currentAlpha === alpha) return;
|
|
|
|
-
|
|
|
|
- this.currentColor = color;
|
|
|
|
- this.currentAlpha = alpha;
|
|
|
|
- const material = Color.toHexString(color) + alpha;
|
|
|
|
- StringBuilder.writeSafe(this.obj, `usemtl ${material}`);
|
|
|
|
- StringBuilder.newline(this.obj);
|
|
|
|
- if (!this.materialSet.has(material)) {
|
|
|
|
- this.materialSet.add(material);
|
|
|
|
- const [r, g, b] = Color.toRgbNormalized(color);
|
|
|
|
- const mtl = this.mtl;
|
|
|
|
- StringBuilder.writeSafe(mtl, `newmtl ${material}\n`);
|
|
|
|
- StringBuilder.writeSafe(mtl, 'illum 2\n'); // illumination model
|
|
|
|
- StringBuilder.writeSafe(mtl, 'Ns 163\n'); // specular exponent
|
|
|
|
- StringBuilder.writeSafe(mtl, 'Ni 0.001\n'); // optical density a.k.a. index of refraction
|
|
|
|
- StringBuilder.writeSafe(mtl, 'Ka 0 0 0\n'); // ambient reflectivity
|
|
|
|
- StringBuilder.writeSafe(mtl, 'Kd '); // diffuse reflectivity
|
|
|
|
- StringBuilder.writeFloat(mtl, r, 1000);
|
|
|
|
- StringBuilder.whitespace1(mtl);
|
|
|
|
- StringBuilder.writeFloat(mtl, g, 1000);
|
|
|
|
- StringBuilder.whitespace1(mtl);
|
|
|
|
- StringBuilder.writeFloat(mtl, b, 1000);
|
|
|
|
- StringBuilder.newline(mtl);
|
|
|
|
- StringBuilder.writeSafe(mtl, 'Ks 0.25 0.25 0.25\n'); // specular reflectivity
|
|
|
|
- StringBuilder.writeSafe(mtl, 'd '); // dissolve
|
|
|
|
- StringBuilder.writeFloat(mtl, alpha, 1000);
|
|
|
|
- StringBuilder.newline(mtl);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private async addMeshWithColors(vertices: Float32Array, normals: Float32Array, indices: Uint32Array | undefined, groups: Float32Array | Uint8Array, vertexCount: number, drawCount: number, values: BaseValues, instanceIndex: number, geoTexture: boolean, ctx: RuntimeContext) {
|
|
|
|
- const obj = this.obj;
|
|
|
|
- const t = Mat4();
|
|
|
|
- const n = Mat3();
|
|
|
|
- const tmpV = Vec3();
|
|
|
|
- const stride = geoTexture ? 4 : 3;
|
|
|
|
-
|
|
|
|
- const colorType = values.dColorType.ref.value;
|
|
|
|
- const tColor = values.tColor.ref.value.array;
|
|
|
|
- const uAlpha = values.uAlpha.ref.value;
|
|
|
|
- const aTransform = values.aTransform.ref.value;
|
|
|
|
-
|
|
|
|
- Mat4.fromArray(t, aTransform, instanceIndex * 16);
|
|
|
|
- mat3directionTransform(n, t);
|
|
|
|
-
|
|
|
|
- const currentProgress = (vertexCount * 2 + drawCount) * instanceIndex;
|
|
|
|
- await ctx.update({ isIndeterminate: false, current: currentProgress, max: (vertexCount * 2 + drawCount) * values.uInstanceCount.ref.value });
|
|
|
|
-
|
|
|
|
- // position
|
|
|
|
- for (let i = 0; i < vertexCount; ++i) {
|
|
|
|
- if (i % 1000 === 0 && ctx.shouldUpdate) await ctx.update({ current: currentProgress + i });
|
|
|
|
- v3transformMat4(tmpV, v3fromArray(tmpV, vertices, i * stride), t);
|
|
|
|
- StringBuilder.writeSafe(obj, 'v ');
|
|
|
|
- StringBuilder.writeFloat(obj, tmpV[0], 1000);
|
|
|
|
- StringBuilder.whitespace1(obj);
|
|
|
|
- StringBuilder.writeFloat(obj, tmpV[1], 1000);
|
|
|
|
- StringBuilder.whitespace1(obj);
|
|
|
|
- StringBuilder.writeFloat(obj, tmpV[2], 1000);
|
|
|
|
- StringBuilder.newline(obj);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // normal
|
|
|
|
- for (let i = 0; i < vertexCount; ++i) {
|
|
|
|
- if (i % 1000 === 0 && ctx.shouldUpdate) await ctx.update({ current: currentProgress + vertexCount + i });
|
|
|
|
- v3transformMat3(tmpV, v3fromArray(tmpV, normals, i * stride), n);
|
|
|
|
- StringBuilder.writeSafe(obj, 'vn ');
|
|
|
|
- StringBuilder.writeFloat(obj, tmpV[0], 100);
|
|
|
|
- StringBuilder.whitespace1(obj);
|
|
|
|
- StringBuilder.writeFloat(obj, tmpV[1], 100);
|
|
|
|
- StringBuilder.whitespace1(obj);
|
|
|
|
- StringBuilder.writeFloat(obj, tmpV[2], 100);
|
|
|
|
- StringBuilder.newline(obj);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // face
|
|
|
|
- for (let i = 0; i < drawCount; i += 3) {
|
|
|
|
- if (i % 3000 === 0 && ctx.shouldUpdate) await ctx.update({ current: currentProgress + vertexCount * 2 + i });
|
|
|
|
- let color: Color;
|
|
|
|
- switch (colorType) {
|
|
|
|
- case 'uniform':
|
|
|
|
- color = Color.fromNormalizedArray(values.uColor.ref.value, 0);
|
|
|
|
- break;
|
|
|
|
- case 'instance':
|
|
|
|
- color = Color.fromArray(tColor, instanceIndex * 3);
|
|
|
|
- break;
|
|
|
|
- case 'group': {
|
|
|
|
- const group = geoTexture ? ObjExporter.getGroup(groups, i) : groups[indices![i]];
|
|
|
|
- color = Color.fromArray(tColor, group * 3);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- case 'groupInstance': {
|
|
|
|
- const groupCount = values.uGroupCount.ref.value;
|
|
|
|
- const group = geoTexture ? ObjExporter.getGroup(groups, i) : groups[indices![i]];
|
|
|
|
- color = Color.fromArray(tColor, (instanceIndex * groupCount + group) * 3);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- case 'vertex':
|
|
|
|
- color = Color.fromArray(tColor, i * 3);
|
|
|
|
- break;
|
|
|
|
- case 'vertexInstance':
|
|
|
|
- color = Color.fromArray(tColor, (instanceIndex * drawCount + i) * 3);
|
|
|
|
- break;
|
|
|
|
- default: throw new Error('Unsupported color type.');
|
|
|
|
- }
|
|
|
|
- this.updateMaterial(color, uAlpha);
|
|
|
|
-
|
|
|
|
- const v1 = this.vertexOffset + (geoTexture ? i : indices![i]) + 1;
|
|
|
|
- const v2 = this.vertexOffset + (geoTexture ? i + 1 : indices![i + 1]) + 1;
|
|
|
|
- const v3 = this.vertexOffset + (geoTexture ? i + 2 : indices![i + 2]) + 1;
|
|
|
|
- StringBuilder.writeSafe(obj, 'f ');
|
|
|
|
- StringBuilder.writeInteger(obj, v1);
|
|
|
|
- StringBuilder.writeSafe(obj, '//');
|
|
|
|
- StringBuilder.writeIntegerAndSpace(obj, v1);
|
|
|
|
- StringBuilder.writeInteger(obj, v2);
|
|
|
|
- StringBuilder.writeSafe(obj, '//');
|
|
|
|
- StringBuilder.writeIntegerAndSpace(obj, v2);
|
|
|
|
- StringBuilder.writeInteger(obj, v3);
|
|
|
|
- StringBuilder.writeSafe(obj, '//');
|
|
|
|
- StringBuilder.writeInteger(obj, v3);
|
|
|
|
- StringBuilder.newline(obj);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- this.vertexOffset += vertexCount;
|
|
|
|
- }
|
|
|
|
|
|
+ protected abstract addMeshWithColors(vertices: Float32Array, normals: Float32Array, indices: Uint32Array | undefined, groups: Float32Array | Uint8Array, vertexCount: number, drawCount: number, values: BaseValues, instanceIndex: number, isGeoTexture: boolean, ctx: RuntimeContext): void;
|
|
|
|
|
|
private async addMesh(values: MeshValues, ctx: RuntimeContext) {
|
|
private async addMesh(values: MeshValues, ctx: RuntimeContext) {
|
|
const aPosition = values.aPosition.ref.value;
|
|
const aPosition = values.aPosition.ref.value;
|
|
@@ -256,7 +107,7 @@ export class ObjExporter implements RenderObjectExporter<ObjData> {
|
|
v3fromArray(center, aPosition, i * 3);
|
|
v3fromArray(center, aPosition, i * 3);
|
|
|
|
|
|
const group = aGroup[i];
|
|
const group = aGroup[i];
|
|
- const radius = ObjExporter.getSize(values, instanceIndex, group);
|
|
|
|
|
|
+ const radius = MeshExporter.getSize(values, instanceIndex, group);
|
|
state.currentGroup = group;
|
|
state.currentGroup = group;
|
|
addSphere(state, center, radius, 2);
|
|
addSphere(state, center, radius, 2);
|
|
}
|
|
}
|
|
@@ -266,7 +117,7 @@ export class ObjExporter implements RenderObjectExporter<ObjData> {
|
|
const normals = mesh.normalBuffer.ref.value;
|
|
const normals = mesh.normalBuffer.ref.value;
|
|
const indices = mesh.indexBuffer.ref.value;
|
|
const indices = mesh.indexBuffer.ref.value;
|
|
const groups = mesh.groupBuffer.ref.value;
|
|
const groups = mesh.groupBuffer.ref.value;
|
|
- await this.addMeshWithColors(vertices, normals, indices, groups, vertices.length / 3, indices.length, values, instanceIndex, false, ctx);
|
|
|
|
|
|
+ await this.addMeshWithColors(vertices, normals, indices, groups, mesh.vertexCount, indices.length, values, instanceIndex, false, ctx);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -290,7 +141,7 @@ export class ObjExporter implements RenderObjectExporter<ObjData> {
|
|
v3fromArray(end, aEnd, i * 3);
|
|
v3fromArray(end, aEnd, i * 3);
|
|
|
|
|
|
const group = aGroup[i];
|
|
const group = aGroup[i];
|
|
- const radius = ObjExporter.getSize(values, instanceIndex, group) * aScale[i];
|
|
|
|
|
|
+ const radius = MeshExporter.getSize(values, instanceIndex, group) * aScale[i];
|
|
const cap = aCap[i];
|
|
const cap = aCap[i];
|
|
const topCap = cap === 1 || cap === 3;
|
|
const topCap = cap === 1 || cap === 3;
|
|
const bottomCap = cap >= 2;
|
|
const bottomCap = cap >= 2;
|
|
@@ -304,7 +155,7 @@ export class ObjExporter implements RenderObjectExporter<ObjData> {
|
|
const normals = mesh.normalBuffer.ref.value;
|
|
const normals = mesh.normalBuffer.ref.value;
|
|
const indices = mesh.indexBuffer.ref.value;
|
|
const indices = mesh.indexBuffer.ref.value;
|
|
const groups = mesh.groupBuffer.ref.value;
|
|
const groups = mesh.groupBuffer.ref.value;
|
|
- await this.addMeshWithColors(vertices, normals, indices, groups, vertices.length / 3, indices.length, values, instanceIndex, false, ctx);
|
|
|
|
|
|
+ await this.addMeshWithColors(vertices, normals, indices, groups, mesh.vertexCount, indices.length, values, instanceIndex, false, ctx);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -356,14 +207,7 @@ export class ObjExporter implements RenderObjectExporter<ObjData> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- getData() {
|
|
|
|
- return {
|
|
|
|
- obj: StringBuilder.getString(this.obj),
|
|
|
|
- mtl: StringBuilder.getString(this.mtl)
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
|
|
+ abstract getData(): D;
|
|
|
|
|
|
- constructor(filename: string) {
|
|
|
|
- StringBuilder.writeSafe(this.obj, `mtllib ${filename}.mtl\n`);
|
|
|
|
- }
|
|
|
|
|
|
+ abstract getBlob(ctx: RuntimeContext): Promise<Blob>;
|
|
}
|
|
}
|