Browse Source

已经增加obj转gltf功能,尚未运行成功

master
yuanjiajia 3 years ago
parent
commit
3acd49956a
  1. 6
      examples/loadObjDemo.php
  2. 115
      examples/printRender.php
  3. 307
      src/core/LoardObj.php
  4. 58
      src/scene/Gltf.php

6
examples/loadObjDemo.php

@ -9,9 +9,9 @@ include dirname(dirname(__FILE__)) . "/vendor/autoload.php";
ini_set('memory_limit','4096M'); ini_set('memory_limit','4096M');
$ply = new LoardObj('/home/yuanjiajia/Desktop/blender/test/6D.obj');
$ply = $ply->toPly(null,LoardObj::TYPE_TXT);
$ply = new LoardObj('/home/yuanjiajia/Desktop/blender/test/3D.obj');
$ply = $ply->toGltf();
var_dump($ply );
var_dump( $ply );
?> ?>

115
examples/printRender.php

@ -7,58 +7,101 @@ namespace Blobt\Luxcore\scene;
include dirname(dirname(__FILE__)) . "/vendor/autoload.php"; include dirname(dirname(__FILE__)) . "/vendor/autoload.php";
//设置打印 渲染引擎 的配置参数
$renderEngine = new render\RenderEngine();
echo $renderEngine;
// //设置打印 渲染引擎 的配置参数
// $renderEngine = new render\RenderEngine();
// echo $renderEngine;
//设置打印GPU渲染设备的配置参数
$openCL = new render\OpenCL();
echo $openCL;
// //设置打印GPU渲染设备的配置参数
// $openCL = new render\OpenCL();
// echo $openCL;
//设置打印光线跟踪的配置参数
$path = new render\Path();
echo $path;
// //设置打印光线跟踪的配置参数
// $path = new render\Path();
// echo $path;
//设置打印 采样器 配置参数
$sampler = new render\Sampler();
echo $sampler;
// //设置打印 采样器 配置参数
// $sampler = new render\Sampler();
// echo $sampler;
//设置打印 灯光策略 配置参数
$lightStrategy = new render\LightStrategy();
echo $lightStrategy;
// //设置打印 灯光策略 配置参数
// $lightStrategy = new render\LightStrategy();
// echo $lightStrategy;
//设置打印 文件储存格式 配置参数
$filesaver = new render\FileSaver();
echo $filesaver;
// //设置打印 文件储存格式 配置参数
// $filesaver = new render\FileSaver();
// echo $filesaver;
//设置打印 渲染终止 配置参数
$batch = new render\Batch();
echo $batch;
// //设置打印 渲染终止 配置参数
// $batch = new render\Batch();
// echo $batch;
//设置打印 场景属性 配置参数
$scene = new render\Scene();
echo $scene;
// //设置打印 场景属性 配置参数
// $scene = new render\Scene();
// echo $scene;
//设置打印 “胶片” 配置参数
$film = new render\Film();//添加一个胶片,
// //设置打印 “胶片” 配置参数
// $film = new render\Film();//添加一个胶片,
$img = new render\Image();
$img->effect = [new render\effect\Pretreatment(),new render\effect\ToneMapLinear(),new render\effect\CammaCorrection()];
$film->addImage($img);
// $img = new render\Image();
// $img->effect = [new render\effect\Pretreatment(),new render\effect\ToneMapLinear(),new render\effect\CammaCorrection()];
// $film->addImage($img);
$film->addImage(new render\Image(['type' => 'ALBEDO']));
$film->addImage(new render\Image(['type' => 'AVG_SHADING_NORMAL']));
// $film->addImage(new render\Image(['type' => 'ALBEDO']));
// $film->addImage(new render\Image(['type' => 'AVG_SHADING_NORMAL']));
$film->addImage(new render\Image(['type' => 'OBJECT_ID']));
// $film->addImage(new render\Image(['type' => 'OBJECT_ID']));
$img = new render\Image();
$img->effect = [new render\effect\NoiseReducerOIDN(),new render\effect\Pretreatment(),new render\effect\ToneMapLinear(),new render\effect\CammaCorrection()];
$film->addImage($img);
// $img = new render\Image();
// $img->effect = [new render\effect\NoiseReducerOIDN(),new render\effect\Pretreatment(),new render\effect\ToneMapLinear(),new render\effect\CammaCorrection()];
// $film->addImage($img);
echo $film;
// echo $film;
// class vector implements \JsonSerializable
// {
// public function __construct( $arr )
// {
// $this->array = $arr;
// }
// public function jsonSerialize()
// {
// return $this->array;
// }
// }
// $test = [
// "x" => "geeks",
// "y" => "for",
// "z" => "geeks"
// ];
// $test = new Gltf();
// echo json_encode($test, JSON_PRETTY_PRINT);
$rr = 'AACAPwAAgD8AAIA/AACAPwAAgD8AAIC/AACAvwAAgD8AAIC/AACAvwAAgD8AAIA/AAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAgPwAAgD4AACA/AAAAPwAAYD8AAAA/AABgPwAAgD4AAAEAAgAAAAIAAwA=';
// echo base64_decode($rr);
// $fileType = base64_decode($rr);
$fileType = "\x04\x00\xa0\x00";
echo $fileType;
echo "\n\n";
$rr = unpack("cchars/nint",$fileType);
var_dump($rr);
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?> ?>

