Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

2.7 KB
Raw Blame
// Bevy's tone mapping in the playable (docs/playables.feature PA10): the 3D drawn into a half-float target with no
// tone curve, then one full-screen pass maps it through TonyMcMapface (Bevy's default, tools/tony.ts's table) and
// encodes sRGB, as bevy_core_pipeline's tonemapping.wesl does. Where nothing was drawn it stays transparent, so the
// page's background shows through as it does under the native engine's 3D.
import * as THREE from "three";
import tony from "./tony.bin";

const N = Math.round(Math.cbrt(tony.length / 3));

const FRAG = /* glsl */ `
precision highp float;
precision highp sampler3D;
uniform sampler2D scene;
uniform sampler3D lut;
in vec2 vUv;
out vec4 color;
void main() {
  vec4 c = texture(scene, vUv);
  vec3 x = max(c.rgb / max(c.a, 1e-5), vec3(0.0));
  vec3 uv = (x / (x + 1.0)) * (${N - 1}.0 / ${N}.0) + 0.5 / ${N}.0;
  // the table holds sRGB-encoded bytes, already the screen's values
  color = vec4(texture(lut, clamp(uv, 0.0, 1.0)).rgb * c.a, c.a);
}`;

const VERT = /* glsl */ `
in vec3 position;
in vec2 uv;
out vec2 vUv;
void main() { vUv = uv; gl_Position = vec4(position.xy, 0.0, 1.0); }`;

export class Tonemap {
  private target: THREE.WebGLRenderTarget;
  private quad: THREE.Mesh;
  private camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);

  constructor(private renderer: THREE.WebGLRenderer) {
    renderer.toneMapping = THREE.NoToneMapping;
    renderer.outputColorSpace = THREE.LinearSRGBColorSpace;
    this.target = new THREE.WebGLRenderTarget(1, 1, { type: THREE.HalfFloatType, samples: 4, colorSpace: THREE.LinearSRGBColorSpace });
    const rgba = new Uint8Array(N * N * N * 4);
    for (let i = 0; i < N * N * N; i++) rgba.set([tony[i * 3], tony[i * 3 + 1], tony[i * 3 + 2], 255], i * 4);
    const lut = new THREE.Data3DTexture(rgba, N, N, N);
    lut.minFilter = lut.magFilter = THREE.LinearFilter;
    lut.unpackAlignment = 1;
    lut.needsUpdate = true;
    const material = new THREE.RawShaderMaterial({ glslVersion: THREE.GLSL3, vertexShader: VERT, fragmentShader: FRAG, uniforms: { scene: { value: this.target.texture }, lut: { value: lut } }, transparent: true, premultipliedAlpha: true, blending: THREE.NoBlending, depthTest: false, depthWrite: false });
    this.quad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), material);
  }

  /** The target sized to the canvas's drawing buffer */
  resize() {
    const s = this.renderer.getDrawingBufferSize(new THREE.Vector2());
    this.target.setSize(s.x, s.y);
  }

  render(scene: THREE.Scene, camera: THREE.Camera) {
    const r = this.renderer;
    r.setRenderTarget(this.target);
    r.setClearColor(0x000000, 0);
    r.clear();
    r.render(scene, camera);
    r.setRenderTarget(null);
    r.render(this.quad, this.camera);
  }
}