Skip to content

Feat sdf loader #31140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft

Feat sdf loader #31140

wants to merge 5 commits into from

Conversation

jakekinchen
Copy link

What

Adds native support for MDL Molfile / SDF data to three.js via an enhanced SDFLoader.

  • The loader now contains a compact inline parser (≈60 LOC) that converts raw SDF text into atom + bond data and then builds the usual instanced-mesh THREE.Group.
  • No external Python/PDB conversion required — molecules can be loaded directly in the browser or Node.

Why

Many chemistry / materials projects store molecules as SDF.
Until now developers had to:

  1. Convert SDF → PDB with a Python tool, or
  2. Write a custom parser.

This PR removes that barrier and lets pure-JS (Next.js, Vite, etc.) apps visualise SDF files out-of-the-box, just like PDB files.

Highlights

  • Loader only – the parser lives inside SDFLoader.js; no new export or dependency added to the Addons bundle.
  • Robust parsing – tolerates ChemDraw-style files where the counts line over-states bond lines, skips malformed records gracefully.
  • Unit-tested – existing SDFLoader.tests.js updated; all addon tests pass.
  • Example updatedexamples/webgl_loader_sdf.html demonstrates direct SDF loading with atom labels.
  • Bundle impact – +≈2 KB (minified) for the inline parser.

Usage

import { SDFLoader } from 'three/addons/loaders/SDFLoader.js';

const loader = new SDFLoader();
loader.load( 'models/sdf/caffeine.sdf', group => scene.add( group ) );

Screenshots

Caffeine rendered from SDF
caffeine

Implementation Notes

  • Parser is wrapped in an IIFE within parse() to keep scope local and avoid public API surface increase.
  • ESLint padded-block warnings are disabled only inside that IIFE (loader file stays otherwise lint-clean).
  • Element colour / radius tables and instanced-mesh logic remain unchanged and customisable via setElementData().

Happy to adjust naming, structure or docs as requested by maintainers — thanks for reviewing!

jakekinchen and others added 4 commits May 19, 2025 22:11
…molecule-rendering

Add SDFLoader for Molfile support
- Improved readability and maintainability of the SDFLoader code.
- Added error handling for invalid atom and bond data in the parsing process.
- Included detailed JSDoc comments for the SDFLoader class.
- Updated unit tests to include SDFLoader tests.
@@ -0,0 +1,66 @@
import { SDFLoader } from '../../../../examples/jsm/loaders/SDFLoader.js';
import { Group, Color } from 'three';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused import Color.
* @param {string} text - Raw SDF or MOL text content.
* @returns {Object} Parsed molecule data with `atoms` and `bonds` arrays.
*/
function parseSDF( text ) {
Copy link
Collaborator

@Mugen87 Mugen87 May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As suggested at the forum, I vote to move this bit out of the library into a separate repository:

https://discourse.threejs.org/t/why-is-there-a-pdb-loader-and-no-sdf-loader/82861/2

You are ignoring many SDF properties right now that might be relevant for other use cases in the future.

*
* @see examples/webgl_loader_sdf
*/
class SDFLoader extends Loader {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vote to remove SDFLoader and implement the JSON parsing right in the example. The loader contains too much application specific code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SDF parsing library could be imported like in other examples:

"three-mesh-bvh": "https://cdn.jsdelivr.net/npm/[email protected]/build/index.module.js",
"web-ifc": "https://cdn.jsdelivr.net/npm/[email protected]/web-ifc-api.js",
"web-ifc-three": "https://cdn.jsdelivr.net/npm/[email protected]/IFCLoader.js"

@Mugen87 Mugen87 marked this pull request as draft May 20, 2025 20:42
@WestLangley
Copy link
Collaborator

New example: https://raw.githack.com/jakekinchen/three.js/feat-sdf-loader/examples/webgl_loader_sdf.html

Tip: Metals require an environment map. I'd suggest removing the lights and adding RoomEnvironment as your light source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants