• This repository has been archived on 10/Apr/2022
  • Stars
    star
    161
  • Rank 225,297 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🧩 Compose modular materials in React

Version Downloads Discord Shield

Component Material

Material is a React utility that helps you compose and modify materials in react-three-fiber and threejs.

Examples

Quick start

yarn add component-material
import Material from 'component-material'

function CustomMaterial(props) {
  return (
    <Material
      {...props}
      // 1️⃣ declare uniforms with the correct type
      uniforms={{
        r: { value: 1, type: 'float' },
        g: { value: 0.5, type: 'float' },
        b: { value: 0, type: 'float' },
      }}>
      <Material.Frag.Body
        // 2️⃣ Access the uniforms in your shader
        children={`gl_FragColor = vec4(r, g, b, 1.0);`}
      />
    </Material>
  )
}

function Sphere() {
  return (
    <mesh>
      <sphereBufferGeometry />
      <CustomMaterial />
    </mesh>

Features

<Material/>

from

By default Material extends three's MeshPhysicalMaterial. If you want to extend a different material just use the from prop passing the desired material constructor.

<Material from={THREE.MeshPhongMaterial} />

uniforms

Uniforms used inside shaders can be defined via the uniforms prop as follows

<Material
  uniforms={{
    myUniform1: { value: 0, type: 'float' },
    myUniform2: { value: [0, 1], type: 'vec2' },
  }}
/>

This will also create setters and getters for the uniforms automatically, allowing you to mutate them using props and effectively making the material reactive:

function CustomMaterial({ color }) {
  return (
    <Material
      uniforms={{ color: { value: color, type: 'vec3' } }}
      color={color} // color uniform will have the value of the color prop
    />
  • The correspondences between glsl and javascript types can be seen here
  • Uniforms cannot be defined twice in the same shader. So be careful not to define the same uniforms inside the head tag.

varyings

Varying variables can be defined directly inside the shader head tag or they can be declared as prop:

<Material
  varyings={{
    myVarying1: { type: 'float' },
    myVarying2: { type: 'vec2' },
  }}
/>

This is equivalent to adding this code to both your vertex and fragment shaders heads:

float myVarying1;
vec2 myVarying2;
  • Varyings don't have an initial value, only a type definition
  • As uniforms, varyings cannot be defined twice in the same shader, this will give a glsl error. So be careful not to define the same varyings inside the head tag.

Fragment- and vertex-shader composition

The Frag and Vert tags have the function of injecting the shader text, passed as children, into the preconfigured shader of the threejs material. Let's see what it means with an example:

<Material uniforms={{ time: { value: 0, type: 'float' } }}>
  <Material.Frag.Head
    children={`
    float quadraticInOut(float t) {
      float p = 2.0 * t * t;
      return t < 0.5 ? p : -p + (4.0 * t) - 1.0;
    }`}
  />
  <Material.Frag.Body
    children={`
    gl_FragColor.a = gl_FragColor.a * quadraticInOut((sin(time) + 1.0) / 2.0);`}
  />

In the code above the Frag.Head component adds an easing function quadraticInOut to the fragment shader of the material, prepending it before the main function of the shader.

The Frag.Body component instead adds a line of code that modify the gl_FragColor alpha value, appending it after the last operation of the main function.

In particular, if we take as an example the fragment shader of the MeshPhysicalMaterial, Frag.Head prepends the code before this shader line, Frag.Body instead posts the code after this shader line (the dithering_fragment chunk).

The same goes for the Vert component, which however acts on the vertex shader. In particular, Vert.Head prepends the code to this shader line, while Vert.Body appends the code to this shader line (the project_vertex chunk).

It is possible to inject the code after a particular chunk just by doing

<Material.Frag.my_chunk children={`// my custom shader`} />

where my_chunk must be replaced with the name of the chunk concerned.

If we wanted to insert some code just after the emissivemap_fragment chunk (here the reference for the MeshPhysicalMaterial) then just use the following code

<Material.Frag.emissivemap_fragment children={`// my custom shader`} />

replaceChunk

The replaceChunk prop is a boolean that allows you to completely replace the chosen chunk, so instead of append the custom shader code after the chunk it will be replaced directly.

<Material.Frag.emissivemap_fragment replaceChunk children={`// my custom shader`} />

Common chunks

The Common tag is useful in case vertex shader and fragment shader share some functions.

❌ If both the fragment shader and the vertex shader share the easing function quadraticInOut, instead of writing

<Material.Vert.Head
  children={`
  float quadraticInOut(float t) {
    float p = 2.0 * t * t;
    return t < 0.5 ? p : -p + (4.0 * t) - 1.0;
  }`}
/>
<Material.Frag.Head
  children={`
  float quadraticInOut(float t) {
    float p = 2.0 * t * t;
    return t < 0.5 ? p : -p + (4.0 * t) - 1.0;
  }`}
/>

✅ we will write

<Material.Common
  children={`
  float quadraticInOut(float t) {
    float p = 2.0 * t * t;
    return t < 0.5 ? p : -p + (4.0 * t) - 1.0;
  }`}
/>

More Repositories

1

zustand

🐻 Bear necessities for state management in React
TypeScript
40,808
star
2

react-spring

✌️ A spring physics based React animation library
TypeScript
27,160
star
3

react-three-fiber

🇨🇭 A React renderer for Three.js
TypeScript
25,320
star
4

jotai

👻 Primitive and flexible state management for React
TypeScript
16,893
star
5

use-gesture

👇Bread n butter utility for component-tied mouse/touch gestures in React and Vanilla Javascript.
TypeScript
8,537
star
6

valtio

💊 Valtio makes proxy-state simple for React and Vanilla
TypeScript
8,257
star
7

drei

🥉 useful helpers for react-three-fiber
JavaScript
7,122
star
8

leva

🌋 React-first components GUI
TypeScript
4,528
star
9

gltfjsx

🎮 Turns GLTFs into JSX components
JavaScript
3,949
star
10

use-cannon

👋💣 physics based hooks for @react-three/fiber
TypeScript
2,654
star
11

react-three-next

React Three Fiber, Threejs, Nextjs starter
JavaScript
2,140
star
12

postprocessing

A post processing library for three.js.
JavaScript
2,100
star
13

racing-game

🏎 Open source racing game developed by everyone willing
TypeScript
2,094
star
14

react-xr

🤳 VR/AR with react-three-fiber
TypeScript
1,890
star
15

react-three-flex

💪📦 Flexbox for react-three-fiber
TypeScript
1,606
star
16

suspend-react

🚥 Async/await for React components
TypeScript
1,308
star
17

react-postprocessing

📬 postprocessing for react-three-fiber
JavaScript
1,009
star
18

detect-gpu

Classifies GPUs based on their 3D rendering benchmark score allowing the developer to provide sensible default settings for graphically intensive applications.
TypeScript
979
star
19

lamina

🍰 An extensible, layer based shader material for ThreeJS
TypeScript
976
star
20

its-fine

🐶🔥 A collection of escape hatches for React.
TypeScript
891
star
21

react-use-measure

🙌 Utility to measure view bounds
TypeScript
799
star
22

react-nil

⃝ A react null renderer
TypeScript
772
star
23

react-three-rapier

🤺 Rapier physics in React
TypeScript
765
star
24

maath

🪶 Math helpers for the rest of us
TypeScript
755
star
25

threejs-journey

⚛️ Bruno Simons journey demos in React
TypeScript
685
star
26

three-stdlib

📚 Stand-alone library of threejs examples designed to run without transpilation in node & browser
JavaScript
624
star
27

react-three-editor

🔌 A one of a kind scene editor that writes changes back into your code
TypeScript
602
star
28

react-three-a11y

♿️ Accessibility tools for React Three Fiber
TypeScript
507
star
29

react-zdog

⚡️🐶 React bindings for zdog
JavaScript
441
star
30

use-asset

📦 A promise caching strategy for React Suspense
TypeScript
414
star
31

react-three-offscreen

📺 Offscreen worker canvas for react-three-fiber
TypeScript
397
star
32

drei-vanilla

🍦 drei-inspired helpers for threejs
TypeScript
364
star
33

ecctrl

🕹️ A floating rigibody character controller
TypeScript
349
star
34

tunnel-rat

🐀 Non gratum anus rodentum
TypeScript
287
star
35

react-three-lgl

🔆 A React abstraction for the LGL Raycaster
TypeScript
260
star
36

market

📦 Download CC0 assets ready to use in your next 3D Project
JavaScript
249
star
37

react-three-csg

🚧 Constructive solid geometry for React
TypeScript
242
star
38

gltf-react-three

Convert GLTF files to React Three Fiber Components
JavaScript
230
star
39

env

💄 An app to create, edit, and preview HDR environment maps in the browser
TypeScript
143
star
40

use-p2

👋💣 2d physics hooks for @react-three/fiber
TypeScript
141
star
41

react-ogl

🦴 A barebones react renderer for ogl.
TypeScript
139
star
42

react-spring-examples

JavaScript
138
star
43

react-three-gpu-pathtracer

⚡️ A React abstraction for the popular three-gpu-pathtracer
TypeScript
125
star
44

react-three-lightmap

In-browser lightmap/AO baker for react-three-fiber and ThreeJS
TypeScript
123
star
45

cannon-es-debugger

Wireframe debugger for use with cannon-es https://github.com/react-spring/cannon-es
HTML
103
star
46

rafz

💍 One loop to frame them all.
TypeScript
96
star
47

website

Poimandres developer collective website
JavaScript
87
star
48

swc-jotai

Rust
85
star
49

assets

📦 Importable base64 encoded CC0 assets
Makefile
84
star
50

react-three-scissor

✂ Multiple scenes, one canvas! WebGL Scissoring implementation for React Three Fiber.
TypeScript
81
star
51

eslint-plugin-valtio

An eslint plugin for better valtio experience
JavaScript
69
star
52

react-spring.io

✌️ A spring physics based React animation library
TypeScript
56
star
53

react-three-babel

🛍 A Babel plugin that automatically builds the extend catalogue of known native Three.js elements
TypeScript
53
star
54

r3f-website

Website for React Three Fiber
JavaScript
26
star
55

market-assets

JavaScript
19
star
56

react-three-8thwall

JavaScript
17
star
57

drei-assets

JavaScript
16
star
58

discord

🤖 Poimandres Discord Bot
TypeScript
10
star
59

react-three-jolt

⚡ Jolt physics in React
CSS
10
star
60

branding

TypeScript
7
star
61

market-assets-do

JavaScript
5
star
62

envinfo

Easily collect useful information for bug reports
JavaScript
4
star
63

leva-wg

1
star