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

react-r3f-shader-hook

v1.1.0

Published

A react hook that efficiently applies a shader to the scene without the need for expensive post-processing

Downloads

1

Readme

What does it do?

This package provides a hook which lets you apply a shader to an entire three.js scene without the hassle and overhead of using the EffectComposer. Simply call the hook with your desired shaders and you're done. It even returns a material object with which you can easily access and manipulate the uniforms.

Under the hood it works by implementing postprocessing with a 3 vertix Quad (a triangle) based on the approach by @luruke. Full credit for the original logic and concepts goes to him. This project builds on his approach by adapting it to the modern React framework, utilizing hooks for state and lifecycle management. This makes it very easy and intuitive to use in react-three-fiber.

Advantages

  • 🚀 More performant than the EffectComposer since it utilizes a quad with just 3 vertices instead of 4

  • 📦 Works out of the box. No need to import other components

  • 🎈 Lightweight. Perfect for projects where using EffectComposer may be overkill

  • 💡Intuitive and simple to use

  • ⚡ Faster. Although EffectComposer offers a variety of complex custom solutions, this provides a quick and dirty way to enahance your scene

  • 📐 Automatically handles resizing, offering an advantage over using Three.js out-of-the-box, where you would need to worry about handling responsiveness.

Documentation

The hook accepts any parameters that the RawShaderMaterial from three.js accepts. The three you will most commonly deal with are the following:

  • vertexShader: string
  • fragmentShader: string
  • uniforms: object

Installation

With Node.js installed on your machine, run the following to install the library:

npm i react-r3f-shader-hook

Keep in mind you will need the following dependencies installed in your project as they are peerDependencies of this library:

{
    "@react-three/fiber": "^8.15.12",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "three": ">=0.159.0 <1.0.0"
}

Usage

Once installed, simply import the hook like so:

import useShaderPass from 'react-r3f-shader-hook'

and call the hook while passing in your desired vertexShader, fragmentShader and uniforms. If you wish to access the uniforms in your code after the shader has been applied to the scene, the hook returns a material of the type RawShaderMaterial.

const shaderPassMaterial = useShaderPass({
  vertexShader,
  fragmentShader,
  uniforms,
})

and there you go. Your custom shaders are applied to the whole scene!

Now, to access the uniforms you provided, you can simply use material returned from the hook to read and change your uniforms. Just like you would with a <shaderMaterial> element when using react-three-fiber! For example:

useFrame((state) => {
  shaderPassMaterial.uniforms.uTime.value = state.clock.getElapsedTime()
})

Keep in mind that, like react-three-fiber hooks, this hook can only be called in a component that is a child of the <Canvas> component:

const Scene = () => {
  const vertexShader = /* Your vertex shader */
  const fragmentShader = /* Your fragment shader */
  const uniforms = /* Your uniforms */

  const shaderPassMaterial = useShaderPass({
    vertexShader,
    fragmentShader,
    uniforms,
  })

  return (
    <mesh>
      <boxGeometry args={[2, 2]} />
      <meshBasicMaterial />
    </mesh>
  )
}

function App() {
  return (
    <Canvas>
      <Scene />
    </Canvas>
  )
}

export default App

Contributing

Documentation on contributing can be found in CONTRIBUTING.md

License

This library is MIT Licensed