diff --git a/zine-constants.js b/zine-constants.js index 29735bb..9d03749 100644 --- a/zine-constants.js +++ b/zine-constants.js @@ -1,9 +1,11 @@ export const panelSize = 1024; +export const pointCloudPositionalStride = 4 + 4 + 4; +export const pointCloudFullStride = pointCloudPositionalStride + 1 + 1 + 1; + export const floorNetWorldSize = 100; export const floorNetWorldDepth = 1000; export const floorNetResolution = 0.1; export const floorNetPixelSize = floorNetWorldSize / floorNetResolution; -export const pointcloudStride = 4 + 4 + 4 + 1 + 1 + 1; export const physicsPixelStride = 8; diff --git a/zine-geometry-utils.js b/zine-geometry-utils.js index 419572d..d98f059 100644 --- a/zine-geometry-utils.js +++ b/zine-geometry-utils.js @@ -1,7 +1,5 @@ import * as THREE from 'three'; -import { - pointcloudStride, -} from './zine-constants.js'; +import { pointCloudPositionalStride } from './zine-constants.js'; // @@ -18,9 +16,9 @@ function pointCloudArrayBufferToPositionAttributeArray( pixelStride = 1, ) { // result in float32Array const dataView = new DataView(arrayBuffer); - for (let i = 0, j = 0; i < arrayBuffer.byteLength; i += pointcloudStride) { + for (let i = 0, j = 0; i < arrayBuffer.byteLength; i += pointCloudPositionalStride) { if (pixelStride !== 1) { - const i2 = i / pointcloudStride; + const i2 = i / pointCloudPositionalStride; const sx = i2 % width; const sy = Math.floor(i2 / width); if (sx % pixelStride !== 0 || sy % pixelStride !== 0) { // skip non-stride points @@ -138,6 +136,32 @@ export function depthFloat32ArrayToGeometry( // +export function getDepthFloat32ArrayWorldPositionPx( + depthFloat32Array, + px, + py, + width, + height, + camera, + scale, + target +) { + px = Math.min(Math.max(px, 0), width - 1); + py = Math.min(Math.max(py, 0), height - 1); + + let x = px / width; + let y = py / height; + + const i = py * width + px; + y = 1 - y; + + const viewZ = depthFloat32Array[i]; + const worldPoint = setCameraViewPositionFromViewZ(x, y, viewZ, camera, target); + worldPoint.multiply(scale); + worldPoint.applyMatrix4(camera.matrixWorld); + return target; +} + export function getDepthFloat32ArrayWorldPosition( depthFloat32Array, x, // 0..1 @@ -289,8 +313,12 @@ export const snapPointCloudToCamera = (pointCloudArrayBuffer, width, height, cam const scaleFactor = 1 / width; const dataView = new DataView(pointCloudArrayBuffer); - for (let i = 0; i < pointCloudArrayBuffer.byteLength; i += pointcloudStride) { - let x = dataView.getFloat32(i + 0, true); + for ( + let i = 0; + i < pointCloudArrayBuffer.byteLength; + i += pointCloudPositionalStride + ) { + let x = dataView.getFloat32(i , true); let y = dataView.getFloat32(i + 4, true); let z = dataView.getFloat32(i + 8, true); @@ -413,8 +441,6 @@ export const setCameraViewPositionFromOrthographicViewZ = (x, y, viewZ, camera, return target; } -// - export const getDoubleSidedGeometry = geometry => { const geometry2 = geometry.clone(); // double-side the geometry @@ -452,4 +478,4 @@ export const getGeometryHeights = (geometry, width, height, heightfieldScale) => } } return heights; -}; \ No newline at end of file +}; diff --git a/zine-renderer.js b/zine-renderer.js index 423d91e..6febc79 100644 --- a/zine-renderer.js +++ b/zine-renderer.js @@ -19,7 +19,7 @@ import { } from './zine-camera-utils.js'; import { floorNetPixelSize, - pointcloudStride, + pointCloudPositionalStride, physicsPixelStride, } from './zine-constants.js'; @@ -218,9 +218,9 @@ class ScenePhysicsMesh extends THREE.Mesh { // } const arrayBuffer = pointCloudArrayBuffer; const pixelStride = physicsPixelStride; - for (let i = 0, j = 0; i < arrayBuffer.byteLength; i += pointcloudStride) { + for (let i = 0, j = 0; i < arrayBuffer.byteLength; i += pointCloudPositionalStride) { if (pixelStride !== 1) { - const i2 = i / pointcloudStride; + const i2 = i / pointCloudPositionalStride; const sx = i2 % width; const sy = Math.floor(i2 / width); if (sx % pixelStride !== 0 || sy % pixelStride !== 0) { // skip non-stride points @@ -228,7 +228,7 @@ class ScenePhysicsMesh extends THREE.Mesh { } } - const s = segmentSpecs.array[i / pointcloudStride]; + const s = segmentSpecs.array[i / pointCloudPositionalStride]; segments[j] = s; j++; @@ -544,4 +544,4 @@ export class ZineRenderer extends EventTarget { ); targetZineRenderer.camera.updateMatrixWorld(); } -} \ No newline at end of file +}