npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

three-shader-baker

v1.0.8

Published

Bake your Three.js shaders or materials into textures!

Downloads

15

Readme

Shader Baker lets you export your Three.js shaders or materials as textures. This is useful when you want to use the same shader in other 3D software like Blender, Unity, etc. You can also use the exported textures in other Three.js shaders/materials to save on performance.

It supports both Vanilla and React!

import { ShaderBaker, getTextureAsDataUrl } from "three-shader-baker";

const mesh = new THREE.Mesh(...)

const baker = new ShaderBaker();
const fbo = baker.bake(renderer, mesh, {
  // Optional options
  scene: scene, // Scene that the mesh is in. If provided, the current env map will be used.
  size: 1024, // Number. Size of the baked texture
  target: null // THREE.WebGLRenderTarget. If provided, the baked texture will be rendered to this target.
})

// Your resulting texture
const dataUrl = getTextureAsDataUrl(renderer, fbo.texture);
import {
  ShaderBaker,
  useShaderBaker,
  getTextureAsDataUrl
} from "three-shader-baker/react";

function Mesh() {
  const { bake } = useShaderBaker();
  const meshRef = useRef();

  useEffect(() => {
    const fbos = bake();
    const targrtFbo = fbos[meshRef.uuid];

    // Your resulting texture
    const dataUrl = getTextureAsDataUrl(renderer, targetFbo.texture);
  }, []);

  return <mesh ref={meshRef}>...</mesh>;
}

<ShaderBaker>
  <Mesh />
</ShaderBaker>;

License

This project is licensed under the AGPL-3.0 License. See the LICENSE file for details.Please email me at [email protected] if you would like to use this library under a different license.

Installation

npm install three-shader-baker
yarn add three-shader-baker

Helpers (React only)

You can set the following helpers on THREE.Mesh.userData to control the baking process:

  • __shaderBaker_isExcluded - Set this to true to exclude the mesh from baking.
  • __shaderBaker_size - Set this to a number to override the size of the baked texture for this mesh. Can be used to bake different textures at different resolutions.

Gotchas

  • (React only) The bake functions from const { bake } = useShaderBaker() is memoized. Make sure to pass it as a dependency in your useEffect hook otherwise it will unnecessary state updates.
  • (React only) The order of priority for the size prop is as follows:
    1. __shaderBaker_size on the mesh
    2. size parameter in the bake function
    3. size prop on the <ShaderBaker /> component
  • Lighting is baked from a single point of view ([0, 0, -1] looking towards [0, 0, 0]). This means specular highlights and reflections will not be accurate if the camera is dynamic. This is a limitation of texture baking in general.
  • Any animated shaders will only be baked at the current frame. Unless you bake the texture at every frame, the animation will not be captured. See the performance section for more details.

Performance

Baking shaders is an expensive operation. It is recommended to bake the shaders only once and then use the resulting texture in your scene or export it out. The cost of baking is directly proportional to the size of the baked texture. The larger the texture, the more time it will take to bake.

If you have animated shaders, you will need to bake the texture at every frame to capture the animation. This can be done by calling the bake function at every frame. This is not recommended for real-time applications as it can be very expensive.

At the moment, baking is a synchronous operation. This means that the browser will freeze while the baking is happening. You will also see a flicker in the scene as the process is happening. I will be looking into making this asynchronous in the future.

For now, it is recommended to black out the screen or show a loading screen while the baking is happening.

API Reference

ShaderBaker

ShaderBaker.constructor()

Creates a new instance of the ShaderBaker.

ShaderBaker.bake(renderer: THREE.WebGLRenderer, mesh: THREE.Mesh, options: Object): THREE.WebGLRenderTarget

Bakes the shader/material of the mesh into a render target.

  • renderer - The renderer instance.
  • mesh - The mesh whose shader/material you want to bake.
  • options - Optional options.
    • scene - The scene that the mesh is in. If provided, the current env map will be used.
    • size - Number. Size of the baked texture. Default is 1024.
    • target - THREE.WebGLRenderTarget. If provided, the baked texture will be rendered to this target.
    • dilation - Number. Number of pixels to dilate the texture by. Default is 2. This is useful to prevent seam artifacts.

ShaderBaker.dispose()

Disposes of the ShaderBaker instance and its associated resources.

getTextureAsDataUrl(renderer: THREE.WebGLRenderer, texture: THREE.Texture): string

Converts a texture to a data URL.

downloadTexture(renderer: THREE.WebGLRenderer, texture: THREE.Texture, filename: string): void

Downloads a texture as a png file.

<ShaderBaker />

A provider component that provides the useShaderBaker hook. Any THREE.Mesh inside this component will be baked into separate render targets.

This props of this component are as follows:

  • size - Number. Size of the baked texture. Default is 1024.
  • dilation - Number. Number of pixels to dilate the texture by. Default is 2. This is useful to prevent seam artifacts.

<ShaderBakerExclusion />

A component that excludes a THREE.Mesh from being baked. Any THREE.Mesh inside this component will be excluded from baking.

You can manually exclude meshes by setting THREE.Mesh.userData["__shaderBaker_isExcluded"] = true.

const obj = useShaderBaker()

A hook that returns useful functions and data for baking. The object returned has the following properties:

  • bake(args): { [mesh.uuid]: THREE.WebGLRenderTarget }
    • This function bakes all the meshes inside the <ShaderBaker /> component.
    • The args are as follows:
      • mesh: mesh: THREE.Mesh | THREE.Mesh[] | null - The mesh(es) to bake. If not provided, all meshes inside the <ShaderBaker /> component will be baked.
      • options: Object
        • dialetion - Number. Number of pixels to dilate the texture by. Default is 2. This is useful to prevent seam artifacts.
        • size - Number. Size of the baked texture. Default is 1024.
  • renderTargets: { [key: string]: THREE.WebGLRenderTarget }
    • An object containing all the render targets of the baked meshes. Keyed by the their respective mesh's uuid.
  • textures: { [key: string]: THREE.Texture }
    • An object containing all the textures of the baked meshes (THREE.WebGLRenderTarget.texture). Keyed by the their respective mesh's uuid.

getTextureAsDataUrl(renderer: THREE.WebGLRenderer, texture: THREE.Texture): string

Converts a texture to a data URL.

downloadTexture(renderer: THREE.WebGLRenderer, texture: THREE.Texture, filename: string): void

Downloads a texture as a png file.