307
src/core/LoardObj.php

@ -25,6 +25,26 @@ class LoardObj
*/ */
public $plyData = []; public $plyData = [];
/**
* @var array 储存gltf模型数据
*/
public $gltfData = [
'asset' => '{ "version" : "2.0" }',
'scene' => 0,
'scenes' => [],
'nodes' => [],
'cameras' => [],
'skins' => [],
'meshes' => [],
'accessors' => [],
'bufferViews' => [],
'buffers' => [],
'material' => [],
'textures' => [],
'images' => [],
'samplers' => []
];
@ -57,9 +77,9 @@ class LoardObj
*/ */
public function toGltf( string $path = null,string $format = self::TYPE_BINARY) public function toGltf( string $path = null,string $format = self::TYPE_BINARY)
{ {
// $this->createGltf();
// if( $path == null) $path = getcwd()."/gltf";
// return $this->writeGltf( $path, $format );
$this->createGltf();
if( $path == null) $path = getcwd()."/gltf";
return $this->writeGltf( $path, $format );
} }
@ -237,6 +257,240 @@ class LoardObj
} }
/**
* 利用已经拆分的 obj 模型数据,生成多个 gltf 模型,并将这些模型储存在 gltfData 数组中
*/
private function createGltf()
{
$scene = null;
$node = null;
$mesh = null;
$camera = null;
$skin = null;
$accessor = null;
$bufferView = null;
$buffer = null;
$material = null;
$texture = null;
$sampler = null;
$image = null;
$animation = null;
$meshCount = 0;
$bufferCount = 0;
foreach( $this->objData['meshs'] as $meshs_key => $meshs_value )
{
$lineFace = null;
$v = null;
$vt = null;
$vn = null;
$byteOffset = 0;
foreach( $meshs_value as $face_value ) // 生成顶点和面数据
{
$face_value = implode('/', $face_value);
$face_value = explode('/', $face_value);
foreach( $face_value as $v_key => $v_value )
{
switch( ($v_key+1) % 3 )
{
case 1:
foreach($this->objData['v'][$v_value-1] as $xyz_value)
{
// $v .= pack('f',$xyz_value);
$v .= (float)$xyz_value;
}
// $lineFace .= pack('L',$v_value);
$lineFace .= (int)$v_value;
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;
}
else
{
foreach(array_slice($this->objData['vt'][$v_value-1],0,2) as $t_value)
{
// $vt .= pack('f',$t_value);
$vt .= (float)$t_value;
}
}
break;
case 0;
foreach($this->objData['vn'][$v_value-1] as $n_value)
{
// $vn .= pack('f',$n_value);
$vn .= (float)$n_value;
}
break;
default:
;
}
}
}
$vtnf = $v.$vn.$vt.$lineFace;
$buffer = null;
$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}\n";
$this->gltfData['buffers'][$meshCount] = $buffer;
$bufferView = null;
$byteOffset = 0;
$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";
$this->gltfData['bufferViews'][$bufferCount] = $bufferView;
$accessor = null;
$accessor = "\t\t{\n";
$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\"type\" : \"VEC3\"\n";
$accessor .= "\t\t}\n";
$this->gltfData['accessors'][$bufferCount] = $accessor;
$bufferCount++;
$bufferView = null;
$byteOffset = $byteOffset + strlen($v);
$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";
$this->gltfData['bufferViews'][$bufferCount] = $bufferView;
$accessor = null;
$accessor = "\t\t{\n";
$accessor .= "\t\t\t\"bufferView\" : ".($bufferCount).",\n";
$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";
$this->gltfData['accessors'][$bufferCount] = $accessor;
$bufferCount++;
$bufferView = null;
$byteOffset = $byteOffset + strlen($vn);
$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";
$this->gltfData['bufferViews'][$bufferCount] = $bufferView;
$accessor = null;
$accessor = "\t\t{\n";
$accessor .= "\t\t\t\"bufferView\" : ".($bufferCount).",\n";
$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";
$this->gltfData['accessors'][$bufferCount] = $accessor;
$bufferCount++;
$bufferView = null;
$byteOffset = $byteOffset + strlen($vt);
$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";
$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\"type\" : \"SCALAR\"\n";
$accessor .= "\t\t}\n";
$this->gltfData['accessors'][$bufferCount] = $accessor;
$mesh = null;
$mesh = "\t\t{\n";
$mesh .= "\t\t\t\"name\" : \"".$meshs_key."\",\n";
$mesh .= "\t\t\t\"primitives\" :\n";
$mesh .= "\t\t\t[\n";
$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}\n";
$mesh .= "\t\t\t]\n";
$mesh .= "\t\t}\n";
$this->gltfData['meshes'][$meshCount] = $mesh;
$node = null;
$node = "\t\t{\n";
$node .= "\t\t\t\"mesh\" : ".$meshCount.",\n";
$node .= "\t\t\t\"name\" : \"".$meshs_key."\"\n";
$node .= "\t\t}\n";
$this->gltfData['nodes'][$meshCount] = $node;
$bufferCount++;
$meshCount++;
}
$scene = null;
$scene = "\t\t{\n";
$scene .= "\t\t\t\"name\" : \"".'Scene'."\",\n";
$scene .= "\t\t\t\"nodes\" :\n";
$scene .= "\t\t\t[\n";
foreach ($this->gltfData['nodes'] as $key => $value)
{
if( ($key+1) == count($this->gltfData['nodes']) )
{
$scene .= "\t\t\t\t".$key."\n";
}
else
{
$scene .= "\t\t\t\t".$key.",\n";
}
}
$scene .= "\t\t\t]\n";
$scene .= "\t\t}\n";
$this->gltfData['scenes'][] = $scene;
$this->gltfData['asset'] = '{ "version" : "2.0" }';
$this->gltfData['scene'] = 0;
}
/** /**
* $plyData 数组中的ply模型 写入硬盘 * $plyData 数组中的ply模型 写入硬盘
@ -307,4 +561,51 @@ class LoardObj
return $plyFile; return $plyFile;
} }
private function writeGltf( string $path, string $format )
{
if( !is_dir($path) )
{
if( !mkdir( $path,0777,true) )
{
throw new \Exception("!!! Failed to create the specified directory:".$path);
}
}
$gltfFile = preg_split("/[.]+/", basename($this->objFile));
$gltfFile = array_reverse($gltfFile);
array_shift($gltfFile);
$gltfFile = implode('.', array_reverse($gltfFile));
$gltfFile = $path.'/'.$gltfFile.".gltf";
$handle = fopen( $gltfFile,'w+');
fwrite($handle,"{\n");
foreach ( $this->gltfData as $key => $value)
{
if( !($value == []) )
{
$data = null;
if( is_array($value) )
{
$data = "\t\"".$key."\" :\n";
$data .= "\t[\n";
foreach( $value as $key => $value )
{
$data .= $value;
}
if( $key == 'buffers' ) $data .= "\t]\n";
else $data .= "\t],\n";
fwrite($handle,$data);
}
else
{
$data = "\t\"".$key."\" :";
$data .= $value.",\n";
fwrite($handle,$data);
}
}
}
fwrite($handle,"}\n");
fclose($handle);
$handle = null;
}
} }

58
src/scene/Gltf.php

@ -0,0 +1,58 @@
<?php
namespace Blobt\Luxcore\scene;
use Blobt\Luxcore\utils\StringHelper;
class Gltf
{
/**
* @var object 用这个对象,标识这是一个gltf文件
*/
private $asset;
/**
* @var integer 用一个整数标识加载一个默认的场景
*/
private $scene = 0;
/**
* @var array 1、场景数组,解析一个gltf文件将从这里开始;
* 2、每一个元素值是一个对象类型,每一个对象描述了对 nodes数组 的索引,同时也是每一个场景的根节点;
*/
private $scenes = [];
/**
* @var array 1、节点数组,每一个元素值是一个对象类型,每一个对象描述一个 mesh(模型)、camera()或skin(蒙皮)
* 数组的索引;
*/
private $nodes = [];
/**
* @var array 1、模型数组,每一个元素值是一个对象类型,每一个对象是对一个模型的描述;
*/
private $meshes = [];
/**
* @var array 1、材质数组,每一个元素值是一个对象类型,每一个对象是对一个材质的描述;
*/
private $material = [];
/**
* @var array 1、纹理数组,每一个元素值是一个对象类型,每一个对象是对一个纹理的描述;
*/
private $textures = [];
/**
* @var array 1、图像数组,每一个元素值是一个对象类型,每一个对象是对一个图像的描述;
*/
private $images = [];
/**
* @var array 1、图像采样数组,每一个元素值是一个对象类型,每一个对象是对一个图像的描述;
*/
private $samplers = [];
}
?>
Loading…
Cancel
Save