Browse Source

第一次修复obj转gltf功能,已经可以转换只有一个mesh的obj文件

master
yuanjiajia 3 years ago
parent
commit
e051cc4079
  1. 9
      examples/loadObjDemo.php
  2. 119
      src/core/LoardObj.php

9
examples/loadObjDemo.php

@ -9,9 +9,14 @@ include dirname(dirname(__FILE__)) . "/vendor/autoload.php";
ini_set('memory_limit','4096M');
$ply = new LoardObj('/home/yuanjiajia/Desktop/blender/test/3D.obj');
$ply = new LoardObj('/home/yuanjiajia/Desktop/blender/test/6D.obj');
$ply = $ply->toGltf();
var_dump( $ply );
// $UUII = base64_decode("AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAA=");
// $finfo = new \finfo(FILEINFO_MIME);
// echo $finfo->buffer($UUII) . "\n";
?>

119
src/core/LoardObj.php

@ -277,6 +277,12 @@ class LoardObj
$animation = null;
$meshCount = 0;
$bufferCount = 0;
$maxX = -10000000;
$maxY = -10000000;
$maxZ = -10000000;
$minX = 10000000;
$minY = 10000000;
$minZ = 10000000;
foreach( $this->objData['meshs'] as $meshs_key => $meshs_value )
{
$lineFace = null;
@ -293,29 +299,42 @@ class LoardObj
switch( ($v_key+1) % 3 )
{
case 1:
foreach($this->objData['v'][$v_value-1] as $xyz_value)
foreach($this->objData['v'][$v_value-1] as $xyz_key => $xyz_value)
{
// $v .= pack('f',$xyz_value);
$v .= (float)$xyz_value;
$xyz_value = (float)$xyz_value;
switch( ($xyz_key+1) % 3 )
{
case 1:
$maxX = $xyz_value > $maxX ? $xyz_value : $maxX;
$minX = $xyz_value < $minX ? $xyz_value : $minX;
break;
case 2:
$maxY = $xyz_value > $maxY ? $xyz_value : $maxY;
$minY = $xyz_value < $minY ? $xyz_value : $minY;
break;
case 0:
$maxZ = $xyz_value > $maxZ ? $xyz_value : $maxZ;
$minZ = $xyz_value < $minZ ? $xyz_value : $minZ;
break;
default:
;
}
$v .= pack('g',(float)$xyz_value);
}
// $lineFace .= pack('L',$v_value);
$lineFace .= (int)$v_value;
$lineFace .= pack('V',((int)$v_value) - 1);
break;
case 2:
if( $v_value == null )
{
// $vt .= pack('f',0.0000);
// $vt .= pack('f',0.0000);
$vt .= (float)0.0000;
$vt .= (float)0.0000;
$vt .= pack('g',(float)0.0000);
$vt .= pack('g',(float)0.0000);
}
else
{
foreach(array_slice($this->objData['vt'][$v_value-1],0,2) as $t_value)
{
// $vt .= pack('f',$t_value);
$vt .= (float)$t_value;
$vt .= pack('g',(float)$t_value);
}
}
break;
@ -323,8 +342,7 @@ class LoardObj
case 0;
foreach($this->objData['vn'][$v_value-1] as $n_value)
{
// $vn .= pack('f',$n_value);
$vn .= (float)$n_value;
$vn .= pack('g',(float)$n_value);
}
break;
@ -339,7 +357,7 @@ class LoardObj
$buffer = "\t\t{\n";
$buffer .= "\t\t\t\"byteLength\" : ".strlen($vtnf).",\n";
$vtnf = base64_encode($vtnf);
$buffer .= "\t\t\t\"uri\" : \"data:application/octet-stream;base64,".$vtnf."\"\n";
$buffer .= "\t\t\t\"uri\" : \"data:application/gltf-buffer;base64,".$vtnf."\"\n";
$buffer .= "\t\t}\n";
$this->gltfData['buffers'][$meshCount] = $buffer;
@ -351,8 +369,9 @@ class LoardObj
$bufferView = "\t\t{\n";
$bufferView .= "\t\t\t\"buffer\" : ".$meshCount.",\n";
$bufferView .= "\t\t\t\"byteLength\" : ".strlen($v).",\n";
$bufferView .= "\t\t\t\"byteOffset\" : ".$byteOffset."\n";
$bufferView .= "\t\t}\n";
$bufferView .= "\t\t\t\"byteOffset\" : ".$byteOffset.",\n";
$bufferView .= "\t\t\t\"target\" : 34962\n";
$bufferView .= "\t\t}";
$this->gltfData['bufferViews'][$bufferCount] = $bufferView;
$accessor = null;
@ -360,10 +379,20 @@ class LoardObj
$accessor .= "\t\t\t\"bufferView\" : ".($bufferCount).",\n";
$accessor .= "\t\t\t\"componentType\" : 5126,\n";
$accessor .= "\t\t\t\"count\" : ".(strlen($v) / 4 / 3).",\n";
// $accessor .= "\t\t\t\"max\" : []";
// $accessor .= "\t\t\t\"min\" : []";
$accessor .= "\t\t\t\"max\" :\n";
$accessor .= "\t\t\t[\n";
$accessor .= "\t\t\t\t".$maxX.",\n";
$accessor .= "\t\t\t\t".$maxY.",\n";
$accessor .= "\t\t\t\t".$maxZ."\n";
$accessor .= "\t\t\t],\n";
$accessor .= "\t\t\t\"min\" :\n";
$accessor .= "\t\t\t[\n";
$accessor .= "\t\t\t\t".$minX.",\n";
$accessor .= "\t\t\t\t".$minY.",\n";
$accessor .= "\t\t\t\t".$minZ."\n";
$accessor .= "\t\t\t],\n";
$accessor .= "\t\t\t\"type\" : \"VEC3\"\n";
$accessor .= "\t\t}\n";
$accessor .= "\t\t}";
$this->gltfData['accessors'][$bufferCount] = $accessor;
$bufferCount++;
@ -375,8 +404,9 @@ class LoardObj
$bufferView = "\t\t{\n";
$bufferView .= "\t\t\t\"buffer\" : ".$meshCount.",\n";
$bufferView .= "\t\t\t\"byteLength\" : ".strlen($vn).",\n";
$bufferView .= "\t\t\t\"byteOffset\" : ".$byteOffset."\n";
$bufferView .= "\t\t}\n";
$bufferView .= "\t\t\t\"byteOffset\" : ".$byteOffset.",\n";
$bufferView .= "\t\t\t\"target\" : 34962\n";
$bufferView .= "\t\t}";
$this->gltfData['bufferViews'][$bufferCount] = $bufferView;
$accessor = null;
@ -385,7 +415,7 @@ class LoardObj
$accessor .= "\t\t\t\"componentType\" : 5126,\n";
$accessor .= "\t\t\t\"count\" : ".(strlen($vn) / 4 / 3).",\n";
$accessor .= "\t\t\t\"type\" : \"VEC3\"\n";
$accessor .= "\t\t}\n";
$accessor .= "\t\t}";
$this->gltfData['accessors'][$bufferCount] = $accessor;
$bufferCount++;
@ -398,8 +428,9 @@ class LoardObj
$bufferView = "\t\t{\n";
$bufferView .= "\t\t\t\"buffer\" : ".$meshCount.",\n";
$bufferView .= "\t\t\t\"byteLength\" : ".strlen($vt).",\n";
$bufferView .= "\t\t\t\"byteOffset\" : ".$byteOffset."\n";
$bufferView .= "\t\t}\n";
$bufferView .= "\t\t\t\"byteOffset\" : ".$byteOffset.",\n";
$bufferView .= "\t\t\t\"target\" : 34962\n";
$bufferView .= "\t\t}";
$this->gltfData['bufferViews'][$bufferCount] = $bufferView;
$accessor = null;
@ -408,7 +439,7 @@ class LoardObj
$accessor .= "\t\t\t\"componentType\" : 5126,\n";
$accessor .= "\t\t\t\"count\" : ".(strlen($vt) / 4 / 2).",\n";
$accessor .= "\t\t\t\"type\" : \"VEC2\"\n";
$accessor .= "\t\t}\n";
$accessor .= "\t\t}";
$this->gltfData['accessors'][$bufferCount] = $accessor;
$bufferCount++;
@ -420,17 +451,18 @@ class LoardObj
$bufferView = "\t\t{\n";
$bufferView .= "\t\t\t\"buffer\" : ".$meshCount.",\n";
$bufferView .= "\t\t\t\"byteLength\" : ".strlen($lineFace).",\n";
$bufferView .= "\t\t\t\"byteOffset\" : ".$byteOffset."\n";
$bufferView .= "\t\t}\n";
$bufferView .= "\t\t\t\"byteOffset\" : ".$byteOffset.",\n";
$bufferView .= "\t\t\t\"target\" : 34963\n";
$bufferView .= "\t\t}";
$this->gltfData['bufferViews'][$bufferCount] = $bufferView;
$accessor = null;
$accessor = "\t\t{\n";
$accessor .= "\t\t\t\"bufferView\" : ".($bufferCount).",\n";
$accessor .= "\t\t\t\"componentType\" : 5125,\n";
$accessor .= "\t\t\t\"count\" : ".(strlen($vt) / 4).",\n";
$accessor .= "\t\t\t\"count\" : ".(strlen($lineFace) / 4).",\n";
$accessor .= "\t\t\t\"type\" : \"SCALAR\"\n";
$accessor .= "\t\t}\n";
$accessor .= "\t\t}";
$this->gltfData['accessors'][$bufferCount] = $accessor;
@ -444,14 +476,14 @@ class LoardObj
$mesh .= "\t\t\t\t{\n";
$mesh .= "\t\t\t\t\t\"attributes\" :\n";
$mesh .= "\t\t\t\t\t{\n";
$mesh .= "\t\t\t\t\t\t\"POSITION\" :".($bufferCount -3).",\n";
$mesh .= "\t\t\t\t\t\t\"NORMAL\" :".($bufferCount -2).",\n";
$mesh .= "\t\t\t\t\t\t\"TEXCOORD_0\" :".($bufferCount -1).",\n";
$mesh .= "\t\t\t\t\t}\n";
$mesh .= "\t\t\t\t\t\"indices\" :".($bufferCount).",\n";
$mesh .= "\t\t\t\t\t\t\"POSITION\" : ".($bufferCount -3).",\n";
$mesh .= "\t\t\t\t\t\t\"NORMAL\" : ".($bufferCount -2).",\n";
$mesh .= "\t\t\t\t\t\t\"TEXCOORD_0\" : ".($bufferCount -1)."\n";
$mesh .= "\t\t\t\t\t},\n";
$mesh .= "\t\t\t\t\t\"indices\" : ".($bufferCount)."\n";
$mesh .= "\t\t\t\t}\n";
$mesh .= "\t\t\t]\n";
$mesh .= "\t\t}\n";
$mesh .= "\t\t}";
$this->gltfData['meshes'][$meshCount] = $mesh;
@ -461,7 +493,7 @@ class LoardObj
$node = "\t\t{\n";
$node .= "\t\t\t\"mesh\" : ".$meshCount.",\n";
$node .= "\t\t\t\"name\" : \"".$meshs_key."\"\n";
$node .= "\t\t}\n";
$node .= "\t\t}";
$this->gltfData['nodes'][$meshCount] = $node;
$bufferCount++;
$meshCount++;
@ -484,11 +516,11 @@ class LoardObj
}
}
$scene .= "\t\t\t]\n";
$scene .= "\t\t}\n";
$scene .= "\t\t}";
$this->gltfData['scenes'][] = $scene;
$this->gltfData['asset'] = '{ "version" : "2.0" }';
$this->gltfData['scene'] = 0;
$this->gltfData['asset'] = ' { "version" : "2.0" }';
$this->gltfData['scene'] = ' 0';
}
@ -561,6 +593,9 @@ class LoardObj
return $plyFile;
}
/**
* $gltfData 数组中的gltf模型 写入硬盘
*/
private function writeGltf( string $path, string $format )
{
if( !is_dir($path) )
@ -586,11 +621,13 @@ class LoardObj
$data = null;
if( is_array($value) )
{
$num = count($value);
$data = "\t\"".$key."\" :\n";
$data .= "\t[\n";
foreach( $value as $key => $value )
foreach( $value as $c_key => $c_value )
{
$data .= $value;
if( ($c_key+1) == $num ) $data .= $c_value."\n";
else $data .= $c_value.",\n";
}
if( $key == 'buffers' ) $data .= "\t]\n";
else $data .= "\t],\n";

Loading…
Cancel
Save