Compare commits

...

3 Commits

Author SHA1 Message Date
DomNomNomVR
82b5852625 added note about how to JS 2025-02-24 21:33:33 +13:00
DomNomNomVR
bda3fd3b37 reverse bit order to little endian 2025-02-24 21:33:13 +13:00
DomNomNomVR
fa9fb27a98 dom's untested FloatToIntBits 2025-02-24 21:19:42 +13:00

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using UnityEngine; using UnityEngine;
using System.Runtime.InteropServices;
using System.IO; using System.IO;
@ -100,18 +102,20 @@ public class RT3script : MonoBehaviour
} }
//encode into pixels this camera's coordinates //encode into pixels this camera's coordinates
var encodedTransform = new Color[256]; var encodedTransform = new Color[32];
var tr = cameraList[i].transform.localToWorldMatrix; var tr = cameraList[i].transform.localToWorldMatrix;
for (int j = 0; j < 256; j++) { uint floatbits = FloatToIntBits(tr[1,1]);
for (int j = 0; j < 32; j++) {
//this logic does a bitwise check on the length value at bit j //this logic does a bitwise check on the length value at bit j
if ((((int)tr[0,0] >> j) & 1)==1) { // use little-endian (ends with least significant bit on the right)
if (((floatbits >> ((31)-j)) & 1)==1) {
encodedTransform[j] = Color.white; encodedTransform[j] = Color.white;
} }
else { else {
encodedTransform[j] = Color.black; encodedTransform[j] = Color.black;
} }
} }
outputImage.SetPixels((256*i),outputImage.height-2,256,1, encodedTransform); outputImage.SetPixels((256*i), outputImage.height-2,32,1, encodedTransform);
//encode into pixels transform matrix for this camera //encode into pixels transform matrix for this camera
outputImage.Apply(); outputImage.Apply();
@ -126,6 +130,30 @@ public class RT3script : MonoBehaviour
frameCount += 1; frameCount += 1;
} }
}
// inverse in javascript of this:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setUint32
// // Create an ArrayBuffer with 32bits (4 bytes)
// const buffer = new ArrayBuffer(4);
// const view = new DataView(buffer);
// view.setUint32(0, 1057279852); // Max unsigned 32-bit integer
// console.log(view.getFloat32(0));
// Based on this: https://stackoverflow.com/a/16822144
[StructLayout(LayoutKind.Explicit)]
private struct FloatAndUIntUnion {
// The memort layout of this struct looks like this
// but both pointers refer to the same memory address.
// UInt32Bits: IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
// FloatValue: SEEEEEEEEFFFFFFFFFFFFFFFFFFFFFFF
[FieldOffset(0)] public uint UInt32Bits;
[FieldOffset(0)] public float FloatValue;
}
public static uint FloatToIntBits(float value) {
FloatAndUIntUnion f2i = default(FloatAndUIntUnion);
f2i.FloatValue = value; // write as float
return f2i.UInt32Bits; // read back as int
} }
void OnDestroy() void OnDestroy()