1 line
11 KiB
Plaintext
1 line
11 KiB
Plaintext
|
|
{"version":3,"file":"VolumeShader.cjs","sources":["../../src/shaders/VolumeShader.ts"],"sourcesContent":["import { Vector2, Vector3 } from 'three'\n\n/**\n * Shaders to render 3D volumes using raycasting.\n * The applied techniques are based on similar implementations in the Visvis and Vispy projects.\n * This is not the only approach, therefore it's marked 1.\n */\n\nexport const VolumeRenderShader1 = {\n uniforms: {\n u_size: { value: /* @__PURE__ */ new Vector3(1, 1, 1) },\n u_renderstyle: { value: 0 },\n u_renderthreshold: { value: 0.5 },\n u_clim: { value: /* @__PURE__ */ new Vector2(1, 1) },\n u_data: { value: null },\n u_cmdata: { value: null },\n },\n vertexShader: /* glsl */ `\n\t\tvarying vec4 v_nearpos;\n\t\tvarying vec4 v_farpos;\n\t\tvarying vec3 v_position;\n\n\t\tvoid main() {\n\t\t\t// Prepare transforms to map to \"camera view\". See also:\n\t\t\t// https://threejs.org/docs/#api/renderers/webgl/WebGLProgram\n\t\t\tmat4 viewtransformf = modelViewMatrix;\n\t\t\tmat4 viewtransformi = inverse(modelViewMatrix);\n\n\t\t\t// Project local vertex coordinate to camera position. Then do a step\n\t\t\t// backward (in cam coords) to the near clipping plane, and project back. Do\n\t\t\t// the same for the far clipping plane. This gives us all the information we\n\t\t\t// need to calculate the ray and truncate it to the viewing cone.\n\t\t\tvec4 position4 = vec4(position, 1.0);\n\t\t\tvec4 pos_in_cam = viewtransformf * position4;\n\n\t\t\t// Intersection of ray and near clipping plane (z = -1 in clip coords)\n\t\t\tpos_in_cam.z = -pos_in_cam.w;\n\t\t\tv_nearpos = viewtransformi * pos_in_cam;\n\n\t\t\t// Intersection of ray and far clipping plane (z = +1 in clip coords)\n\t\t\tpos_in_cam.z = pos_in_cam.w;\n\t\t\tv_farpos = viewtransformi * pos_in_cam;\n\n\t\t\t// Set varyings and output pos\n\t\t\tv_position = position;\n\t\t\tgl_Position = projectionMatrix * viewMatrix * modelMatrix * position4;\n\t\t}\n `,\n fragmentShader: /* glsl */ `\n\t\tprecision highp float;\n\t\tprecision mediump sampler3D;\n\n\t\tuniform vec3 u_size;\n\t\tuniform int u_renderstyle;\n\t\tuniform float u_renderthreshold;\n\t\tuniform vec2 u_clim;\n\n\t\tuniform sampler3D u_data;\n\t\tuniform sampler2D u_cmdata;\n\n\t\tvarying vec3 v_position;\n\t\tvarying vec4 v_nearpos;\n\t\tvarying vec4 v_farpos;\n\n\t// The maximum distance through our rendering volume is sqrt(3).\n\t\tconst int MAX_STEPS = 887;\t// 887 for 512^3, 1774 for 1024^3\n\t\tconst int REFINEMENT_STEPS = 4;\n\t\tconst float relative_step_size = 1.0;\n\t\tconst vec4 ambient_color = vec4(0.2, 0.4, 0.2, 1.0);\n\t\tconst vec4 diffuse_color = vec4(0.8, 0.2, 0.2, 1.0);\n\t\tconst vec4 specular_color = vec4(1.0, 1.0, 1.0, 1.0);\n\t\tconst float shininess = 40.0;\n\n\t\tvoid cast_mip(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray);\n\t\tvoid cast_iso(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray);\n\n\t\tfloat sample1(vec3 texcoords);\n\t\tvec4 apply_colormap(float val);\n\t\tvec4 add_lighting(float val, vec3 loc, vec3 step, vec3 view_ray);\n\n\t\tvoid main() {\n\t// Normalize clipping plane info\n\t\t\tvec3 farpos = v_farpos.xyz / v_farpos.w;\n\t\t\tvec3 nearpos = v_nearpos.xyz / v_nearpos.w;\n\n\t// Calculate unit vector pointing in the view direction through this fragment.\n\t\t\tvec3 view_ray = normalize(nearpos.xyz - farpos.xyz);\n\n\t// Compute the (negative) distance to the front surface or near clipping plane.\n\t// v_position is the back face of the cuboid, so the initial distance calculated in the dot\n\t// product below is the distance from near clip plane to the back of the cuboid\n\t\t\tfloat distance = dot(nearpos - v_position, view_ray);\n\t\t\tdistance = max(distance, min((-0.5 - v_position.x) / view_ray.x,\n\t\t\t\t\t\t\t\t\t\t(u_size.x - 0.5 - v_position.x) / view_ray.x));\n\t\t\tdistance = max(distance, min((-0.5 - v_position.y) / view_ray.y,\n\t\t\t\t\t\t\t\t\t\t(u_size.y - 0.5 - v_position.y) / view_ray.y));\n\t\t\tdistance = max(distance, min((-0.5 - v_position.z) / view_ray.z,\n\t\t\t\t\t\t\t\t\t\t(u_size.z - 0.5 - v_position.z) /
|