UNPKG

molstar

Version:

A comprehensive macromolecular library.

107 lines (106 loc) 4.83 kB
/** * Copyright (c) 2020-2021 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { __assign } from "tslib"; import { createNullTexture } from '../../mol-gl/webgl/texture'; import { ValueCell } from '../../mol-util'; import { ValueSpec, AttributeSpec, UniformSpec, TextureSpec } from '../../mol-gl/renderable/schema'; import { Vec2 } from '../../mol-math/linear-algebra'; import { ShaderCode } from '../shader-code'; import { copy_frag } from '../shader/copy.frag'; import { quad_vert } from '../shader/quad.vert'; import { createComputeRenderItem } from '../webgl/render-item'; import { createComputeRenderable } from '../renderable'; export var QuadPositions = new Float32Array([ 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0 // Second triangle ]); export var QuadSchema = { drawCount: ValueSpec('number'), instanceCount: ValueSpec('number'), aPosition: AttributeSpec('float32', 2, 0), uQuadScale: UniformSpec('v2'), }; export var QuadValues = { drawCount: ValueCell.create(6), instanceCount: ValueCell.create(1), aPosition: ValueCell.create(QuadPositions), uQuadScale: ValueCell.create(Vec2.create(1, 1)), }; // var CopySchema = __assign(__assign({}, QuadSchema), { tColor: TextureSpec('texture', 'rgba', 'ubyte', 'nearest'), uTexSize: UniformSpec('v2') }); var CopyShaderCode = ShaderCode('copy', quad_vert, copy_frag); export function createCopyRenderable(ctx, texture) { var values = __assign(__assign({}, QuadValues), { tColor: ValueCell.create(texture), uTexSize: ValueCell.create(Vec2.create(texture.getWidth(), texture.getHeight())) }); var schema = __assign({}, CopySchema); var renderItem = createComputeRenderItem(ctx, 'triangles', CopyShaderCode, schema, values); return createComputeRenderable(renderItem, values); } var SharedCopyName = 'shared-copy'; export function getSharedCopyRenderable(ctx, texture) { if (!ctx.namedComputeRenderables[SharedCopyName]) { ctx.namedComputeRenderables[SharedCopyName] = createCopyRenderable(ctx, createNullTexture()); } var copy = ctx.namedComputeRenderables[SharedCopyName]; ValueCell.update(copy.values.tColor, texture); ValueCell.update(copy.values.uTexSize, Vec2.set(copy.values.uTexSize.ref.value, texture.getWidth(), texture.getHeight())); copy.update(); return copy; } // var ReadTextureName = 'read-texture'; var ReadAlphaTextureName = 'read-alpha-texture'; export function readTexture(ctx, texture, array) { var gl = ctx.gl, resources = ctx.resources; if (!array && texture.type !== gl.UNSIGNED_BYTE) throw new Error('unsupported texture type'); if (!ctx.namedFramebuffers[ReadTextureName]) { ctx.namedFramebuffers[ReadTextureName] = resources.framebuffer(); } var framebuffer = ctx.namedFramebuffers[ReadTextureName]; var width = texture.getWidth(); var height = texture.getHeight(); if (!array) array = new Uint8Array(width * height * 4); framebuffer.bind(); texture.attachFramebuffer(framebuffer, 0); ctx.readPixels(0, 0, width, height, array); return { array: array, width: width, height: height }; } export function readAlphaTexture(ctx, texture) { var gl = ctx.gl, state = ctx.state, resources = ctx.resources; if (texture.type !== gl.UNSIGNED_BYTE) throw new Error('unsupported texture type'); var width = texture.getWidth(); var height = texture.getHeight(); var copy = getSharedCopyRenderable(ctx, texture); state.currentRenderItemId = -1; if (!ctx.namedFramebuffers[ReadAlphaTextureName]) { ctx.namedFramebuffers[ReadAlphaTextureName] = resources.framebuffer(); } var framebuffer = ctx.namedFramebuffers[ReadAlphaTextureName]; framebuffer.bind(); if (!ctx.namedTextures[ReadAlphaTextureName]) { ctx.namedTextures[ReadAlphaTextureName] = resources.texture('image-uint8', 'rgba', 'ubyte', 'linear'); } var copyTex = ctx.namedTextures[ReadAlphaTextureName]; copyTex.define(width, height); copyTex.attachFramebuffer(framebuffer, 0); state.disable(gl.CULL_FACE); state.enable(gl.BLEND); state.disable(gl.DEPTH_TEST); state.enable(gl.SCISSOR_TEST); state.depthMask(false); state.clearColor(0, 0, 0, 0); state.blendFunc(gl.ONE, gl.ONE); state.blendEquation(gl.FUNC_ADD); state.viewport(0, 0, width, height); state.scissor(0, 0, width, height); gl.clear(gl.COLOR_BUFFER_BIT); copy.render(); var array = new Uint8Array(width * height * 4); ctx.readPixels(0, 0, width, height, array); return { array: array, width: width, height: height }; }