Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gltf-loader

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gltf-loader

A Webpack loader for glTF files.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

gltf-loader

A comprehensive Webpack loader for glTF files.

v5 v2.0

Rationale

TL;DR: I wanted my glTF assets loaded the same as all my other image assets. :upside_down_face:

By design, glTF files are comprised of multiple assets representing the various components of a 3D scene. Typically, these files contain external references to binary files for geometries and animations, and image files for textures.

When using a module bundler such as Webpack, image assets can be optimized, versioned, and then referenced in JavaScript by importing them directly. However, if you're using something like Three.js to import glTF files, its assets are requested during runtime and thus won't have any of the optimizations or versioning applied to them in the way they would if they were handled by Webpack.

This loader fixes that problem by iterating through glTF JSON data and loading its assets automatically, replacing any uri references with the asset's final output URI.

You can read more about the glTF 2.0 specification here.

Installation

npm install gltf-loader

Configuration

All loader options are typed, documented, and available in the declaration file here.

webpack.config.js

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.(gltf)$/,
        loader: "gltf-loader",
        /**
         * @type {import("gltf-loader").GLTFLoaderOptions}
         */
        options: {
          // ...
        },
      },
      {
        test: /\.(bin|png|jpe?g)$/,
        type: "asset/resource",
      },
    ],
  },
};

As a path

By default, the loader injects the glTF file path during import. This is especially useful when using Three.js:

import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import myModel from "../assets/my-model.gltf"; // e.g. /dist/static/media/my-model.a1b2c3d4.gltf

const loader = new GLTFLoader();
loader.load(myModel, (gltf) => {
  // ...
});

As JSON data

Alternatively, you can set the loader option inline: true if you wish to import the raw JSON data instead:

import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import myModel from "../assets/my-model.gltf"; // JSON data

const loader = new GLTFLoader();
loader.parse(JSON.stringify(myModel), window.location.origin + "/", (gltf) => {
  // ...
});

TypeScript

For TypeScript users, adding the following module declaration will fix any "cannot find module" errors:

modules.d.ts

declare module "*.gltf" {
  const content: string;
  export default content;
}

Note that if you supply the inline: true config option, you would want to change the above declaration to something like the following:

declare module "*.gltf" {
  const content: Record<string, unknown>; // Or a glTF interface if you have/need one
  export default content;
}

tsconfig.json

  {
    "compilerOptions": {
      // ...
    }.
    "include": [
+     "modules.d.ts",
      "foo.d.ts",
      "bar.d.ts
    ],
    "exclude": []
  }

Keywords

FAQs

Package last updated on 22 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc