Troubleshooting problem with array of 2D textures in Threejs caused by reading raw image data

Whenever I attempt to run this example, it works perfectly fine with the provided sample. However, when I try to use my own binary file that contains a series of continuous 2D frames in JPG format which were exported from here, I end up with an image lacking detail like this:

https://i.sstatic.net/z0RU4.gif

This is the code used for converting the binary file:

<?php
header("Content-type: application/octet-stream");
header('Content-disposition: attachment; filename=head256x256x1092.bin');

$dirs = scandir('512');
unset($dirs[0]);
unset($dirs[1]);
natsort($dirs);
foreach($dirs as $dir):
$fp = fopen('512/'.$dir, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); // this is essential for large downloads
}
fclose($fp);

endforeach;
?>
~       

This is the three.js file reader implementation:

new THREE.FileLoader()
      .setResponseType( 'arraybuffer' )
      .load( 'head256x256x1092.zip', function ( data ) {

        var zip = new JSZip( data );
        var array = zip.files[ 'head256x256x1092.bin' ].asUint8Array();

        var texture = new THREE.DataTexture2DArray( array, 256, 256, 109 );
        texture.format = THREE.RedFormat;
        texture.type = THREE.UnsignedByteType;
        texture.needsUpdate = true;
        var material = new THREE.ShaderMaterial( {
          uniforms: {
            diffuse: { value: texture },
            depth: { value: 0 },
            size: { value: new THREE.Vector2( planeWidth, planeHeight ) }
          },
          vertexShader: document.getElementById( 'vs' ).textContent.trim(),
          fragmentShader: document.getElementById( 'fs' ).textContent.trim()
        } );
        var geometry = new THREE.PlaneBufferGeometry( planeWidth, planeHeight );
        mesh = new THREE.Mesh( geometry, material );
        scene.add( mesh );
      } );

Answer №1

DataTexture consists of arrays containing rgb values, whereas jpeg files contain compressed data which is not the same. To convert a jpeg file into binary data, you can utilize the following function:

function convertJpegToBinaryData($filename) {
  $data = '';
  $img = imagecreatefromjpeg($filename);
  for($y = 0; $y < imagesy($img); $y++) {
    for($x = 0; $x < imagesx($img); $x++) {
        $rgb = imagecolorat($img, $x, $y);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        $data .= chr($r) . chr($g) . chr($b);
    }
  }
  return $data;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I turn off warnings for angular-translation?

A series of translation related warnings are appearing in the browser console, and I am looking to suppress or disable all of these warnings to prevent them from being shown to the user. Warnings: Translation for Department doesn't exist a.(anonymous ...

Choosing the Right JS Framework for an iPad HTML Prototype

Is there a quality Javascript framework available specifically for creating iPad prototypes? I've been searching but haven't found anything helpful yet. Cheers, Stefan ...

Combining React Components and HTML Elements

I've always been puzzled by the prevalence of using components in React examples. For instance, consider a scenario where I only need to combine some basic HTML with my components: class Homepage extends Component { render() { return ( &l ...

ghostly indentation post resizing

Can you please explain the mysterious appearance of ghostly indents upon clicking when the height is set to 0? These indents only become visible after the dropdown opens and the height is subsequently adjusted to zero. Also, what causes the jerky animation ...

Why is my jQuery clearQueue function malfunctioning?

I'm facing an issue with my AJAX function where multiple notifications are being displayed if the user calls it repeatedly in a short span of time. I want to show the notification only once and avoid queuing them up. AJAX FUNCTION function update(up ...

Obtain the sender using an href JavaScript code

Is there a way to retrieve the tag that called a javascript function if the anchor has a href tag of javascript:someFunc()? I know that in the onclick attribute, you can pass this, but when called from the href tag, this references the DOMWindow. Unfortu ...

PDF Embed Whitelist

Is there a way to embed PDFs on a specific domain in a read-only format without allowing saving or downloading? While services like Scribd and Slideshare offer private options, I haven't found one that allows whitelisting for embeds on certain domains ...

add an svg string element to a pre-existing svg element

I already have an <svg></svg> element in my code and now I want to add another svg element to it as a string. For instance: var new_svg = '<g><text x="100" y="100">Hello</text></g>'; d3.select('svg' ...

Tips for customizing the appearance of the Android status bar in Nativescript?

I am facing a challenge in styling the status bar in NativeScript and have not yet found a solution. I attempted to use this package but encountered issues when inserting the following line into the main-page.xml file: <x:StatusBar android:barStyle=&quo ...

Creating a webpage that loads directly to a specific section of content

After searching online, I couldn't find the solution I was looking for. My goal is to have the visible part of the page load halfway down the actual page. This way, when users visit the site, they can immediately scroll up to see more content. I hope ...

Leveraging the power of Angular.JS to synchronize a JSON value with a Syncfusion Circular gauge

I've been working on a project that involves using the Syncfusion Javascript gauge control to show a weekly pay bonus. The data for this is stored in a SharePoint list, and I have written a JavaScript code to convert the SharePoint list from XML to JS ...

Performing a `contains` operation with JQuery on this variable or object

Help! I'm completely confused about this problem. First, I am trying to use jQuery to select an li object and then check if a certain text exists within it using the "contains" function. However, for some reason, the line with the "contains" function ...

Angular dropdown menu not being populated

I need assistance in creating a menu that allows me to modify the "cell" attribute of an individual. The cells are specified as follows: Cells: [ { name: 'NE', id: 1 }, { name: 'NW', id: 2 }, { name: 'SE', id: 3 }, { name: & ...

AJAX throws an error when a function is used with a variable

My knowledge of jQuery and Javascript is quite limited, and I prefer not to work with them extensively. However, I always encounter an error that I can't seem to troubleshoot. Below is my code for ajax / javascript: function eintragen(id){ $.post( ...

a responsive grid view featuring automatically updated column headings

Can a table be populated in angular.js without predefining column names before the 'ng-repeat'? Currently, the table is populated like this.. <table class="table"> <thead> ...

React is throwing an error because it cannot access the property 'length' of an undefined value

TypeError: Cannot read property 'length' of undefined When I try to run my React app, I keep getting this error message from the compiler. What steps should I take to resolve this issue? request = (start,end) => { if(this.state.teams.lengt ...

What is the best way to include a date range together with other placeholder variables in a PostgreSQL and Express setup?

Forgive me for what may be a basic question, but as I dive headfirst into teaching myself SQL & PostgresSQL, I've stumbled upon an issue. I'm attempting to insert a large number of placeholders into an INSERT statement, but I'm struggling t ...

React: Error parsing - Missing closing JSX tag for <input> element

I am currently learning React and am in the process of working on a new component. Here is the code snippet that I have so far: order-item.component.jsx: import React, { Component } from 'react'; import { connect } from 'react-redux'; ...

Adding a JavaScript file in a Ctools modal popup in Drupal: a step-by-step guide

Is there a way to include a JavaScript file in a Ctools modal popup? I tried adding the js file (datatables.js) in the .tpl.php file, but it doesn't seem to be working. The popup isn't recognizing this file. Any suggestions on how to make it wo ...

Generating a port file upon starting the Express server

I have encountered a problem while using the node v20.12.2 version in my application and starting the express server. Every time I start the server, it creates a file with port 5004; in my project folder. Subsequently, when I attempt to restart the server, ...