Giter Site home page Giter Site logo

thor85 / glsl-float-to-rgba Goto Github PK

View Code? Open in Web Editor NEW

This project forked from equinor/glsl-float-to-rgba

0.0 1.0 0.0 2 KB

WebGL shader function to encode a 32-bit float to the RGBA color channels of a texel

License: MIT License

GLSL 100.00%

glsl-float-to-rgba's Introduction

glsl-float-to-rgba

Installation

npm install --save glsl-float-to-rgba

Signature

vec4 floatToRgba(float texelFloat, bool littleEndian);

Usage

This package exports a GLSL function, floatToRgba, that encodes a 32-bit floating point values to the RGBA channels of a WebGL texture. This is a workaround for the lack of support in baseline WebGL 1.0 for floating-point textures. WebGL 2.0 allows floating-point textures, and WebGL 1.0 allows them when the extension OES_texture_float is available, but OES_texture_float only guarantees reading from floating-point textures, not writing to. This package seeks to fill that gap.

floatToRgba is designed to be used as a GLSLify module. To encode a texel in your shader code, pass the float to the function floatToRgba. Note that because the function has to encode the float to its constituent bits, it needs to know whether the machine on which it's running uses big-endian or little-endian representation.

// fragment shader

#pragma glslify: floatToRgba = require('glsl-float-to-rgba')

// does the machine use little-endian representation?
uniform bool littleEndian;

void main() {
  float floatValue = ...

  gl_FragColor = floatToRgba(floatValue, littleEndian);
}
var pixels = new Uint8Array(CANVAS.width * CANVAS.height * 4);
gl.readPixels(0, 0, CANVAS.width, CANVAS.height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
pixels = new Float32Array(pixels.buffer);

Here's a simple way to compute endianness in JavaScript. Pass the result as a uniform variable to your shader.

const littleEndian = (function machineIsLittleEndian() {
  const uint8Array = new Uint8Array([0xAA, 0xBB]);
  const uint16array = new Uint16Array(uint8Array.buffer);
  return uint16array[0] === 0xBBAA;
})();

Attribution

Based on routine described here.

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.