Coder Social home page Coder Social logo

pmndrs / react-three-lightmap Goto Github PK

View Code? Open in Web Editor NEW
127.0 9.0 8.0 685 KB

In-browser lightmap/AO baker for react-three-fiber and ThreeJS

License: MIT License

JavaScript 2.92% TypeScript 96.96% CSS 0.12%
react threejs lightmap react-three-fiber r3f lightmap-baker ao ambient-occlusion ao-baker

react-three-lightmap's Introduction

@react-three/lightmap

In-browser lightmap and ambient occlusion (AO map) baker for react-three-fiber and ThreeJS.

example screenshot of lightmap baker output

Example:

<Lightmap>
  <mesh position={[0, 0, 0]} castShadow receiveShadow>
    <boxBufferGeometry attach="geometry" args={[3, 3, 1]} />
    <meshStandardMaterial attach="material" color="#ff6080" />
  </mesh>

  <mesh position={[0, 0, 1.8]} castShadow receiveShadow>
    <boxBufferGeometry attach="geometry" args={[2, 2, 2]} />
    <meshStandardMaterial attach="material" color="#4080ff" />
  </mesh>
</Lightmap>

Try it in this editable sandbox.

NOTE: actual lightmap rendering is performed on a separate hidden canvas and WebGL context. If you are consuming any context in your lightmapped content, you will need to "bridge" that context.

To track when baking is complete, provide onComplete callback to Lightmap - it will be called with the resulting texture as the first argument. The library does automatically assign that texture as the lightmap on all the baked mesh materials too.

Local Development

git clone [email protected]:pmndrs/react-three-lightmap.git
cd react-three-lightmap
yarn
yarn storybook

Wishlist

  • onComplete callback
  • proper denoising, calibrate the light sampler
  • much more optimization
  • composited multi-layer lightmap based on several distinct groups of light sources
    • e.g. for individual flickering lights, neon signs, etc
  • rudimentary light probe support for dynamic meshes/sprites
    • can start with just omnidirectional total amounts collected in 2D grid textures
    • might want the light probe pattern to be customizable
  • bake-only lights (turned off after bake)
    • useful for game levels - e.g. could have hundreds of lights baked in and then discarded
    • currently the lightmap is indirect-only, so this needs an extra step to sample direct light contribution
  • saving/loading the generated lightmap texture (useful for game levels)

Notes

Based on original experimental implementation by @unframework.

react-three-lightmap's People

Contributors

drcmda avatar unframework 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-three-lightmap's Issues

Update to work with latest r3f (^6) and drei (^4)

Expectation

  • react-three-lightmap works with most recent r3f (6.0.13) and drei (5.1.17)

Actual

  • react-three-lightmap doesn't work if r3f is above version 5 and if drei is above version 3

Notes

  • r3f dependency for react-three-lightmap is currently set to react-three-fiber (legacy?)
  • Drei dependency for react-three-lightmap is set to a much older v3 (current is v5)

Example

  • This CodeSandbox works as expected
  • Changing the r3f dependency on the above sandbox to 6 or above causes things to stop working
  • Changing the drei dependency on the above sandbox to 4 or above causes things to stop working

image

Question: What is the internal basis of the "generatePixelAreaLookup" function

I have been reading the source code of this repo recently and have some questions about the function used to generate probePixelAreaLookup data. What is the internal basis of the generatePixelAreaLookup function?

export function generatePixelAreaLookup(probeTargetSize: number) {
  const probePixelCount = probeTargetSize * probeTargetSize;
  const lookup = new Array(probePixelCount) as number[];

  const probePixelBias = 0.5 / probeTargetSize;

  for (let py = 0; py < probeTargetSize; py += 1) {
    // compute offset from center (with a bias for target pixel size)
    const dy = py / probeTargetSize - 0.5 + probePixelBias;

    for (let px = 0; px < probeTargetSize; px += 1) {
      // compute offset from center (with a bias for target pixel size)
      const dx = px / probeTargetSize - 0.5 + probePixelBias;

      // compute multiplier as affected by inclination of corresponding ray
      const span = Math.hypot(dx * 2, dy * 2);
      const hypo = Math.hypot(span, 1);
      const area = 1 / hypo;

      lookup[py * probeTargetSize + px] = area;
    }
  }

  return lookup;
}

Is there any calculation basis?

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.