Compare commits

...

124 Commits

Author SHA1 Message Date
Alexander Rose
3b97bfd9b6 5.0.0 2025-09-28 10:41:25 -07:00
Alexander Rose
9b12623131 changelog 2025-09-28 10:39:51 -07:00
Alexander Rose
425370d63e package updates 2025-09-28 10:36:37 -07:00
Alexander Rose
1666c89222 doc tweak 2025-09-28 10:30:28 -07:00
Alexander Rose
a7dd4fc555 change viewer.show-xr to allow 'auto' | 'always' | 'never' 2025-09-28 10:25:48 -07:00
Alexander Rose
9f1760fbf2 pointer: end gesture only if all button are released 2025-09-28 09:54:10 -07:00
Alexander Rose
d7fb040b77 fix shader-manager update 2025-09-21 15:40:27 -07:00
Alexander Rose
2d7c1bcea2 Merge pull request #1665 from molstar/global-defines
global defines
2025-09-21 15:27:22 -07:00
Alexander Rose
a08c434f35 add missing schema 2025-09-21 09:34:42 -07:00
Alexander Rose
45d402bb9f global defines 2025-09-20 21:08:46 -07:00
Alexander Rose
4556544043 package updates 2025-09-20 14:55:58 -07:00
Alexander Rose
921d700761 remove unused dep 2025-09-20 14:52:11 -07:00
Alexander Rose
9605783f41 defer readPixels call in texture-mesh position-iterator 2025-09-20 14:51:23 -07:00
Alexander Rose
f23329dc68 improve resource byte count logging 2025-09-20 14:48:50 -07:00
Alexander Rose
5f4ac6b2c0 remove unused properties 2025-09-20 14:36:39 -07:00
David Sehnal
f0c2961e95 use esbuild jest transformer (#1662) 2025-09-16 20:28:34 +02:00
David Sehnal
2bdaa565b4 Fix screenshot animation loop handling (#1660) 2025-09-16 20:09:33 +02:00
Jose Manuel Duarte
ab2bcde794 Add robots.txt to ModelServer (#1659) 2025-09-16 08:05:30 +02:00
Alexander Rose
0b9674e14c Merge pull request #1655 from molstar/parallel-shader-compile
Adaptive parallel shader compilation
2025-09-15 21:32:52 -07:00
Alexander Rose
07cbeb524e Merge pull request #1653 from giagitom/fix-illum-denoise
Fix illumination denoising with transparency on transparent background
2025-09-15 21:31:31 -07:00
Alexander Rose
8ff75ea2ab Merge branch 'master' into fix-illum-denoise 2025-09-15 21:31:10 -07:00
Alexander Rose
6f5db94b2f add shader-manager
- ensure required shaders for image pass
- take scene content into account
2025-09-15 21:30:12 -07:00
dsehnal
2637957141 pass isSynchronous to finalizePrograms 2025-09-15 08:59:49 +02:00
Alexander Rose
c1bb6f3987 changelog 2025-09-14 21:50:47 -07:00
Alexander Rose
d8df904951 Merge branch 'master' of https://github.com/molstar/molstar into parallel-shader-compile 2025-09-14 21:43:07 -07:00
Alexander Rose
a7ca7c922d adaptive parallel shader compile
- split shader compilation into linking and finalizing
- avoid compiling un-needed shaders
2025-09-14 21:38:27 -07:00
Alexander Rose
f257992a5a Revert "add "ready" commit queue"
This reverts commit bdd1805620.
2025-09-14 21:23:52 -07:00
김주호
62f9f6077d Update to_mmCIF function to accept multiple structures (#1658)
* update to_mmCIF function to accept multiple structures

* update changelog and code header
2025-09-12 09:51:11 +02:00
midlik
e4edb67f62 export class Layout extends PluginUIComponent (#1657) 2025-09-10 14:05:50 +02:00
dsehnal
185ccf5ca6 tweak story title 2025-09-09 11:34:09 +02:00
dsehnal
bdd1805620 add "ready" commit queue 2025-09-09 11:01:13 +02:00
Alexander Rose
29f2722851 wip 2025-09-09 00:07:07 -07:00
giagitom
b38f8b08da Fix illumination denoising with transparency on transparent background 2025-09-08 13:23:34 +02:00
Alexander Rose
6d02889f84 type fixes 2025-09-07 22:25:32 -07:00
Alexander Rose
b864634f1d spec fixes 2025-09-07 19:36:33 -07:00
Alexander Rose
248662b95c update workflow 2025-09-07 19:31:43 -07:00
Alexander Rose
0eb28bd89e schema updates 2025-09-07 18:41:12 -07:00
Alexander Rose
e466bf9ba9 package updates 2025-09-07 18:38:29 -07:00
Alexander Rose
a14c4faefd Merge pull request #1639 from giagitom/fix-transparency-check
Outlines improvements
2025-09-07 16:56:54 -07:00
Alexander Rose
b87a7f069e Merge branch 'master' into fix-transparency-check 2025-09-07 16:56:45 -07:00
Alexander Rose
674a56e2f3 Merge pull request #1590 from molstar/webxr
WebXR
2025-09-07 16:55:56 -07:00
Alexander Rose
521d8cb4f8 Merge branch 'master' of https://github.com/molstar/molstar into webxr 2025-09-07 16:53:14 -07:00
Alexander Rose
bd1d85e927 Merge pull request #1651 from molstar/picking-too-many-groups
improve picking of objects with many groups
2025-09-07 16:49:02 -07:00
Alexander Rose
4d62b928f8 improve picking of objects with many groups
- if to many groups (currently >=2^24-2) pick whole instance/object
2025-09-06 14:05:22 -07:00
Jose Manuel Duarte
014c9607d9 Use new validation/view endpoint at files.rcsb.org (#1649)
* Use new validation/view endpoint at files.rcsb.org

* Update changelog
2025-09-06 11:56:03 +02:00
midlik
98ef24fc9e Sequence color 2 (#1644)
* Sequence color extension - allow props to be provided

* typing fix
2025-09-04 19:49:20 +02:00
dsehnal
c04580377b 5.0.0-dev.13 2025-09-03 09:20:26 +02:00
David Sehnal
a492b38368 fix mutative use & assign NODE_ENV=production for prd builds (#1642)
* fix mutative use & assign NODE_ENV=production for prd builds

* fix type
2025-09-03 09:07:23 +02:00
midlik
518f21531e SequenceColor extension (#1611)
* MinimizeRmsd.Result include nAlignedElements

* SequenceColor extension

* SequenceColor extension - forceUpdate when custom prop changes

* SequenceColor extension - proper caching

* Update CHANGELOG

* SequenceColor extension - registry

* SequenceColor extension - refactor

* SequenceColor extension - minor changes

* SequenceColor extension - switch to experimentalSequenceColorTheme

* SequenceColor extension - ensureCustomProperties

* SequenceColor extension - clean

* SequenceColor extension - avoid repeated allocation for Location

* SequenceColor extension - memoizeLatest, but wrong

* SequenceColor extension - memoizeLatest fixed

* SequenceColor extension - remove unnecessary loci caching

* SequenceColor extension - clean up
2025-09-03 07:29:40 +02:00
David Sehnal
36fd40ee09 VolumeServer: Default to P1 spacegroup (#1640)
* CCP4 parser defaultToP1 option

* volume server: default to P1

* tweaks

* tweak
2025-09-02 17:47:58 +02:00
giagitom
6b8c604762 improvements 2025-09-02 17:28:16 +02:00
giagitom
c10382d1fb Handle illumination 2025-09-02 15:23:50 +02:00
Alexander Rose
0e968ae59c Fix ColorScale for continuous case without offsets 2025-09-01 16:09:12 -07:00
giagitom
1286a9e560 Fix tests 2025-09-01 22:10:49 +02:00
giagitom
bf73712781 Add packing/unpacking functions 2025-09-01 22:03:36 +02:00
giagitom
53922db113 Outlines improvements 2025-09-01 17:56:03 +02:00
giagitom
799037d657 Merge branch 'master' of https://github.com/molstar/molstar into fix-transparency-check 2025-09-01 17:52:15 +02:00
Alexander Rose
5cb7a3cc8e pixel-based size of pointer helper points 2025-08-31 18:33:46 -07:00
Alexander Rose
c14cbb258d fix size calculation and update of text geometry 2025-08-31 18:33:07 -07:00
Alexander Rose
8a860497f1 support ray-picking of text geometry
- uses extra eye camera in text shader
2025-08-31 18:32:38 -07:00
Alexander Rose
77d4d0007c Merge branch 'master' of https://github.com/molstar/molstar into webxr 2025-08-31 17:45:45 -07:00
dsehnal
005824eb24 5.0.0-dev.12 2025-08-31 10:08:11 +02:00
dsehnal
259e04a6ce move mvs validation to a separate file 2025-08-31 10:06:20 +02:00
dsehnal
966bc14c67 5.0.0-dev.11 2025-08-31 09:51:54 +02:00
David Sehnal
f752b7e155 MVS: Color map interpolation & canvas backgrounds (#1636)
* MVS: Color map interpolation

* print validation errors to plugin log

* fixes

* background postprocessing

* fix resetCanvasProps

* fix link

* add example
2025-08-31 09:36:50 +02:00
Gianluca Tomasello
255b8b9ac3 Fix renderer transparency check (#1635)
* Fix renderer transparency check

* Fading transparent outlines

* improvements
2025-08-29 17:59:26 +02:00
giagitom
15c4fb3c01 improvements 2025-08-29 15:01:31 +02:00
giagitom
9fba0c08b2 Fading transparent outlines 2025-08-28 21:26:06 +02:00
giagitom
f08dd0255d Fix renderer transparency check 2025-08-28 11:44:05 +02:00
Victoria Doshchenko
42d969bbeb MVS: example story improvements (#1632)
* add intro scene

* fixes

* add author name
2025-08-26 18:55:34 +02:00
dsehnal
fdc33e44dc 5.0.0-dev.10 2025-08-26 17:43:06 +02:00
David Sehnal
b0aa889a0a MVS: Animation improvements (#1631)
* allow interpolation "keyframing"

* animation fixes
2025-08-26 17:41:21 +02:00
David Sehnal
4d7bd53231 Additional markdown commands (#1630) 2025-08-26 06:58:40 +02:00
David Sehnal
c11cf665c9 Additional markdown extensions (#1629)
* additional markdown extensions

* fixes
2025-08-25 20:12:10 +02:00
dsehnal
a4b09d3a0c 5.0.0-dev.9 2025-08-25 17:00:54 +02:00
David Sehnal
6e488b0f80 MotM1 Story tweaks (#1627)
* tweak story

* bugfixes & tweaks

* linting

* support "discrete" scalar transform

* tweak audio path

* tweak ui
2025-08-25 08:22:36 +02:00
Alexander Rose
2cef723483 naming fix 2025-08-24 22:46:41 -07:00
ludovic autin
6164281a50 initial work on MOM number 1 with audio comments (#1624)
* initial work on MOM number 1 with audio comments

* add some TODO comments

* separate audio as mp3. Do coloring with DG scheme

* move audio in root example folder. test some query in mol-script

* salt bridge.

* better coloring

* support for entry-id test in MolScriptBuilder.

* lint

* update audio, sync animation

* cleanup

* clean up and sync audio/anim

* add reference to MOM1
2025-08-25 07:14:46 +02:00
Alexander Rose
c74a014ab7 update package.lock for ci 2025-08-24 14:54:27 -07:00
Alexander Rose
4bbf1dc8aa refactor xr input handling 2025-08-24 14:52:58 -07:00
Alexander Rose
6e53621e01 Merge branch 'master' of https://github.com/molstar/molstar into webxr 2025-08-24 13:42:49 -07:00
Alexander Rose
2db7171e2a Merge pull request #1625 from molstar/trackball-state-tweaks
Trackball & Snapshot handling tweaks
2025-08-24 13:42:24 -07:00
Alexander Rose
edfc094952 re-add the !isBusy check
Co-authored-by: David Sehnal <dsehnal@users.noreply.github.com>
2025-08-24 13:35:37 -07:00
David Sehnal
b3e1e2900b Fix Markdown Commands query focus (#1626) 2025-08-24 13:18:36 +02:00
Alexander Rose
ba2bc206cc Merge branch 'master' of https://github.com/molstar/molstar into webxr 2025-08-23 14:59:17 -07:00
Alexander Rose
1e498d535a add SnapshotControls behavior 2025-08-23 10:44:50 -07:00
Alexander Rose
6ed969cd1b don't save behaviors in me snapshots 2025-08-23 10:42:05 -07:00
Alexander Rose
27bb4f4bca remove unused trackball param 2025-08-23 10:41:21 -07:00
Alexander Rose
6ce2139272 add canvas3d/trackball attribs
- attribs are configurable but are not saved in the state like props
2025-08-23 10:41:04 -07:00
Alexander Rose
856eff5127 ensure xr props are set each frame, make passthrough default 2025-08-23 10:19:50 -07:00
dsehnal
13cf6613a6 fix typo 2025-08-23 19:18:21 +02:00
Alexander Rose
52b141c4fa fix pointer-helper camera 2025-08-23 10:15:39 -07:00
Alexander Rose
701844ca7c remove unused uniform 2025-08-23 10:14:49 -07:00
Alexander Rose
bcc572bd18 move scale, minTargetDistance, forceFull to camera properties 2025-08-23 10:14:29 -07:00
David Sehnal
c5bb13e295 Execute markdown commands on snapshot load (#1622) 2025-08-22 18:25:24 +02:00
dsehnal
34c8257848 5.0.0-dev.8 2025-08-22 16:05:31 +02:00
David Sehnal
fcbf39c935 Markdown & MVS: Audio support (#1621)
* audio playback markdown commands

* trigger markdown commands from MVS primitives

* docs

* fix usage of mutative
2025-08-22 16:00:30 +02:00
Alexander Rose
4b58ce94ee Merge branch 'master' of https://github.com/molstar/molstar into webxr 2025-08-17 23:43:19 -07:00
Alexander Rose
16b0374eac tweak trackball forward/backward movement behavior 2025-08-16 16:10:54 -07:00
Alexander Rose
67e63dccb4 improve/fix sphere rendering for asymmetric projections 2025-08-16 14:42:03 -07:00
Alexander Rose
2cc600cc52 add xr.sceneRadiusInMeters param
- tweak mesoscale explorer xr defaults
2025-08-16 14:40:43 -07:00
Alexander Rose
cb499ce42e Merge pull request #1607 from corredD/webxr
fix shadow
2025-08-12 22:51:28 -07:00
Alexander Rose
23701bf8e8 always consider bounds 2025-08-12 22:47:56 -07:00
ludovic autin
2e1f2e7eec fix shadow 2025-08-12 10:58:05 -07:00
Alexander Rose
fdb3ff54f1 fix: apply model-scale to alpha-thickness 2025-08-11 22:19:52 -07:00
Alexander Rose
d5fd56718d tweak shadow pass 2025-08-10 20:52:39 -07:00
Alexander Rose
0698ac6dd5 changelog 2025-08-10 11:55:06 -07:00
Alexander Rose
825b59ab1e fix multi scale ssao update when camera scale changes 2025-08-10 11:21:17 -07:00
Alexander Rose
3086d1a5c8 Merge branch 'master' of https://github.com/molstar/molstar into webxr 2025-08-10 10:47:45 -07:00
Alexander Rose
8f7fda4919 cleanup 2025-08-09 23:14:48 -07:00
Alexander Rose
470ccd333f improve forward/back movement
- when camera is outside of visible boundingsphere, move a fraction of the distance to the target
2025-08-09 23:00:28 -07:00
Alexander Rose
2b6d067b0e reset xr scale when camera resets in canvas3d 2025-08-09 20:29:31 -07:00
Alexander Rose
0b928888a5 apply standard pixel scale in addition to xr resolution scale 2025-08-09 18:18:37 -07:00
Alexander Rose
28edfd44cb add xr settings to mesoscale explorer ui 2025-08-09 18:17:25 -07:00
Alexander Rose
3391c6de07 add simple culling for scaled scenes 2025-08-09 18:16:55 -07:00
Alexander Rose
12b7951700 better model scale handling for clipping and lods 2025-08-09 18:15:52 -07:00
Alexander Rose
cbc0e857fc log entering/exiting xr 2025-08-04 22:20:10 -07:00
Alexander Rose
01ce306405 handle multiple input sources 2025-08-04 22:16:26 -07:00
Alexander Rose
a39a49e884 improve event handling and listen to device change 2025-08-03 23:08:30 -07:00
Alexander Rose
887a39dde9 more cleanup/refactoring and basic error handling 2025-08-03 18:14:31 -07:00
Alexander Rose
84a45fabdc cleanup/refactor
- moved button out of canvas3d
- add button to render-structure browser test
- add button to plugin viewport
2025-08-02 18:54:17 -07:00
Alexander Rose
ea17902aa6 handle webxr with external "animation" control
- add Canvas3D.request/cancelAnimationFrame
- keep copy of animation callback to automatically resume
2025-08-02 07:23:46 -07:00
Alexander Rose
2abbb843f8 update lock file 2025-07-27 18:14:15 -07:00
Alexander Rose
32179f31c2 webxr, wip
- add pointer-helper
- add xr-manager to handle xr input and session
- support xr render loop in canvas3d
- add camera.state.minTargetDistance (don't want things too close to your eyes)
- add camera.state.forceFull to show frustum from camera to end of scene
- basic input handling (single controller)
2025-07-27 17:38:53 -07:00
207 changed files with 18508 additions and 11870 deletions

View File

@@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18
node-version: 20
- run: npm ci
- run: sudo apt-get install xvfb
- name: Lint

View File

@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file, following t
Note that since we don't clearly distinguish between a public and private interfaces there will be changes in non-major versions that are potentially breaking. If we make breaking changes to less used interfaces we will highlight it in here.
## [Unreleased]
## [v5.0.0] - 2025-09-28
- [Breaking] Renamed some color schemes ('inferno' -> 'inferno-no-black', 'magma' -> 'magma-no-black', 'turbo' -> 'turbo-no-black', 'rainbow' -> 'simple-rainbow')
- [Breaking] `Box3D.nearestIntersectionWithRay` -> `Ray3D.intersectBox3D`
- [Breaking] `Plane3D.distanceToSpher3D` -> `distanceToSphere3D` (fix spelling)
@@ -13,6 +15,7 @@ Note that since we don't clearly distinguish between a public and private interf
- This change is breaking because all volume objects require the `instances` field now.
- [Breaking] `Canvas3D.identify` now expects `Vec2` or `Ray3D`
- [Breaking] `TrackballControlsParams.animate.spin.speed` now means "Number of rotations per second" instead of "radians per second"
- [Breaking] `PluginStateSnapshotManager.play` now accepts an options object instead of a single boolean value
- Update production build to use `esbuild`
- Emit explicit paths in `import`s in `lib/`
- Fix outlines on opaque elements using illumination mode
@@ -20,10 +23,10 @@ Note that since we don't clearly distinguish between a public and private interf
- MolViewSpec extension:
- Generic color schemes (`palette` parameter for color_from_* nodes)
- Annotation field remapping (`field_remapping` parameter for color_from_* nodes)
- `representation` node: support custom property `molstar_reprepresentation_params`
- `representation` node: support custom property `molstar_representation_params`
- Add `backbone` and `line` representation types
- `primitives` node: support custom property `molstar_mesh/label/line_params`
- `canvas` node: support custom property `molstar_postprocessing` with the ability to customize outline, depth of field, bloom, shadow, occlusion (SSAO), and fog
- `canvas` node: support custom property `molstar_postprocessing` with the ability to customize outline, depth of field, bloom, shadow, occlusion (SSAO), fog, and background
- `clip` node support for structure and volume representations
- `grid_slice` representation support for volumes
- Support tethers and background for primitive labels
@@ -41,14 +44,19 @@ Note that since we don't clearly distinguish between a public and private interf
- MVSX - use Murmur hash instead of FNV in archive URI
- Support additional file formats (pdbqt, gro, xyz, mol, sdf, mol2, xtc, lammpstrj)
- Support loading trajectory coordinates from separate nodes
- Trigger markdown commands from primitives using `molstar_markdown_commands` custom extensions
- Support `molstar_on_load_markdown_commands` custom state on the `root` node
- Print tree validation errors to plugin log
- Added new color schemes, synchronized with D3.js ('inferno', 'magma', 'turbo', 'rainbow', 'sinebow', 'warm', 'cool', 'cubehelix-default', 'category-10', 'observable-10', 'tableau-10')
- Snapshot Markdown improvements
- Add `MarkdownExtensionManager` (`PluginContext.managers.markdownExtensions`)
- Support custom markdown commands to control the plugin via the `[link](!command)` pattern
- Support rendering custom elements via the `![alt](!parameters)` pattern
- Support tables
- Support loading images from MVSX files
- Support loading images and audio from MVSX files
- Indicate external links with ⤴
- Audio support
- Add `PluginState.Snapshot.onLoadMarkdownCommands`
- Avoid calculating rings for coarse-grained structures
- Fix isosurface compute shader normals when transformation matrix is applied to volume
- Symmetry operator naming for spacegroup symmetry - parenthesize multi-character indices (1_111-1 -> 1_(11)1(-1))
@@ -95,6 +103,30 @@ Note that since we don't clearly distinguish between a public and private interf
- Add plugin config item ShowReset (shows/hides "Reset Zoom" button)
- Fix transform params not being normalized when used together with param hash version
- Replace `immer` with `mutative`
- Fix renderer transparency check
- Add outlines improvements
- VolumeServer & "VolumeCIF": default to P 1 spacegroup
- Fix `ColorScale` for continuous case without offsets (broke in v4.13.0)
- Experimental: support for custom color themes in Sequence Panel
- Switch files.rcsb.org validation report URL to new endpoint /validation/view
- Improve picking of objects with too many groups, pick whole instance/object
- Add WebXR support
- Requires immersive AR/VR headset
- Supplements non-XR: enter/exit XR anytime and see (mostly) the same scene
- Add `Canvas3D.xr` for managing XR sessions
- Add `PointerHelper` for rendering XR input devices
- Add XR button to Viewer and Mesoscale Explorer
- Add XR button to render-structure in tests/browser
- Fix illumination denoising with transparency on transparent background
- Change the `to_mmCIF` function parameter from `structure` to `structures` to support either a single structure or an array of structures
- ModelServer and VolumeServer: add configurable robots.txt
- Adaptive parallel shader compilation
- Split shader compilation into linking and finalizing
- Start linking as early as possible and wait with finalizing to avoid blocking main thread
- Use of `KHR_parallel_shader_compile` extension when available to check status
- Add `ShaderManager` to compile shaders based on `Canvas3D` params and `Scene` content
- Draw `Scene` only when shaders are ready
- Fix incorrect animation loop handling in the screenshot code
## [v4.18.0] - 2025-06-08
- MolViewSpec extension:

View File

@@ -20,14 +20,19 @@ Generally, the command should be URL encoded, e.g., `a b` => `a%20b` (in JS, `en
- `center-camera` - Centers the camera
- `apply-snapshot=key` - Loads snapshots with the provided key
- `next-snapshot[=-1|1]` - Loads next/previous snapshot, the direction is optional and default to `1`
- `play-snapshots` - Starts playback of state snapshots
- `play-transition` - Plays an animation associated with the given snapshot
- `stop-animation` - Stops currently playing animation
- `focus-refs=ref1,ref2,...` - On click, focuses nodes with the provided refs
- `highlight-refs=ref1,ref2,...` - On mouse over, highlights the provided refs
- `query=...&lang=...&action=highlight,focus&focus-radius=...`
- `query` is an expression (e.g., `resn HEM` when using PyMol syntax)
- (optional) `lang` is one of `mol-script` (default), `pymol`, `vmd`, `jmol`
- (optional) `action` is an array of `highlight` (default), `focus` (multiple actions can be specified)
- (optional) `focus-radius` is extra distance applied when focusing the selection (default is `3`)
- Example: `[HEM](!query%3Dresn%20HEM%26lang%3Dpymol%26action%3Dhighlight%2Cfocus)` highlights or focuses the HEM residue (the command must be URL encoded because it contains spaces and possibly other special characters)
- `query` is an expression (e.g., `resn HEM` when using PyMol syntax)
- (optional) `lang` is one of `mol-script` (default), `pymol`, `vmd`, `jmol`
- (optional) `action` is an array of `highlight` (default), `focus` (multiple actions can be specified)
- (optional) `focus-radius` is extra distance applied when focusing the selection (default is `3`)
- Example: `[HEM](!query=resn%20HEM%26lang=pymol&action=highlight,focus)` highlights or focuses the HEM residue (the query must be URL encoded because it contains spaces and possibly other special characters)
- `play-audio=src`, `toggle-audio[=src]`, `stop-audio`, `pause-audio`, `dispose-audio` - Audio playback support
## Custom Content
@@ -36,11 +41,11 @@ Extends Markdown Image syntax to support expressions of the form `![alt](!c1=v1&
### Built-in Custom Content
- `color-swatch=color` - Renders a box with the provided color
- Color palettes:
- `color-palette-name=name` - Renders a gradient with the provided named color palette (see `mol-util/color/lists.ts` for supported color schemes)
- `color-palette-colors=color1,color2` - Renders a gradient with the provided colors
- `color-palette-width=CCS-value` - Specifies the width of the element, defaults to `150px`
- `color-palette-height=CCS-value` - Specified the height of the element, defaults to `0.5em`
- `color-palette-discrete` - Renders discrete color list instead of interpolating
- `color-palette-name=name` - Renders a gradient with the provided named color palette (see `mol-util/color/lists.ts` for supported color schemes)
- `color-palette-colors=color1,color2` - Renders a gradient with the provided colors
- `color-palette-width=CCS-value` - Specifies the width of the element, defaults to `150px`
- `color-palette-height=CCS-value` - Specified the height of the element, defaults to `0.5em`
- `color-palette-discrete` - Renders discrete color list instead of interpolating
## Example

View File

@@ -39,6 +39,7 @@ export default defineConfig([{
"comma-spacing": "off",
"space-infix-ops": "off",
"comma-dangle": "off",
quotes: ["warn", "single", { "allowTemplateLiterals": true, "avoidEscape": true }],
eqeqeq: ["error", "smart"],
"import/order": "off",
"no-eval": "warn",

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

24496
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "molstar",
"version": "5.0.0-dev.7",
"version": "5.0.0",
"description": "A comprehensive macromolecular library.",
"homepage": "https://github.com/molstar/molstar#readme",
"repository": {
@@ -11,7 +11,7 @@
"url": "https://github.com/molstar/molstar/issues"
},
"engines": {
"node": ">=18.0.0"
"node": ">=20.0.0"
},
"scripts": {
"lint": "eslint .",
@@ -74,7 +74,7 @@
"js"
],
"transform": {
"\\.ts$": "ts-jest"
"\\.ts$": "esbuild-jest-transform"
},
"moduleDirectories": [
"node_modules",
@@ -122,58 +122,60 @@
"Lukáš Polák <admin@lukaspolak.cz>",
"Chetan Mishra <chetan.s115@gmail.com>",
"Zach Charlop-Powers <zach.charlop.powers@gmail.com>",
"Kim Juho <juho_kim@outlook.com>"
"Kim Juho <juho_kim@outlook.com>",
"Victoria Doshchenko <doshchenko.victoria@gmail.com>"
],
"license": "MIT",
"devDependencies": {
"@types/cors": "^2.8.19",
"@types/gl": "^6.0.5",
"@types/jest": "^29.5.14",
"@types/jest": "^30.0.0",
"@types/pngjs": "^6.0.5",
"@types/react": "^18.3.23",
"@types/react": "^18.3.24",
"@types/react-dom": "^18.3.7",
"@typescript-eslint/eslint-plugin": "^8.34.0",
"@typescript-eslint/parser": "^8.34.0",
"@types/webxr": "^0.5.23",
"@typescript-eslint/eslint-plugin": "^8.44.1",
"@typescript-eslint/parser": "^8.44.1",
"benchmark": "^2.1.4",
"concurrently": "^9.1.2",
"concurrently": "^9.2.1",
"cpx2": "^8.0.0",
"css-loader": "^7.1.2",
"esbuild": "^0.25.5",
"esbuild": "^0.25.10",
"esbuild-jest-transform": "^2.0.1",
"esbuild-sass-plugin": "^3.3.1",
"eslint": "^9.29.0",
"fs-extra": "^11.3.0",
"eslint": "^9.36.0",
"fs-extra": "^11.3.2",
"http-server": "^14.1.1",
"jest": "^29.7.0",
"jest": "^30.2.0",
"jpeg-js": "^0.4.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"sass": "^1.89.1",
"sass": "^1.93.2",
"simple-git": "^3.28.0",
"ts-jest": "^29.3.4",
"tsc-alias": "^1.8.16",
"typescript": "^5.8.3"
"typescript": "^5.9.2"
},
"dependencies": {
"@types/argparse": "^2.0.17",
"@types/benchmark": "^2.1.5",
"@types/compression": "1.8.1",
"@types/express": "^5.0.3",
"@types/node": "^18.19.111",
"@types/node-fetch": "^2.6.12",
"@types/swagger-ui-dist": "3.30.5",
"@types/node": "^20.19.17",
"@types/node-fetch": "^2.6.13",
"@types/swagger-ui-dist": "3.30.6",
"argparse": "^2.0.1",
"compression": "^1.8.0",
"compression": "^1.8.1",
"cors": "^2.8.5",
"express": "^5.1.0",
"h264-mp4-encoder": "^1.0.12",
"immutable": "^5.1.2",
"immutable": "^5.1.3",
"io-ts": "^2.2.22",
"mutative": "^1.2.0",
"mutative": "^1.3.0",
"node-fetch": "^2.7.0",
"react-markdown": "^10.1.0",
"remark-gfm": "^4.0.1",
"rxjs": "^7.8.2",
"swagger-ui-dist": "^5.24.0",
"swagger-ui-dist": "^5.29.0",
"tslib": "^2.8.1",
"util.promisify": "^1.1.3"
},

View File

@@ -131,8 +131,8 @@ function getPaths(app) {
async function createBundle(app) {
const { name, kind } = app;
const { prefix, entry, outfile } = getPaths(app);
const NODE_ENV_PRD = isProduction || process.env.NODE_ENV === 'production';
const ctx = await esbuild.context({
entryPoints: [entry],
@@ -161,6 +161,7 @@ async function createBundle(app) {
color: true,
logLevel: 'info',
define: {
'process.env.NODE_ENV': JSON.stringify(NODE_ENV_PRD ? 'production' : 'development'),
'process.env.DEBUG': JSON.stringify(process.env.DEBUG || false),
__MOLSTAR_PLUGIN_VERSION__: JSON.stringify(VERSION),
__MOLSTAR_BUILD_TIMESTAMP__: `${TIMESTAMP}`,

View File

@@ -7,14 +7,14 @@
const git = require('simple-git');
const path = require('path');
const fs = require("fs");
const fse = require("fs-extra");
const fs = require('fs');
const fse = require('fs-extra');
const argparse = require('argparse');
const VERSION = require(path.resolve(__dirname, '../package.json')).version;
const MVS_STORIES_VERSION = require(path.resolve(__dirname, '../src/apps/mvs-stories/version.ts')).VERSION;
const remoteUrl = "https://github.com/molstar/molstar.github.io.git";
const remoteUrl = 'https://github.com/molstar/molstar.github.io.git';
const dataDir = path.resolve(__dirname, '../data/');
const buildDir = path.resolve(__dirname, '../build/');
const deployDir = path.resolve(__dirname, '../deploy/');

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022-2024 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2022-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -147,6 +147,7 @@ export class MesoscaleExplorer {
behaviors: [
PluginSpec.Behavior(PluginBehaviors.Camera.CameraAxisHelper),
PluginSpec.Behavior(PluginBehaviors.Camera.CameraControls),
PluginSpec.Behavior(PluginBehaviors.State.SnapshotControls),
PluginSpec.Behavior(MesoFocusLoci),
PluginSpec.Behavior(MesoSelectLoci),
@@ -252,6 +253,10 @@ export class MesoscaleExplorer {
},
cameraFog: { name: 'off', params: {} },
hiZ: { enabled: true },
xr: {
disablePostprocessing: false,
sceneRadiusInMeters: 0.75,
},
});
plugin.representation.structure.registry.clear();
@@ -261,7 +266,6 @@ export class MesoscaleExplorer {
image: true,
componentManager: false,
structureSelection: true,
behavior: true,
});
plugin.managers.lociLabels.clearProviders();

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2023-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -21,6 +21,7 @@ const Key = Binding.TriggerKey;
const DefaultMesoFocusLociBindings = {
clickCenter: Binding([
Trigger(B.Flag.Primary, M.create()),
Trigger(B.Flag.Trigger),
], 'Camera center', 'Click element using ${triggers}'),
clickCenterFocus: Binding([
Trigger(B.Flag.Secondary, M.create()),

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2023-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -24,7 +24,8 @@ const Trigger = Binding.Trigger;
const DefaultMesoSelectLociBindings = {
click: Binding([
Trigger(B.Flag.Primary, M.create())
Trigger(B.Flag.Primary, M.create()),
Trigger(B.Flag.Trigger),
], 'Click', 'Click element using ${triggers}'),
clickToggleSelect: Binding([
Trigger(B.Flag.Primary, M.create({ shift: true })),

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2022-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -18,12 +18,14 @@ import { ParamDefinition as PD } from '../../../mol-util/param-definition';
import { TuneSvg } from '../../../mol-plugin-ui/controls/icons';
import { RendererParams } from '../../../mol-gl/renderer';
import { TrackballControlsParams } from '../../../mol-canvas3d/controls/trackball';
import { XRManagerParams } from '../../../mol-canvas3d/helper/xr-manager';
const Spacer = () => <div style={{ height: '2em' }} />;
const ViewportParams = {
renderer: PD.Group(RendererParams),
trackball: PD.Group(TrackballControlsParams),
xr: PD.Group(XRManagerParams, { label: 'XR' }),
};
class ViewportSettingsUI extends CollapsableControls<{}, {}> {

View File

@@ -9,14 +9,14 @@ import { MVSData } from '../../extensions/mvs/mvs-data';
import type { MVSStoriesViewerModel } from './elements/viewer';
export type MVSStoriesCommand =
| { kind: 'load-mvs', format?: 'mvsj' | 'mvsx', url?: string, data?: MVSData | string | Uint8Array }
| { kind: 'load-mvs', format?: 'mvsj' | 'mvsx', url?: string, data?: MVSData | string | Uint8Array<ArrayBuffer> }
export class MVSStoriesContext {
commands = new BehaviorSubject<MVSStoriesCommand | undefined>(undefined);
state = {
viewers: new BehaviorSubject<{ name?: string, model: MVSStoriesViewerModel }[]>([]),
currentStoryData: new BehaviorSubject<string | Uint8Array | undefined>(undefined),
currentStoryData: new BehaviorSubject<string | Uint8Array<ArrayBuffer> | undefined>(undefined),
isLoading: new BehaviorSubject(false),
};

View File

@@ -66,7 +66,7 @@ export class MVSStoriesViewerModel extends PluginComponent {
loadedData = await loadMVSData(this.plugin, cmd.data, cmd.format ?? 'mvsj');
}
if (StringLike.is(loadedData) || loadedData instanceof Uint8Array) {
this.context.state.currentStoryData.next(loadedData as string | Uint8Array);
this.context.state.currentStoryData.next(loadedData as string | Uint8Array<ArrayBuffer>);
} else if (loadedData) {
this.context.state.currentStoryData.next(JSON.stringify(loadedData));
}

View File

@@ -28,7 +28,7 @@ export function loadFromURL(url: string, options?: { format: 'mvsx' | 'mvsj', co
}, 0);
}
export function loadFromData(data: MVSData | string | Uint8Array, options?: { format: 'mvsx' | 'mvsj', contextName?: string }) {
export function loadFromData(data: MVSData | string | Uint8Array<ArrayBuffer>, options?: { format: 'mvsx' | 'mvsj', contextName?: string }) {
setTimeout(() => {
getContext(options?.contextName).dispatch({
kind: 'load-mvs',

View File

@@ -182,6 +182,15 @@
max-width: 100%;
height: auto;
}
a {
text-decoration: none;
color: #1d4ed7;
&:hover {
text-decoration: underline;
}
}
}
@media (orientation:portrait) {

View File

@@ -540,7 +540,7 @@ export class Viewer {
/** Load MolViewSpec from `data`.
* If `format` is 'mvsj', `data` must be a string or a Uint8Array containing a UTF8-encoded string.
* If `format` is 'mvsx', `data` must be a Uint8Array or a string containing base64-encoded binary data prefixed with 'base64,'. */
loadMvsData(data: string | Uint8Array, format: 'mvsj' | 'mvsx', options?: { appendSnapshots?: boolean, keepCamera?: boolean, extensions?: MolstarLoadingExtension<any>[] }) {
loadMvsData(data: string | Uint8Array<ArrayBuffer>, format: 'mvsj' | 'mvsx', options?: { appendSnapshots?: boolean, keepCamera?: boolean, extensions?: MolstarLoadingExtension<any>[] }) {
return loadMVSData(this.plugin, data, format, options);
}
@@ -584,12 +584,12 @@ export interface VolumeIsovalueInfo {
export interface LoadTrajectoryParams {
model: { kind: 'model-url', url: string, format?: BuiltInTrajectoryFormat /* mmcif */, isBinary?: boolean }
| { kind: 'model-data', data: string | number[] | ArrayBuffer | Uint8Array, format?: BuiltInTrajectoryFormat /* mmcif */ }
| { kind: 'model-data', data: string | number[] | ArrayBuffer | Uint8Array<ArrayBuffer>, format?: BuiltInTrajectoryFormat /* mmcif */ }
| { kind: 'topology-url', url: string, format: BuiltInTopologyFormat, isBinary?: boolean }
| { kind: 'topology-data', data: string | number[] | ArrayBuffer | Uint8Array, format: BuiltInTopologyFormat },
| { kind: 'topology-data', data: string | number[] | ArrayBuffer | Uint8Array<ArrayBuffer>, format: BuiltInTopologyFormat },
modelLabel?: string,
coordinates: { kind: 'coordinates-url', url: string, format: BuiltInCoordinatesFormat, isBinary?: boolean }
| { kind: 'coordinates-data', data: string | number[] | ArrayBuffer | Uint8Array, format: BuiltInCoordinatesFormat },
| { kind: 'coordinates-data', data: string | number[] | ArrayBuffer | Uint8Array<ArrayBuffer>, format: BuiltInCoordinatesFormat },
coordinatesLabel?: string,
preset?: keyof PresetTrajectoryHierarchy
}

View File

@@ -29,7 +29,7 @@ async function readFile(ctx: RuntimeContext, filename: string): Promise<ReaderRe
const isGz = /\.gz$/i.test(filename);
if (filename.match(/\.bcif/)) {
let input = await readFileAsync(filename);
if (isGz) input = await unzipAsync(input);
if (isGz) input = await unzipAsync(input) as NonSharedBuffer;
return await CIF.parseBinary(new Uint8Array(input)).runInContext(ctx);
} else {
const data = isGz ? await unzipAsync(await readFileAsync(filename)) : await readFileAsync(filename);

View File

@@ -11,7 +11,7 @@
*/
import { ArgumentParser } from 'argparse';
import { treeSchemaToMarkdown, treeSchemaToString } from '../../extensions/mvs/tree/generic/tree-schema';
import { treeSchemaToMarkdown, treeSchemaToString } from '../../extensions/mvs/tree/generic/tree-validation';
import { MVSTreeSchema } from '../../extensions/mvs/tree/mvs/mvs-tree';

View File

@@ -22,7 +22,15 @@ const Steps = [
header: 'Animation Demo',
key: 'intro',
description: `### Molecular Animation
A story showcasing MolViewSpec animation capabilities.`,
A story showcasing MolViewSpec animation capabilities.
[\[**🔄 Replay Intro**\]](!play-transition)
[\[**⏵ Play Snapshots**\]](!play-snapshots)
[\[**⏹ Stop Animation**\]](!stop-animation)
[\[**➡️ Next Snapshot**\]](!next-snapshot)
`,
linger_duration_ms: 2000,
transition_duration_ms: 500,
state: (): Root => {
@@ -41,7 +49,7 @@ A story showcasing MolViewSpec animation capabilities.`,
custom: {
molstar_trackball: {
name: 'rock',
params: { speed: 1.5 },
params: { speed: 0.5 },
}
}
});
@@ -49,11 +57,23 @@ A story showcasing MolViewSpec animation capabilities.`,
kind: 'scalar',
ref: 'prims-opacity',
target_ref: 'prims',
duration_ms: 1000,
start_ms: 500,
duration_ms: 500,
property: 'label_opacity',
end: 1,
});
anim.interpolate({
kind: 'scalar',
ref: 'prims-opacity',
target_ref: 'prims',
start_ms: 1500,
duration_ms: 500,
property: 'label_opacity',
start: 1,
end: 0.66,
});
// Uncomment this to make 2nd frame render much faster
// It will cause shader compilation to happen during the 1st snapshot
@@ -91,12 +111,28 @@ A story showcasing MolViewSpec animation capabilities.`,
const builder = createMVSBuilder();
const _1cbs = structure(builder, '1cbs');
const [poly,] = polymer(_1cbs, { color: Colors['1cbs'] });
const [poly, repr] = polymer(_1cbs, { color: Colors['1cbs'] });
repr.colorFromSource({
ref: 'residue_colors',
schema: 'residue',
category_name: 'atom_site',
field_name: 'label_comp_id',
palette: {
kind: 'categorical',
missing_color: 'white',
colors: {
ALA: 'red',
ILE: 'white',
LYS: 'white',
}
}
});
const surface = poly.representation({
type: 'surface',
surface_type: 'gaussian',
});
}).opacity({ opacity: 0.33 });
_1cbs.component({ selector: 'ligand' })
.transform({
@@ -119,14 +155,32 @@ A story showcasing MolViewSpec animation capabilities.`,
anim.interpolate({
kind: 'scalar',
ref: 'clip-transition',
target_ref: 'clip',
duration_ms: 2000,
duration_ms: 500,
property: ['point', 2],
end: 55,
easing: 'sin-in',
});
anim.interpolate({
kind: 'scalar',
target_ref: 'clip',
start_ms: 600,
duration_ms: 800,
property: ['point', 2],
end: 0,
easing: 'sin-out',
});
anim.interpolate({
kind: 'scalar',
target_ref: 'clip',
start_ms: 1500,
duration_ms: 500,
property: ['point', 2],
end: 55,
});
anim.interpolate({
kind: 'vec3',
target_ref: 'xform',
@@ -152,6 +206,20 @@ A story showcasing MolViewSpec animation capabilities.`,
end: Colors['ligand-docked'],
});
anim.interpolate({
kind: 'color',
target_ref: 'residue_colors',
duration_ms: 2000,
property: ['palette', 'colors'],
start: {
ALA: 'yellow',
},
end: {
ILE: 'blue',
LYS: 'purple',
},
});
return builder;
},
camera: {
@@ -182,7 +250,7 @@ A story showcasing MolViewSpec animation capabilities.`,
ref: 'repr',
type: 'ball_and_stick',
custom: {
molstar_reprepresentation_params: {
molstar_representation_params: {
emissive: 0,
}
}
@@ -209,7 +277,7 @@ A story showcasing MolViewSpec animation capabilities.`,
kind: 'scalar',
target_ref: 'repr',
duration_ms: 1000,
property: ['custom', 'molstar_reprepresentation_params', 'emissive'],
property: ['custom', 'molstar_representation_params', 'emissive'],
end: 0.2,
});
@@ -273,10 +341,12 @@ function structure(builder: Root, id: string): MVSStructure {
.modelStructure();
}
function polymer(structure: MVSStructure, options: { color: ColorT }) {
function polymer(structure: MVSStructure, options?: { color?: ColorT }) {
const component = structure.component({ selector: { label_asym_id: 'A' } });
const reprensentation = component.representation({ type: 'cartoon' });
reprensentation.color({ color: options.color });
if (options?.color) {
reprensentation.color({ color: options.color });
}
return [component, reprensentation] as const;
}
@@ -294,6 +364,21 @@ export function buildStory(): MVSData_States {
molstar_postprocessing: {
enable_outline: true,
enable_ssao: true,
background: {
name: 'horizontalGradient',
params: {
topColor: 0x777777,
bottomColor: 0xffffff,
}
},
// Example with background image:
// background: {
// name: 'image',
// params: {
// // URL can also be filename in MVSX archive
// source: { name: 'url', params: 'URL' }
// }
// }
}
}
});

File diff suppressed because one or more lines are too long

View File

@@ -7,9 +7,13 @@
import { buildStory as kinase } from './kinase';
import { buildStory as tbp } from './tbp';
import { buildStory as animation } from './animation';
import { buildStory as audio } from './audio';
import { buildStory as motm1 } from './motm1';
export const Stories = [
{ id: 'kinase', name: 'BCR-ABL: A Kinase Out of Control', buildStory: kinase },
{ id: 'tata', name: 'TATA-Binding Protein and its Role in Transcription Initiation ', buildStory: tbp },
{ id: 'animation', name: 'Molecular Animation', buildStory: animation },
{ id: 'motm1', name: 'RCSB PDB Molecule of the Month #1', buildStory: motm1 },
{ id: 'animation-example', name: 'Molecular Animation Example', buildStory: animation },
{ id: 'audio-example', name: 'Audio Playback Example', buildStory: audio },
] as const;

File diff suppressed because it is too large Load Diff

View File

@@ -56,7 +56,7 @@ export class GlbExporter extends MeshExporter<GlbData> {
private materialMap = new Map<string, number>();
private accessors: Record<string, any>[] = [];
private bufferViews: Record<string, any>[] = [];
private binaryBuffer: ArrayBuffer[] = [];
private binaryBuffer: ArrayBufferLike[] = [];
private byteOffset = 0;
private centerTransform: Mat4;
@@ -72,7 +72,7 @@ export class GlbExporter extends MeshExporter<GlbData> {
return [min, max];
}
private addBuffer(buffer: ArrayBuffer, componentType: number, type: string, count: number, target: number, min?: any, max?: any, normalized?: boolean) {
private addBuffer(buffer: ArrayBufferLike, componentType: number, type: string, count: number, target: number, min?: any, max?: any, normalized?: boolean) {
this.binaryBuffer.push(buffer);
const bufferViewOffset = this.bufferViews.length;
@@ -304,7 +304,7 @@ export class GlbExporter extends MeshExporter<GlbData> {
materials: this.materials
};
const createChunk = (chunkType: number, data: ArrayBuffer[], byteLength: number, padChar: number): [ArrayBuffer[], number] => {
const createChunk = (chunkType: number, data: ArrayBufferLike[], byteLength: number, padChar: number): [ArrayBufferLike[], number] => {
let padding = null;
if (byteLength % 4 !== 0) {
const pad = 4 - (byteLength % 4);

View File

@@ -305,7 +305,7 @@ function PlotInteractivity({ drawing, interactity }: { drawing: MAPairwiseMetric
let labelNode: ReactNode | undefined;
if (label) {
const labelStyle: CSSProperties | undefined = label ? { fontSize: '45px', fill: 'black', fontWeight: 'bold', pointerEvents: 'none', userSelect: 'none' } : undefined;
let x: number, y: number, anchor: string;
let x: number, y: number, anchor: 'start' | 'end';
if (crosshairOffset![0] < PlotSize / 2) {
x = PlotOffset + crosshairOffset![0] + 20;
anchor = 'start';

View File

@@ -39,7 +39,7 @@ function _exportHierarchy(plugin: PluginContext, options?: { format?: 'cif' | 'b
const format = options?.format ?? 'cif';
const { structures } = plugin.managers.structure.hierarchy.current;
const files: [name: string, data: string | Uint8Array][] = [];
const files: [name: string, data: string | Uint8Array<ArrayBuffer>][] = [];
const entryMap = new Map<string, number>();
for (const _s of structures) {
@@ -80,7 +80,7 @@ function _exportHierarchy(plugin: PluginContext, options?: { format?: 'cif' | 'b
if (files.length === 1) {
download(new Blob([files[0][1]]), files[0][0]);
} else if (files.length > 1) {
const zipData: Record<string, Uint8Array> = {};
const zipData: Record<string, Uint8Array<ArrayBuffer>> = {};
for (const [fn, data] of files) {
if (data instanceof Uint8Array) {
zipData[fn] = data;

View File

@@ -25,7 +25,7 @@ export interface Mp4EncoderParams<A extends PluginStateAnimation = PluginStateAn
quantizationParameter?: number
}
export async function encodeMp4Animation<A extends PluginStateAnimation>(plugin: PluginContext, ctx: RuntimeContext, params: Mp4EncoderParams<A>) {
export async function encodeMp4Animation<A extends PluginStateAnimation>(plugin: PluginContext, ctx: RuntimeContext, params: Mp4EncoderParams<A>): Promise<Uint8Array<ArrayBuffer>> {
await ctx.update({ message: 'Initializing...', isIndeterminate: true });
validateViewport(params);
@@ -88,7 +88,7 @@ export async function encodeMp4Animation<A extends PluginStateAnimation>(plugin:
stoppedAnimation = true;
encoder.finalize();
finalized = true;
return encoder.FS.readFile(encoder.outputFilename);
return encoder.FS.readFile(encoder.outputFilename) as Uint8Array<ArrayBuffer>;
} finally {
if (finalized) encoder.delete();
if (params.customBackground !== void 0) {

View File

@@ -15,7 +15,7 @@ import { Mp4AnimationParams, Mp4Controls } from './controls';
interface State {
busy?: boolean,
data?: { movie: Uint8Array, filename: string };
data?: { movie: Uint8Array<ArrayBuffer>, filename: string };
}
export class Mp4EncoderUI extends CollapsableControls<{}, State> {

View File

@@ -1 +1 @@
Find the MVS extension documentation [here](../../../docs/extensions/mvs/README.md).
Please refer to the standalone documentation [here](https://molstar.org/mol-view-spec-docs/).

View File

@@ -8,6 +8,7 @@
import { Camera } from '../../mol-canvas3d/camera';
import { CameraFogParams, Canvas3DParams, Canvas3DProps, DefaultCanvas3DParams } from '../../mol-canvas3d/canvas3d';
import { TrackballControlsParams } from '../../mol-canvas3d/controls/trackball';
import { BackgroundParams } from '../../mol-canvas3d/passes/background';
import { BloomParams } from '../../mol-canvas3d/passes/bloom';
import { DofParams } from '../../mol-canvas3d/passes/dof';
import { OutlineParams } from '../../mol-canvas3d/passes/outline';
@@ -21,6 +22,7 @@ import { PluginState } from '../../mol-plugin/state';
import { StateObjectSelector } from '../../mol-state';
import { fovAdjustedPosition } from '../../mol-util/camera';
import { ColorNames } from '../../mol-util/color/names';
import { deepClone } from '../../mol-util/object';
import { ParamDefinition } from '../../mol-util/param-definition';
import { decodeColor } from './helpers/utils';
import { MolstarLoadingContext } from './load';
@@ -132,6 +134,11 @@ function optionalParams(enable: boolean | undefined, values: any, params: ParamD
return fallback;
}
function normalizeBackground(variant: any, prev: any): any {
if (!variant) return prev;
return ParamDefinition.normalizeParams(BackgroundParams, { variant }, 'children');
}
/** Create a deep copy of `oldCanvasProps` with values modified according to a canvas node params. */
export function modifyCanvasProps(oldCanvasProps: Canvas3DProps, canvasNode: MolstarNode<'canvas'> | undefined, animationNode: MVSAnimationNode<'animation'> | undefined): Canvas3DProps {
const params = canvasNode?.params;
@@ -157,6 +164,8 @@ export function modifyCanvasProps(oldCanvasProps: Canvas3DProps, canvasNode: Mol
const bloom = molstar_postprocessing?.enable_bloom;
const bloomParams = molstar_postprocessing?.bloom_params;
const background = molstar_postprocessing?.background;
const trackballAnimation = animationNode?.custom?.molstar_trackball;
const trackballAnimationName = trackballAnimation?.name;
const trackballAnimationParams = trackballAnimation?.params ?? {};
@@ -170,6 +179,7 @@ export function modifyCanvasProps(oldCanvasProps: Canvas3DProps, canvasNode: Mol
occlusion: optionalParams(occlusion, occlusionParams, SsaoParams, oldCanvasProps.postprocessing.occlusion),
dof: optionalParams(dof, dofParams, DofParams, oldCanvasProps.postprocessing.dof),
bloom: optionalParams(bloom, bloomParams, BloomParams, oldCanvasProps.postprocessing.bloom),
background: normalizeBackground(background, oldCanvasProps.postprocessing.background),
},
cameraFog: optionalParams(fog, fogParams, CameraFogParams, oldCanvasProps.cameraFog),
renderer: {
@@ -200,13 +210,14 @@ export function resetCanvasProps(plugin: PluginContext) {
...old,
postprocessing: {
...old,
outline: DefaultCanvas3DParams.postprocessing.outline,
shadow: DefaultCanvas3DParams.postprocessing.shadow,
occlusion: DefaultCanvas3DParams.postprocessing.occlusion,
dof: DefaultCanvas3DParams.postprocessing.dof,
bloom: DefaultCanvas3DParams.postprocessing.bloom,
outline: deepClone(DefaultCanvas3DParams.postprocessing.outline),
shadow: deepClone(DefaultCanvas3DParams.postprocessing.shadow),
occlusion: deepClone(DefaultCanvas3DParams.postprocessing.occlusion),
dof: deepClone(DefaultCanvas3DParams.postprocessing.dof),
bloom: deepClone(DefaultCanvas3DParams.postprocessing.bloom),
background: deepClone(DefaultCanvas3DParams.postprocessing.background),
},
cameraFog: DefaultCanvas3DParams.cameraFog,
cameraFog: deepClone(DefaultCanvas3DParams.cameraFog),
trackball: {
...old?.trackball,
animate: { name: 'off', params: {} },

View File

@@ -112,16 +112,16 @@ export const MVSXFormatProvider: DataFormatProvider<{}, StateObjectRef<Mvs>, any
* add all contained files to `plugin`'s asset manager,
* and parse the main file in the archive as MVSJ.
* Return parsed MVS data and `sourceUrl` for resolution of relative URIs. */
export async function loadMVSX(plugin: PluginContext, runtimeCtx: RuntimeContext, data: Uint8Array, mainFilePath: string = 'index.mvsj'): Promise<{ mvsData: MVSData, sourceUrl: string }> {
export async function loadMVSX(plugin: PluginContext, runtimeCtx: RuntimeContext, data: Uint8Array<ArrayBuffer>, mainFilePath: string = 'index.mvsj'): Promise<{ mvsData: MVSData, sourceUrl: string }> {
// Ensure at most one generation of MVSX file assets exists in the asset manager.
// Hopefully, this is a reasonable compromise to ensure MVSX files work in multi-snapshot
// states.
clearMVSXFileAssets(plugin);
const archiveId = `ni,MurmurHash3_128;${murmurHash3_128_fromBytes(data, 42)}`;
let files: { [path: string]: Uint8Array };
let files: { [path: string]: Uint8Array<ArrayBuffer> };
try {
files = await unzip(runtimeCtx, data) as typeof files;
files = await unzip(runtimeCtx, data.buffer) as typeof files;
} catch (err) {
plugin.log.error('Invalid MVSX file');
throw err;
@@ -138,7 +138,7 @@ export async function loadMVSX(plugin: PluginContext, runtimeCtx: RuntimeContext
return { mvsData, sourceUrl };
}
export async function loadMVSData(plugin: PluginContext, data: MVSData | StringLike | Uint8Array, format: 'mvsj' | 'mvsx', options?: MVSLoadOptions) {
export async function loadMVSData(plugin: PluginContext, data: MVSData | StringLike | Uint8Array<ArrayBuffer>, format: 'mvsj' | 'mvsx', options?: MVSLoadOptions) {
if (typeof data === 'string' && data.startsWith('base64')) {
data = Uint8Array.from(atob(data.substring(7)), c => c.charCodeAt(0)); // Decode base64 string to Uint8Array
}
@@ -160,7 +160,7 @@ export async function loadMVSData(plugin: PluginContext, data: MVSData | StringL
throw new Error("loadMvsData: if `format` is 'mvsx', then `data` must be a Uint8Array or a base64-encoded string prefixed with 'base64,'.");
}
await plugin.runTask(Task.create('Load MVSX file', async ctx => {
const parsed = await loadMVSX(plugin, ctx, data as Uint8Array);
const parsed = await loadMVSX(plugin, ctx, data as Uint8Array<ArrayBuffer>);
await loadMVS(plugin, parsed.mvsData, { sanityChecks: true, ...options, sourceUrl: parsed.sourceUrl });
}));
} else {
@@ -190,7 +190,7 @@ function arcpUri(archiveId: string, path: string): string {
/** Add a URL asset to asset manager.
* Skip if an asset with the same URL already exists. */
function ensureUrlAsset(manager: AssetManager, url: string, data: Uint8Array, options?: { isFile?: boolean }) {
function ensureUrlAsset(manager: AssetManager, url: string, data: Uint8Array<ArrayBuffer>, options?: { isFile?: boolean }) {
const asset = Asset.getUrlAsset(manager, url);
if (!manager.has(asset)) {
const filename = url.split('/').pop() ?? 'file';

View File

@@ -149,6 +149,7 @@ export const MVSBuildPrimitiveShape = MVSTransform({
const context: PrimitiveBuilderContext = { ...a.data, structureRefs };
const snapshotKey = { snapshotKey: { ...SnapshotKey, defaultValue: a.data.options?.snapshot_key ?? '' } };
const markdownCommands = { markdownCommands: { ...MarkdownCommands, defaultValue: a.data.node?.custom?.molstar_markdown_commands } };
const label = capitalize(params.kind);
if (params.kind === 'mesh') {
@@ -161,6 +162,7 @@ export const MVSBuildPrimitiveShape = MVSTransform({
params: {
...PD.withDefaults(Mesh.Params, { alpha: a.data.options?.opacity ?? 1, ...customMeshParams }),
...snapshotKey,
...markdownCommands,
},
getShape: (_, data, __, prev: any) => buildPrimitiveMesh(data, prev?.geometry),
geometryUtils: Mesh.Utils,
@@ -185,6 +187,7 @@ export const MVSBuildPrimitiveShape = MVSTransform({
...customLabelParams,
}),
...snapshotKey,
...markdownCommands,
},
getShape: (_, data, props, prev: any) => buildPrimitiveLabels(data, prev?.geometry, props),
geometryUtils: Text.Utils,
@@ -199,6 +202,7 @@ export const MVSBuildPrimitiveShape = MVSTransform({
params: {
...PD.withDefaults(Lines.Params, { alpha: a.data.options?.opacity ?? 1, ...customLineParams }),
...snapshotKey,
...markdownCommands,
},
getShape: (_, data, __, prev: any) => buildPrimitiveLines(data, prev?.geometry),
geometryUtils: Lines.Utils,
@@ -227,7 +231,7 @@ export const MVSShapeRepresentation3D = MVSTransform({
const repr = ShapeRepresentation(a.data.getShape, a.data.geometryUtils);
await repr.createOrUpdate(props, a.data.data).runInContext(ctx);
const pickable = !!(params as any).snapshotKey?.trim();
const pickable = !!(params as any).snapshotKey?.trim() || !!(params as any).markdownCommands;
if (pickable) {
repr.setState({ pickable, markerActions: MarkerActions.Highlighting });
}
@@ -241,7 +245,7 @@ export const MVSShapeRepresentation3D = MVSTransform({
await b.data.repr.createOrUpdate(props, a.data.data).runInContext(ctx);
b.data.sourceData = a.data;
const pickable = !!(newParams as any).snapshotKey?.trim();
const pickable = !!(newParams as any).snapshotKey?.trim() || !!(newParams as any).markdownCommands;
if (pickable) {
b.data.repr.setState({ pickable, markerActions: MarkerActions.Highlighting });
}
@@ -252,6 +256,7 @@ export const MVSShapeRepresentation3D = MVSTransform({
});
const SnapshotKey = PD.Text('', { isEssential: true, disableInteractiveUpdates: true, description: 'Activate the snapshot with the provided key when clicking on the label' });
const MarkdownCommands = PD.Value<any>(undefined, { isHidden: true });
/* **************************************************** */

View File

@@ -10,9 +10,9 @@ import { MVSData } from './mvs-data';
/**
* Creates an MVSX zip file with from the provided data and assets
*/
export async function createMVSX(data: MVSData, assets: { name: string, content: string | Uint8Array }[]) {
export async function createMVSX(data: MVSData, assets: { name: string, content: string | Uint8Array<ArrayBuffer> }[]) {
const encoder = new TextEncoder();
const files: Record<string, Uint8Array> = {
const files: Record<string, Uint8Array<ArrayBuffer>> = {
'index.mvsj': encoder.encode(JSON.stringify(data)),
};
for (const asset of assets) {

View File

@@ -5,20 +5,22 @@
* @author Ludovic Autin <ludovic.autin@gmail.com>
*/
import { create } from 'mutative';
import { Snapshot } from '../mvs-data';
import { Tree } from '../tree/generic/tree-schema';
import { clamp, lerp } from '../../../mol-math/interpolate';
import { MVSAnimationEasing, MVSAnimationNode, MVSAnimationSchema } from '../tree/animation/animation-tree';
import { MVSTree } from '../tree/mvs/mvs-tree';
import { SortedArray } from '../../../mol-data/int';
import * as EasingFns from '../../../mol-math/easing';
import { addDefaults } from '../tree/generic/tree-utils';
import { RuntimeContext } from '../../../mol-task';
import { clamp, lerp } from '../../../mol-math/interpolate';
import { EPSILON, Mat3, Mat4, Quat, Vec3 } from '../../../mol-math/linear-algebra';
import { RuntimeContext } from '../../../mol-task';
import { deepEqual } from '../../../mol-util';
import { Color } from '../../../mol-util/color';
import { decodeColor } from '../../../mol-util/color/utils';
import { produce } from '../../../mol-util/produce';
import { makeContinuousPaletteCheckpoints, MVSContinuousPaletteProps, MVSDiscretePaletteProps } from '../components/annotation-color-theme';
import { palettePropsFromMVSPalette } from '../load-helpers';
import { SortedArray } from '../../../mol-data/int';
import { Snapshot } from '../mvs-data';
import { MVSAnimationEasing, MVSAnimationNode, MVSAnimationSchema } from '../tree/animation/animation-tree';
import { Tree } from '../tree/generic/tree-schema';
import { addDefaults } from '../tree/generic/tree-utils';
import { MVSTree } from '../tree/mvs/mvs-tree';
import { ColorT } from '../tree/mvs/param-types';
export async function generateStateTransition(ctx: RuntimeContext, snapshot: Snapshot, snapshotIndex: number, snapshotCount: number) {
@@ -33,17 +35,27 @@ export async function generateStateTransition(ctx: RuntimeContext, snapshot: Sna
...transitions.map(t => (t.params.start_ms ?? 0) + t.params.duration_ms)
);
const frames: MVSTree[] = [];
const frames: [tree: MVSTree, time: number][] = [];
const dt = tree.params?.frame_time_ms ?? (1000 / 60);
const N = Math.ceil(duration / dt);
const nodeMap = makeNodeMap(snapshot.root, new Map(), []);
const cache = new Map<any, InterpolationCacheEntry>();
const transitionGroups = groupTranstions(transitions);
let prevRoot: MVSTree | undefined;
for (let i = 0; i <= N; i++) {
const t = i * dt;
const root = createSnapshot(snapshot.root, transitions, t, cache, nodeMap);
frames.push(root);
const root = createSnapshot(snapshot.root, transitionGroups, t, cache, nodeMap);
if (root === prevRoot || (prevRoot && deepEqual(root, prevRoot))) {
frames[frames.length - 1][1] += dt;
} else {
frames.push([root, dt]);
}
prevRoot = root;
if (ctx.shouldUpdate) {
await ctx.update({ message: `Generating transition for snapshot ${snapshotIndex + 1}/${snapshotCount}`, current: i + 1, max: N });
@@ -77,74 +89,96 @@ const EasingFnMap: Record<MVSAnimationEasing, (t: number) => number> = {
interface InterpolationCacheEntry {
paletteFn?: (value: number) => Color,
startColor?: Color | Record<number | string, Color>,
endColor?: Color | Record<number | string, Color>,
rotation?: { axis: Vec3, angle: number, start: Quat, end: Quat },
}
function createSnapshot(tree: MVSTree, transitions: MVSAnimationNode<'interpolate'>[], time: number, cache: Map<any, InterpolationCacheEntry>, nodeMap: Map<string, (string | number)[]>) {
return create(tree, (draft) => {
for (const transition of transitions) {
const nodePath = nodeMap.get(transition.params.target_ref);
function getTransitionKey(transition: MVSAnimationNode<'interpolate'>) {
const prop = transition.params.property;
if (Array.isArray(prop)) {
return `${transition.params.target_ref}:${prop.join('.')}`;
}
return `${transition.params.target_ref}:${prop}`;
}
function groupTranstions(transitions: MVSAnimationNode<'interpolate'>[]) {
const map = new Map<string, MVSAnimationNode<'interpolate'>[]>();
const groups: MVSAnimationNode<'interpolate'>[][] = [];
for (const t of transitions) {
const key = getTransitionKey(t);
if (!map.has(key)) {
const group: MVSAnimationNode<'interpolate'>[] = [];
map.set(key, group);
groups.push(group);
}
map.get(key)!.push(t);
}
for (const group of groups) {
group.sort((a, b) => {
const s = (a.params.start_ms ?? 0) - (b.params.start_ms ?? 0);
if (s !== 0) return s;
return a.params.duration_ms - b.params.duration_ms;
});
}
return groups;
}
function createSnapshot(tree: MVSTree, transitionGroups: MVSAnimationNode<'interpolate'>[][], time: number, cache: Map<any, InterpolationCacheEntry>, nodeMap: Map<string, (string | number)[]>) {
let modified = false;
const ret = produce(tree, (draft) => {
for (const transitionGroup of transitionGroups) {
const pivot = transitionGroup[0];
const nodePath = nodeMap.get(pivot.params.target_ref);
if (!nodePath) continue;
const node = select(draft, nodePath, 0);
const target = transition.params.property[0] === 'custom' ? node?.custom : node?.params;
const target = pivot.params.property[0] === 'custom' ? node?.custom : node?.params;
if (!target) continue;
const offset = pivot.params.property[0] === 'custom' ? 1 : 0;
let transition: MVSAnimationNode<'interpolate'> = pivot;
let previous: MVSAnimationNode<'interpolate'> | undefined;
for (let i = transitionGroup.length - 1; i > 0; i--) {
const current = transitionGroup[i];
const currentStart = current.params.start_ms ?? 0;
if (time >= currentStart) {
transition = current;
previous = i > 0 ? transitionGroup[i - 1] : undefined;
break;
}
}
if (!cache.has(transition)) {
cache.set(transition, {});
}
const cacheEntry: InterpolationCacheEntry = cache.get(transition)!;
const startTime = transition.params.start_ms ?? 0;
let t = clamp((time - startTime) / transition.params.duration_ms, 0, 1);
if (transition.params.kind === 'transform_matrix') {
processTransformMatrix(transition, target, t, cacheEntry);
continue;
}
t = applyFrequency(t, transition.params.frequency ?? 1, !!transition.params.alternate_direction);
const offset = transition.params.property[0] === 'custom' ? 1 : 0;
const startBase = transition.params.start ?? select(target, transition.params.property, offset);
if (transition.params.kind === 'color' && !cacheEntry.paletteFn) {
cacheEntry.paletteFn = makePaletteFunction(transition, startBase, transition.params.end as ColorT | undefined);
}
const paletteFn = cacheEntry.paletteFn!;
const startValue: any = transition.params.kind === 'color'
? Color.toHexStyle(paletteFn(0))
: startBase;
const endValue: any = transition.params.kind === 'color'
? Color.toHexStyle(paletteFn(1))
: transition.params.end;
if (time <= startTime) {
assign(target, transition.params.property, startValue, offset);
continue;
}
const easing = EasingFnMap[transition.params.easing ?? 'linear'] ?? EasingFnMap['linear'];
t = easing(t);
const startTime: number = transition.params.start_ms ?? 0;
const durationMs: number = transition.params.duration_ms ?? 0;
const t = (time - startTime) / durationMs;
let next: any;
if (transition.params.kind === 'scalar') {
next = interpolateScalars(startValue, endValue, t, transition.params.noise_magnitude ?? 0);
} else if (transition.params.kind === 'vec3') {
next = interpolateVectors(startValue, endValue, t, transition.params.noise_magnitude ?? 0, !!transition.params.spherical);
} else if (transition.params.kind === 'rotation_matrix') {
next = interpolateRotation(startValue, endValue, t, transition.params.noise_magnitude ?? 0, cacheEntry);
} else if (transition.params.kind === 'color') {
const color = paletteFn(t);
next = Color.toHexStyle(color);
if (transition.params.kind === 'transform_matrix') {
next = processTransformMatrix(transition, target, clamp(t, 0, 1), cacheEntry, offset, previous);
} else {
next = processScalarLike(transition, target, t, cacheEntry, offset, previous);
}
if (next === undefined) {
continue;
}
modified = true;
assign(target, transition.params.property, next, offset);
}
});
return modified ? ret : tree;
}
function applyFrequency(t: number, frequency: number, alternate: boolean) {
@@ -163,6 +197,53 @@ function applyFrequency(t: number, frequency: number, alternate: boolean) {
return v;
}
function getPreviousScalarEnd(previous: MVSAnimationNode<'interpolate'> | undefined) {
if (!previous || previous.params.kind === 'transform_matrix') return undefined;
return previous.params.end;
}
function processScalarLike(transition: MVSAnimationNode<'interpolate'>, target: any, time: number, cacheEntry: InterpolationCacheEntry, offset: number, previous: MVSAnimationNode<'interpolate'> | undefined) {
if (transition.params.kind === 'transform_matrix') return;
if (previous && previous.params.kind === 'transform_matrix') return;
const startValue = transition.params.start ?? getPreviousScalarEnd(previous) ?? select(target, transition.params.property, offset);
if (transition.params.kind === 'color' && !cacheEntry.paletteFn) {
cacheEntry.paletteFn = makePaletteFunction(transition);
}
const endValue: any = transition.params.end;
if (time <= 0) return startValue;
else if (time >= 1 - EPSILON && !transition.params.alternate_direction && transition.params.kind !== 'color') return endValue;
let t = clamp(time, 0, 1);
t = applyFrequency(t, transition.params.frequency ?? 1, !!transition.params.alternate_direction);
const easing = EasingFnMap[transition.params.easing ?? 'linear'] ?? EasingFnMap['linear'];
t = easing(t);
if (transition.params.kind === 'scalar') {
return interpolateScalars(startValue, endValue, t, transition.params.noise_magnitude ?? 0, !!transition.params.discrete);
} else if (transition.params.kind === 'vec3') {
return interpolateVectors(startValue, endValue, t, transition.params.noise_magnitude ?? 0, !!transition.params.spherical);
} else if (transition.params.kind === 'rotation_matrix') {
return interpolateRotation(startValue, endValue, t, transition.params.noise_magnitude ?? 0, cacheEntry);
} else if (transition.params.kind === 'color') {
if (cacheEntry.paletteFn) {
const color = cacheEntry.paletteFn(t);
return Color.toHexStyle(color);
}
const baseColors = typeof startValue === 'object' ? select(target, transition.params.property, offset) : undefined;
return interpolateColors(startValue, endValue, t, cacheEntry, baseColors);
}
}
function getPreviousMatrixEnd(previous: MVSAnimationNode<'interpolate'> | undefined, prop: 'rotation_start' | 'translation_start' | 'scale_start') {
if (!previous || previous.params.kind !== 'transform_matrix') return undefined;
return previous.params[prop];
}
const TransformState = {
pivotTranslation: Mat4(),
pivotTranslationInv: Mat4(),
@@ -172,31 +253,41 @@ const TransformState = {
pivotNeg: Vec3(),
temp: Mat4(),
};
function processTransformMatrix(transition: MVSAnimationNode<'interpolate'>, target: any, time: number, cache: InterpolationCacheEntry) {
function processTransformMatrix(transition: MVSAnimationNode<'interpolate'>, target: any, time: number, cache: InterpolationCacheEntry, offset: number, previous: MVSAnimationNode<'interpolate'> | undefined) {
if (transition.params.kind !== 'transform_matrix') return;
if (previous && previous.params.kind !== 'transform_matrix') return;
const offset = transition.params.property[0] === 'custom' ? 1 : 0;
const transform = select(target, transition.params.property, offset) ?? Mat4.identity();
const startRotation = transition.params.rotation_start ?? Mat3.fromMat4(Mat3(), transform);
const startTranslation = transition.params.translation_start ?? Mat4.getTranslation(Vec3(), transform);
const startScale = transition.params.scale_start ?? Mat4.getScaling(Vec3(), transform);
const startRotation = transition.params.rotation_start ?? getPreviousMatrixEnd(previous, 'rotation_start') ?? Mat3.fromMat4(Mat3(), transform);
const startTranslation = transition.params.translation_start ?? getPreviousMatrixEnd(previous, 'translation_start') ?? Mat4.getTranslation(Vec3(), transform);
const startScale = transition.params.scale_start ?? getPreviousMatrixEnd(previous, 'scale_start') ?? Mat4.getScaling(Vec3(), transform);
const endRotation = transition.params.rotation_end;
const endTranslation = transition.params.translation_end;
const endScale = transition.params.scale_end;
let t = applyFrequency(time, transition.params.rotation_frequency ?? 1, !!transition.params.rotation_alternate_direction);
let easing = EasingFnMap[transition.params.rotation_easing ?? 'linear'] ?? EasingFnMap['linear'];
const rotation = interpolateRotation(startRotation as Mat3, endRotation as Mat3, easing(t), transition.params.rotation_noise_magnitude ?? 0, cache);
let rotation, translation, scale;
t = applyFrequency(time, transition.params.translation_frequency ?? 1, !!transition.params.translation_alternate_direction);
easing = EasingFnMap[transition.params.translation_easing ?? 'linear'] ?? EasingFnMap['linear'];
const translation = interpolateVec3(startTranslation as Vec3, endTranslation as Vec3 | undefined, easing(t), transition.params.translation_noise_magnitude ?? 0, false);
if (time <= 0) {
rotation = startRotation as Mat3;
translation = startTranslation as Vec3;
scale = startScale as Vec3;
} else {
const clampedTime = clamp(time, 0, 1);
t = applyFrequency(time, transition.params.scale_frequency ?? 1, !!transition.params.scale_alternate_direction);
easing = EasingFnMap[transition.params.scale_easing ?? 'linear'] ?? EasingFnMap['linear'];
const scale = interpolateVec3(startScale as Vec3, endScale as Vec3 | undefined, easing(t), transition.params.scale_noise_magnitude ?? 0, false);
let t = applyFrequency(clampedTime, transition.params.rotation_frequency ?? 1, !!transition.params.rotation_alternate_direction);
let easing = EasingFnMap[transition.params.rotation_easing ?? 'linear'] ?? EasingFnMap['linear'];
rotation = interpolateRotation(startRotation as Mat3, endRotation as Mat3, easing(t), transition.params.rotation_noise_magnitude ?? 0, cache);
t = applyFrequency(clampedTime, transition.params.translation_frequency ?? 1, !!transition.params.translation_alternate_direction);
easing = EasingFnMap[transition.params.translation_easing ?? 'linear'] ?? EasingFnMap['linear'];
translation = interpolateVec3(startTranslation as Vec3, endTranslation as Vec3 | undefined, easing(t), transition.params.translation_noise_magnitude ?? 0, false);
t = applyFrequency(clampedTime, transition.params.scale_frequency ?? 1, !!transition.params.scale_alternate_direction);
easing = EasingFnMap[transition.params.scale_easing ?? 'linear'] ?? EasingFnMap['linear'];
scale = interpolateVec3(startScale as Vec3, endScale as Vec3 | undefined, easing(t), transition.params.scale_noise_magnitude ?? 0, false);
}
const pivot = transition.params.pivot ?? Vec3.zero();
@@ -213,21 +304,21 @@ function processTransformMatrix(transition: MVSAnimationNode<'interpolate'>, tar
Mat4.mul(result, TransformState.rotation, result);
Mat4.mul(result, TransformState.translation, result);
assign(target, transition.params.property, result, offset);
return result;
}
function interpolateScalars(start: number | number[], end: number | number[] | undefined, t: number, noise: number) {
function interpolateScalars(start: number | number[], end: number | number[] | undefined, t: number, noise: number, discrete: boolean) {
if (Array.isArray(start)) {
const ret = Array.from<number>({ length: start.length }).fill(0.1);
if (!end || !Array.isArray(end)) {
for (let i = 0; i < start.length; i++) {
ret[i] = interpolateScalar(start[i], end, t, noise);
ret[i] = interpolateScalar(start[i], end, t, noise, discrete);
}
return ret;
}
for (let i = 0; i < start.length; i++) {
ret[i] = interpolateScalar(start[i], end[i], t, noise);
ret[i] = interpolateScalar(start[i], end[i], t, noise, discrete);
}
return ret;
}
@@ -235,19 +326,22 @@ function interpolateScalars(start: number | number[], end: number | number[] | u
if (Array.isArray(end)) {
const ret = Array.from<number>({ length: end.length }).fill(0.1);
for (let i = 0; i < end.length; i++) {
ret[i] = interpolateScalar(start, end[i], t, noise);
ret[i] = interpolateScalar(start, end[i], t, noise, discrete);
}
return ret;
}
return interpolateScalar(start, end, t, noise);
return interpolateScalar(start, end, t, noise, discrete);
}
function interpolateScalar(start: number, end: number | undefined, t: number, noise: number) {
function interpolateScalar(start: number, end: number | undefined, t: number, noise: number, discrete: boolean) {
let v = typeof end === 'number' ? lerp(start, end, t) : start;
if (noise) {
v += (Math.random() - 0.5) * noise;
}
if (discrete) {
v = Math.round(v);
}
return v;
}
@@ -348,6 +442,76 @@ function interpolateRotation(start: Mat3, end: Mat3 | undefined, t: number, nois
return Mat3.fromMat4(Mat3(), RotationState.temp);
}
function decodeColors(color: ColorT | Record<number | string, ColorT> | undefined, baseColors: Record<number | string, ColorT> | undefined) {
if (color === undefined || color === null) return undefined;
if (typeof color === 'object') {
const ret: Record<number | string, Color> = {};
if (baseColors) {
for (const key of Object.keys(baseColors)) {
const decoded = decodeColor(baseColors[key]);
if (decoded !== undefined) {
ret[key] = decoded;
}
}
}
for (const key of Object.keys(color)) {
const decoded = decodeColor(color[key]);
if (decoded !== undefined) {
ret[key] = decoded;
}
}
return ret;
}
return decodeColor(color);
}
function interpolateColors(start: ColorT | Record<number, ColorT>, end: ColorT | Record<number, ColorT> | undefined, time: number, cacheEntry: InterpolationCacheEntry, baseColors: Record<number, ColorT> | undefined) {
const t = clamp(time, 0, 1);
if (cacheEntry.paletteFn) {
const c = cacheEntry.paletteFn(t);
return Color.toHexStyle(c);
}
if (cacheEntry.startColor === undefined) {
cacheEntry.startColor = decodeColors(start, baseColors);
}
if (cacheEntry.endColor === undefined) {
cacheEntry.endColor = decodeColors(end, undefined);
}
const { startColor, endColor } = cacheEntry;
if (typeof startColor === 'object') {
if (typeof baseColors !== 'object') {
throw new Error('Cannot interpolate from scalar color to color mapping');
}
const ret = { ...baseColors as any, ...startColor as any };
if (typeof endColor === 'object') {
for (const key of Object.keys(endColor)) {
ret[key] = Color.toHexStyle(Color.interpolate(startColor[key], endColor[key], t));
}
} else if (typeof endColor === 'number') {
for (const key of Object.keys(startColor)) {
ret[key] = Color.toHexStyle(Color.interpolate(startColor[key], endColor, t));
}
}
return ret;
}
if (typeof endColor === 'object') {
throw new Error('Cannot interpolate from scalar color to color mapping');
}
if (typeof endColor === 'number' && typeof startColor === 'number') {
return Color.toHexStyle(Color.interpolate(startColor, endColor, t));
}
return start;
}
function select(params: any, path: string | (string | number)[], offset: number) {
if (typeof path === 'string') {
return params?.[path];
@@ -400,12 +564,10 @@ function makeNodeMap(tree: Tree, map: Map<string, (string | number)[]>, currentP
return map;
}
function makePaletteFunction(props: MVSAnimationNode<'interpolate'>, start: ColorT | undefined | null, end: ColorT | undefined | null): ((value: number) => Color) | undefined {
if (props.params.kind !== 'color') return undefined;
function makePaletteFunction(props: MVSAnimationNode<'interpolate'>): ((value: number) => Color) | undefined {
if (props.params.kind !== 'color' || !props.params.palette) return undefined;
const params = props.params.palette
? palettePropsFromMVSPalette(props.params.palette)
: palettePropsFromMVSPalette({ kind: 'continuous', colors: [start ?? 'black', end ?? start ?? 'black'] });
const params = palettePropsFromMVSPalette(props.params.palette);
if (params.name === 'discrete') return makePaletteFunctionDiscrete(params.params);
if (params.name === 'continuous') return makePaletteFunctionContinuous(params.params);
throw new Error(`NotImplementedError: makePaletteFunction for ${(props as any).name}`);

View File

@@ -219,7 +219,7 @@ export function collectInlineTooltips(tree: MolstarSubtree<'structure'>, context
text: node.params.text,
selector: {
name: 'annotation',
params: { annotationId: p.annotationId, fieldName: p.fieldName, fieldValues: p.fieldValues },
params: { annotationId: p.annotationId, fieldName: p.fieldName, fieldValues: p.fieldValues, label: p.label || 'Annotation' },
},
});
}
@@ -253,7 +253,7 @@ export function collectInlineLabels(tree: MolstarSubtree<'structure'>, context:
params: {
selector: {
name: 'annotation',
params: { annotationId: p.annotationId, fieldName: p.fieldName, fieldValues: p.fieldValues },
params: { annotationId: p.annotationId, fieldName: p.fieldName, fieldValues: p.fieldValues, label: p.label || 'Annotation' },
},
},
},
@@ -412,8 +412,8 @@ export function representationProps(node: MolstarSubtree<'representation'>): Par
if (clip) {
base.type!.params = { ...base.type?.params, clip };
}
if (node.custom?.molstar_reprepresentation_params) {
base.type!.params = { ...base.type!.params, ...node.custom.molstar_reprepresentation_params };
if (node.custom?.molstar_representation_params) {
base.type!.params = { ...base.type!.params, ...node.custom.molstar_representation_params };
}
return base;
}

View File

@@ -33,8 +33,8 @@ import { NonCovalentInteractionsExtension } from './load-extensions/non-covalent
import { LoadingActions, LoadingExtension, loadTreeVirtual, UpdateTarget } from './load-generic';
import { AnnotationFromSourceKind, AnnotationFromUriKind, collectAnnotationReferences, collectAnnotationTooltips, collectInlineLabels, collectInlineTooltips, colorThemeForNode, componentFromXProps, componentPropsFromSelector, isPhantomComponent, labelFromXProps, makeNearestReprMap, prettyNameFromSelector, representationProps, structureProps, transformAndInstantiateStructure, transformAndInstantiateVolume, volumeColorThemeForNode, volumeRepresentationProps } from './load-helpers';
import { MVSData, MVSData_States, Snapshot, SnapshotMetadata } from './mvs-data';
import { MVSAnimationNode } from './tree/animation/animation-tree';
import { validateTree } from './tree/generic/tree-schema';
import { MVSAnimationNode, MVSAnimationSchema } from './tree/animation/animation-tree';
import { validateTree } from './tree/generic/tree-validation';
import { convertMvsToMolstar, mvsSanityCheck } from './tree/molstar/conversion';
import { MolstarNode, MolstarNodeParams, MolstarSubtree, MolstarTree, MolstarTreeSchema } from './tree/molstar/molstar-tree';
import { MVSTreeSchema } from './tree/mvs/mvs-tree';
@@ -51,6 +51,7 @@ export interface MVSLoadOptions {
sanityChecks?: boolean,
/** Base for resolving relative URLs/URIs. May itself be a relative URL (relative to the window URL). */
sourceUrl?: string,
doNotReportErrors?: boolean
}
@@ -66,6 +67,9 @@ async function _loadMVS(ctx: RuntimeContext, plugin: PluginContext, data: MVSDat
const mvsExtensionLoaded = plugin.state.hasBehavior(MolViewSpec);
if (!mvsExtensionLoaded) throw new Error('MolViewSpec extension is not loaded.');
// Stop any currently running audio
plugin.managers.markdownExtensions.audio.dispose();
// Reset canvas props to default so that modifyCanvasProps works as expected
resetCanvasProps(plugin);
@@ -75,10 +79,13 @@ async function _loadMVS(ctx: RuntimeContext, plugin: PluginContext, data: MVSDat
for (let i = 0; i < multiData.snapshots.length; i++) {
const snapshot = multiData.snapshots[i];
const previousSnapshot = i > 0 ? multiData.snapshots[i - 1] : multiData.snapshots[multiData.snapshots.length - 1];
validateTree(MVSTreeSchema, snapshot.root, 'MVS');
validateTree(MVSTreeSchema, snapshot.root, 'MVS', plugin);
if (snapshot.animation) {
validateTree(MVSAnimationSchema, snapshot.animation, 'Animation', plugin);
}
if (options.sanityChecks) mvsSanityCheck(snapshot.root);
const molstarTree = convertMvsToMolstar(snapshot.root, options.sourceUrl);
validateTree(MolstarTreeSchema, molstarTree, 'Converted Molstar');
validateTree(MolstarTreeSchema, molstarTree, 'Converted Molstar', plugin);
const entry = molstarTreeToEntry(
plugin,
molstarTree,
@@ -133,7 +140,7 @@ async function assignStateTransition(ctx: RuntimeContext, plugin: PluginContext,
for (let i = 0; i < transitions.frames.length; i++) {
const frame = transitions.frames[i];
const molstarTree = convertMvsToMolstar(frame, options.sourceUrl);
const molstarTree = convertMvsToMolstar(frame[0], options.sourceUrl);
const entry = molstarTreeToEntry(
plugin,
molstarTree,
@@ -145,7 +152,7 @@ async function assignStateTransition(ctx: RuntimeContext, plugin: PluginContext,
StateTree.reuseTransformParams(entry.snapshot.data!.tree, parentEntry.snapshot.data!.tree);
animation.frames.push({
durationInMs: transitions.frametimeMs,
durationInMs: frame[1],
data: entry.snapshot.data!,
camera: transitions.tree.params?.include_camera ? entry.snapshot.camera : undefined,
canvas3d: transitions.tree.params?.include_canvas ? entry.snapshot.canvas3d : undefined,
@@ -176,6 +183,10 @@ function molstarTreeToEntry(
}
snapshot.durationInMs = metadata.linger_duration_ms + (metadata.previousTransitionDurationMs ?? 0);
if (tree.custom?.molstar_on_load_markdown_commands) {
snapshot.onLoadMarkdownCommands = tree.custom.molstar_on_load_markdown_commands;
}
const entryParams: PluginStateSnapshotManager.EntryParams = {
key: metadata.key,
name: metadata.title,

View File

@@ -5,7 +5,7 @@
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { treeValidationIssues } from './tree/generic/tree-schema';
import { treeValidationIssues } from './tree/generic/tree-validation';
import { treeToString } from './tree/generic/tree-utils';
import { MVSAnimationSchema, MVSAnimationTree } from './tree/animation/animation-tree';
import { Root, createMVSBuilder } from './tree/mvs/mvs-builder';

View File

@@ -4,7 +4,7 @@
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { bool, float, int, list, OptionalField, RequiredField, str, union, nullable, literal, ValueFor } from '../generic/field-schema';
import { bool, float, int, list, OptionalField, RequiredField, str, union, nullable, literal, ValueFor, dict } from '../generic/field-schema';
import { SimpleParamsSchema, UnionParamsSchema } from '../generic/params-schema';
import { NodeFor, ParamsOfKind, SubtreeOfKind, TreeFor, TreeSchema } from '../generic/tree-schema';
import { ColorT, ContinuousPalette, DiscretePalette, Matrix, Vector3 } from '../mvs/param-types';
@@ -48,6 +48,7 @@ const ScalarInterpolation = {
..._Easing,
start: OptionalField(nullable(union(float, list(float))), null, 'Start value. If a list of values is provided, each element will be interpolated separately. If unset, parent state value is used.'),
end: OptionalField(nullable(union(float, list(float))), null, 'End value. If a list of values is provided, each element will be interpolated separately. If unset, only noise is applied.'),
discrete: OptionalField(bool, false, 'Whether to round the values to the closest integer. Useful for example for trajectory animation.'),
..._Noise,
};
@@ -74,8 +75,8 @@ const ColorInterpolation = {
..._Common,
..._Frequency,
..._Easing,
start: OptionalField(nullable(ColorT), null, 'Start value. If unset, parent state value is used.'),
end: OptionalField(nullable(ColorT), null, 'End value.'),
start: OptionalField(union(nullable(ColorT), dict(union(int, str), ColorT)), null, 'Start value. If unset, parent state value is used.'),
end: OptionalField(union(nullable(ColorT), dict(union(int, str), ColorT)), null, 'End value.'),
palette: OptionalField(nullable(union(DiscretePalette, ContinuousPalette)), null, 'Palette to sample colors from. Overrides start and end values.'),
};

View File

@@ -4,12 +4,8 @@
* @author Adam Midlik <midlik@gmail.com>
*/
import { onelinerJsonString } from '../../../../mol-util/json';
import { isPlainObject, mapObjectMap } from '../../../../mol-util/object';
import { Field } from './field-schema';
import { AllRequired, ParamsSchema, SimpleParamsSchema, ValuesFor, paramsValidationIssues } from './params-schema';
import { treeToString } from './tree-utils';
import { mapObjectMap } from '../../../../mol-util/object';
import { AllRequired, ParamsSchema, ValuesFor } from './params-schema';
/** Type of "custom" of a tree node (key-value storage with arbitrary JSONable values) */
export type CustomProps = Partial<Record<string, any>>
@@ -114,120 +110,3 @@ export type NodeFor<TTreeSchema extends TreeSchema, TKind extends keyof ParamsSc
/** Type of tree which conforms to tree schema `TTreeSchema` */
export type TreeFor<TTreeSchema extends TreeSchema> = Tree<NodeFor<TTreeSchema>, RootFor<TTreeSchema> & NodeFor<TTreeSchema>>
/** Return `undefined` if a tree conforms to the given schema,
* return validation issues (as a list of lines) if it does not conform.
* If `options.requireAll`, all parameters (including optional) must have a value provided.
* If `options.noExtra` is true, presence of any extra parameters is treated as an issue.
* If `options.anyRoot` is true, the kind of the root node is not enforced.
*/
export function treeValidationIssues(schema: TreeSchema, tree: Tree, options: { requireAll?: boolean, noExtra?: boolean, anyRoot?: boolean, parent?: string } = {}): string[] | undefined {
if (!isPlainObject(tree)) return [`Node must be an object, not ${tree}`];
if (!options.anyRoot && tree.kind !== schema.rootKind) return [`Invalid root node kind "${tree.kind}", root must be of kind "${schema.rootKind}"`];
const nodeSchema = schema.nodes[tree.kind];
if (!nodeSchema) return [`Unknown node kind "${tree.kind}"`];
if (nodeSchema.parent && (options.parent !== undefined) && !nodeSchema.parent.includes(options.parent)) {
return [`Node of kind "${tree.kind}" cannot appear as a child of "${options.parent}". Allowed parents for "${tree.kind}" are: ${nodeSchema.parent.map(s => `"${s}"`).join(', ')}`];
}
const issues = paramsValidationIssues(nodeSchema.params, getParams(tree), options);
if (issues) return [`Invalid parameters for node of kind "${tree.kind}":`, ...issues.map(s => ' ' + s)];
if (tree.custom !== undefined && (typeof tree.custom !== 'object' || tree.custom === null)) {
return [`Invalid "custom" for node of kind "${tree.kind}": must be an object, not ${tree.custom}.`];
}
for (const child of getChildren(tree)) {
const issues = treeValidationIssues(schema, child, { ...options, anyRoot: true, parent: tree.kind });
if (issues) return issues;
}
return undefined;
}
/** Validate a tree against the given schema.
* Do nothing if OK; print validation issues on console and throw an error is the tree does not conform.
* Include `label` in the printed output. */
export function validateTree(schema: TreeSchema, tree: Tree, label: string): void {
const issues = treeValidationIssues(schema, tree, { noExtra: true });
if (issues) {
console.warn(`Invalid ${label} tree:\n${treeToString(tree)}`);
console.error(`${label} tree validation issues:`);
for (const line of issues) {
console.error(' ', line);
}
throw new Error('FormatError');
}
}
/** Return documentation for a tree schema as plain text */
export function treeSchemaToString<S extends TreeSchema>(schema: S): string {
return treeSchemaToString_(schema, false);
}
/** Return documentation for a tree schema as markdown text */
export function treeSchemaToMarkdown<S extends TreeSchema>(schema: S): string {
return treeSchemaToString_(schema, true);
}
function treeSchemaToString_<S extends TreeSchema>(schema: S, markdown: boolean = false): string {
const out: string[] = [];
const bold = (str: string) => markdown ? `**${str}**` : str;
const code = (str: string) => markdown ? `\`${str}\`` : str;
const h1 = markdown ? '## ' : ' - ';
const p1 = markdown ? '' : ' ';
const h2 = markdown ? '- ' : ' - ';
const p2 = markdown ? ' ' : ' ';
const h3 = markdown ? ' - ' : ' - ';
const p3 = markdown ? ' ' : ' ';
const newline = markdown ? '\n\n' : '\n';
out.push(`Tree schema:`);
for (const kind in schema.nodes) {
const { description, params, parent } = schema.nodes[kind];
out.push(`${h1}${code(kind)}`);
if (kind === schema.rootKind) {
out.push(`${p1}[Root of the tree must be of this kind]`);
}
if (description) {
out.push(`${p1}${description}`);
}
out.push(`${p1}Parent: ${!parent ? 'any' : parent.length === 0 ? 'none' : parent.map(code).join(' or ')}`);
out.push(`${p1}Params:${Object.keys(params).length > 0 ? '' : ' none'}`);
if (params.type === 'simple') {
formatSimpleParams(out, params, { h: h2, p: p2, code, bold });
} else {
const key = params.discriminator;
const casesStr = Object.keys(params.cases).join(' | ');
out.push(`${h2}${bold(code(key + ': '))}${code(casesStr)}`);
if (params.discriminatorDescription) {
out.push(`${p2}${params.discriminatorDescription}`);
}
out.push(`${p2}[This parameter determines the rest of parameters]`);
for (const case_ in params.cases) {
const caseStr = `${params.discriminator}: "${case_}"`;
out.push(`${p2}${bold(`Case ${code(caseStr)}:`)}`);
formatSimpleParams(out, params.cases[case_], { h: h3, p: p3, code, bold });
}
}
}
return out.join(newline);
}
function formatSimpleParams(out: string[], params: SimpleParamsSchema, formatting: { h: string, p: string, code: (str: string) => string, bold: (str: string) => string }): void {
const { h, p, code, bold } = formatting;
for (const key in params.fields) {
const field = params.fields[key];
out.push(`${h}${bold(code(key + (field.required ? ': ' : '?: ')))}${code(formatFieldType(field))}`);
const defaultValue = field.required ? undefined : field.default;
if (field.description) {
out.push(`${p}${field.description}`);
}
if (defaultValue !== undefined) {
out.push(`${p}Default: ${code(onelinerJsonString(defaultValue))}`);
}
}
}
function formatFieldType(field: Field): string {
const typeString = field.type.name;
if (typeString.startsWith('(') && typeString.endsWith(')')) {
return typeString.slice(1, -1);
} else {
return typeString;
}
}

View File

@@ -0,0 +1,125 @@
import { PluginContext } from '../../../../mol-plugin/context';
import { onelinerJsonString } from '../../../../mol-util/json';
import { isPlainObject } from '../../../../mol-util/object';
import { Field } from './field-schema';
import { SimpleParamsSchema, paramsValidationIssues } from './params-schema';
import { getChildren, getParams, Tree, TreeSchema } from './tree-schema';
import { treeToString } from './tree-utils';
/** Return `undefined` if a tree conforms to the given schema,
* return validation issues (as a list of lines) if it does not conform.
* If `options.requireAll`, all parameters (including optional) must have a value provided.
* If `options.noExtra` is true, presence of any extra parameters is treated as an issue.
* If `options.anyRoot` is true, the kind of the root node is not enforced.
*/
export function treeValidationIssues(schema: TreeSchema, tree: Tree, options: { requireAll?: boolean, noExtra?: boolean, anyRoot?: boolean, parent?: string } = {}): string[] | undefined {
if (!isPlainObject(tree)) return [`Node must be an object, not ${tree}`];
if (!options.anyRoot && tree.kind !== schema.rootKind) return [`Invalid root node kind "${tree.kind}", root must be of kind "${schema.rootKind}"`];
const nodeSchema = schema.nodes[tree.kind];
if (!nodeSchema) return [`Unknown node kind "${tree.kind}"`];
if (nodeSchema.parent && (options.parent !== undefined) && !nodeSchema.parent.includes(options.parent)) {
return [`Node of kind "${tree.kind}" cannot appear as a child of "${options.parent}". Allowed parents for "${tree.kind}" are: ${nodeSchema.parent.map(s => `"${s}"`).join(', ')}`];
}
const issues = paramsValidationIssues(nodeSchema.params, getParams(tree), options);
if (issues) return [`Invalid parameters for node of kind "${tree.kind}":`, ...issues.map(s => ' ' + s)];
if (tree.custom !== undefined && (typeof tree.custom !== 'object' || tree.custom === null)) {
return [`Invalid "custom" for node of kind "${tree.kind}": must be an object, not ${tree.custom}.`];
}
for (const child of getChildren(tree)) {
const issues = treeValidationIssues(schema, child, { ...options, anyRoot: true, parent: tree.kind });
if (issues) return issues;
}
return undefined;
}
/** Validate a tree against the given schema.
* Do nothing if OK; print validation issues on console and throw an error is the tree does not conform.
* Include `label` in the printed output. */
export function validateTree(schema: TreeSchema, tree: Tree, label: string, plugin: PluginContext): void {
const issues = treeValidationIssues(schema, tree, { noExtra: true });
if (issues) {
console.warn(`Invalid ${label} tree:\n${treeToString(tree)}`);
console.error(`${label} tree validation issues:`);
plugin.log.error(`${label} tree validation issues:`);
for (const line of issues) {
console.error(' ', line);
plugin.log.error(line);
}
throw new Error('FormatError');
}
}
/** Return documentation for a tree schema as plain text */
export function treeSchemaToString<S extends TreeSchema>(schema: S): string {
return treeSchemaToString_(schema, false);
}
/** Return documentation for a tree schema as markdown text */
export function treeSchemaToMarkdown<S extends TreeSchema>(schema: S): string {
return treeSchemaToString_(schema, true);
}
function treeSchemaToString_<S extends TreeSchema>(schema: S, markdown: boolean = false): string {
const out: string[] = [];
const bold = (str: string) => markdown ? `**${str}**` : str;
const code = (str: string) => markdown ? `\`${str}\`` : str;
const h1 = markdown ? '## ' : ' - ';
const p1 = markdown ? '' : ' ';
const h2 = markdown ? '- ' : ' - ';
const p2 = markdown ? ' ' : ' ';
const h3 = markdown ? ' - ' : ' - ';
const p3 = markdown ? ' ' : ' ';
const newline = markdown ? '\n\n' : '\n';
out.push(`Tree schema:`);
for (const kind in schema.nodes) {
const { description, params, parent } = schema.nodes[kind];
out.push(`${h1}${code(kind)}`);
if (kind === schema.rootKind) {
out.push(`${p1}[Root of the tree must be of this kind]`);
}
if (description) {
out.push(`${p1}${description}`);
}
out.push(`${p1}Parent: ${!parent ? 'any' : parent.length === 0 ? 'none' : parent.map(code).join(' or ')}`);
out.push(`${p1}Params:${Object.keys(params).length > 0 ? '' : ' none'}`);
if (params.type === 'simple') {
formatSimpleParams(out, params, { h: h2, p: p2, code, bold });
} else {
const key = params.discriminator;
const casesStr = Object.keys(params.cases).join(' | ');
out.push(`${h2}${bold(code(key + ': '))}${code(casesStr)}`);
if (params.discriminatorDescription) {
out.push(`${p2}${params.discriminatorDescription}`);
}
out.push(`${p2}[This parameter determines the rest of parameters]`);
for (const case_ in params.cases) {
const caseStr = `${params.discriminator}: "${case_}"`;
out.push(`${p2}${bold(`Case ${code(caseStr)}:`)}`);
formatSimpleParams(out, params.cases[case_], { h: h3, p: p3, code, bold });
}
}
}
return out.join(newline);
}
function formatSimpleParams(out: string[], params: SimpleParamsSchema, formatting: { h: string, p: string, code: (str: string) => string, bold: (str: string) => string }): void {
const { h, p, code, bold } = formatting;
for (const key in params.fields) {
const field = params.fields[key];
out.push(`${h}${bold(code(key + (field.required ? ': ' : '?: ')))}${code(formatFieldType(field))}`);
const defaultValue = field.required ? undefined : field.default;
if (field.description) {
out.push(`${p}${field.description}`);
}
if (defaultValue !== undefined) {
out.push(`${p}Default: ${code(onelinerJsonString(defaultValue))}`);
}
}
}
function formatFieldType(field: Field): string {
const typeString = field.type.name;
if (typeString.startsWith('(') && typeString.endsWith(')')) {
return typeString.slice(1, -1);
} else {
return typeString;
}
}

View File

@@ -49,7 +49,7 @@ export const MolstarTreeSchema = TreeSchema({
},
/** Auxiliary node corresponding to Molstar's TrajectoryFrom*. */
trajectory_with_coordinates: {
description: "Auxiliary node corresponding to assigning a separate coordinates to a trajectory.",
description: 'Auxiliary node corresponding to assigning a separate coordinates to a trajectory.',
parent: ['model'],
params: SimpleParamsSchema({
coordinates_ref: RequiredField(str, 'Coordinates reference'),

View File

@@ -98,6 +98,12 @@ export class Root extends _Base<'root'> implements FocusMixin, PrimitivesMixin {
this._animation ??= new Animation(params);
return this._animation;
}
/** Modifies custom state of the root */
extendRootCustomState(custom: Record<string, any>): this {
this._node.custom = { ...this._node.custom, ...custom };
return this;
}
}
export class Animation {

View File

@@ -53,7 +53,7 @@ const LinesParams = {
vertices: RequiredField(FloatList, '3*n_vertices length array of floats with vertex position (x1, y1, z1, ...).'),
/** 2*n_lines length array of indices into vertices that form lines (l1_1, l1_2, ...). */
indices: RequiredField(IntList, '2*n_lines length array of indices into vertices that form lines (l1_1, l1_2, ...).'),
/** Assign a number to each triangle to group them. If not specified, each line is considered a separate group (line i = group i). */
/** Assign a number to each line to group them. If not specified, each line is considered a separate group (line i = group i). */
line_groups: OptionalField(nullable(IntList), null, 'Assign a number to each triangle to group them. If not specified, each line is considered a separate group (line i = group i).'),
/** Assign a color to each group. Where not assigned, uses `color`. */
group_colors: OptionalField(dict(int, ColorT), {}, 'Assign a color to each group. Where not assigned, uses `color`.'),

View File

@@ -85,10 +85,10 @@ namespace ValidationReport {
Clashes = 'rcsb-clashes',
}
export const DefaultBaseUrl = 'https://files.rcsb.org/pub/pdb/validation_reports';
export const DefaultBaseUrl = 'https://files.rcsb.org/validation/view';
export function getEntryUrl(pdbId: string, baseUrl: string) {
const id = pdbId.toLowerCase();
return `${baseUrl}/${id.substr(1, 2)}/${id}/${id}_validation.xml.gz`;
return `${baseUrl}/${id}_validation.xml`;
}
export function isApplicable(model?: Model): boolean {

View File

@@ -28,6 +28,12 @@ interface ICamera {
readonly fogFar: number,
readonly fogNear: number,
readonly headRotation: Mat4,
readonly viewEye: Mat4,
readonly isAsymmetricProjection: boolean,
readonly forceFull: boolean;
readonly scale: number;
readonly minTargetDistance: number;
}
const tmpClip = Vec4();
@@ -38,6 +44,8 @@ export class Camera implements ICamera {
readonly projectionView: Mat4 = Mat4.identity();
readonly inverseProjectionView: Mat4 = Mat4.identity();
readonly headRotation: Mat4 = Mat4.zero();
readonly viewEye: Mat4 = Mat4.zero();
readonly isAsymmetricProjection = false;
readonly viewport: Viewport;
readonly state: Readonly<Camera.Snapshot> = Camera.createDefaultSnapshot();
@@ -49,6 +57,10 @@ export class Camera implements ICamera {
fogFar = 10000;
zoom = 1;
forceFull = false;
scale = 1;
minTargetDistance = 0;
readonly transition: CameraTransitionManager = new CameraTransitionManager(this);
readonly stateChanged = new BehaviorSubject<Partial<Camera.Snapshot>>(this.state);
@@ -72,7 +84,15 @@ export class Camera implements ICamera {
return false;
}
const height = 2 * Math.tan(snapshot.fov / 2) * Vec3.distance(snapshot.position, snapshot.target) * this.state.scale;
const distance = Vec3.distance(snapshot.position, snapshot.target);
const minTargetDistance = this.minTargetDistance / this.scale;
if (distance < minTargetDistance) {
Vec3.sub(this.deltaDirection, snapshot.target, snapshot.position);
Vec3.setMagnitude(this.deltaDirection, this.deltaDirection, minTargetDistance);
Vec3.sub(snapshot.position, snapshot.target, this.deltaDirection);
}
const height = 2 * Math.tan(snapshot.fov / 2) * Vec3.distance(snapshot.position, snapshot.target) * this.scale;
this.zoom = this.viewport.height / height;
updateClip(this);
@@ -111,7 +131,7 @@ export class Camera implements ICamera {
}
getTargetDistance(radius: number) {
return Camera.targetDistance(radius, this.state.mode, this.state.fov, this.viewport.width, this.viewport.height);
return Math.max(this.minTargetDistance / this.scale, Camera.targetDistance(radius, this.state.mode, this.state.fov, this.viewport.width, this.viewport.height));
}
getFocus(target: Vec3, radius: number, up?: Vec3, dir?: Vec3, snapshot?: Partial<Camera.Snapshot>): Partial<Camera.Snapshot> {
@@ -202,7 +222,7 @@ export class Camera implements ICamera {
Vec3.scaleAndAdd(out.origin, out.origin, out.direction, -this.near);
} else {
Vec3.copy(out.origin, this.state.position);
Vec3.scale(out.origin, out.origin, this.state.scale);
Vec3.scale(out.origin, out.origin, this.scale);
Vec3.set(out.direction, x, y, 0.5);
this.unproject(out.direction, out.direction);
Vec3.normalize(out.direction, Vec3.sub(out.direction, out.direction, out.origin));
@@ -289,8 +309,6 @@ export namespace Camera {
clipFar: true,
minNear: 5,
minFar: 0,
scale: 1,
};
}
@@ -308,8 +326,6 @@ export namespace Camera {
clipFar: boolean
minNear: number
minFar: number
scale: number
}
export function copySnapshot(out: Snapshot, source?: Partial<Snapshot>) {
@@ -329,8 +345,6 @@ export namespace Camera {
if (typeof source.minNear !== 'undefined') out.minNear = source.minNear;
if (typeof source.minFar !== 'undefined') out.minFar = source.minFar;
if (typeof source.scale !== 'undefined') out.scale = source.scale;
return out;
}
@@ -343,7 +357,6 @@ export namespace Camera {
&& a.clipFar === b.clipFar
&& a.minNear === b.minNear
&& a.minFar === b.minFar
&& a.scale === b.scale
&& Vec3.exactEquals(a.position, b.position)
&& Vec3.exactEquals(a.up, b.up)
&& Vec3.exactEquals(a.target, b.target);
@@ -354,11 +367,11 @@ const tmpPosition = Vec3();
const tmpTarget = Vec3();
function updateView(camera: Camera) {
if (camera.state.scale === 1) {
if (camera.scale === 1) {
Mat4.lookAt(camera.view, camera.state.position, camera.state.target, camera.state.up);
} else {
Vec3.scale(tmpPosition, camera.state.position, camera.state.scale);
Vec3.scale(tmpTarget, camera.state.target, camera.state.scale);
Vec3.scale(tmpPosition, camera.state.position, camera.scale);
Vec3.scale(tmpTarget, camera.state.target, camera.scale);
Mat4.lookAt(camera.view, tmpPosition, tmpTarget, camera.state.up);
}
}
@@ -424,11 +437,13 @@ function updatePers(camera: Camera) {
}
function updateClip(camera: Camera) {
let { radius, radiusMax, mode, fog, clipFar, minNear, minFar, scale } = camera.state;
const { forceFull, scale } = camera;
let { radius, radiusMax, mode, fog, clipFar, minNear, minFar } = camera.state;
radiusMax *= scale;
minFar *= scale;
minNear *= scale;
radius *= scale;
if (forceFull) radius = radiusMax;
const minRadius = 0.01 * scale;
if (radius < minRadius) radius = minRadius;
@@ -437,8 +452,9 @@ function updateClip(camera: Camera) {
Vec3.scale(tmpTarget, camera.state.target, scale);
Vec3.scale(tmpPosition, camera.state.position, scale);
const cameraDistance = Vec3.distance(tmpPosition, tmpTarget);
let near = cameraDistance - radius;
let near = forceFull ? 0.01 : cameraDistance - radius;
let far = cameraDistance + normalizedFar;
if (forceFull) minNear = near;
if (mode === 'perspective') {
// set at least to 5 to avoid slow sphere impostor rendering

View File

@@ -43,9 +43,13 @@ class StereoCamera {
Object.assign(this.props, props);
}
update() {
update(xr?: { pose: XRViewerPose, layer: XRWebGLLayer }) {
this.parent.update();
update(this.parent, this.props, this.left as EyeCamera, this.right as EyeCamera);
if (xr) {
xrUpdate(this.parent, this.left as EyeCamera, this.right as EyeCamera, xr);
} else {
update(this.parent, this.props, this.left as EyeCamera, this.right as EyeCamera);
}
}
}
@@ -62,12 +66,19 @@ class EyeCamera implements ICamera {
projectionView = Mat4();
inverseProjectionView = Mat4();
headRotation = Mat4();
viewEye = Mat4();
isAsymmetricProjection = true;
state: Readonly<Camera.Snapshot> = Camera.createDefaultSnapshot();
viewOffset: Readonly<Camera.ViewOffset> = Camera.ViewOffset();
far: number = 0;
near: number = 0;
fogFar: number = 0;
fogNear: number = 0;
forceFull: boolean = false;
scale: number = 0;
minTargetDistance: number = 0;
}
const tmpEyeLeft = Mat4.identity();
@@ -84,6 +95,10 @@ function copyStates(parent: Camera, eye: EyeCamera) {
eye.near = parent.near;
eye.fogFar = parent.fogFar;
eye.fogNear = parent.fogNear;
eye.forceFull = parent.forceFull;
eye.scale = parent.scale;
eye.minTargetDistance = parent.minTargetDistance;
}
//
@@ -138,4 +153,21 @@ function update(camera: Camera, props: StereoCameraProps, left: EyeCamera, right
Mat4.mul(right.view, right.view, tmpEyeRight);
Mat4.mul(right.projectionView, right.projection, right.view);
Mat4.invert(right.inverseProjectionView, right.projectionView);
}
}
//
function xrUpdate(camera: Camera, left: EyeCamera, right: EyeCamera, xr: { pose: XRViewerPose, layer: XRWebGLLayer }) {
_xrUpdate(camera, left, xr.pose.views[0], xr.layer);
_xrUpdate(camera, right, xr.pose.views[1], xr.layer);
}
function _xrUpdate(camera: Camera, eye: EyeCamera, view: XRView, layer: XRWebGLLayer) {
copyStates(camera, eye);
const lvp = layer.getViewport(view)!;
Viewport.set(eye.viewport, lvp.x, lvp.y, lvp.width, lvp.height);
Mat4.fromArray(eye.projection, view.projectionMatrix, 0);
Mat4.fromArray(eye.view, view.transform.inverse.matrix, 0);
Mat4.mul(eye.projectionView, eye.projection, eye.view);
Mat4.invert(eye.inverseProjectionView, eye.projectionView);
}

View File

@@ -13,7 +13,7 @@ import { Vec3, Vec2 } from '../mol-math/linear-algebra';
import { InputObserver, ModifiersKeys, ButtonsType } from '../mol-util/input/input-observer';
import { Renderer, RendererStats, RendererParams } from '../mol-gl/renderer';
import { GraphicsRenderObject } from '../mol-gl/render-object';
import { TrackballControls, TrackballControlsParams } from './controls/trackball';
import { DefaultTrackballControlsAttribs, TrackballControls, TrackballControlsParams } from './controls/trackball';
import { Viewport } from './camera/util';
import { createContext, WebGLContext, getGLContext } from '../mol-gl/webgl/context';
import { Representation } from '../mol-repr/representation';
@@ -34,7 +34,6 @@ import { ImagePass, ImageProps } from './passes/image';
import { Sphere3D } from '../mol-math/geometry';
import { addConsoleStatsProvider, isDebugMode, isTimingMode, removeConsoleStatsProvider } from '../mol-util/debug';
import { CameraHelperParams } from './helper/camera-helper';
import { create as produce } from 'mutative';
import { HandleHelperParams } from './helper/handle-helper';
import { StereoCamera, StereoCameraParams } from './camera/stereo';
import { Helper } from './helper/helper';
@@ -47,8 +46,13 @@ import { deepClone } from '../mol-util/object';
import { HiZParams, HiZPass } from './passes/hi-z';
import { IlluminationParams } from './passes/illumination';
import { isMobileBrowser } from '../mol-util/browser';
import { PointerHelperParams } from './helper/pointer-helper';
import { DefaultXRManagerAttribs, XRManager, XRManagerParams } from './helper/xr-manager';
import { Ray3D } from '../mol-math/geometry/primitives/ray3d';
import { RayHelper } from './helper/ray-helper';
import { produce } from '../mol-util/produce';
import { ShaderManager } from './helper/shader-manager';
import { toFixed } from '../mol-util/number';
export const CameraFogParams = {
intensity: PD.Numeric(15, { min: 1, max: 100, step: 1 }),
@@ -62,7 +66,6 @@ export const Canvas3DParams = {
off: PD.Group({})
}, { cycle: true, hideIf: p => p?.mode !== 'perspective' }),
fov: PD.Numeric(45, { min: 10, max: 130, step: 1 }, { label: 'Field of View' }),
scale: PD.Numeric(1, { min: 0.001, max: 1, step: 0.001 }, { label: 'Scene scale' }),
manualReset: PD.Boolean(false, { isHidden: true }),
}, { pivot: 'mode' }),
cameraFog: PD.MappedStatic('on', {
@@ -107,6 +110,8 @@ export const Canvas3DParams = {
interaction: PD.Group(Canvas3dInteractionHelperParams),
debug: PD.Group(DebugHelperParams),
handle: PD.Group(HandleHelperParams),
pointer: PD.Group(PointerHelperParams),
xr: PD.Group(XRManagerParams, { label: 'XR' }),
};
export const DefaultCanvas3DParams = PD.getDefaultValues(Canvas3DParams);
export type Canvas3DProps = PD.Values<typeof Canvas3DParams>
@@ -114,6 +119,12 @@ export type PartialCanvas3DProps = {
[K in keyof Canvas3DProps]?: Canvas3DProps[K] extends { name: string, params: any } ? Canvas3DProps[K] : Partial<Canvas3DProps[K]>
}
export const DefaultCanvas3DAttribs = {
trackball: DefaultTrackballControlsAttribs,
xr: DefaultXRManagerAttribs,
};
export type Canvas3DAttribs = typeof DefaultCanvas3DAttribs
export { Canvas3DContext };
/** Can be used to create multiple Canvas3D objects */
@@ -297,6 +308,9 @@ namespace Canvas3DContext {
canvas.removeEventListener('webglcontextlost', handleWebglContextLost, false);
canvas.removeEventListener('webglcontextrestored', handlewWebglContextRestored, false);
webgl.destroy(options);
contextLost.complete();
changed.complete();
}
};
}
@@ -317,7 +331,7 @@ interface Canvas3D {
* Function for external "animation" control
* Calls commit.
*/
tick(t: now.Timestamp, options?: { isSynchronous?: boolean, manualDraw?: boolean, updateControls?: boolean }): void
tick(t: now.Timestamp, options?: { isSynchronous?: boolean, manualDraw?: boolean, updateControls?: boolean, xrFrame?: XRFrame }): void
update(repr?: Representation.Any, keepBoundingSphere?: boolean): void
clear(): void
syncVisibility(): void
@@ -334,6 +348,10 @@ interface Canvas3D {
pause(noDraw?: boolean): void
/** Sets drawPaused = false without starting the built in animation loop */
resume(): void
requestAnimationFrame(callback: FrameRequestCallback | XRFrameRequestCallback): number
cancelAnimationFrame(handle: number): void
identify(target: Vec2 | Ray3D): PickData | undefined
asyncIdentify(target: Vec2 | Ray3D): AsyncPickData | undefined
mark(loci: Representation.Loci, action: MarkerAction): void
@@ -360,10 +378,19 @@ interface Canvas3D {
/** Returns a copy of the current Canvas3D instance props */
readonly props: Readonly<Canvas3DProps>
readonly attribs: Readonly<Canvas3DAttribs>
readonly input: InputObserver
readonly stats: RendererStats
readonly interaction: Canvas3dInteractionHelper['events']
readonly xr: {
request(): Promise<void>
end(): Promise<void>
readonly isSupported: BehaviorSubject<boolean>
readonly isPresenting: BehaviorSubject<boolean>
readonly requestFailed: Subject<string>
}
dispose(): void
}
@@ -379,9 +406,10 @@ namespace Canvas3D {
export interface DragEvent { current: Representation.Loci, buttons: ButtonsType, button: ButtonsType.Flag, modifiers: ModifiersKeys, pageStart: Vec2, pageEnd: Vec2 }
export interface ClickEvent { current: Representation.Loci, buttons: ButtonsType, button: ButtonsType.Flag, modifiers: ModifiersKeys, page?: Vec2, position?: Vec3 }
export function create(ctx: Canvas3DContext, props: Partial<Canvas3DProps> = {}): Canvas3D {
export function create(ctx: Canvas3DContext, props: Partial<Canvas3DProps> = {}, attribs: Partial<Canvas3DAttribs> = {}): Canvas3D {
const { webgl, input, passes, assetManager, canvas, contextLost } = ctx;
const p: Canvas3DProps = { ...deepClone(DefaultCanvas3DParams), ...deepClone(props) };
const a = { ...deepClone(DefaultCanvas3DAttribs), ...deepClone(attribs) };
const reprRenderObjects = new Map<Representation.Any, Set<GraphicsRenderObject>>();
const reprUpdatedSubscriptions = new Map<Representation.Any, Subscription>();
@@ -404,7 +432,10 @@ namespace Canvas3D {
let currentTime = 0;
updateViewport();
const scene = Scene.create(webgl, passes.draw.transparency);
const scene = Scene.create(webgl, passes.draw.transparency, {
dColorMarker: p.renderer.colorMarker,
dLightCount: p.renderer.light?.length,
});
function getSceneRadius() {
return scene.boundingSphere.radius * p.sceneRadiusFactor;
@@ -417,17 +448,19 @@ namespace Canvas3D {
clipFar: p.cameraClipping.far,
minNear: p.cameraClipping.minNear,
fov: degToRad(p.camera.fov),
scale: p.camera.scale,
}, { x, y, width, height });
const stereoCamera = new StereoCamera(camera, p.camera.stereo.params);
const controls = TrackballControls.create(input, camera, scene, p.trackball);
const controls = TrackballControls.create(input, camera, scene, p.trackball, a.trackball);
const helper = new Helper(webgl, scene, p);
const hiZ = new HiZPass(webgl, passes.draw, canvas, p.hiZ);
const renderer = Renderer.create(webgl, p.renderer);
renderer.setOcclusionTest(hiZ.isOccluded);
const shaderManager = new ShaderManager(webgl, scene);
shaderManager.updateRequired(p);
const pickOptions = {
pickPadding: p.pickPadding,
maxAsyncReadLag: DefaultPickOptions.maxAsyncReadLag,
@@ -446,6 +479,85 @@ namespace Canvas3D {
let nextCameraResetSnapshot: Camera.SnapshotProvider | undefined = void 0;
let resizeRequested = false;
//
function getNonXRProps() {
return {
transparency: ctx.props.transparency,
transparentBackground: p.transparentBackground,
hiZ: hiZ.props.enabled,
postprocessing: p.postprocessing.enabled,
axes: deepClone(helper.camera.props.axes),
};
}
const nonXRProps = getNonXRProps();
function saveNonXRProps() {
Object.assign(nonXRProps, getNonXRProps());
}
function loadNonXRProps() {
p.postprocessing.enabled = nonXRProps.postprocessing;
p.transparentBackground = nonXRProps.transparentBackground;
ctx.setProps({ transparency: nonXRProps.transparency });
hiZ.setProps({ enabled: nonXRProps.hiZ });
helper.camera.setProps({ axes: nonXRProps.axes });
}
function setXRProps() {
p.postprocessing.enabled = !xrManager.props.disablePostprocessing;
ctx.setProps({ transparency: 'blended' });
hiZ.setProps({ enabled: false });
helper.camera.setProps({ axes: { name: 'off', params: {} } });
if (xrManager.session?.environmentBlendMode === 'alpha-blend') {
p.transparentBackground = xrPassthrough;
}
}
const xrManager = new XRManager(webgl, input, scene, camera, stereoCamera, helper.pointer, interactionHelper);
const xr = {
request: async () => {
try {
await xrManager.request();
} catch (e) {
console.error(e);
xr.requestFailed.next(e);
}
},
end: () => xrManager.end(),
isSupported: new BehaviorSubject(false),
isPresenting: new BehaviorSubject(false),
requestFailed: new Subject<string>(),
};
let xrPassthrough = false;
const xrSubs = [
xrManager.isSupported.subscribe(e => xr.isSupported.next(e)),
xrManager.togglePassthrough.subscribe(() => {
if (xrManager.session?.environmentBlendMode === 'alpha-blend') {
xrPassthrough = !p.transparentBackground;
}
}),
xrManager.sessionChanged.subscribe(() => {
fenceSync = null;
resizeRequested = true;
if (xrManager.session) {
saveNonXRProps();
xrPassthrough = xrManager.session?.environmentBlendMode === 'alpha-blend';
setXRProps();
} else {
loadNonXRProps();
}
resume();
xr.isPresenting.next(!!xrManager.session);
}),
];
//
let notifyDidDraw = true;
function getLoci(pickingId: PickingId | undefined) {
@@ -491,6 +603,9 @@ namespace Canvas3D {
helper.handle.scene.update(void 0, true);
helper.camera.scene.update(void 0, true);
shaderManager.updateRequired(p);
shaderManager.finalizeRequired(true);
interactionEvent.next();
}
return changed;
@@ -511,8 +626,9 @@ namespace Canvas3D {
let fenceSync: WebGLSync | null = null;
function render(force: boolean) {
function render(force: boolean, xrFrame?: XRFrame) {
if (webgl.isContextLost) return false;
if (webgl.xr.session && !xrFrame) return false;
let resized = false;
if (resizeRequested) {
@@ -526,7 +642,7 @@ namespace Canvas3D {
y > drs.height || y + height < 0
) return false;
if (fenceSync !== null) {
if (fenceSync !== null && !xrFrame) {
if (webgl.checkSyncStatus(fenceSync)) {
fenceSync = null;
} else {
@@ -534,22 +650,29 @@ namespace Canvas3D {
}
}
if (xrFrame) {
setXRProps();
p.transparentBackground = xrPassthrough;
}
const markingUpdated = resolveMarking() && (renderer.props.colorMarker || p.marking.enabled);
let didRender = false;
controls.update(currentTime);
const cameraChanged = camera.update();
const xrChanged = xrManager.update(xrFrame);
if (!xrChanged && xrFrame) return false;
const shouldRender = force || cameraChanged || resized || forceNextRender;
const shouldRender = force || cameraChanged || resized || forceNextRender || xrChanged;
forceNextRender = false;
if (passes.illumination.supported && p.illumination.enabled) {
if (passes.illumination.supported && p.illumination.enabled && !xrFrame) {
if (shouldRender || markingUpdated) {
renderer.setOcclusionTest(null);
passes.illumination.restart();
}
if (passes.illumination.shouldRender(p)
if (passes.illumination.shouldRender(p.illumination)
&& ((!isActivelyInteracting && scene.count > 0) || passes.illumination.iteration === 0 || p.userInteractionReleaseMs === 0)
) {
if (isTimingMode) webgl.timer.mark('Canvas3D.render', { captureStats: true });
@@ -562,20 +685,20 @@ namespace Canvas3D {
didRender = true;
}
} else {
const multiSampleChanged = multiSampleHelper.update(markingUpdated || shouldRender, p.multiSample);
const multiSampleChanged = multiSampleHelper.update(markingUpdated || shouldRender, p.multiSample) && !xrFrame;
if (shouldRender || multiSampleChanged || markingUpdated) {
renderer.setOcclusionTest(hiZ.isOccluded);
let cam: Camera | StereoCamera = camera;
if (p.camera.stereo.name === 'on') {
stereoCamera.update();
if (p.camera.stereo.name === 'on' || xrChanged) {
if (!xrChanged) stereoCamera.update();
cam = stereoCamera;
}
if (isTimingMode) webgl.timer.mark('Canvas3D.render', { captureStats: true });
const ctx = { renderer, camera: cam, scene, helper };
if (MultiSamplePass.isEnabled(p.multiSample)) {
if (MultiSamplePass.isEnabled(p.multiSample) && !xrFrame) {
const forceOn = p.multiSample.reduceFlicker && !cameraChanged && markingUpdated && !controls.isAnimating;
multiSampleHelper.render(ctx, p, true, forceOn);
} else {
@@ -590,7 +713,7 @@ namespace Canvas3D {
}
}
if (didRender) {
if (didRender && !xrFrame) {
fenceSync = webgl.getFenceSync();
}
@@ -601,9 +724,13 @@ namespace Canvas3D {
let drawPaused = false;
let isContextLost = false;
function draw(options?: { force?: boolean }) {
function draw(options?: { force?: boolean, isSynchronous?: boolean, xrFrame?: XRFrame }) {
if (drawPaused || isContextLost) return;
if (render(!!options?.force) && notifyDidDraw) {
if (!shaderManager.finalizeRequired(options?.isSynchronous)) {
forceNextRender = true;
return;
}
if (render(!!options?.force, options?.xrFrame) && notifyDidDraw) {
didDraw.next(now() - startTime as now.Timestamp);
}
}
@@ -614,8 +741,9 @@ namespace Canvas3D {
let animationFrameHandle = 0;
function tick(t: now.Timestamp, options?: { isSynchronous?: boolean, manualDraw?: boolean, updateControls?: boolean }) {
function tick(t: now.Timestamp, options?: { isSynchronous?: boolean, manualDraw?: boolean, updateControls?: boolean, xrFrame?: XRFrame }) {
if (isContextLost) return;
if (webgl.xr.session && !options?.xrFrame) return;
currentTime = t;
commit(options?.isSynchronous);
@@ -632,15 +760,31 @@ namespace Canvas3D {
return;
}
draw();
draw({ isSynchronous: options?.isSynchronous, xrFrame: options?.xrFrame });
if (!camera.transition.inTransition && !webgl.isContextLost) {
interactionHelper.tick(currentTime);
}
}
function _animate() {
tick(now());
animationFrameHandle = requestAnimationFrame(_animate);
let animationFrameCB: FrameRequestCallback | XRFrameRequestCallback | undefined = undefined;
function _requestAnimationFrame(callback: FrameRequestCallback | XRFrameRequestCallback): number {
animationFrameCB = callback;
return webgl.xr.session
? webgl.xr.session.requestAnimationFrame(callback)
: requestAnimationFrame(callback as FrameRequestCallback);
}
function _cancelAnimationFrame(handle: number): void {
animationFrameCB = undefined;
webgl.xr.session
? webgl.xr.session.cancelAnimationFrame(handle)
: cancelAnimationFrame(handle);
}
function _animate(_timestamp: number, xrFrame?: XRFrame) {
tick(now(), { xrFrame });
animationFrameHandle = _requestAnimationFrame(_animate);
}
function resetTime(t: now.Timestamp) {
@@ -651,17 +795,25 @@ namespace Canvas3D {
function animate() {
drawPaused = false;
controls.start(now());
if (animationFrameHandle === 0) _animate();
if (animationFrameHandle === 0) _animate(0);
}
function pause(noDraw = false) {
drawPaused = noDraw;
cancelAnimationFrame(animationFrameHandle);
animationFrameHandle = 0;
if (animationFrameHandle !== 0) {
_cancelAnimationFrame(animationFrameHandle);
animationFrameHandle = 0;
}
}
function resume() {
drawPaused = false;
if (animationFrameCB) _requestAnimationFrame(animationFrameCB);
}
function identify(target: Vec2 | Ray3D): PickData | undefined {
if (webgl.isContextLost) return undefined;
shaderManager.finalize(['pick'], true);
if ('origin' in target) {
return rayHelper.identify(target, camera);
@@ -673,6 +825,7 @@ namespace Canvas3D {
function asyncIdentify(target: Vec2 | Ray3D): AsyncPickData | undefined {
if (webgl.isContextLost) return undefined;
shaderManager.finalize(['pick'], true);
if ('origin' in target) {
return rayHelper.asyncIdentify(target, camera);
@@ -684,6 +837,7 @@ namespace Canvas3D {
function commit(isSynchronous: boolean = false) {
const allCommited = commitScene(isSynchronous);
shaderManager.updateRequired(p);
// Only reset the camera after the full scene has been commited.
if (allCommited) {
resolveCameraReset();
@@ -699,6 +853,10 @@ namespace Canvas3D {
function resolveCameraReset() {
if (!cameraResetRequested) return;
if (!xr.isPresenting.value) {
xrManager.resetScale();
}
const boundingSphere = scene.boundingSphereVisible;
const { center, radius } = boundingSphere;
@@ -785,25 +943,42 @@ namespace Canvas3D {
instanceCount: r.values.instanceCount.ref.value,
materialId: r.materialId,
renderItemId: r.id,
geometryType: r.values.dGeometryType.ref.value,
'byteCount [MiB]': toFixed(r.getByteCount() / 1024 / 1024, 3),
}));
console.groupCollapsed(`${items.length} RenderItems`);
if (items.length < 50) {
if (items.length <= 64) {
console.table(items);
} else {
console.log(items);
}
console.log(JSON.stringify(webgl.stats, undefined, 4));
const { texture, attribute, elements } = webgl.resources.getByteCounts();
const { texture, cubeTexture, attribute, elements, pixelPack, renderbuffer } = webgl.resources.getByteCounts();
console.log(JSON.stringify({
texture: `${(texture / 1024 / 1024).toFixed(3)} MiB`,
cubeTexture: `${(cubeTexture / 1024 / 1024).toFixed(3)} MiB`,
attribute: `${(attribute / 1024 / 1024).toFixed(3)} MiB`,
elements: `${(elements / 1024 / 1024).toFixed(3)} MiB`,
pixelPack: `${(pixelPack / 1024 / 1024).toFixed(3)} MiB`,
renderbuffer: `${(renderbuffer / 1024 / 1024).toFixed(3)} MiB`,
}, undefined, 4));
console.log(JSON.stringify(webgl.timer.formatedStats(), undefined, 4));
console.log(JSON.stringify({
renderables: `${(scene.renderables.reduce((sum, r) => sum + r.getByteCount(), 0) / 1024 / 1024).toFixed(3)} MiB`,
passes: {
draw: `${(passes.draw.getByteCount() / 1024 / 1024).toFixed(3)} MiB`,
illumination: `${(passes.illumination.getByteCount() / 1024 / 1024).toFixed(3)} MiB`,
pick: `${(passes.pick.getByteCount() / 1024 / 1024).toFixed(3)} MiB`,
hiZ: `${(hiZ.getByteCount() / 1024 / 1024).toFixed(3)} MiB`,
}
}, undefined, 4));
if (isTimingMode) {
console.log(JSON.stringify(webgl.timer.formatedStats(), undefined, 4));
}
console.groupEnd();
}
@@ -869,13 +1044,16 @@ namespace Canvas3D {
helper: { ...helper.camera.props },
stereo: { ...p.camera.stereo },
fov: Math.round(radToDeg(camera.state.fov)),
scale: camera.state.scale,
manualReset: !!p.camera.manualReset
},
cameraFog: camera.state.fog > 0
? { name: 'on' as const, params: { intensity: camera.state.fog } }
: { name: 'off' as const, params: {} },
cameraClipping: { far: camera.state.clipFar, radius, minNear: camera.state.minNear },
cameraClipping: {
far: camera.state.clipFar,
radius,
minNear: camera.state.minNear,
},
cameraResetDurationMs: p.cameraResetDurationMs,
sceneRadiusFactor: p.sceneRadiusFactor,
transparentBackground: p.transparentBackground,
@@ -894,6 +1072,8 @@ namespace Canvas3D {
interaction: { ...interactionHelper.props },
debug: { ...helper.debug.props },
handle: { ...helper.handle.props },
pointer: { ...helper.pointer.props },
xr: { ...xrManager.props },
};
}
@@ -944,7 +1124,7 @@ namespace Canvas3D {
// Monitor user interactions
let isDragging = false;
let isActivelyInteracting = false;
let interactionSubs = [
const interactionSubs = [
input.drag.subscribe(() => {
isDragging = true;
}),
@@ -1051,7 +1231,11 @@ namespace Canvas3D {
animate,
resetTime,
pause,
resume: () => { drawPaused = false; },
resume,
requestAnimationFrame: _requestAnimationFrame,
cancelAnimationFrame: _cancelAnimationFrame,
identify,
asyncIdentify,
mark,
@@ -1094,9 +1278,6 @@ namespace Canvas3D {
if (props.camera && props.camera.fov !== undefined && props.camera.fov !== oldFov) {
cameraState.fov = degToRad(props.camera.fov);
}
if (props.camera && props.camera.scale !== undefined && props.camera.scale !== cameraState.scale) {
cameraState.scale = props.camera.scale;
}
if (props.cameraFog !== undefined && props.cameraFog.params) {
const newFog = props.cameraFog.name === 'on' ? props.cameraFog.params.intensity : 0;
if (newFog !== camera.state.fog) cameraState.fog = newFog;
@@ -1154,16 +1335,25 @@ namespace Canvas3D {
if (props.illumination) Object.assign(p.illumination, props.illumination);
if (props.multiSample) Object.assign(p.multiSample, props.multiSample);
if (props.hiZ) hiZ.setProps(props.hiZ);
if (props.renderer) renderer.setProps(props.renderer);
if (props.renderer) {
scene.setGlobals({
dColorMarker: props.renderer.colorMarker ?? renderer.props.colorMarker,
dLightCount: props.renderer.light?.length ?? renderer.props.light.length,
});
renderer.setProps(props.renderer);
}
if (props.trackball) controls.setProps(props.trackball);
if (props.interaction) interactionHelper.setProps(props.interaction);
if (props.debug) helper.debug.setProps(props.debug);
if (props.handle) helper.handle.setProps(props.handle);
if (props.pointer) helper.pointer.setProps(props.pointer);
if (props.xr) xrManager.setProps(props.xr);
if (cameraState.mode === 'orthographic') {
p.camera.stereo.name = 'off';
}
shaderManager.updateRequired(p);
if (!doNotRequestDraw) {
requestDraw();
}
@@ -1180,6 +1370,9 @@ namespace Canvas3D {
get props() {
return getProps();
},
get attribs() {
return a;
},
get input() {
return input;
},
@@ -1189,15 +1382,20 @@ namespace Canvas3D {
get interaction() {
return interactionHelper.events;
},
xr,
dispose: () => {
contextLostSub?.unsubscribe();
contextRestoredSub.unsubscribe();
ctxChangedSub?.unsubscribe();
for (const s of xrSubs) s.unsubscribe();
xrSubs.length = 0;
for (const s of interactionSubs) s.unsubscribe();
interactionSubs = [];
interactionSubs.length = 0;
cancelAnimationFrame(animationFrameHandle);
animationFrameCB = undefined;
markBuffer = [];
@@ -1209,12 +1407,23 @@ namespace Canvas3D {
hiZ.dispose();
pickHelper.dispose();
rayHelper.dispose();
xrManager.dispose();
if (fenceSync !== null) {
webgl.deleteSync(fenceSync);
fenceSync = null;
}
reprCount.complete();
interactionEvent.complete();
didDraw.complete();
resized.complete();
commited.complete();
commitQueueSize.complete();
xr.isPresenting.complete();
xr.isSupported.complete();
xr.requestFailed.complete();
removeConsoleStatsProvider(consoleStats);
}
};

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2018-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author David Sehnal <david.sehnal@gmail.com>
@@ -24,7 +24,10 @@ const Trigger = Binding.Trigger;
const Key = Binding.TriggerKey;
export const DefaultTrackballBindings = {
dragRotate: Binding([Trigger(B.Flag.Primary, M.create())], 'Rotate', 'Drag using ${triggers}'),
dragRotate: Binding([
Trigger(B.Flag.Primary, M.create()),
Trigger(B.Flag.Trigger)
], 'Rotate', 'Drag using ${triggers}'),
dragRotateZ: Binding([Trigger(B.Flag.Primary, M.create({ shift: true, control: true }))], 'Rotate around z-axis (roll)', 'Drag using ${triggers}'),
dragPan: Binding([
Trigger(B.Flag.Secondary, M.create()),
@@ -38,8 +41,14 @@ export const DefaultTrackballBindings = {
scrollFocus: Binding([Trigger(B.Flag.Auxilary, M.create({ shift: true }))], 'Clip', 'Scroll using ${triggers}'),
scrollFocusZoom: Binding.Empty,
keyMoveForward: Binding([Key('KeyW')], 'Move forward', 'Press ${triggers}'),
keyMoveBack: Binding([Key('KeyS')], 'Move back', 'Press ${triggers}'),
keyMoveForward: Binding([
Key('KeyW'),
Key('GamepadUp'),
], 'Move forward', 'Press ${triggers}'),
keyMoveBack: Binding([
Key('KeyS'),
Key('GamepadDown'),
], 'Move back', 'Press ${triggers}'),
keyMoveLeft: Binding([Key('KeyA')], 'Move left', 'Press ${triggers}'),
keyMoveRight: Binding([Key('KeyD')], 'Move right', 'Press ${triggers}'),
keyMoveUp: Binding([Key('KeyR')], 'Move up', 'Press ${triggers}'),
@@ -56,8 +65,6 @@ export const DefaultTrackballBindings = {
};
export const TrackballControlsParams = {
noScroll: PD.Boolean(true, { isHidden: true }),
rotateSpeed: PD.Numeric(5.0, { min: 1, max: 10, step: 1 }),
zoomSpeed: PD.Numeric(7.0, { min: 1, max: 15, step: 1 }),
panSpeed: PD.Numeric(1.0, { min: 0.1, max: 5, step: 0.1 }),
@@ -85,8 +92,6 @@ export const TrackballControlsParams = {
gestureScaleFactor: PD.Numeric(1, {}, { isHidden: true }),
maxWheelDelta: PD.Numeric(0.02, {}, { isHidden: true }),
bindings: PD.Value(DefaultTrackballBindings, { isHidden: true }),
/**
* minDistance = minDistanceFactor * boundingSphere.radius + minDistancePadding
* maxDistance = max(maxDistanceFactor * boundingSphere.radius, maxDistanceMin)
@@ -103,6 +108,11 @@ export const TrackballControlsParams = {
};
export type TrackballControlsProps = PD.Values<typeof TrackballControlsParams>
export const DefaultTrackballControlsAttribs = {
bindings: DefaultTrackballBindings,
};
export type TrackballControlsAttribs = typeof DefaultTrackballControlsAttribs
export { TrackballControls };
interface TrackballControls {
readonly viewport: Viewport
@@ -112,20 +122,25 @@ interface TrackballControls {
readonly props: Readonly<TrackballControlsProps>
setProps: (props: Partial<TrackballControlsProps>) => void
readonly attribs: Readonly<TrackballControlsAttribs>
setAttribs: (attribs: Partial<TrackballControlsAttribs>) => void
start: (t: number) => void
update: (t: number) => void
reset: () => void
dispose: () => void
}
namespace TrackballControls {
export function create(input: InputObserver, camera: Camera, scene: Scene, props: Partial<TrackballControlsProps> = {}): TrackballControls {
export function create(input: InputObserver, camera: Camera, scene: Scene, props: Partial<TrackballControlsProps> = {}, attribs: Partial<TrackballControlsAttribs> = {}): TrackballControls {
const p: TrackballControlsProps = {
...PD.getDefaultValues(TrackballControlsParams),
...props,
// include default bindings for backwards state compatibility
bindings: { ...DefaultTrackballBindings, ...props.bindings }
};
const b = p.bindings;
const a: TrackballControlsAttribs = {
...DefaultTrackballControlsAttribs,
...attribs
};
const b = a.bindings;
const viewport = Viewport.clone(camera.viewport);
@@ -384,20 +399,35 @@ namespace TrackballControls {
const minDistance = Math.max(camera.state.minNear, p.minDistance);
Vec3.setMagnitude(moveEye, moveEye, minDistance);
const moveTarget = p.flyMode || input.pointerLock;
const moveSpeed = deltaT * (60 / 1000) * p.moveSpeed * (keyState.boostMove === 1 ? p.boostMoveFactor : 1);
if (keyState.moveForward === 1) {
Vec3.normalize(moveDir, moveEye);
Vec3.scaleAndSub(camera.position, camera.position, moveDir, moveSpeed);
if (p.flyMode || input.pointerLock) {
const cameraDistance = Vec3.distance(camera.position, scene.boundingSphereVisible.center);
if (cameraDistance < scene.boundingSphereVisible.radius && moveTarget) {
Vec3.normalize(moveDir, moveEye);
Vec3.scaleAndSub(camera.position, camera.position, moveDir, moveSpeed);
} else {
Vec3.sub(moveDir, camera.position, camera.target);
Vec3.scale(moveDir, moveDir, 1 - moveSpeed / 100);
Vec3.add(camera.position, camera.target, moveDir);
}
if (moveTarget) {
Vec3.sub(camera.target, camera.position, moveEye);
}
}
if (keyState.moveBack === 1) {
Vec3.normalize(moveDir, moveEye);
Vec3.scaleAndAdd(camera.position, camera.position, moveDir, moveSpeed);
if (p.flyMode || input.pointerLock) {
const cameraDistance = Vec3.distance(camera.position, scene.boundingSphereVisible.center);
if (cameraDistance < scene.boundingSphereVisible.radius && moveTarget) {
Vec3.normalize(moveDir, moveEye);
Vec3.scaleAndAdd(camera.position, camera.position, moveDir, moveSpeed);
} else {
Vec3.sub(moveDir, camera.position, camera.target);
Vec3.scale(moveDir, moveDir, 1 + moveSpeed / 100);
Vec3.add(camera.position, camera.target, moveDir);
}
if (moveTarget) {
Vec3.sub(camera.target, camera.position, moveEye);
}
}
@@ -405,7 +435,7 @@ namespace TrackballControls {
if (keyState.moveLeft === 1) {
Vec3.cross(moveDir, moveEye, camera.up);
Vec3.normalize(moveDir, moveDir);
if (p.flyMode || input.pointerLock) {
if (moveTarget) {
Vec3.scaleAndAdd(camera.position, camera.position, moveDir, moveSpeed);
Vec3.sub(camera.target, camera.position, moveEye);
} else {
@@ -417,7 +447,7 @@ namespace TrackballControls {
if (keyState.moveRight === 1) {
Vec3.cross(moveDir, moveEye, camera.up);
Vec3.normalize(moveDir, moveDir);
if (p.flyMode || input.pointerLock) {
if (moveTarget) {
Vec3.scaleAndSub(camera.position, camera.position, moveDir, moveSpeed);
Vec3.sub(camera.target, camera.position, moveEye);
} else {
@@ -428,7 +458,7 @@ namespace TrackballControls {
if (keyState.moveUp === 1) {
Vec3.normalize(moveDir, camera.up);
if (p.flyMode || input.pointerLock) {
if (moveTarget) {
Vec3.scaleAndAdd(camera.position, camera.position, moveDir, moveSpeed);
Vec3.sub(camera.target, camera.position, moveEye);
} else {
@@ -439,7 +469,7 @@ namespace TrackballControls {
if (keyState.moveDown === 1) {
Vec3.normalize(moveDir, camera.up);
if (p.flyMode || input.pointerLock) {
if (moveTarget) {
Vec3.scaleAndSub(camera.position, camera.position, moveDir, moveSpeed);
Vec3.sub(camera.target, camera.position, moveEye);
} else {
@@ -448,7 +478,7 @@ namespace TrackballControls {
}
}
if (p.flyMode || input.pointerLock) {
if (moveTarget) {
const cameraDistance = Vec3.distance(camera.position, scene.boundingSphereVisible.center);
camera.setState({ minFar: cameraDistance + scene.boundingSphereVisible.radius });
}
@@ -538,8 +568,8 @@ namespace TrackballControls {
// listeners
function onDrag({ x, y, pageX, pageY, buttons, modifiers, isStart }: DragInput) {
const isOutside = outsideViewport(x, y);
function onDrag({ x, y, dx, dy, pageX, pageY, buttons, modifiers, isStart, useDelta }: DragInput) {
const isOutside = !useDelta && outsideViewport(x, y);
if (isStart && isOutside) return;
if (!isStart && !_isInteracting) return;
@@ -554,6 +584,10 @@ namespace TrackballControls {
const dragFocus = Binding.match(b.dragFocus, buttons, modifiers);
const dragFocusZoom = Binding.match(b.dragFocusZoom, buttons, modifiers);
if (useDelta && dragRotate) {
Vec2.copy(_rotPrev, getMouseOnCircle(pageX - dx, pageY - dy));
}
getMouseOnCircle(pageX, pageY);
getMouseOnScreen(pageX, pageY);
@@ -885,7 +919,12 @@ namespace TrackballControls {
}
}
Object.assign(p, props);
Object.assign(b, props.bindings);
},
get attribs() { return a as Readonly<TrackballControlsAttribs>; },
setAttribs: (attribs: Partial<TrackballControlsAttribs>) => {
Object.assign(a, attribs);
Object.assign(b, a.bindings);
},
start,

View File

@@ -4,7 +4,7 @@
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { create as produce } from 'mutative';
import { produce } from '../../mol-util/produce';
import { Interval } from '../../mol-data/int/interval';
import { addCylinder } from '../../mol-geo/geometry/mesh/builder/cylinder';
import { addSphere } from '../../mol-geo/geometry/mesh/builder/sphere';

View File

@@ -16,7 +16,7 @@ import { addCylinder } from '../../mol-geo/geometry/mesh/builder/cylinder';
import { ValueCell } from '../../mol-util';
import { Sphere3D } from '../../mol-math/geometry';
import { ParamDefinition as PD } from '../../mol-util/param-definition';
import { create as produce } from 'mutative';
import { produce } from '../../mol-util/produce';
import { Shape } from '../../mol-model/shape';
import { PickingId } from '../../mol-geo/geometry/picking';
import { Camera } from '../camera';

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2020-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -10,6 +10,7 @@ import { ParamDefinition as PD } from '../../mol-util/param-definition';
import { BoundingSphereHelper, DebugHelperParams } from './bounding-sphere-helper';
import { CameraHelper, CameraHelperParams } from './camera-helper';
import { HandleHelper, HandleHelperParams } from './handle-helper';
import { PointerHelper, PointerHelperParams } from './pointer-helper';
export const HelperParams = {
debug: PD.Group(DebugHelperParams),
@@ -17,6 +18,7 @@ export const HelperParams = {
helper: PD.Group(CameraHelperParams)
}),
handle: PD.Group(HandleHelperParams),
pointer: PD.Group(PointerHelperParams),
};
export const DefaultHelperProps = PD.getDefaultValues(HelperParams);
export type HelperProps = PD.Values<typeof HelperParams>
@@ -26,6 +28,7 @@ export class Helper {
readonly debug: BoundingSphereHelper;
readonly camera: CameraHelper;
readonly handle: HandleHelper;
readonly pointer: PointerHelper;
constructor(webgl: WebGLContext, scene: Scene, props: Partial<HelperProps> = {}) {
const p = { ...DefaultHelperProps, ...props };
@@ -33,5 +36,6 @@ export class Helper {
this.debug = new BoundingSphereHelper(webgl, scene, p.debug);
this.camera = new CameraHelper(webgl, p.camera.helper);
this.handle = new HandleHelper(webgl, p.handle);
this.pointer = new PointerHelper(webgl, p.pointer);
}
}

View File

@@ -0,0 +1,196 @@
/**
* Copyright (c) 2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { WebGLContext } from '../../mol-gl/webgl/context';
import { Scene } from '../../mol-gl/scene';
import { MeshBuilder } from '../../mol-geo/geometry/mesh/mesh-builder';
import { Mat4, Vec3 } from '../../mol-math/linear-algebra';
import { GraphicsRenderObject } from '../../mol-gl/render-object';
import { Mesh } from '../../mol-geo/geometry/mesh/mesh';
import { ColorNames } from '../../mol-util/color/names';
import { ValueCell } from '../../mol-util';
import { ParamDefinition as PD } from '../../mol-util/param-definition';
import { Geometry } from '../../mol-geo/geometry/geometry';
import { addCylinderFromRay3D } from '../../mol-geo/geometry/mesh/builder/cylinder';
import { addSphere } from '../../mol-geo/geometry/mesh/builder/sphere';
import { Camera, ICamera } from '../camera';
import { Ray3D } from '../../mol-math/geometry/primitives/ray3d';
import { Viewport } from '../camera/util';
import { Shape } from '../../mol-model/shape/shape';
export const PointerHelperParams = {
...Mesh.Params,
enabled: PD.Select('off', PD.arrayToOptions(['on', 'off']), { isEssential: true }),
ignoreLight: { ...Mesh.Params.ignoreLight, defaultValue: true },
color: PD.Color(ColorNames.grey, { isEssential: true }),
hitColor: PD.Color(ColorNames.pink, { isEssential: true }),
};
export type PointerHelperParams = typeof PointerHelperParams
export type PointerHelperProps = PD.Values<PointerHelperParams>
export class PointerHelper {
readonly scene: Scene;
readonly camera: Camera;
readonly props: PointerHelperProps;
pixelScale = 1;
private renderObject: GraphicsRenderObject<'mesh'>;
private shape: Shape<Mesh>;
private modelScale = 1;
private pointers: Ray3D[] = [];
private points: Vec3[] = [];
private hit: Vec3 | undefined = undefined;
setProps(props: Partial<PointerHelperProps>) {
Object.assign(this.props, props);
if (this.isEnabled) this.update(this.pointers, this.points, this.hit);
}
ensureEnabled() {
if (this.props.enabled !== 'on') this.props.enabled = 'on';
}
get isEnabled() {
return this.props.enabled === 'on';
}
setCamera(camera: ICamera) {
Camera.copySnapshot(this.camera.state, camera.state);
Viewport.copy(this.camera.viewport, camera.viewport);
Mat4.copy(this.camera.view, camera.view);
Mat4.copy(this.camera.projection, camera.projection);
Mat4.copy(this.camera.projectionView, camera.projectionView);
Mat4.copy(this.camera.headRotation, camera.headRotation);
Camera.copyViewOffset(this.camera.viewOffset, camera.viewOffset);
this.camera.far = camera.far;
this.camera.near = camera.near;
this.camera.fogFar = camera.fogFar;
this.camera.fogNear = camera.fogNear;
this.camera.forceFull = camera.forceFull;
this.camera.scale = 1;
this.modelScale = camera.scale;
}
update(pointers: Ray3D[], points: Vec3[], hit: Vec3 | undefined) {
this.pointers = pointers;
this.points = points;
this.hit = hit;
const p = this.props;
if (p.enabled !== 'on') {
if (this.renderObject) this.renderObject.state.visible = false;
return;
}
this.shape = getPointerMeshShape(this.getData(), this.props, this.shape);
ValueCell.updateIfChanged(this.renderObject.values.drawCount, Geometry.getDrawCount(this.shape.geometry));
ValueCell.updateIfChanged(this.renderObject.values.uVertexCount, Geometry.getVertexCount(this.shape.geometry));
ValueCell.updateIfChanged(this.renderObject.values.uGroupCount, 2);
Mesh.Utils.updateBoundingSphere(this.renderObject.values, this.shape.geometry);
Mesh.Utils.updateValues(this.renderObject.values, this.props);
Mesh.Utils.updateRenderableState(this.renderObject.state, this.props);
this.renderObject.state.visible = true;
this.scene.update(void 0, false);
this.scene.commit();
}
private getData() {
return {
pointers: this.pointers,
points: this.points,
hit: this.hit,
modelScale: this.modelScale,
camera: this.camera,
pixels: 12,
};
}
constructor(webgl: WebGLContext, props: Partial<PointerHelperProps> = {}) {
this.scene = Scene.create(webgl, 'blended');
this.props = { ...PD.getDefaultValues(PointerHelperParams), ...props };
this.camera = new Camera();
this.shape = getPointerMeshShape(this.getData(), this.props, this.shape);
this.renderObject = createMeshRenderObject(this.shape, this.props);
this.scene.add(this.renderObject);
}
}
type PointerData = {
pointers: Ray3D[]
points: Vec3[]
hit?: Vec3
modelScale: number
camera: ICamera
pixels: number
}
export enum PointerHelperGroup {
None = 0,
Hit,
}
const tmpV = Vec3();
function getSizeForPixels(position: Vec3, pixels: number, camera: ICamera, modelScale: number) {
const cameraPosition = Vec3.scale(tmpV, camera.state.position, modelScale);
const d = Vec3.distance(position, cameraPosition);
const height = 2 * Math.tan(camera.state.fov / 2) * d;
return (height / camera.viewport.height) * pixels;
};
function createPointerMesh(data: PointerData, mesh?: Mesh) {
const state = MeshBuilder.createState(512, 256, mesh);
const radius = 0.0005;
const cylinderProps = { radiusTop: radius, radiusBottom: radius, radialSegments: 32 };
const { modelScale, camera, pixels } = data;
state.currentGroup = PointerHelperGroup.None;
for (const pointer of data.pointers) {
addCylinderFromRay3D(state, pointer, 0.2, cylinderProps);
const size = getSizeForPixels(pointer.origin, pixels, camera, modelScale);
addSphere(state, pointer.origin, size, 1);
}
for (const point of data.points) {
const size = getSizeForPixels(point, pixels, camera, modelScale);
addSphere(state, point, size, 1);
}
if (data.hit) {
state.currentGroup = PointerHelperGroup.Hit;
const size = getSizeForPixels(data.hit, pixels, camera, modelScale);
addSphere(state, data.hit, size, 1);
}
return MeshBuilder.getMesh(state);
}
function getPointerMeshShape(data: PointerData, props: PointerHelperProps, shape?: Shape<Mesh>) {
const mesh = createPointerMesh(data, shape?.geometry);
const getColor = (groupId: number) => {
switch (groupId) {
case PointerHelperGroup.Hit: return props.hitColor;
default: return props.color;
}
};
return Shape.create('pointer-mesh', data, mesh, getColor, () => 1, () => '', undefined, 2);
}
function createMeshRenderObject(shape: Shape<Mesh>, props: PointerHelperProps) {
return Shape.createRenderObject(shape, {
...PD.getDefaultValues(Mesh.Params),
...props,
ignoreLight: props.ignoreLight,
cellSize: 0,
}) as GraphicsRenderObject<'mesh'>;
}

View File

@@ -80,12 +80,16 @@ export class RayHelper {
this.camera.near = cam.near;
this.camera.fogFar = cam.fogFar;
this.camera.fogNear = cam.fogNear;
this.camera.forceFull = cam.forceFull;
this.camera.scale = cam.scale;
Viewport.copy(this.camera.viewport, this.viewport);
Camera.copySnapshot(this.camera.state, { ...cam.state, mode: 'orthographic' });
updateOrthoRayCamera(this.camera, ray);
updateOrthoRayCamera(this.camera, ray, cam.up);
Mat4.mul(this.camera.projectionView, this.camera.projection, this.camera.view);
Mat4.tryInvert(this.camera.inverseProjectionView, this.camera.projectionView);
Mat4.copy(this.camera.viewEye, cam.view);
}
private getPickData(): PickData | undefined {
@@ -104,7 +108,7 @@ export class RayHelper {
}
identify(ray: Ray3D, cam: Camera): PickData | undefined {
if (!this.intersectsScene(ray, cam.state.scale)) return;
if (!this.intersectsScene(ray, cam.scale)) return;
this.prepare(ray, cam);
@@ -117,7 +121,7 @@ export class RayHelper {
}
asyncIdentify(ray: Ray3D, cam: Camera): AsyncPickData | undefined {
if (!this.intersectsScene(ray, cam.state.scale)) return;
if (!this.intersectsScene(ray, cam.scale)) return;
this.prepare(ray, cam);
@@ -168,10 +172,10 @@ export class RayHelper {
//
function updateOrthoRayCamera(camera: Camera, ray: Ray3D) {
function updateOrthoRayCamera(camera: Camera, ray: Ray3D, up: Vec3) {
const { near, far, viewport } = camera;
const height = 2 * Math.tan(degToRad(0.1) / 2) * Vec3.distance(camera.position, camera.target) * camera.state.scale;
const height = 2 * Math.tan(degToRad(0.1) / 2) * Vec3.distance(camera.position, camera.target) * camera.scale;
const zoom = viewport.height / height;
const fullLeft = -viewport.width / 2;
@@ -197,7 +201,6 @@ function updateOrthoRayCamera(camera: Camera, ray: Ray3D) {
Quat.invert(r, r);
const eye = Vec3.clone(ray.origin);
const up = Vec3.transformQuat(Vec3(), Vec3.unitY, r);
const target = Vec3.add(Vec3(), eye, direction);
// build view matrix

View File

@@ -0,0 +1,57 @@
/**
* Copyright (c) 2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { Scene } from '../../mol-gl/scene';
import { WebGLContext } from '../../mol-gl/webgl/context';
import { GraphicsRenderVariant } from '../../mol-gl/webgl/render-item';
import { BloomPass } from '../passes/bloom';
import { IlluminationPass, IlluminationProps } from '../passes/illumination';
import { MarkingPass, MarkingProps } from '../passes/marking';
import { PostprocessingPass, PostprocessingProps } from '../passes/postprocessing';
export type ShaderManagerProps = {
marking: MarkingProps
postprocessing: PostprocessingProps
illumination: IlluminationProps
}
export class ShaderManager {
static ensureRequired(webgl: WebGLContext, scene: Scene, p: ShaderManagerProps) {
const sm = new ShaderManager(webgl, scene);
sm.updateRequired(p);
sm.finalizeRequired(true);
}
private readonly required: GraphicsRenderVariant[] = [];
constructor(private readonly webgl: WebGLContext, private readonly scene: Scene) { }
updateRequired(p: ShaderManagerProps) {
this.required.length = 0;
this.required.push('color');
if (IlluminationPass.isEnabled(this.webgl, p.illumination)) {
this.required.push('tracing');
}
if (MarkingPass.isEnabled(p.marking) && this.scene.markerAverage > 0) {
this.required.push('marking');
}
if (BloomPass.isEnabled(p.postprocessing) && this.scene.emissiveAverage > 0) {
this.required.push('emissive');
}
if (PostprocessingPass.isTransparentDepthRequired(this.scene, p.postprocessing) || !this.webgl.extensions.drawBuffers || !this.webgl.extensions.depthTexture || IlluminationPass.isEnabled(this.webgl, p.illumination)) {
this.required.push('depth');
}
this.webgl.resources.linkPrograms(this.required);
}
finalizeRequired(isSynchronous?: boolean) {
return this.finalize(this.required, isSynchronous);
}
finalize(variants?: GraphicsRenderVariant[], isSynchronous?: boolean) {
return this.webgl.resources.finalizePrograms(variants, isSynchronous);
}
}

View File

@@ -0,0 +1,329 @@
/**
* Copyright (c) 2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { BehaviorSubject, Subject, Subscription } from 'rxjs';
import { WebGLContext } from '../../mol-gl/webgl/context';
import { Vec3 } from '../../mol-math/linear-algebra/3d/vec3';
import { Quat } from '../../mol-math/linear-algebra/3d/quat';
import { Mat4 } from '../../mol-math/linear-algebra/3d/mat4';
import { Camera, ICamera } from '../camera';
import { PointerHelper } from './pointer-helper';
import { Vec2 } from '../../mol-math/linear-algebra/3d/vec2';
import { ButtonsType, InputObserver, TrackedPointerInput } from '../../mol-util/input/input-observer';
import { Plane3D } from '../../mol-math/geometry/primitives/plane3d';
import { Vec4 } from '../../mol-math/linear-algebra/3d/vec4';
import { StereoCamera } from '../camera/stereo';
import { Ray3D } from '../../mol-math/geometry/primitives/ray3d';
import { Scene } from '../../mol-gl/scene';
import { Sphere3D } from '../../mol-math/geometry';
import { Canvas3dInteractionHelper } from './interaction-events';
import { ParamDefinition as PD } from '../../mol-util/param-definition';
import { cameraProject } from '../camera/util';
import { Binding } from '../../mol-util/binding';
const B = ButtonsType;
const Trigger = Binding.Trigger;
const Key = Binding.TriggerKey;
function getRigidTransformFromMat4(m: Mat4): XRRigidTransform {
const d = Mat4.getDecomposition(m);
return new XRRigidTransform(Vec3.toObj(d.position), Quat.toObj(d.quaternion));
}
function getRayFromPose(pose: XRPose, view?: Mat4): Ray3D {
const origin = Vec3.fromObj(pose.transform.position);
const t = Mat4.fromArray(Mat4(), pose.transform.matrix, 0);
const td = Mat4.getDecomposition(t);
const m = Mat4.fromQuat(Mat4(), td.quaternion);
const direction = Vec3.transformMat4(Vec3(), Vec3.negUnitZ, m);
const ray = Ray3D.create(origin, direction);
if (view) Ray3D.transform(ray, ray, Mat4.invert(Mat4(), view));
return ray;
}
type InputInfo = {
targetRayPose: XRPose,
}
export const DefaultXRManagerBindings = {
exit: Binding([Key('GamepadB')]),
togglePassthrough: Binding([Key('GamepadA')]),
gestureScale: Binding([Trigger(B.Flag.Trigger)]),
};
export const DefaultXRManagerAttribs = {
bindings: DefaultXRManagerBindings,
};
export type XRManagerAttribs = typeof DefaultXRManagerAttribs
export const XRManagerParams = {
minTargetDistance: PD.Numeric(0.4, { min: 0.001, max: 1, step: 0.001 }),
disablePostprocessing: PD.Boolean(true),
resolutionScale: PD.Numeric(1, { min: 0.1, max: 2, step: 0.1 }),
sceneRadiusInMeters: PD.Numeric(0.25, { min: 0.01, max: 2, step: 0.01 }, { description: 'The radius of the scene bounding sphere in meters, used to set the initial camera scale.' }),
};
export type XRManagerParams = typeof XRManagerParams
export type XRManagerProps = PD.Values<XRManagerParams>
export class XRManager {
private hoverSub: Subscription;
private keyUpSub: Subscription;
private gestureSub: Subscription;
private sessionChangedSub: Subscription;
readonly togglePassthrough = new Subject<void>();
readonly sessionChanged = new Subject<void>();
readonly isSupported = new BehaviorSubject(false);
private xrSession: XRSession | undefined = undefined;
get session() {
return this.xrSession;
}
private xrRefSpace: XRReferenceSpace | undefined = undefined;
private scaleFactor = 1;
private prevScale = 0;
private prevInput: { left?: InputInfo, right?: InputInfo } = {};
private hit: Vec3 | undefined = undefined;
readonly props: XRManagerProps;
setProps(props: Partial<XRManagerProps>) {
Object.assign(this.props, props);
}
private intersect(camera: ICamera, view: Mat4, plane: Plane3D, targetRayPose: XRPose): { point: Vec3, screen: Vec2 } | undefined {
const point = Vec3();
const ray = getRayFromPose(targetRayPose, view);
if (Plane3D.intersectRay3D(point, plane, ray)) {
const { height } = camera.viewport;
const v = cameraProject(Vec4(), point, camera.viewport, camera.projectionView);
const screen = Vec2.create(Math.floor(v[0]), height - Math.floor(v[1]));
return { point, screen };
}
}
setScaleFactor(factor: number) {
this.scaleFactor = factor;
}
resetScale() {
this.scaleFactor = 1;
this.prevScale = 0;
}
update(xrFrame?: XRFrame): boolean {
const { xrSession, xrRefSpace, input, camera, stereoCamera, pointerHelper } = this;
if (!xrFrame || !xrSession || !xrRefSpace) return false;
camera.scale = camera.scale * this.scaleFactor;
this.prevScale = camera.scale;
const camDirUnscaled = Vec3.sub(Vec3(), camera.position, camera.target);
Vec3.scaleAndAdd(camera.position, camera.position, camDirUnscaled, 1 - this.scaleFactor);
this.scaleFactor = 1;
const xform = getRigidTransformFromMat4(camera.view);
const xrOffsetRefSpace = xrRefSpace.getOffsetReferenceSpace(xform);
const xrPose = xrFrame.getViewerPose(xrOffsetRefSpace);
if (!xrPose) return false;
const xrHeadPose = xrFrame.getViewerPose(xrRefSpace);
if (xrHeadPose) {
const hq = Quat.fromObj(xrHeadPose.transform.orientation);
Mat4.fromQuat(camera.headRotation, hq);
}
const { depthFar, depthNear, baseLayer } = xrSession.renderState;
if (!baseLayer) return false;
if (depthFar !== camera.far || depthNear !== camera.near) {
xrSession.updateRenderState({
depthNear: camera.near,
depthFar: camera.far,
});
}
stereoCamera.update({ pose: xrPose, layer: baseLayer });
const camLeft = stereoCamera.left;
const cameraTarget = Vec3.scale(Vec3(), camLeft.state.target, camLeft.scale);
const cameraPosition = Mat4.getTranslation(Vec3(), Mat4.invert(Mat4(), camLeft.view));
const cameraDirection = Vec3.sub(Vec3(), cameraPosition, cameraTarget);
const cameraPlane = Plane3D.fromNormalAndCoplanarPoint(Plane3D(), cameraDirection, cameraTarget);
//
const pointers: Ray3D[] = [];
const points: Vec3[] = [];
const trackedPointers: TrackedPointerInput[] = [];
if (xrSession.inputSources) {
for (const inputSource of xrSession.inputSources) {
if (inputSource.targetRayMode !== 'tracked-pointer') continue;
const { handedness, targetRaySpace, gamepad } = inputSource;
if (!handedness) continue;
const targetRayPose = xrFrame.getPose(targetRaySpace!, xrRefSpace);
if (!targetRayPose) continue;
const ray = getRayFromPose(targetRayPose, camera.view);
pointers.push(ray);
const sceneBoundingSphere = Sphere3D.scaleNX(Sphere3D(), this.scene.boundingSphereVisible, camLeft.scale);
const si = Vec3();
if (Ray3D.intersectSphere3D(si, ray, sceneBoundingSphere)) {
points.push(si);
}
let buttons = ButtonsType.create(ButtonsType.Flag.None);
if (gamepad?.buttons[0]?.pressed) buttons |= ButtonsType.Flag.Primary;
if (gamepad?.buttons[1]?.pressed) buttons |= ButtonsType.Flag.Secondary;
if (gamepad?.buttons[3]?.pressed) buttons |= ButtonsType.Flag.Auxilary;
if (gamepad?.buttons[4]?.pressed) buttons |= ButtonsType.Flag.Forth;
if (gamepad?.buttons[5]?.pressed) buttons |= ButtonsType.Flag.Five;
const prevInput = handedness === 'left' ? this.prevInput.left : this.prevInput.right;
const intersection = this.intersect(camLeft, camera.view, cameraPlane, targetRayPose);
const prevIntersection = prevInput ? this.intersect(camLeft, camera.view, cameraPlane, prevInput.targetRayPose) : undefined;
const [x, y] = intersection?.screen ?? [0, 0];
const [prevX, prevY] = prevIntersection?.screen ?? [x, y];
const dd = Vec2.set(Vec2(), x - prevX, y - prevY);
Vec2.setMagnitude(dd, dd, Math.min(100, Vec2.magnitude(dd)));
const [dx, dy] = Vec2.round(dd, dd);
trackedPointers.push({
handedness,
buttons,
x, y, dx, dy, ray,
axes: gamepad?.axes
});
if (handedness === 'left') {
this.prevInput.left = { targetRayPose };
} else {
this.prevInput.right = { targetRayPose };
}
}
} else {
this.prevInput.left = undefined;
this.prevInput.right = undefined;
}
input.updateTrackedPointers(trackedPointers);
pointerHelper.ensureEnabled();
pointerHelper.update(pointers, points, this.hit);
return true;
}
private async setSession(xrSession: XRSession | undefined) {
if (this.xrSession === xrSession) return;
await this.webgl.xr.set(xrSession, { resolutionScale: this.props.resolutionScale });
this.xrSession = this.webgl.xr.session;
this.prevInput = {};
this.hit = undefined;
if (this.xrSession) {
this.xrRefSpace = await this.xrSession.requestReferenceSpace('local');
this.pointerHelper.setProps({ enabled: 'on' });
let scale = this.prevScale;
if (scale === 0) {
const { radius } = this.scene.boundingSphereVisible;
scale = radius ? (1 / radius) * this.props.sceneRadiusInMeters : 0.01;
}
this.camera.forceFull = true;
this.camera.scale = scale;
this.camera.minTargetDistance = this.props.minTargetDistance;
this.prevScale = scale;
} else {
this.xrRefSpace = undefined;
Mat4.setZero(this.camera.headRotation);
this.pointerHelper.setProps({ enabled: 'off' });
this.camera.forceFull = false;
this.camera.scale = 1;
this.camera.minTargetDistance = 0;
}
}
async end() {
await this.webgl.xr.end();
}
private checkSupported = async () => {
if (!navigator.xr) return false;
const [arSupported, vrSupported] = await Promise.all([
navigator.xr.isSessionSupported('immersive-ar'),
navigator.xr.isSessionSupported('immersive-vr'),
]);
this.isSupported.next(arSupported || vrSupported);
};
async request() {
if (!navigator.xr) return;
const session = await navigator.xr.isSessionSupported('immersive-ar')
? await navigator.xr.requestSession('immersive-ar')
: await navigator.xr.requestSession('immersive-vr');
await this.setSession(session);
}
dispose() {
this.hoverSub.unsubscribe();
this.keyUpSub.unsubscribe();
this.gestureSub.unsubscribe();
this.sessionChangedSub.unsubscribe();
this.togglePassthrough.complete();
this.sessionChanged.complete();
this.isSupported.complete();
navigator.xr?.removeEventListener('devicechange', this.checkSupported);
}
constructor(private webgl: WebGLContext, private input: InputObserver, private scene: Scene, private camera: Camera, private stereoCamera: StereoCamera, private pointerHelper: PointerHelper, private interactionHelper: Canvas3dInteractionHelper, props: Partial<XRManagerProps> = {}, attribs: Partial<XRManagerAttribs> = {}) {
this.props = { ...PD.getDefaultValues(XRManagerParams), ...props };
this.hoverSub = this.interactionHelper.events.hover.subscribe(({ position }) => {
this.hit = position;
});
this.sessionChangedSub = webgl.xr.changed.subscribe(async () => {
await this.setSession(webgl.xr.session);
this.sessionChanged.next();
});
this.checkSupported();
navigator.xr?.addEventListener('devicechange', this.checkSupported);
const b = { ...DefaultXRManagerBindings, ...attribs.bindings };
this.keyUpSub = input.keyUp.subscribe(({ code, modifiers, key }) => {
if (Binding.matchKey(b.exit, code, modifiers, key)) {
this.end();
}
if (Binding.matchKey(b.togglePassthrough, code, modifiers, key)) {
this.togglePassthrough.next();
}
});
this.gestureSub = input.gesture.subscribe(({ scale, button, modifiers }) => {
if (Binding.match(b.gestureScale, button, modifiers)) {
this.setScaleFactor(scale);
}
});
}
}

View File

@@ -121,8 +121,6 @@ export class BackgroundPass {
private readonly position = Vec3();
private readonly dir = Vec3();
readonly texture: Texture;
constructor(private readonly webgl: WebGLContext, private readonly assetManager: AssetManager, width: number, height: number) {
this.renderable = getBackgroundRenderable(webgl, width, height);
}
@@ -445,8 +443,9 @@ function areImageTexturePropsEqual(sourceA: ImageProps['source'], sourceB: Image
function getImageTexture(ctx: WebGLContext, assetManager: AssetManager, source: ImageProps['source'], onload?: (errored?: boolean) => void): { texture: Texture, asset: Asset } {
const asset = source.name === 'url'
? Asset.getUrlAsset(assetManager, source.params)
? assetManager.tryFindFilename(source.params) ?? Asset.getUrlAsset(assetManager, source.params)
: source.params!;
if (typeof HTMLImageElement === 'undefined') {
console.error(`Missing "HTMLImageElement" required for background image`);
onload?.(true);

View File

@@ -75,6 +75,16 @@ export class BloomPass {
this.copyRenderable = createCopyRenderable(webgl, this.compositeTarget.texture);
}
getByteCount() {
return (
this.emissiveTarget.getByteCount() +
this.luminosityTarget.getByteCount() +
this.compositeTarget.getByteCount() +
this.horizontalBlurTargets.reduce((sum, t) => sum + t.getByteCount(), 0) +
this.verticalBlurTargets.reduce((sum, t) => sum + t.getByteCount(), 0)
);
}
setSize(width: number, height: number) {
const w = this.luminosityTarget.getWidth();
const h = this.luminosityTarget.getHeight();

View File

@@ -50,6 +50,10 @@ export class DofPass {
this.renderable = getDofRenderable(webgl, nullTexture, nullTexture, nullTexture);
}
getByteCount() {
return this.target.getByteCount();
}
private updateState(viewport: Viewport) {
const { gl, state } = this.webgl;
@@ -122,7 +126,7 @@ export class DofPass {
const worldCenter = (props.center === 'scene-center' ? sphere.center : camera.state.target);
const distance = Vec3.distance(camera.state.position, worldCenter);
const inFocus = distance + props.inFocus;
ValueCell.updateIfChanged(this.renderable.values.uInFocus, inFocus * camera.state.scale);
ValueCell.updateIfChanged(this.renderable.values.uInFocus, inFocus * camera.scale);
// transform center in view space
const center = this.renderable.values.uCenter.ref.value;
@@ -130,7 +134,7 @@ export class DofPass {
ValueCell.update(this.renderable.values.uCenter, center);
ValueCell.updateIfChanged(this.renderable.values.uBlurSpread, props.blurSpread);
ValueCell.updateIfChanged(this.renderable.values.uPPM, props.PPM * camera.state.scale);
ValueCell.updateIfChanged(this.renderable.values.uPPM, props.PPM * camera.scale);
if (needsUpdate) {
this.renderable.update();

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2022-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Gianluca Tomasello <giagitom@gmail.com>
* @author Alexander Rose <alexander.rose@weirdbyte.de>
@@ -89,6 +89,18 @@ export class DpoitPass {
return this._supported;
}
getByteCount() {
if (!this._supported) return 0;
return (
this.depthTextures[0].getByteCount() +
this.depthTextures[1].getByteCount() +
this.colorFrontTextures[0].getByteCount() +
this.colorFrontTextures[1].getByteCount() +
this.colorBackTextures[0].getByteCount() +
this.colorBackTextures[1].getByteCount()
);
}
bind() {
const { state, gl, extensions: { blendMinMax } } = this.webgl;

View File

@@ -120,6 +120,25 @@ export class DrawPass {
this.setTransparency(transparency);
}
getByteCount() {
return (
this.drawTarget.getByteCount() +
this.colorTarget.getByteCount() +
this.transparentColorTarget.getByteCount() +
this.depthTargetTransparent.getByteCount() +
(this.depthTargetOpaque
? this.depthTargetOpaque.getByteCount()
: this.depthTextureOpaque.getByteCount()) +
this.wboit.getByteCount() +
this.dpoit.getByteCount() +
this.marking.getByteCount() +
this.postprocessing.getByteCount() +
this.antialiasing.getByteCount() +
this.bloom.getByteCount() +
this.dof.getByteCount()
);
}
reset() {
this.wboit.reset();
this.dpoit.reset();
@@ -432,6 +451,11 @@ export class DrawPass {
if (helper.handle.isEnabled) {
renderer.renderBlended(helper.handle.scene, camera);
}
if (helper.pointer.isEnabled) {
helper.pointer.setCamera(camera);
renderer.update(helper.pointer.camera, helper.pointer.scene);
renderer.renderBlended(helper.pointer.scene, helper.pointer.camera);
}
if (helper.camera.isEnabled) {
helper.camera.update(camera);
renderer.update(helper.camera.camera, helper.camera.scene);

View File

@@ -140,6 +140,15 @@ export class HiZPass {
readonly props: HiZProps;
getByteCount() {
if (!this.supported) return 0;
return (
this.tex.getByteCount() +
this.buf.getByteCount() +
this.levelData.reduce((sum, l) => sum + l.texture.getByteCount(), 0)
);
}
clear() {
if (!this.supported) return;

View File

@@ -36,6 +36,26 @@ import { SsaoProps } from './ssao';
import { OutlinePass } from './outline';
import { BloomPass } from './bloom';
let IlluminationWarningShown = false;
function checkIlluminationSupport(webgl: WebGLContext) {
const { drawBuffers, textureFloat, colorBufferFloat, depthTexture } = webgl.extensions;
if (!textureFloat || !colorBufferFloat || !depthTexture || !drawBuffers) {
if (isDebugMode && !IlluminationWarningShown) {
const missing: string[] = [];
if (!textureFloat) missing.push('textureFloat');
if (!colorBufferFloat) missing.push('colorBufferFloat');
if (!depthTexture) missing.push('depthTexture');
if (!drawBuffers) missing.push('drawBuffers');
console.log(`Missing "${missing.join('", "')}" extensions required for "illumination"`);
IlluminationWarningShown = true;
}
return false;
} else {
return true;
}
}
type Props = {
transparentBackground: boolean;
dpoitIterations: number;
@@ -90,25 +110,28 @@ export class IlluminationPass {
return this._supported;
}
getMaxIterations(props: Props) {
return Math.pow(2, props.illumination.maxIterations);
getByteCount() {
if (!this._supported) return 0;
return (
this.tracing.getByteCount() +
this.transparentTarget.getByteCount() +
this.outputTarget.getByteCount() +
this.multiSampleComposeTarget.getByteCount() +
this.multiSampleHoldTarget.getByteCount() +
this.multiSampleAccumulateTarget.getByteCount()
);
}
getMaxIterations(props: IlluminationProps) {
return Math.pow(2, props.maxIterations);
}
static isSupported(webgl: WebGLContext) {
const { drawBuffers, textureFloat, colorBufferFloat, depthTexture } = webgl.extensions;
if (!textureFloat || !colorBufferFloat || !depthTexture || !drawBuffers) {
if (isDebugMode) {
const missing: string[] = [];
if (!textureFloat) missing.push('textureFloat');
if (!colorBufferFloat) missing.push('colorBufferFloat');
if (!depthTexture) missing.push('depthTexture');
if (!drawBuffers) missing.push('drawBuffers');
console.log(`Missing "${missing.join('", "')}" extensions required for "illumination"`);
}
return false;
} else {
return true;
}
return checkIlluminationSupport(webgl);
}
static isEnabled(webgl: WebGLContext, props: IlluminationProps) {
return props.enabled && checkIlluminationSupport(webgl);
}
constructor(private readonly webgl: WebGLContext, private readonly drawPass: DrawPass) {
@@ -240,8 +263,8 @@ export class IlluminationPass {
if (isTimingMode) this.webgl.timer.markEnd('IlluminationPass.renderInput');
}
shouldRender(props: Props) {
return this._supported && props.illumination.enabled && this._iteration < this.getMaxIterations(props);
shouldRender(props: IlluminationProps) {
return this._supported && props.enabled && this._iteration < this.getMaxIterations(props);
}
setSize(width: number, height: number) {
@@ -285,11 +308,11 @@ export class IlluminationPass {
}
private renderInternal(ctx: RenderContext, props: Props, toDrawingBuffer: boolean, forceRenderInput: boolean) {
if (!this.shouldRender(props)) return;
if (!this.shouldRender(props.illumination)) return;
if (isTimingMode) {
this.webgl.timer.mark('IlluminationPass.render', {
note: `iteration ${this._iteration + 1} of ${this.getMaxIterations(props)}`
note: `iteration ${this._iteration + 1} of ${this.getMaxIterations(props.illumination)}`
});
}
this.tracing.render(ctx, props.transparentBackground, props.illumination, this._iteration, forceRenderInput);
@@ -398,7 +421,7 @@ export class IlluminationPass {
}
const denoiseThreshold = props.multiSample.mode === 'on'
? props.illumination.denoiseThreshold[0]
: lerp(props.illumination.denoiseThreshold[1], props.illumination.denoiseThreshold[0], clamp(this.iteration / (this.getMaxIterations(props) / 2), 0, 1));
: lerp(props.illumination.denoiseThreshold[1], props.illumination.denoiseThreshold[0], clamp(this.iteration / (this.getMaxIterations(props.illumination) / 2), 0, 1));
ValueCell.updateIfChanged(this.composeRenderable.values.uDenoiseThreshold, denoiseThreshold);
if (needsUpdateCompose) this.composeRenderable.update();
this.composeRenderable.render();
@@ -476,7 +499,7 @@ export class IlluminationPass {
// each sample with camera jitter and accumulates the results.
const offsetList = JitterVectors[Math.max(0, Math.min(props.multiSample.sampleLevel, 5))];
const maxIterations = this.getMaxIterations(props);
const maxIterations = this.getMaxIterations(props.illumination);
const iteration = Math.min(this._iteration, maxIterations);
const sampleIndex = Math.floor((iteration / maxIterations) * offsetList.length);

View File

@@ -21,8 +21,9 @@ import { MarkingParams } from './marking';
import { AssetManager } from '../../mol-util/assets';
import { IlluminationParams, IlluminationPass } from './illumination';
import { RuntimeContext } from '../../mol-task';
import { isTimingMode } from '../../mol-util/debug';
import { isDebugMode, isTimingMode } from '../../mol-util/debug';
import { printTimerResults } from '../../mol-gl/webgl/timer';
import { ShaderManager } from '../helper/shader-manager';
export const ImageParams = {
transparentBackground: PD.Boolean(false),
@@ -68,11 +69,16 @@ export class ImagePass {
camera: new CameraHelper(webgl, this.props.cameraHelper),
debug: helper.debug,
handle: helper.handle,
pointer: helper.pointer,
};
this.setSize(1024, 768);
}
getByteCount() {
return this.drawPass.getByteCount() + this.illuminationPass.getByteCount() + this.multiSamplePass.getByteCount();
}
updateBackground() {
return new Promise<void>(resolve => {
this.drawPass.postprocessing.background.update(this.camera, this.props.postprocessing.background, () => {
@@ -98,15 +104,16 @@ export class ImagePass {
}
async render(runtime: RuntimeContext) {
ShaderManager.ensureRequired(this.webgl, this.scene, this.props);
Camera.copySnapshot(this._camera.state, this.camera.state);
Viewport.set(this._camera.viewport, 0, 0, this._width, this._height);
this._camera.update();
const ctx = { renderer: this.renderer, camera: this._camera, scene: this.scene, helper: this.helper };
if (this.illuminationPass.supported && this.props.illumination.enabled) {
await runtime.update({ message: 'Tracing...', current: 1, max: this.illuminationPass.getMaxIterations(this.props) });
await runtime.update({ message: 'Tracing...', current: 1, max: this.illuminationPass.getMaxIterations(this.props.illumination) });
this.illuminationPass.restart(true);
while (this.illuminationPass.shouldRender(this.props)) {
while (this.illuminationPass.shouldRender(this.props.illumination)) {
if (isTimingMode) this.webgl.timer.mark('ImagePass.render', { captureStats: true });
this.illuminationPass.render(ctx, this.props, false);
if (isTimingMode) this.webgl.timer.markEnd('ImagePass.render');
@@ -137,13 +144,8 @@ export class ImagePass {
}
}
if (isTimingMode) {
const timerResults = this.webgl.timer.resolve();
if (timerResults) {
for (const result of timerResults) {
printTimerResults([result]);
}
}
if (isDebugMode) {
console.log(`image pass byte count ${(this.getByteCount() / 1024 / 1024).toFixed(3)} MiB`);
}
}

View File

@@ -55,6 +55,10 @@ export class MarkingPass {
this.overlay = getOverlayRenderable(webgl, this.edgesTarget.texture);
}
getByteCount() {
return this.depthTarget.getByteCount() + this.maskTarget.getByteCount() + this.edgesTarget.getByteCount();
}
private setEdgeState(viewport: Viewport) {
const { gl, state } = this.webgl;

View File

@@ -96,6 +96,10 @@ export class MultiSamplePass {
this.compose = getComposeRenderable(webgl, drawPass.colorTarget.texture);
}
getByteCount() {
return this.colorTarget.getByteCount() + this.composeTarget.getByteCount() + this.holdTarget.getByteCount();
}
syncSize() {
const width = this.drawPass.colorTarget.getWidth();
const height = this.drawPass.colorTarget.getHeight();

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019-2024 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2019-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author Áron Samuel Kovács <aron.kovacs@mail.muni.cz>
@@ -47,6 +47,10 @@ export class OutlinePass {
this.renderable = getOutlinesRenderable(webgl, depthTextureOpaque, depthTextureTransparent, true);
}
getByteCount() {
return this.target.getByteCount();
}
setSize(width: number, height: number) {
const [w, h] = this.renderable.values.uTexSize.ref.value;
if (width !== w || height !== h) {

View File

@@ -25,6 +25,10 @@ export class Passes {
this.illumination = new IlluminationPass(webgl, this.draw);
}
getByteCount() {
return this.draw.getByteCount() + this.pick.getByteCount() + this.multiSample.getByteCount() + this.illumination.getByteCount();
}
setPickScale(pickScale: number) {
this.pick.setPickScale(pickScale);
}

View File

@@ -22,8 +22,6 @@ import { ICamera } from '../camera';
import { Viewport } from '../camera/util';
import { Helper } from '../helper/helper';
const NullId = Math.pow(2, 24) - 2;
export type PickData = { id: PickingId, position: Vec3 }
export type AsyncPickData = {
@@ -119,6 +117,25 @@ export class PickPass {
}
}
getByteCount() {
if (this.webgl.extensions.drawBuffers) {
return (
this.objectPickTexture.getByteCount() +
this.instancePickTexture.getByteCount() +
this.groupPickTexture.getByteCount() +
this.depthPickTexture.getByteCount() +
this.depthRenderbuffer.getByteCount()
);
} else {
return (
this.objectPickTarget.getByteCount() +
this.instancePickTarget.getByteCount() +
this.groupPickTarget.getByteCount() +
this.depthPickTarget.getByteCount()
);
}
}
dispose() {
if (this.webgl.extensions.drawBuffers) {
this.framebuffer.destroy();
@@ -261,7 +278,9 @@ export class PickPass {
if (this.webgl.extensions.drawBuffers) {
this.framebuffer.bind();
this.renderVariant(renderer, camera, scene, helper, 'pick', PickType.None);
// printTextureImage(readTexture(this.webgl, this.groupPickTexture, new Uint8Array(this.pickWidth * this.pickHeight * 4)), { scale: 16, id: 'group', pixelated: true, useCanvas: true, flipY: true });
// if (this.pickWidth < 256) {
// printTextureImage(readTexture(this.webgl, this.groupPickTexture, new Uint8Array(this.pickWidth * this.pickHeight * 4)), { scale: 16, id: 'group', pixelated: true, useCanvas: true, flipY: true });
// }
} else {
this.objectPickTarget.bind();
this.renderVariant(renderer, camera, scene, helper, 'pick', PickType.Object);
@@ -450,15 +469,15 @@ export class PickBuffers {
getPickingId(x: number, y: number): PickingId | undefined {
const objectId = this.getObjectId(x, y);
// console.log('objectId', objectId);
if (objectId === -1 || objectId === NullId) return;
if (objectId === -1 || objectId === PickingId.Null) return;
const instanceId = this.getInstanceId(x, y);
// console.log('instanceId', instanceId);
if (instanceId === -1 || instanceId === NullId) return;
if (instanceId === -1 || instanceId === PickingId.Null) return;
const groupId = this.getGroupId(x, y);
// console.log('groupId', groupId);
if (groupId === -1 || groupId === NullId) return;
if (groupId === -1) return;
return { objectId, instanceId, groupId };
}

View File

@@ -161,7 +161,7 @@ export class PostprocessingPass {
}
static isTransparentDepthRequired(scene: Scene, props: PostprocessingProps) {
return props.enabled && (DofPass.isEnabled(props) || OutlinePass.isEnabled(props) && PostprocessingPass.isTransparentOutlineEnabled(props) || SsaoPass.isEnabled(props) && PostprocessingPass.isTransparentSsaoEnabled(scene, props));
return props.enabled && (DofPass.isEnabled(props) || OutlinePass.isEnabled(props) && PostprocessingPass.isTransparentOutlineEnabled(props) || SsaoPass.isEnabled(props) && PostprocessingPass.isTransparentSsaoEnabled(scene, props)) && scene.opacityAverage < 1;
}
static isTransparentOutlineEnabled(props: PostprocessingProps) {
@@ -202,6 +202,15 @@ export class PostprocessingPass {
this.background = new BackgroundPass(webgl, assetManager, width, height);
}
getByteCount() {
return (
this.target.getByteCount() +
this.ssao.getByteCount() +
this.shadow.getByteCount() +
this.outline.getByteCount()
);
}
setSize(width: number, height: number) {
const [w, h] = this.renderable.values.uTexSize.ref.value;
@@ -374,6 +383,14 @@ export class AntialiasingPass {
this.cas = new CasPass(webgl, this.target.texture);
}
getByteCount() {
return (
this.target.getByteCount() +
this.internalTarget.getByteCount() +
this.smaa.getByteCount()
);
}
setSize(width: number, height: number) {
const w = this.target.texture.getWidth();
const h = this.target.texture.getHeight();

View File

@@ -21,7 +21,7 @@ import { RenderTarget } from '../../mol-gl/webgl/render-target';
import { ICamera } from '../../mol-canvas3d/camera';
import { quad_vert } from '../../mol-gl/shader/quad.vert';
import { isTimingMode } from '../../mol-util/debug';
import { Light } from '../../mol-gl/renderer';
import { getTransformedLightDirection, Light } from '../../mol-gl/renderer';
import { shadows_frag } from '../../mol-gl/shader/shadows.frag';
import { PostprocessingProps } from './postprocessing';
@@ -41,11 +41,18 @@ export class ShadowPass {
readonly target: RenderTarget;
private readonly renderable: ShadowsRenderable;
private invProjection = Mat4.identity();
private invHeadRotation = Mat4.identity();
constructor(readonly webgl: WebGLContext, width: number, height: number, depthTextureOpaque: Texture) {
this.target = webgl.createRenderTarget(width, height, false);
this.renderable = getShadowsRenderable(webgl, depthTextureOpaque);
}
getByteCount() {
return this.target.getByteCount();
}
setSize(width: number, height: number) {
const [w, h] = this.renderable.values.uTexSize.ref.value;
if (width !== w || height !== h) {
@@ -59,14 +66,11 @@ export class ShadowPass {
const orthographic = camera.state.mode === 'orthographic' ? 1 : 0;
const invProjection = Mat4.identity();
Mat4.invert(invProjection, camera.projection);
const [w, h] = this.renderable.values.uTexSize.ref.value;
const v = camera.viewport;
ValueCell.update(this.renderable.values.uProjection, camera.projection);
ValueCell.update(this.renderable.values.uInvProjection, invProjection);
ValueCell.update(this.renderable.values.uInvProjection, Mat4.invert(this.invProjection, camera.projection));
Vec4.set(this.renderable.values.uBounds.ref.value,
v.x / w,
@@ -83,14 +87,19 @@ export class ShadowPass {
needsUpdateShadows = true;
}
ValueCell.updateIfChanged(this.renderable.values.uMaxDistance, props.maxDistance * camera.state.scale);
ValueCell.updateIfChanged(this.renderable.values.uTolerance, props.tolerance * camera.state.scale);
ValueCell.updateIfChanged(this.renderable.values.uMaxDistance, props.maxDistance * camera.scale);
ValueCell.updateIfChanged(this.renderable.values.uTolerance, props.tolerance * camera.scale);
if (this.renderable.values.dSteps.ref.value !== props.steps) {
ValueCell.update(this.renderable.values.dSteps, props.steps);
needsUpdateShadows = true;
}
ValueCell.update(this.renderable.values.uLightDirection, light.direction);
const hasHeadRotation = !Mat4.isZero(camera.headRotation);
if (hasHeadRotation) {
ValueCell.update(this.renderable.values.uLightDirection, getTransformedLightDirection(light, Mat4.invert(this.invHeadRotation, camera.headRotation)));
} else {
ValueCell.update(this.renderable.values.uLightDirection, light.direction);
}
ValueCell.update(this.renderable.values.uLightColor, light.color);
if (this.renderable.values.dLightCount.ref.value !== light.count) {
ValueCell.update(this.renderable.values.dLightCount, light.count);

View File

@@ -62,6 +62,11 @@ export class SmaaPass {
this._supported = true;
}
getByteCount() {
if (!this.supported) return 0;
return this.edgesTarget.getByteCount() + this.weightsTarget.getByteCount();
}
private updateState(viewport: Viewport) {
const { gl, state } = this.webgl;

View File

@@ -80,15 +80,13 @@ function getLevels(props: { radius: number, bias: number }[], scale: number, lev
export class SsaoPass {
static isEnabled(props: PostprocessingProps) {
return props.occlusion.name !== 'off';
return props.enabled && props.occlusion.name !== 'off';
}
static isTransparentEnabled(scene: Scene, props: SsaoProps) {
return scene.opacityAverage < 1 && scene.transparencyMin < props.transparentThreshold;
}
readonly target: RenderTarget;
private readonly framebuffer: Framebuffer;
private readonly blurFirstPassFramebuffer: Framebuffer;
private readonly blurSecondPassFramebuffer: Framebuffer;
@@ -134,6 +132,7 @@ export class SsaoPass {
return Math.min(1, 1 / this.webgl.pixelRatio) * resolutionScale;
}
private levelsCameraScale = -1;
private levels: { radius: number, bias: number }[];
private getDepthTexture() {
@@ -213,6 +212,20 @@ export class SsaoPass {
this.blurSecondPassRenderable = getSsaoBlurRenderable(webgl, this.depthBlurProxyTexture, 'vertical');
}
getByteCount() {
return (
this.downsampledDepthTargetOpaque.getByteCount() +
this.depthHalfTargetOpaque.getByteCount() +
this.depthQuarterTargetOpaque.getByteCount() +
this.downsampledDepthTargetTransparent.getByteCount() +
this.depthHalfTargetTransparent.getByteCount() +
this.depthQuarterTargetTransparent.getByteCount() +
this.ssaoDepthTexture.getByteCount() +
this.ssaoDepthTransparentTexture.getByteCount() +
this.depthBlurProxyTexture.getByteCount()
);
}
setSize(width: number, height: number) {
const [w, h] = this.texSize;
const ssaoScale = this.calcSsaoScale(1);
@@ -305,8 +318,8 @@ export class SsaoPass {
ValueCell.update(this.blurFirstPassRenderable.values.uInvProjection, invProjection);
ValueCell.update(this.blurSecondPassRenderable.values.uInvProjection, invProjection);
ValueCell.update(this.blurFirstPassRenderable.values.uBlurDepthBias, props.blurDepthBias * camera.state.scale);
ValueCell.update(this.blurSecondPassRenderable.values.uBlurDepthBias, props.blurDepthBias * camera.state.scale);
ValueCell.update(this.blurFirstPassRenderable.values.uBlurDepthBias, props.blurDepthBias * camera.scale);
ValueCell.update(this.blurSecondPassRenderable.values.uBlurDepthBias, props.blurDepthBias * camera.scale);
if (this.blurFirstPassRenderable.values.dOrthographic.ref.value !== orthographic) {
needsUpdateSsaoBlur = true;
@@ -344,11 +357,12 @@ export class SsaoPass {
if (props.multiScale.name === 'on') {
const mp = props.multiScale.params;
if (!deepEqual(this.levels, mp.levels)) {
if (this.levelsCameraScale !== camera.scale || !deepEqual(this.levels, mp.levels)) {
needsUpdateSsao = true;
this.levelsCameraScale = camera.scale;
this.levels = mp.levels;
const levels = getLevels(mp.levels, camera.state.scale);
const levels = getLevels(mp.levels, camera.scale);
ValueCell.updateIfChanged(this.renderable.values.dLevels, levels.count);
ValueCell.update(this.renderable.values.uLevelRadius, levels.radius);
@@ -357,7 +371,7 @@ export class SsaoPass {
ValueCell.updateIfChanged(this.renderable.values.uNearThreshold, mp.nearThreshold);
ValueCell.updateIfChanged(this.renderable.values.uFarThreshold, mp.farThreshold);
} else {
ValueCell.updateIfChanged(this.renderable.values.uRadius, Math.pow(2, props.radius) * camera.state.scale);
ValueCell.updateIfChanged(this.renderable.values.uRadius, Math.pow(2, props.radius) * camera.scale);
}
ValueCell.updateIfChanged(this.renderable.values.uBias, props.bias);

View File

@@ -126,6 +126,18 @@ export class TracingPass {
this.accumulateRenderable = getAccumulateRenderable(webgl, this.holdTarget.texture);
}
getByteCount() {
return (
this.thicknessTarget.getByteCount() +
this.holdTarget.getByteCount() +
this.accumulateTarget.getByteCount() +
this.composeTarget.getByteCount() +
this.colorTextureOpaque.getByteCount() +
this.normalTextureOpaque.getByteCount() +
this.shadedTextureOpaque.getByteCount()
);
}
private renderInput(renderer: Renderer, camera: ICamera, scene: Scene, props: TracingProps) {
if (isTimingMode) this.webgl.timer.mark('TracePass.renderInput');
const { gl, state } = this.webgl;

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2020-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>
@@ -59,6 +59,11 @@ export class WboitPass {
return this._supported;
}
getByteCount() {
if (!this._supported) return 0;
return this.textureA.getByteCount() + this.textureB.getByteCount() + this.depthRenderbuffer.getByteCount();
}
bind() {
const { state, gl } = this.webgl;

View File

@@ -120,8 +120,6 @@ export namespace BaseGeometry {
uBumpiness: ValueCell.create(props.material.bumpiness),
uEmissive: ValueCell.create(props.emissive),
uDensity: ValueCell.create(props.density),
dLightCount: ValueCell.create(1),
dColorMarker: ValueCell.create(true),
dClipObjectCount: ValueCell.create(clip.objects.count),
dClipVariant: ValueCell.create(clip.variant),

View File

@@ -560,7 +560,7 @@ export namespace Mesh {
const mu = -lambda;
let dst = new Float32Array(mesh.vertexBuffer.ref.value.length);
let dst: Float32Array<ArrayBufferLike> = new Float32Array(mesh.vertexBuffer.ref.value.length);
const step = (f: number) => {
const pos = mesh.vertexBuffer.ref.value;

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2018-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -11,6 +11,8 @@ export interface PickingId {
}
export namespace PickingId {
export const Null = 16777214 as const; // Math.pow(2, 24) - 2
export function areSame(a: PickingId, b: PickingId) {
return a.objectId === b.objectId && a.instanceId === b.instanceId && a.groupId === b.groupId;
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019-2024 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2019-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -223,7 +223,8 @@ export namespace Text {
const counts = { drawCount: text.charCount * 2 * 3, vertexCount: text.charCount * 4, groupCount, instanceCount };
const padding = getPadding(text.mappingBuffer.ref.value, text.depthBuffer.ref.value, text.charCount, getMaxSize(size));
const scale = getMaxSize(size) * props.sizeFactor;
const padding = getPadding(text.mappingBuffer.ref.value, text.depthBuffer.ref.value, text.charCount, scale);
const invariantBoundingSphere = Sphere3D.expand(Sphere3D(), text.boundingSphere, padding);
const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount, 0);
@@ -291,7 +292,8 @@ export namespace Text {
}
function updateBoundingSphere(values: TextValues, text: Text) {
const padding = getPadding(values.aMapping.ref.value, values.aDepth.ref.value, text.charCount, getMaxSize(values));
const scale = getMaxSize(values) * values.uSizeFactor.ref.value;
const padding = getPadding(values.aMapping.ref.value, values.aDepth.ref.value, text.charCount, scale);
const invariantBoundingSphere = Sphere3D.expand(Sphere3D(), text.boundingSphere, padding);
const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value, 0);
@@ -319,7 +321,7 @@ export namespace Text {
}
}
function getPadding(mappings: Float32Array, depths: Float32Array, charCount: number, maxSize: number) {
function getPadding(mappings: Float32Array, depths: Float32Array, charCount: number, scale: number) {
let maxOffset = 0;
let maxDepth = 0;
for (let i = 0, il = charCount * 4; i < il; ++i) {
@@ -331,7 +333,5 @@ function getPadding(mappings: Float32Array, depths: Float32Array, charCount: num
const d = Math.abs(depths[i]);
if (d > maxDepth) maxDepth = d;
}
// console.log(maxDepth + maxSize, maxDepth, maxSize, maxSize + maxSize * maxOffset, depths)
return Math.max(maxDepth, maxSize + maxSize * maxOffset);
// return maxSize + maxSize * maxOffset + maxDepth
return scale * Math.max(maxDepth, maxOffset);
}

View File

@@ -154,15 +154,24 @@ export namespace TextureMesh {
}
const framebuffer = webgl.namedFramebuffers[TextureMeshName];
const [width, height] = textureMesh.geoTextureDim.ref.value;
const vertices = new Float32Array(width * height * 4);
framebuffer.bind();
textureMesh.vertexTexture.ref.value.attachFramebuffer(framebuffer, 0);
webgl.readPixels(0, 0, width, height, vertices);
const normals = new Float32Array(width * height * 4);
framebuffer.bind();
textureMesh.normalTexture.ref.value.attachFramebuffer(framebuffer, 0);
webgl.readPixels(0, 0, width, height, normals);
let data: { vertices: Float32Array, normals: Float32Array } | undefined = undefined;
const getData = () => {
if (!data) {
const vertices = new Float32Array(width * height * 4);
framebuffer.bind();
textureMesh.vertexTexture.ref.value.attachFramebuffer(framebuffer, 0);
webgl.readPixels(0, 0, width, height, vertices);
const normals = new Float32Array(width * height * 4);
framebuffer.bind();
textureMesh.normalTexture.ref.value.attachFramebuffer(framebuffer, 0);
webgl.readPixels(0, 0, width, height, normals);
data = { vertices, normals };
}
return data;
};
const groupCount = textureMesh.vertexCount;
const instanceCount = transform.instanceCount.ref.value;
@@ -171,6 +180,7 @@ export namespace TextureMesh {
const n = location.normal;
const m = transform.aTransform.ref.value;
const getLocation = (groupIndex: number, instanceIndex: number) => {
const { vertices, normals } = getData();
if (instanceIndex < 0) {
Vec3.fromArray(p, vertices, groupIndex * 4);
Vec3.fromArray(n, normals, groupIndex * 4);

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2018-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -17,6 +17,8 @@ import { TextureMeshValues, TextureMeshRenderable } from './renderable/texture-m
import { ImageValues, ImageRenderable } from './renderable/image';
import { CylindersRenderable, CylindersValues } from './renderable/cylinders';
import { Transparency } from './webgl/render-item';
import { GlobalDefines } from './renderable/schema';
import { assertUnreachable } from '../mol-util/type-helpers';
const getNextId = idFactory(0, 0x7FFFFFFF);
@@ -49,17 +51,17 @@ export function createRenderObject<T extends RenderObjectType>(type: T, values:
return { id: getNextId(), type, values, state, materialId } as GraphicsRenderObject<T>;
}
export function createRenderable<T extends RenderObjectType>(ctx: WebGLContext, o: GraphicsRenderObject<T>, transparency: Transparency): Renderable<any> {
export function createRenderable<T extends RenderObjectType>(ctx: WebGLContext, o: GraphicsRenderObject<T>, transparency: Transparency, globals: GlobalDefines): Renderable<any> {
switch (o.type) {
case 'mesh': return MeshRenderable(ctx, o.id, o.values as MeshValues, o.state, o.materialId, transparency);
case 'points': return PointsRenderable(ctx, o.id, o.values as PointsValues, o.state, o.materialId, transparency);
case 'spheres': return SpheresRenderable(ctx, o.id, o.values as SpheresValues, o.state, o.materialId, transparency);
case 'cylinders': return CylindersRenderable(ctx, o.id, o.values as CylindersValues, o.state, o.materialId, transparency);
case 'text': return TextRenderable(ctx, o.id, o.values as TextValues, o.state, o.materialId, transparency);
case 'lines': return LinesRenderable(ctx, o.id, o.values as LinesValues, o.state, o.materialId, transparency);
case 'direct-volume': return DirectVolumeRenderable(ctx, o.id, o.values as DirectVolumeValues, o.state, o.materialId, transparency);
case 'image': return ImageRenderable(ctx, o.id, o.values as ImageValues, o.state, o.materialId, transparency);
case 'texture-mesh': return TextureMeshRenderable(ctx, o.id, o.values as TextureMeshValues, o.state, o.materialId, transparency);
case 'mesh': return MeshRenderable(ctx, o.id, o.values as MeshValues, o.state, o.materialId, transparency, globals);
case 'points': return PointsRenderable(ctx, o.id, o.values as PointsValues, o.state, o.materialId, transparency, globals);
case 'spheres': return SpheresRenderable(ctx, o.id, o.values as SpheresValues, o.state, o.materialId, transparency, globals);
case 'cylinders': return CylindersRenderable(ctx, o.id, o.values as CylindersValues, o.state, o.materialId, transparency, globals);
case 'text': return TextRenderable(ctx, o.id, o.values as TextValues, o.state, o.materialId, transparency, globals);
case 'lines': return LinesRenderable(ctx, o.id, o.values as LinesValues, o.state, o.materialId, transparency, globals);
case 'direct-volume': return DirectVolumeRenderable(ctx, o.id, o.values as DirectVolumeValues, o.state, o.materialId, transparency, globals);
case 'image': return ImageRenderable(ctx, o.id, o.values as ImageValues, o.state, o.materialId, transparency, globals);
case 'texture-mesh': return TextureMeshRenderable(ctx, o.id, o.values as TextureMeshValues, o.state, o.materialId, transparency, globals);
}
throw new Error('unsupported type');
assertUnreachable(o.type);
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018-2024 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2018-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -42,7 +42,9 @@ export interface Renderable<T extends RenderableValues> {
cull: (cameraPlane: Plane3D, frustum: Frustum3D, isOccluded: ((s: Sphere3D) => boolean) | null, stats: WebGLStats) => void
uncull: () => void
cullSimple: (d: number, radius: number, scale: number) => void
render: (variant: GraphicsRenderVariant, sharedTexturesCount: number) => void
getByteCount: () => number
getProgram: (variant: GraphicsRenderVariant) => Program
setTransparency: (transparency: Transparency) => void
update: () => void
@@ -304,12 +306,40 @@ export function createRenderable<T extends GraphicsRenderableValues>(renderItem:
uncull: () => {
cullEnabled = false;
},
cullSimple: (d: number, radius: number, scale: number) => {
const lodLevels: [minDistance: number, maxDistance: number, overlap: number, count: number, sizeFactor: number][] | undefined = values.lodLevels?.ref.value;
if (!lodLevels || lodLevels.length === 0) return;
if (values.lodLevels?.ref.version !== lodLevelsVersion) {
updateLodLevels();
} else {
for (let i = 0, il = lodLevels.length; i < il; ++i) {
mdbDataList[i].count = 0;
}
}
for (let j = 0, jl = lodLevels.length; j < jl; ++j) {
if (d + radius < lodLevels[j][1] * scale) {
const l = mdbDataList[j];
const o = l.count;
l.counts[o] = lodLevels[j][3];
l.instanceCounts[o] = values.instanceCount.ref.value;
l.baseInstances[o] = 0;
l.count += 1;
break;
}
}
cullEnabled = true;
},
render: (variant: GraphicsRenderVariant, sharedTexturesCount: number) => {
if (values.uAlpha && values.alpha) {
ValueCell.updateIfChanged(values.uAlpha, clamp(values.alpha.ref.value * state.alphaFactor, 0, 1));
}
renderItem.render(variant, sharedTexturesCount, cullEnabled ? mdbDataList : undefined);
},
getByteCount: () => renderItem.getByteCount(),
getProgram: (variant: GraphicsRenderVariant) => renderItem.getProgram(variant),
setTransparency: (transparency: Transparency) => renderItem.setTransparency(transparency),
update: () => {
@@ -338,7 +368,10 @@ export function createComputeRenderable<T extends Values<RenderableSchema>>(rend
id: getNextRenderableId(),
values,
render: () => renderItem.render('compute', 0),
render: () => {
renderItem.getProgram('compute').finalize(true);
renderItem.render('compute', 0);
},
update: () => renderItem.update(),
dispose: () => renderItem.destroy()
};

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2020-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2020-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -7,7 +7,7 @@
import { Renderable, RenderableState, createRenderable } from '../renderable';
import { WebGLContext } from '../webgl/context';
import { createGraphicsRenderItem, Transparency } from '../webgl/render-item';
import { GlobalUniformSchema, BaseSchema, AttributeSpec, Values, InternalSchema, SizeSchema, InternalValues, ElementsSpec, ValueSpec, DefineSpec, GlobalTextureSchema, UniformSpec } from './schema';
import { GlobalUniformSchema, BaseSchema, AttributeSpec, Values, InternalSchema, SizeSchema, InternalValues, ElementsSpec, ValueSpec, DefineSpec, GlobalTextureSchema, UniformSpec, GlobalDefineValues, GlobalDefines, GlobalDefineSchema } from './schema';
import { CylindersShaderCode } from '../shader-code';
import { ValueCell } from '../../mol-util';
@@ -37,12 +37,15 @@ export const CylindersSchema = {
export type CylindersSchema = typeof CylindersSchema
export type CylindersValues = Values<CylindersSchema>
export function CylindersRenderable(ctx: WebGLContext, id: number, values: CylindersValues, state: RenderableState, materialId: number, transparency: Transparency): Renderable<CylindersValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...InternalSchema, ...CylindersSchema };
const internalValues: InternalValues = {
export function CylindersRenderable(ctx: WebGLContext, id: number, values: CylindersValues, state: RenderableState, materialId: number, transparency: Transparency, globals: GlobalDefines): Renderable<CylindersValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...GlobalDefineSchema, ...InternalSchema, ...CylindersSchema };
const renderValues: CylindersValues & InternalValues & GlobalDefineValues = {
...values,
uObjectId: ValueCell.create(id),
dLightCount: ValueCell.create(globals.dLightCount),
dColorMarker: ValueCell.create(globals.dColorMarker),
};
const shaderCode = CylindersShaderCode;
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, { ...values, ...internalValues }, materialId, transparency);
return createRenderable(renderItem, values, state);
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, renderValues, materialId, transparency);
return createRenderable(renderItem, renderValues, state);
}

View File

@@ -7,7 +7,7 @@
import { Renderable, RenderableState, createRenderable } from '../renderable';
import { WebGLContext } from '../webgl/context';
import { createGraphicsRenderItem, Transparency } from '../webgl/render-item';
import { AttributeSpec, Values, UniformSpec, GlobalUniformSchema, InternalSchema, TextureSpec, ElementsSpec, DefineSpec, InternalValues, GlobalTextureSchema, BaseSchema, ValueSpec } from './schema';
import { AttributeSpec, Values, UniformSpec, GlobalUniformSchema, InternalSchema, TextureSpec, ElementsSpec, DefineSpec, InternalValues, GlobalTextureSchema, BaseSchema, ValueSpec, GlobalDefineValues, GlobalDefines, GlobalDefineSchema } from './schema';
import { DirectVolumeShaderCode } from '../shader-code';
import { ValueCell } from '../../mol-util';
@@ -48,16 +48,19 @@ export const DirectVolumeSchema = {
export type DirectVolumeSchema = typeof DirectVolumeSchema
export type DirectVolumeValues = Values<DirectVolumeSchema>
export function DirectVolumeRenderable(ctx: WebGLContext, id: number, values: DirectVolumeValues, state: RenderableState, materialId: number, transparency: Transparency): Renderable<DirectVolumeValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...InternalSchema, ...DirectVolumeSchema };
export function DirectVolumeRenderable(ctx: WebGLContext, id: number, values: DirectVolumeValues, state: RenderableState, materialId: number, transparency: Transparency, globals: GlobalDefines): Renderable<DirectVolumeValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...GlobalDefineSchema, ...InternalSchema, ...DirectVolumeSchema };
if (!ctx.isWebGL2) {
// workaround for webgl1 limitation that loop counters need to be `const`
(schema.uMaxSteps as any) = DefineSpec('number');
}
const internalValues: InternalValues = {
const renderValues: DirectVolumeValues & InternalValues & GlobalDefineValues = {
...values,
uObjectId: ValueCell.create(id),
dLightCount: ValueCell.create(globals.dLightCount),
dColorMarker: ValueCell.create(globals.dColorMarker),
};
const shaderCode = DirectVolumeShaderCode;
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, { ...values, ...internalValues }, materialId, transparency);
return createRenderable(renderItem, values, state);
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, renderValues, materialId, transparency);
return createRenderable(renderItem, renderValues, state);
}

View File

@@ -7,7 +7,7 @@
import { Renderable, RenderableState, createRenderable } from '../renderable';
import { WebGLContext } from '../webgl/context';
import { createGraphicsRenderItem, Transparency } from '../webgl/render-item';
import { AttributeSpec, Values, GlobalUniformSchema, InternalSchema, TextureSpec, ElementsSpec, DefineSpec, InternalValues, BaseSchema, UniformSpec, GlobalTextureSchema } from './schema';
import { AttributeSpec, Values, GlobalUniformSchema, InternalSchema, TextureSpec, ElementsSpec, DefineSpec, InternalValues, BaseSchema, UniformSpec, GlobalTextureSchema, GlobalDefineValues, GlobalDefines, GlobalDefineSchema } from './schema';
import { ImageShaderCode } from '../shader-code';
import { ValueCell } from '../../mol-util';
import { InterpolationTypeNames } from '../../mol-geo/geometry/image/image';
@@ -38,12 +38,15 @@ export const ImageSchema = {
export type ImageSchema = typeof ImageSchema
export type ImageValues = Values<ImageSchema>
export function ImageRenderable(ctx: WebGLContext, id: number, values: ImageValues, state: RenderableState, materialId: number, transparency: Transparency): Renderable<ImageValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...InternalSchema, ...ImageSchema };
const internalValues: InternalValues = {
export function ImageRenderable(ctx: WebGLContext, id: number, values: ImageValues, state: RenderableState, materialId: number, transparency: Transparency, globals: GlobalDefines): Renderable<ImageValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...GlobalDefineSchema, ...InternalSchema, ...ImageSchema };
const renderValues: ImageValues & InternalValues & GlobalDefineValues = {
...values,
uObjectId: ValueCell.create(id),
dLightCount: ValueCell.create(globals.dLightCount),
dColorMarker: ValueCell.create(globals.dColorMarker),
};
const shaderCode = ImageShaderCode;
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, { ...values, ...internalValues }, materialId, transparency);
return createRenderable(renderItem, values, state);
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, renderValues, materialId, transparency);
return createRenderable(renderItem, renderValues, state);
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2018-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -7,7 +7,7 @@
import { Renderable, RenderableState, createRenderable } from '../renderable';
import { WebGLContext } from '../webgl/context';
import { createGraphicsRenderItem, Transparency } from '../webgl/render-item';
import { GlobalUniformSchema, BaseSchema, AttributeSpec, DefineSpec, Values, InternalSchema, SizeSchema, ElementsSpec, InternalValues, GlobalTextureSchema, UniformSpec } from './schema';
import { GlobalUniformSchema, BaseSchema, AttributeSpec, DefineSpec, Values, InternalSchema, SizeSchema, ElementsSpec, InternalValues, GlobalTextureSchema, UniformSpec, GlobalDefineValues, GlobalDefines, GlobalDefineSchema } from './schema';
import { ValueCell } from '../../mol-util';
import { LinesShaderCode } from '../shader-code';
@@ -26,13 +26,16 @@ export const LinesSchema = {
export type LinesSchema = typeof LinesSchema
export type LinesValues = Values<LinesSchema>
export function LinesRenderable(ctx: WebGLContext, id: number, values: LinesValues, state: RenderableState, materialId: number, transparency: Transparency): Renderable<LinesValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...InternalSchema, ...LinesSchema };
const internalValues: InternalValues = {
export function LinesRenderable(ctx: WebGLContext, id: number, values: LinesValues, state: RenderableState, materialId: number, transparency: Transparency, globals: GlobalDefines): Renderable<LinesValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...GlobalDefineSchema, ...InternalSchema, ...LinesSchema };
const renderValues: LinesValues & InternalValues & GlobalDefineValues = {
...values,
uObjectId: ValueCell.create(id),
dLightCount: ValueCell.create(globals.dLightCount),
dColorMarker: ValueCell.create(globals.dColorMarker),
};
const shaderCode = LinesShaderCode;
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, { ...values, ...internalValues }, materialId, transparency);
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, renderValues, materialId, transparency);
return createRenderable(renderItem, values, state);
return createRenderable(renderItem, renderValues, state);
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2018-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -7,7 +7,7 @@
import { Renderable, RenderableState, createRenderable } from '../renderable';
import { WebGLContext } from '../webgl/context';
import { createGraphicsRenderItem, Transparency } from '../webgl/render-item';
import { GlobalUniformSchema, BaseSchema, AttributeSpec, ElementsSpec, DefineSpec, Values, InternalSchema, InternalValues, GlobalTextureSchema, ValueSpec, UniformSpec } from './schema';
import { GlobalUniformSchema, BaseSchema, AttributeSpec, ElementsSpec, DefineSpec, Values, InternalSchema, InternalValues, GlobalTextureSchema, ValueSpec, UniformSpec, GlobalDefineSchema, GlobalDefineValues, GlobalDefines } from './schema';
import { MeshShaderCode } from '../shader-code';
import { ValueCell } from '../../mol-util';
@@ -32,13 +32,16 @@ export const MeshSchema = {
export type MeshSchema = typeof MeshSchema
export type MeshValues = Values<MeshSchema>
export function MeshRenderable(ctx: WebGLContext, id: number, values: MeshValues, state: RenderableState, materialId: number, transparency: Transparency): Renderable<MeshValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...InternalSchema, ...MeshSchema };
const internalValues: InternalValues = {
export function MeshRenderable(ctx: WebGLContext, id: number, values: MeshValues, state: RenderableState, materialId: number, transparency: Transparency, globals: GlobalDefines): Renderable<MeshValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...GlobalDefineSchema, ...InternalSchema, ...MeshSchema };
const renderValues: MeshValues & InternalValues & GlobalDefineValues = {
...values,
uObjectId: ValueCell.create(id),
dLightCount: ValueCell.create(globals.dLightCount),
dColorMarker: ValueCell.create(globals.dColorMarker),
};
const shaderCode = MeshShaderCode;
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, { ...values, ...internalValues }, materialId, transparency);
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, renderValues, materialId, transparency);
return createRenderable(renderItem, values, state);
return createRenderable(renderItem, renderValues, state);
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2018-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -7,7 +7,7 @@
import { Renderable, RenderableState, createRenderable } from '../renderable';
import { WebGLContext } from '../webgl/context';
import { createGraphicsRenderItem, Transparency } from '../webgl/render-item';
import { GlobalUniformSchema, BaseSchema, AttributeSpec, DefineSpec, Values, InternalSchema, SizeSchema, InternalValues, GlobalTextureSchema } from './schema';
import { GlobalUniformSchema, BaseSchema, AttributeSpec, DefineSpec, Values, InternalSchema, SizeSchema, InternalValues, GlobalTextureSchema, GlobalDefineValues, GlobalDefines, GlobalDefineSchema } from './schema';
import { PointsShaderCode } from '../shader-code';
import { ValueCell } from '../../mol-util';
@@ -22,12 +22,15 @@ export const PointsSchema = {
export type PointsSchema = typeof PointsSchema
export type PointsValues = Values<PointsSchema>
export function PointsRenderable(ctx: WebGLContext, id: number, values: PointsValues, state: RenderableState, materialId: number, transparency: Transparency): Renderable<PointsValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...InternalSchema, ...PointsSchema };
const internalValues: InternalValues = {
export function PointsRenderable(ctx: WebGLContext, id: number, values: PointsValues, state: RenderableState, materialId: number, transparency: Transparency, globals: GlobalDefines): Renderable<PointsValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...GlobalDefineSchema, ...InternalSchema, ...PointsSchema };
const renderValues: PointsValues & InternalValues & GlobalDefineValues = {
...values,
uObjectId: ValueCell.create(id),
dLightCount: ValueCell.create(globals.dLightCount),
dColorMarker: ValueCell.create(globals.dColorMarker),
};
const shaderCode = PointsShaderCode;
const renderItem = createGraphicsRenderItem(ctx, 'points', shaderCode, schema, { ...values, ...internalValues }, materialId, transparency);
return createRenderable(renderItem, values, state);
const renderItem = createGraphicsRenderItem(ctx, 'points', shaderCode, schema, renderValues, materialId, transparency);
return createRenderable(renderItem, renderValues, state);
}

View File

@@ -132,6 +132,10 @@ export const GlobalUniformSchema = {
uInvModelViewProjection: UniformSpec('m4'),
uHasHeadRotation: UniformSpec('b'),
uInvHeadRotation: UniformSpec('m4'),
uIsAsymmetricProjection: UniformSpec('b'),
uHasEyeCamera: UniformSpec('b'),
uModelViewEye: UniformSpec('m4'),
uInvModelViewEye: UniformSpec('m4'),
uIsOrtho: UniformSpec('f'),
uPixelRatio: UniformSpec('f'),
@@ -194,6 +198,14 @@ export const GlobalTextureSchema = {
export type GlobalTextureSchema = typeof GlobalTextureSchema
export type GlobalTextureValues = Values<GlobalTextureSchema>
export const GlobalDefineSchema = {
dLightCount: DefineSpec('number'),
dColorMarker: DefineSpec('boolean'),
} as const;
export type GlobalDefineSchema = typeof GlobalDefineSchema
export type GlobalDefineValues = Values<GlobalDefineSchema>
export type GlobalDefines = UnboxedValues<GlobalDefineSchema>
export const InternalSchema = {
uObjectId: UniformSpec('i'),
} as const;
@@ -318,9 +330,6 @@ export const BaseSchema = {
...SubstanceSchema,
...ClippingSchema,
dLightCount: DefineSpec('number'),
dColorMarker: DefineSpec('boolean'),
dClipObjectCount: DefineSpec('number'),
dClipVariant: DefineSpec('string', ['instance', 'pixel']),
uClipObjectType: UniformSpec('i[]', 'material'),

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2019-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -7,7 +7,7 @@
import { Renderable, RenderableState, createRenderable } from '../renderable';
import { WebGLContext } from '../webgl/context';
import { createGraphicsRenderItem, Transparency } from '../webgl/render-item';
import { GlobalUniformSchema, BaseSchema, Values, InternalSchema, SizeSchema, InternalValues, ValueSpec, DefineSpec, GlobalTextureSchema, UniformSpec, TextureSpec } from './schema';
import { GlobalUniformSchema, BaseSchema, Values, InternalSchema, SizeSchema, InternalValues, ValueSpec, DefineSpec, GlobalTextureSchema, UniformSpec, TextureSpec, GlobalDefineValues, GlobalDefines, GlobalDefineSchema } from './schema';
import { SpheresShaderCode } from '../shader-code';
import { ValueCell } from '../../mol-util';
@@ -38,12 +38,15 @@ export const SpheresSchema = {
export type SpheresSchema = typeof SpheresSchema
export type SpheresValues = Values<SpheresSchema>
export function SpheresRenderable(ctx: WebGLContext, id: number, values: SpheresValues, state: RenderableState, materialId: number, transparency: Transparency): Renderable<SpheresValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...InternalSchema, ...SpheresSchema };
const internalValues: InternalValues = {
export function SpheresRenderable(ctx: WebGLContext, id: number, values: SpheresValues, state: RenderableState, materialId: number, transparency: Transparency, globals: GlobalDefines): Renderable<SpheresValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...GlobalDefineSchema, ...InternalSchema, ...SpheresSchema };
const renderValues: SpheresValues & InternalValues & GlobalDefineValues = {
...values,
uObjectId: ValueCell.create(id),
dLightCount: ValueCell.create(globals.dLightCount),
dColorMarker: ValueCell.create(globals.dColorMarker),
};
const shaderCode = SpheresShaderCode;
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, { ...values, ...internalValues }, materialId, transparency);
return createRenderable(renderItem, values, state);
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, renderValues, materialId, transparency);
return createRenderable(renderItem, renderValues, state);
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2019-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -7,7 +7,7 @@
import { Renderable, RenderableState, createRenderable } from '../renderable';
import { WebGLContext } from '../webgl/context';
import { createGraphicsRenderItem, Transparency } from '../webgl/render-item';
import { GlobalUniformSchema, BaseSchema, AttributeSpec, UniformSpec, Values, InternalSchema, SizeSchema, InternalValues, TextureSpec, ElementsSpec, ValueSpec, GlobalTextureSchema } from './schema';
import { GlobalUniformSchema, BaseSchema, AttributeSpec, UniformSpec, Values, InternalSchema, SizeSchema, InternalValues, TextureSpec, ElementsSpec, ValueSpec, GlobalTextureSchema, GlobalDefineValues, GlobalDefines, GlobalDefineSchema } from './schema';
import { TextShaderCode } from '../shader-code';
import { ValueCell } from '../../mol-util';
@@ -35,12 +35,15 @@ export const TextSchema = {
export type TextSchema = typeof TextSchema
export type TextValues = Values<TextSchema>
export function TextRenderable(ctx: WebGLContext, id: number, values: TextValues, state: RenderableState, materialId: number, transparency: Transparency): Renderable<TextValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...InternalSchema, ...TextSchema };
const internalValues: InternalValues = {
export function TextRenderable(ctx: WebGLContext, id: number, values: TextValues, state: RenderableState, materialId: number, transparency: Transparency, globals: GlobalDefines): Renderable<TextValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...GlobalDefineSchema, ...InternalSchema, ...TextSchema };
const renderValues: TextValues & InternalValues & GlobalDefineValues = {
...values,
uObjectId: ValueCell.create(id),
dLightCount: ValueCell.create(globals.dLightCount),
dColorMarker: ValueCell.create(globals.dColorMarker),
};
const shaderCode = TextShaderCode;
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, { ...values, ...internalValues }, materialId, transparency);
return createRenderable(renderItem, values, state);
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, renderValues, materialId, transparency);
return createRenderable(renderItem, renderValues, state);
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2019-2025 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
@@ -7,7 +7,7 @@
import { Renderable, RenderableState, createRenderable } from '../renderable';
import { WebGLContext } from '../webgl/context';
import { createGraphicsRenderItem, Transparency } from '../webgl/render-item';
import { GlobalUniformSchema, BaseSchema, DefineSpec, Values, InternalSchema, InternalValues, UniformSpec, TextureSpec, GlobalTextureSchema, ValueSpec } from './schema';
import { GlobalUniformSchema, BaseSchema, DefineSpec, Values, InternalSchema, InternalValues, UniformSpec, TextureSpec, GlobalTextureSchema, ValueSpec, GlobalDefineValues, GlobalDefines, GlobalDefineSchema } from './schema';
import { MeshShaderCode } from '../shader-code';
import { ValueCell } from '../../mol-util';
@@ -32,13 +32,16 @@ export const TextureMeshSchema = {
export type TextureMeshSchema = typeof TextureMeshSchema
export type TextureMeshValues = Values<TextureMeshSchema>
export function TextureMeshRenderable(ctx: WebGLContext, id: number, values: TextureMeshValues, state: RenderableState, materialId: number, transparency: Transparency): Renderable<TextureMeshValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...InternalSchema, ...TextureMeshSchema };
const internalValues: InternalValues = {
export function TextureMeshRenderable(ctx: WebGLContext, id: number, values: TextureMeshValues, state: RenderableState, materialId: number, transparency: Transparency, globals: GlobalDefines): Renderable<TextureMeshValues> {
const schema = { ...GlobalUniformSchema, ...GlobalTextureSchema, ...GlobalDefineSchema, ...InternalSchema, ...TextureMeshSchema };
const renderValues: TextureMeshValues & InternalValues & GlobalDefineValues = {
...values,
uObjectId: ValueCell.create(id),
dLightCount: ValueCell.create(globals.dLightCount),
dColorMarker: ValueCell.create(globals.dColorMarker),
};
const shaderCode = MeshShaderCode;
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, { ...values, ...internalValues }, materialId, transparency);
const renderItem = createGraphicsRenderItem(ctx, 'triangles', shaderCode, schema, renderValues, materialId, transparency);
return createRenderable(renderItem, values, state);
return createRenderable(renderItem, renderValues, state);
}

View File

@@ -203,9 +203,12 @@ namespace Renderer {
const modelViewProjection = Mat4();
const invModelViewProjection = Mat4();
const invHeadRotation = Mat4();
const modelViewEye = Mat4();
const invModelViewEye = Mat4();
const cameraDir = Vec3();
const cameraPosition = Vec3();
const cameraTarget = Vec3();
const cameraPlane = Plane3D();
const viewOffset = Vec2();
const frustum = Frustum3D();
@@ -230,6 +233,10 @@ namespace Renderer {
uInvModelViewProjection: ValueCell.create(invModelViewProjection),
uHasHeadRotation: ValueCell.create(false),
uInvHeadRotation: ValueCell.create(invHeadRotation),
uHasEyeCamera: ValueCell.create(false),
uModelViewEye: ValueCell.create(modelViewEye),
uInvModelViewEye: ValueCell.create(invModelViewEye),
uIsAsymmetricProjection: ValueCell.create(false),
uIsOrtho: ValueCell.create(1),
uViewOffset: ValueCell.create(viewOffset),
@@ -319,19 +326,16 @@ namespace Renderer {
} else {
r.uncull();
}
} else {
if (r.values.lodLevels) {
const { center, radius } = boundingSphere;
const d = Plane3D.distanceToPoint(cameraPlane, center);
r.cullSimple(d, radius, modelScale);
} else {
r.uncull();
}
}
let needUpdate = false;
if (r.values.dLightCount.ref.value !== light.count) {
ValueCell.update(r.values.dLightCount, light.count);
needUpdate = true;
}
if (r.values.dColorMarker.ref.value !== p.colorMarker) {
ValueCell.update(r.values.dColorMarker, p.colorMarker);
needUpdate = true;
}
if (needUpdate) r.update();
const program = r.getProgram(variant);
if (state.currentProgramId !== program.id) {
// console.log('new program')
@@ -405,10 +409,10 @@ namespace Renderer {
ValueCell.updateIfChanged(globalUniforms.uIsOrtho, camera.state.mode === 'orthographic' ? 1 : 0);
ValueCell.update(globalUniforms.uViewOffset, camera.viewOffset.enabled ? Vec2.set(viewOffset, camera.viewOffset.offsetX * 16, camera.viewOffset.offsetY * 16) : Vec2.set(viewOffset, 0, 0));
ValueCell.updateIfChanged(globalUniforms.uModelScale, camera.state.scale);
ValueCell.updateIfChanged(globalUniforms.uModelScale, camera.scale);
ValueCell.update(globalUniforms.uCameraPosition, Mat4.getTranslation(cameraPosition, invView));
const cameraTarget = Vec3.scale(Vec3(), camera.state.target, camera.state.scale);
Vec3.scale(cameraTarget, camera.state.target, camera.scale);
Vec3.normalize(cameraDir, Vec3.sub(cameraDir, cameraTarget, cameraPosition));
ValueCell.update(globalUniforms.uCameraDir, cameraDir);
@@ -429,22 +433,24 @@ namespace Renderer {
const hasHeadRotation = !Mat4.isZero(camera.headRotation);
if (hasHeadRotation) {
ValueCell.updateIfChanged(globalUniforms.uHasHeadRotation, hasHeadRotation);
ValueCell.updateIfChanged(globalUniforms.uHasHeadRotation, true);
ValueCell.update(globalUniforms.uInvHeadRotation, Mat4.invert(invHeadRotation, camera.headRotation));
ValueCell.update(globalUniforms.uLightDirection, getTransformedLightDirection(light, invHeadRotation));
} else {
ValueCell.update(globalUniforms.uHasHeadRotation, false);
ValueCell.update(globalUniforms.uInvHeadRotation, Mat4.id);
ValueCell.updateIfChanged(globalUniforms.uHasHeadRotation, false);
ValueCell.updateIfChanged(globalUniforms.uInvHeadRotation, Mat4.id);
ValueCell.update(globalUniforms.uLightDirection, light.direction);
}
ValueCell.update(globalUniforms.uIsAsymmetricProjection, camera.isAsymmetricProjection);
};
const updateInternal = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null, renderMask: Mask, markingDepthTest: boolean) => {
arrayMapUpsert(sharedTexturesList, 'tDepth', depthTexture || emptyDepthTexture);
modelScale = camera.state.scale;
modelScale = camera.scale;
ValueCell.update(globalUniforms.uModel, Mat4.scaleUniformly(model, group.view, camera.state.scale));
ValueCell.update(globalUniforms.uModel, Mat4.scaleUniformly(model, group.view, modelScale));
ValueCell.update(globalUniforms.uModelView, Mat4.mul(modelView, camera.view, model));
ValueCell.update(globalUniforms.uInvModelView, Mat4.invert(invModelView, modelView));
ValueCell.update(globalUniforms.uModelViewProjection, Mat4.mul(modelViewProjection, modelView, camera.projection));
@@ -453,6 +459,17 @@ namespace Renderer {
ValueCell.updateIfChanged(globalUniforms.uRenderMask, renderMask);
ValueCell.updateIfChanged(globalUniforms.uMarkingDepthTest, markingDepthTest);
const hasEyeCamera = !Mat4.isZero(camera.viewEye);
if (hasEyeCamera) {
ValueCell.updateIfChanged(globalUniforms.uHasEyeCamera, true);
ValueCell.update(globalUniforms.uModelViewEye, Mat4.mul(modelViewEye, camera.viewEye, model));
ValueCell.update(globalUniforms.uInvModelViewEye, Mat4.invert(invModelViewEye, modelViewEye));
} else {
ValueCell.updateIfChanged(globalUniforms.uHasEyeCamera, false);
ValueCell.updateIfChanged(globalUniforms.uModelViewEye, Mat4.id);
ValueCell.updateIfChanged(globalUniforms.uInvModelViewEye, Mat4.id);
}
state.enable(gl.SCISSOR_TEST);
state.colorMask(true, true, true, true);
@@ -483,17 +500,17 @@ namespace Renderer {
const alpha = clamp(r.values.alpha.ref.value * r.state.alphaFactor, 0, 1);
const xrayShaded = r.values.dXrayShaded?.ref.value === 'on' || r.values.dXrayShaded?.ref.value === 'inverted';
return (
(alpha < 1 && alpha !== 0) ||
alpha !== 0 && (alpha < 1 ||
r.values.transparencyAverage.ref.value > 0 ||
r.values.dGeometryType.ref.value === 'directVolume' ||
r.values.dPointStyle?.ref.value === 'fuzzy' ||
r.values.dGeometryType.ref.value === 'text' ||
r.values.dGeometryType.ref.value === 'image' ||
xrayShaded
xrayShaded)
);
};
const renderPick = (group: Scene.Group, camera: ICamera, variant: GraphicsRenderVariant, pickType: PickType) => {
const renderPick = (group: Scene.Group, camera: ICamera, variant: 'pick' | 'depth', pickType: PickType) => {
if (isTimingMode) ctx.timer.mark('Renderer.renderPick');
state.disable(gl.BLEND);
state.enable(gl.DEPTH_TEST);

View File

@@ -17,6 +17,8 @@ import { hash1 } from '../mol-data/util';
import { GraphicsRenderable } from './renderable';
import { Transparency } from './webgl/render-item';
import { clamp } from '../mol-math/interpolate';
import { GlobalDefines } from './renderable/schema';
import { ValueCell } from '../mol-util/value-cell';
const boundaryHelper = new BoundaryHelper('98');
@@ -75,6 +77,7 @@ interface Scene extends Object3D {
/** Returns `true` if some visibility has changed, `false` otherwise. */
syncVisibility: () => boolean
setTransparency: (transparency: Transparency) => void
setGlobals: (values: GlobalDefines) => void
update: (objects: ArrayLike<GraphicsRenderObject> | undefined, keepBoundingSphere: boolean) => void
add: (o: GraphicsRenderObject) => void // GraphicsRenderable
remove: (o: GraphicsRenderObject) => void
@@ -101,7 +104,7 @@ namespace Scene {
readonly renderables: ReadonlyArray<GraphicsRenderable>
}
export function create(ctx: WebGLContext, transparency: Transparency = 'blended'): Scene {
export function create(ctx: WebGLContext, transparency: Transparency = 'blended', globals: GlobalDefines = { dColorMarker: true, dLightCount: 1 }): Scene {
const renderableMap = new Map<GraphicsRenderObject, GraphicsRenderable>();
const renderables: GraphicsRenderable[] = [];
const boundingSphere = Sphere3D();
@@ -130,7 +133,7 @@ namespace Scene {
function add(o: GraphicsRenderObject) {
if (!renderableMap.has(o)) {
const renderable = createRenderable(ctx, o, transparency);
const renderable = createRenderable(ctx, o, transparency, globals);
renderables.push(renderable);
if (o.type === 'direct-volume') {
volumes.push(renderable);
@@ -311,6 +314,14 @@ namespace Scene {
renderables[i].setTransparency(value);
}
},
setGlobals: (values: GlobalDefines) => {
globals = values;
for (const r of renderables) {
ValueCell.updateIfChanged(r.values.dColorMarker, values.dColorMarker);
ValueCell.updateIfChanged(r.values.dLightCount, values.dLightCount);
r.update();
}
},
update(objects, keepBoundingSphere) {
if (objects) {
for (let i = 0, il = objects.length; i < il; ++i) {

View File

@@ -9,6 +9,7 @@ import { ValueCell } from '../mol-util';
import { idFactory } from '../mol-util/id-factory';
import { WebGLExtensions } from './webgl/extensions';
import { isWebGL2, GLRenderingContext } from './webgl/compat';
import { WebGLParameters } from './webgl/context';
import { assertUnreachable } from '../mol-util/type-helpers';
export type DefineKind = 'boolean' | 'string' | 'number'
@@ -375,7 +376,7 @@ function getGlsl300VertPrefix(extensions: WebGLExtensions, shaderExtensions: Sha
return prefix.join('\n') + '\n';
}
function getGlsl300FragPrefix(gl: WebGL2RenderingContext, extensions: WebGLExtensions, shaderExtensions: ShaderExtensions, outTypes: FragOutTypes) {
function getGlsl300FragPrefix(extensions: WebGLExtensions, parameters: WebGLParameters, shaderExtensions: ShaderExtensions, outTypes: FragOutTypes) {
const prefix = [
'#version 300 es',
`layout(location = 0) out highp ${outTypes[0] || 'vec4'} out_FragData0;`
@@ -388,8 +389,7 @@ function getGlsl300FragPrefix(gl: WebGL2RenderingContext, extensions: WebGLExten
if (shaderExtensions.drawBuffers) {
if (extensions.drawBuffers) {
prefix.push('#define requiredDrawBuffers');
const maxDrawBuffers = gl.getParameter(gl.MAX_DRAW_BUFFERS) as number;
for (let i = 1, il = maxDrawBuffers; i < il; ++i) {
for (let i = 1, il = parameters.maxDrawBuffers; i < il; ++i) {
prefix.push(`layout(location = ${i}) out highp ${outTypes[i] || 'vec4'} out_FragData${i};`);
}
}
@@ -410,14 +410,14 @@ function transformGlsl300Frag(frag: string) {
return frag.replace(/gl_FragData\[([0-9]+)\]/g, 'out_FragData$1');
}
export function addShaderDefines(gl: GLRenderingContext, extensions: WebGLExtensions, defines: ShaderDefines, shaders: ShaderCode): ShaderCode {
export function addShaderDefines(gl: GLRenderingContext, extensions: WebGLExtensions, parameters: WebGLParameters, defines: ShaderDefines, shaders: ShaderCode): ShaderCode {
const vertHeader = getDefinesCode(defines, shaders.ignoreDefine);
const fragHeader = getDefinesCode(defines, shaders.ignoreDefine);
const vertPrefix = isWebGL2(gl)
? getGlsl300VertPrefix(extensions, shaders.extensions)
: getGlsl100VertPrefix(extensions, shaders.extensions);
const fragPrefix = isWebGL2(gl)
? getGlsl300FragPrefix(gl, extensions, shaders.extensions, shaders.outTypes)
? getGlsl300FragPrefix(extensions, parameters, shaders.extensions, shaders.outTypes)
: getGlsl100FragPrefix(extensions, shaders.extensions);
const frag = isWebGL2(gl) ? transformGlsl300Frag(shaders.frag) : shaders.frag;
return {

View File

@@ -1,7 +1,7 @@
export const clip_instance = `
#if defined(dClipVariant_instance) && dClipObjectCount != 0
vec3 mCenter = (uModel * aTransform * vec4(uInvariantBoundingSphere.xyz, 1.0)).xyz;
if (clipTest(mCenter)) {
if (clipTest(mCenter / uModelScale)) {
// move out of [ -w, +w ] to 'discard' in vert shader
gl_Position.z = 2.0 * gl_Position.w;
}

View File

@@ -1,6 +1,6 @@
export const clip_pixel = `
#if defined(dClipVariant_pixel) && dClipObjectCount != 0
if (clipTest(vModelPosition))
if (clipTest(vModelPosition / uModelScale))
discard;
#endif
`;

Some files were not shown because too many files have changed in this diff Show More