webxr_test_constants_fake_depth.js (3068B)
1 'use strict'; 2 3 // This file introduces constants used to mock depth data for depth sensing API. 4 5 const convertDepthBufferToArrayBuffer = function (data, desiredFormat) { 6 if(desiredFormat == "luminance-alpha" || desiredFormat == "unsigned-short") { 7 const result = new ArrayBuffer(data.length * 2); // each entry has 2 bytes 8 const view = new Uint16Array(result); 9 10 for(let i = 0; i < data.length; ++i) { 11 view[i] = data[i]; 12 } 13 14 return new Uint8Array(result); 15 } else if(desiredFormat == "float32") { 16 const result = new ArrayBuffer(data.length * 4); // each entry has 4 bytes 17 const view = new Float32Array(result); 18 19 for(let i = 0; i < data.length; ++i) { 20 view[i] = data[i]; 21 } 22 23 return new Uint8Array(result); 24 } else { 25 throw new Error("Unrecognized data format!"); 26 } 27 } 28 29 // Let's assume that the depth values are in cm, Xcm = x * 1/100m 30 const RAW_VALUE_TO_METERS = 1/100; 31 32 const createDepthSensingData = function() { 33 const depthSensingBufferHeight = 5; 34 const depthSensingBufferWidth = 7; 35 const depthSensingBuffer = [ 36 1, 1, 1, 1, 1, 1, 1, // first row 37 1, 2, 3, 4, 5, 6, 7, 38 1, 4, 9, 16, 25, 36, 49, 39 1, 8, 27, 64, 125, 216, 343, 40 1, 16, 81, 256, 625, 1296, 2401, 41 ]; // depthSensingBuffer value at column c, row r is Math.pow(c+1, r). 42 43 // Let's assume that the origin of the depth buffer is in the bottom right 44 // corner, with X's growing to the left and Y's growing upwards. 45 // This corresponds to the origin at 2401 in the above matrix, with X axis 46 // growing from 2401 towards 1296, and Y axis growing from 2401 towards 343. 47 // This corresponds to a rotation around Z axis by 180 degrees, with origin at [1,1]. 48 const depthSensingBufferFromViewerTransform = { 49 position: [1, 1, 0], 50 orientation: [0, 0, 1, 0], 51 }; 52 53 return { 54 depthData: convertDepthBufferToArrayBuffer(depthSensingBuffer, "luminance-alpha"), 55 depthFormat: "luminance-alpha", 56 width: depthSensingBufferWidth, 57 height: depthSensingBufferHeight, 58 normDepthBufferFromNormView: depthSensingBufferFromViewerTransform, 59 rawValueToMeters: RAW_VALUE_TO_METERS, 60 }; 61 }; 62 63 const DEPTH_SENSING_DATA = createDepthSensingData(); 64 65 const OFFSET_DEPTH_SENSING_DATA = { 66 ...DEPTH_SENSING_DATA, 67 projectionMatrix: VALID_DEPTH_PROJECTION_MATRIX, 68 viewOffset: DEPTH_OFFSET, 69 }; 70 71 // Returns expected depth value at |column|, |row| coordinates, expressed 72 // in depth buffer's coordinate system. 73 const getExpectedValueAt = function(column, row) { 74 return Math.pow(column+1, row) * RAW_VALUE_TO_METERS; 75 }; 76 77 const DEPTH_CONFIG_ALL_FORMATS = ['luminance-alpha', 'float32', 'unsigned-short']; 78 const DEPTH_CONFIG_ALL_USAGES= ['gpu-optimized', 'cpu-optimized']; 79 80 const VALID_DEPTH_CONFIG_CPU_USAGE = { 81 usagePreference: ['cpu-optimized'], 82 dataFormatPreference: ['luminance-alpha', 'float32', 'unsigned-short'], 83 }; 84 85 const VALID_DEPTH_CONFIG_GPU_USAGE = { 86 usagePreference: ['gpu-optimized'], 87 dataFormatPreference: ['luminance-alpha', 'float32', 'unsigned-short'], 88 };