Giter Club home page Giter Club logo

cubemap-sh's Introduction

cubemap-sh

unstable

alt text

Generate spherical harmonics from a cubemap. The example screenshot shows them used to cheaply generate environmental diffuse light for a mesh.

Installing

$ npm i -S cubemap-sh

Usage

You pass in:

  • an array of faces in the shown order (opengl order)
  • size of each face (default is 128)
  • number of channels (3 for rgb or 4 for rgba, default is 4)
const coefficients = sh([posx, negx, posy, negy, posz, negz], CUBE_MAP_SIZE, NUM_CHANNELS)

What you get back is an array of 9 vec3s (an array of arrays, each one having 3 elements for r g and b). To use them for example to light up your mesh, you can use the corresponding glsl-sh module and pass them to a shader that looks like this:

precision mediump float;
#pragma glslify: sh = require('glsl-sh') // import using glslify
varying vec3 vWorldNormal;
uniform vec3 c[9]; // this is what you get from the function above, pass it straight to the shader
uniform vec3 color;
void main() {
  vec3 n = normalize(vWorldNormal);
  vec3 shColor = sh(c, n) * color; // here we get diffuse light calculated by the sperhical harmonics multiplied by the color of the mesh
  gl_FragColor = vec4(shColor, 1.0);
  gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0 / 2.2)); // gamma correction
}

Please note this example uses glslify.

API

const sh = require('cubemap-sh')

sh([faces], cubemapSize=128, numberOfChannels=4)

faces:

  • faces: array of 6 cubemap faces in opengl order ([psx, negx, posy, negy, posz, negz]), each face is an array of pixels
  • cubemapSize: size of each face, default is 128
  • numberOfChannels: 3 for rgb textures, 4, for rgba. defaults to 4

returns array of 9 vec3s which correspond to the spherical harmonics coefficients for the first 2 bands (3 levels) for each of the color channels (RGB) in linear color space

License

MIT

cubemap-sh's People

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.