Coder Social home page Coder Social logo

ektogamat / fake-glow-material-r3f Goto Github PK

View Code? Open in Web Editor NEW
115.0 1.0 6.0 4.6 MB

A fake glow material for react three fiber.

Home Page: https://r3f-fake-glow-material.vercel.app/

License: MIT License

HTML 18.69% JavaScript 62.53% CSS 17.26% TypeScript 1.51%
glow glsl-shaders react-three-fiber threejs

fake-glow-material-r3f's Introduction

Fake Glow Material for React Three Fiber

A simple to use glow material for react three fiber that can create glow effect from a mesh.

Live link: https://r3f-fake-glow-material.vercel.app/

Code Sandbox example: https://codesandbox.io/p/devbox/crazy-elbakyan-r8qz9n

Introduction

This material enables the utilization of a 3D mesh as a GLOW effect through GLSL Shaders, all without the need for any post-processing effects.

Unlike traditional approaches using post-processing, my new React Three Fiber material employs GLSL Shaders directly for creating glow effects on 3D meshes. This method avoids the usual performance hit associated with post-processing, such as the bloom effect, making it a more efficient solution for integrating glow effects in 3D scenes.

HOW TO USE?

1: Download the Material

Create a new file with the contents of this gist on your project.

Using TypeScript? Use this one instead - by (https://github.com/nirtamir2)

2: Import the material

import FakeGlowMaterial from './FakeGlowMaterial'

3: Add this as a material for your mesh

...
<mesh>
 <sphereGeometry />
 <FakeGlowMaterial />
</mesh>
...

Done. That's all that you need to use this component.

This effect is mesh based, meaning that you need to provide a mesh for this to work properly

When you give it a mesh, remember that it must be smooth enough so glsl can calculate normals and create the effect. For example, for sharp meshes like a cube, you can use a sphere to simulate the glow, instead of a copy of the cube.

โŒ Bad: Cube mesh for glow โœ… Good: Sphere mesh for glow

Customization properties

You can use VSCODE Intellisense feature to get all the explanation for each property. Just press CRTL + SPACE BAR.

Name Type Default Description
falloff Number 0.1 Controls the value of the Falloff effect. Ranges from 0.0 to 1.0.
glowInternalRadius Number 6.0 Controls the internal glow radius. Ranges from -1.0 to 1.0. Set a darker color to get the fresnel effect only.
glowColor String "#00ff00" Specifies the color of the hologram. Use hexadecimal format.
glowSharpness Number 1.0 Specifies the edges sharpness. Defaults to 1.0.
side String "THREE.FrontSide" Specifies side for the material, as THREE.DoubleSide. Options are "THREE.FrontSide", "THREE.BackSide", "THREE.DoubleSide". Defaults to "THREE.FrontSide".
opacity Number 1.0 Controls the opacity for the effect. Defaults to 1.0
depthTest Boolean false Enable or disable depthTest, controling if the mesh should not be visible when hided by other meshes. In case true, setting the side to FrontSide only is recommended.

Here is an example of how you can pass these props. If you pass any of those props, the default values will be overwritten.

<FakeGlowMaterial
    falloff={0.2}
    glowInternalRadius={0.7}
    glowColor={"#ff0000"}
    glowSharpness={2.3}
    side={"THREE.FrontSide"}
    opacity={1.0}
    depthTest={false}
/>

You can also use LEVA to create a panel, like in the demo, so you can tweak the parameters live.

const glowControls = useControls({
    falloff: { value: 0.45, min: 0.0, max: 1.0 },
    glowInternalRadius: { value: 0.7, min: -1.0, max: 1.0 },
    glowColor: { value: "#51a4de" },
    glowSharpness: { value: 1.0, min: 0.0, max: 2.0 },
    side: { value: "THREE.FrontSide", options: ["THREE.FrontSide", "THREE.BackSide", "THREE.DoubleSide"] },
    opacity: { value: 1.0, min: 0.0, max: 1.0 },
    depthTest: false,
});

Then you can just spread this object to use it in FakeGlowMaterial

 <FakeGlowMaterial  {...glowControls} />

Show, don't tell

Here you can watch a video with it in action and some options

Next steps

  • Adding the possibility to receive a mesh and auto scale its normals

License

This component is an open-source creation released under the (MIT) license. While you're free to integrate and innovate with it, I would genuinely appreciate knowing when this Fake Glow Material component sparks creativity in your projects!

Resources

Resources: Threejs, WebGL, React Three Fiber, React three post processing

Can you leave a star please?

Would be really appreciated if you are willing to give me a star here on GitHub ๐ŸŽ‰ or buy me a coffee โ˜• https://www.buymeacoffee.com/andersonmancini. The money will be used to produce more content about threejs or to buy new courses.

fake-glow-material-r3f's People

Contributors

ektogamat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

fake-glow-material-r3f's Issues

Vanilla Version

Awesome job on this!
You mentioned in your tweet that you were working on a Vanilla version. Just wondering if you've had any progress with that. If it's still planned, shall I watch for updates on this issue? Or do you have a separate repo?

Thanks for your time, and for open-sourcing this great example!

Mistake in leva example code

Hey there,

just letting you know that there is a small mistake in the leva example for the glowInternalRadius variable. In the example it looks like this:

const glowControls = useControls({
    falloff: { value: 0.45, min: 0.0, max: 1.0 },
    glowInternalRadius: { value: 0.7, min: -1.0, max: 1.0 },
    glowColor: { value: "#51a4de" },
    glowSharpness: { value: 1.0, min: 0.0, max: 2.0 },
    side: { value: "THREE.FrontSide", options: ["THREE.FrontSide", "THREE.BackSide", "THREE.DoubleSide"] },
    opacity: { value: 1.0, min: 0.0, max: 1.0 },
    depthTest: false,
});

However, it seems like glowInternalRadius is not supposed to be between -1 and 1, but rather between -10 and 10? As the default value is 6 and not 0.6.

This took me a second to notice since I was playing around with values unable to get the effect desired.

Best!

Geting a type error when attempting to use this in a TS project

The error:

Property 'fakeGlowMaterial' does not exist on type 'JSX.IntrinsicElements'.ts(2339)

Steps to reproduce:

Copy the code from the gist in the readme

Versions used currently:
"three": "^0.160.0",
"@react-three/drei": "9.96.0",
"@react-three/fiber": "8.15.13",

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.