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.

R3F23c: a DataTexture keeps its rows in order (texture.ts bytesOf turned a flipY=false one upside down, on maps and in shaders): its first row is the picture's top, Bevy's v 0 as it is three's unflipped; a test reads the PNG's rows back

shinyobjectze67910028a6c
diff --git a/sdk/r3f/src/input/texture.ts b/sdk/r3f/src/input/texture.ts
index fe1371f4..08c711b9 100644
--- a/sdk/r3f/src/input/texture.ts
+++ b/sdk/r3f/src/input/texture.ts
@@ -244,7 +244,8 @@ export function pngOf(rgba: Uint8Array, width: number, height: number): string {
return "data:image/png;base64," + base64(all);
}
-/** a DataTexture's pixels as RGBA bytes, top row first (three's flipY puts its first row at the bottom) */
+/** a DataTexture's pixels as RGBA bytes in its own row order: its first row is the image's top, where Bevy's v 0 is,
+ * as three's v 0 is its first row unflipped; a flipY one is read with v turned over (flipsY), as three turns it over */
function bytesOf(t: THREE.DataTexture): Uint8Array | undefined {
const { data, width, height } = t.image as { data: ArrayLike<number> | null; width: number; height: number };
if (!data || !width || !height) return undefined;
@@ -254,9 +255,8 @@ function bytesOf(t: THREE.DataTexture): Uint8Array | undefined {
const half = t.type === THREE.HalfFloatType;
const out = new Uint8Array(width * height * 4);
for (let y = 0; y < height; y++) {
- const from = t.flipY ? y : height - 1 - y;
for (let x = 0; x < width; x++) {
- const i = (from * width + x) * per, o = (y * width + x) * 4;
+ const i = (y * width + x) * per, o = i / per * 4;
for (let c = 0; c < 4; c++) {
let v = c < per ? Number(data[i + c]) : c === 3 ? (float ? 1 : 255) : per === 1 ? Number(data[i]) : 0;
if (half && c < per) v = THREE.DataUtils.fromHalfFloat(v);
diff --git a/sdk/r3f/test/input.test.tsx b/sdk/r3f/test/input.test.tsx
index fb30bbfb..f5a01e89 100644
--- a/sdk/r3f/test/input.test.tsx
+++ b/sdk/r3f/test/input.test.tsx
@@ -3,6 +3,7 @@ import { describe, expect, test } from "bun:test";
import { useFrame } from "@react-three/fiber";
import { createContext, useContext, useRef } from "react";
import { renderToStaticMarkup } from "react-dom/server";
+import { inflateSync } from "node:zlib";
import * as THREE from "three";
import { Clock, useCurrentFrame } from "@raea/app/clock";
import { convert } from "../src/input/scene";
@@ -218,4 +219,15 @@ describe("textures", () => {
expect(srcOf(img)).toBe("media/wood.jpg");
expect(pngOf(new Uint8Array(4), 1, 1).length).toBeGreaterThan(40);
});
+
+ test("a DataTexture's first row is its picture's top: three's v 0 unflipped, Bevy's v 0", () => {
+ const t = new THREE.DataTexture(new Uint8Array([255, 0, 0, 255, 0, 0, 255, 255]), 1, 2);
+ t.needsUpdate = true;
+ const png = Uint8Array.from(atob(srcOf(t)!.split(",")[1]), (c) => c.charCodeAt(0));
+ const at = png.findIndex((_, i) => String.fromCharCode(...png.subarray(i, i + 4)) === "IDAT");
+ const len = new DataView(png.buffer).getUint32(at - 4);
+ const rows = inflateSync(png.subarray(at + 4, at + 4 + len));
+ // each row a filter byte then rgba: red first, blue below it
+ expect([...rows.subarray(1, 4), ...rows.subarray(6, 9)]).toEqual([255, 0, 0, 0, 0, 255]);
+ });
});