UNPKG

molstar

Version:

A comprehensive macromolecular library.

132 lines (110 loc) 5.34 kB
"use strict"; /** * Copyright (c) 2019-2025 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Áron Samuel Kovács <aron.kovacs@mail.muni.cz> * @author Alexander Rose <alexander.rose@weirdbyte.de> * @author Gianluca Tomasello <giagitom@gmail.com> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.outlines_frag = void 0; exports.outlines_frag = ` precision highp float; precision highp int; precision highp sampler2D; uniform sampler2D tDepthOpaque; uniform sampler2D tDepthTransparent; uniform vec2 uTexSize; uniform float uNear; uniform float uFar; uniform mat4 uInvProjection; uniform float uOutlineThreshold; #include common float getViewZ(const in float depth) { #if dOrthographic == 1 return orthographicDepthToViewZ(depth, uNear, uFar); #else return perspectiveDepthToViewZ(depth, uNear, uFar); #endif } float getDepthOpaque(const in vec2 coords) { #ifdef depthTextureSupport return texture2D(tDepthOpaque, coords).r; #else return unpackRGBAToDepth(texture2D(tDepthOpaque, coords)); #endif } vec2 getDepthTransparentWithAlpha(const in vec2 coords) { #ifdef dTransparentOutline return unpackRGBAToDepthWithAlpha(texture2D(tDepthTransparent, coords)); #else return vec2(1.0, 0.0); #endif } bool isBackground(const in float depth) { return depth == 1.0; } float getPixelSize(const in vec2 coords, const in float depth) { vec3 viewPos0 = screenSpaceToViewSpace(vec3(coords, depth), uInvProjection); vec3 viewPos1 = screenSpaceToViewSpace(vec3(coords + vec2(1.0, 0.0) / uTexSize, depth), uInvProjection); return distance(viewPos0, viewPos1); } void main(void) { float backgroundViewZ = 2.0 * uFar; vec2 coords = gl_FragCoord.xy / uTexSize; vec2 invTexSize = 1.0 / uTexSize; float selfDepthOpaque = getDepthOpaque(coords); float selfViewZOpaque = isBackground(selfDepthOpaque) ? backgroundViewZ : getViewZ(selfDepthOpaque); float pixelSizeOpaque = getPixelSize(coords, selfDepthOpaque) * uOutlineThreshold; vec2 selfDepthTransparentWithAlpha = getDepthTransparentWithAlpha(coords); float selfDepthTransparent = selfDepthTransparentWithAlpha.x; float selfViewZTransparent = isBackground(selfDepthTransparent) ? backgroundViewZ : getViewZ(selfDepthTransparent); float pixelSizeTransparent = getPixelSize(coords, selfDepthTransparent) * uOutlineThreshold; float bestOpaqueDepth = 1.0; float bestTransparentDepth = 1.0; float bestTransparentAlpha = 0.0; float opaqueOutlineFlag = 0.0; float transparentOutlineFlag = 0.0; for (int y = -1; y <= 1; y++) { for (int x = -1; x <= 1; x++) { vec2 sampleCoords = coords + vec2(float(x), float(y)) * invTexSize; // Opaque float sampleDepthOpaque = getDepthOpaque(sampleCoords); float sampleViewZOpaque = isBackground(sampleDepthOpaque) ? backgroundViewZ : getViewZ(sampleDepthOpaque); if (abs(selfViewZOpaque - sampleViewZOpaque) > pixelSizeOpaque && selfDepthOpaque > sampleDepthOpaque && sampleDepthOpaque <= bestOpaqueDepth) { bestOpaqueDepth = sampleDepthOpaque; opaqueOutlineFlag = 1.0; } // Transparent vec2 sampleDepthTransparentWithAlpha = getDepthTransparentWithAlpha(sampleCoords); float sampleDepthTransparent = sampleDepthTransparentWithAlpha.x; float sampleAlphaTransparent = sampleDepthTransparentWithAlpha.y; float sampleViewZTransparent = isBackground(sampleDepthTransparent) ? backgroundViewZ : getViewZ(sampleDepthTransparent); if (abs(selfViewZTransparent - sampleViewZTransparent) > pixelSizeTransparent && selfDepthTransparent > sampleDepthTransparent && sampleDepthTransparent <= bestTransparentDepth) { bestTransparentDepth = sampleDepthTransparent; bestTransparentAlpha = sampleAlphaTransparent; transparentOutlineFlag = 1.0; } } } if (transparentOutlineFlag > 0.0 && bestOpaqueDepth < 1.0 && bestTransparentDepth > bestOpaqueDepth) { transparentOutlineFlag = 0.0; bestTransparentAlpha = 0.0; } vec2 depthPacked; // Pack depth in G/B channels float outlineTypeFlag = 0.0; if (opaqueOutlineFlag > 0.0 && transparentOutlineFlag > 0.0) { outlineTypeFlag = 0.75; // Both depthPacked = packUnitIntervalToRG(bestOpaqueDepth); } else if (transparentOutlineFlag > 0.0) { outlineTypeFlag = 0.5; // Transparent only depthPacked = packUnitIntervalToRG(bestTransparentDepth); } else if (opaqueOutlineFlag > 0.0) { outlineTypeFlag = 0.25; // Opaque only depthPacked = packUnitIntervalToRG(bestOpaqueDepth); } float alpha = clamp(bestTransparentAlpha, 0.0, 0.5) * 2.0; // limiting to range [0.0, 0.5] to improve alpha precision since we don't need a wider range float packedFlagWithAlpha = pack2x4(vec2(outlineTypeFlag, alpha)); // pack outlineType with alpha gl_FragColor = vec4(packedFlagWithAlpha, depthPacked.x, depthPacked.y, bestTransparentDepth); } `;