简体   繁体   中英

Threejs 2D Texture array issue when read raw image data

When I Try To execute this example , I see its work fine with current sample , But when I try to put my binary file which is contain continuous 2D frames jpg image was exported from here I got a none detailed image like this:

在此处输入图片说明

And this is converter binary code:

<?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;
?>
~       

And this is threejs file reader:

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 );
      } );

DataTexture are arrays of rgb jpeg files are compressed data, it's not the same. You can use this function:

function jpegToBinaryData( $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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM