UNPKG

speck-renderer

Version:

Browser-based WebGL molecule renderer with the goal of producing figures that are as attractive as they are practical.

8 lines (6 loc) 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var atomsShader = "#version 100\nprecision highp float;\n\nattribute vec3 aImposter;\nattribute vec3 aPosition;\nattribute float aRadius;\nattribute vec3 aColor;\n\nuniform mat4 uView;\nuniform mat4 uProjection;\nuniform mat4 uModel;\nuniform float uAtomScale;\nuniform float uRelativeAtomScale;\nuniform float uAtomShade;\n\nvarying vec3 vColor;\nvarying vec3 vPosition;\nvarying float vRadius;\n\nvoid main() {\n vRadius = uAtomScale * (1.0 + (aRadius - 1.0) * uRelativeAtomScale);\n gl_Position = uProjection * uView * uModel * vec4(vRadius * aImposter + aPosition, 1.0);\n vColor = mix(aColor, vec3(1,1,1), uAtomShade);\n vPosition = vec3(uModel * vec4(aPosition, 1));\n}\n\n\n// __split__\n\n\n#version 100\n#extension GL_EXT_frag_depth: enable\nprecision highp float;\n\nuniform vec2 uBottomLeft;\nuniform vec2 uTopRight;\nuniform float uRes;\nuniform float uDepth;\nuniform int uMode;\n\nvarying vec3 vPosition;\nvarying float vRadius;\nvarying vec3 vColor;\n\nvec2 res = vec2(uRes, uRes);\n\nfloat raySphereIntersect(vec3 r0, vec3 rd) {\n float a = dot(rd, rd);\n vec3 s0_r0 = r0 - vPosition;\n float b = 2.0 * dot(rd, s0_r0);\n float c = dot(s0_r0, s0_r0) - (vRadius * vRadius);\n float disc = b*b - 4.0*a*c;\n if (disc <= 0.0) {\n return -1.0;\n }\n return (-b - sqrt(disc))/(2.0*a);\n}\n\nvoid main() {\n vec3 r0 = vec3(uBottomLeft + (gl_FragCoord.xy/res) * (uTopRight - uBottomLeft), 0.0);\n vec3 rd = vec3(0, 0, -1);\n float t = raySphereIntersect(r0, rd);\n if (t < 0.0) {\n discard;\n }\n vec3 coord = r0 + rd * t;\n vec3 normal = normalize(coord - vPosition);\n if (uMode == 0) {\n gl_FragColor = vec4(vColor, 1);\n } else if (uMode == 1) {\n gl_FragColor = vec4(normal * 0.5 + 0.5, 1.0);\n }\n gl_FragDepthEXT = -coord.z/uDepth;\n}"; exports.default = atomsShader;