Browse Source

优化ply模型文件大小

master
yuanjiajia 3 years ago
parent
commit
d54f58a218
  1. 4
      examples/loadObjDemo.php
  2. 32
      src/core/LoardObj.php

4
examples/loadObjDemo.php

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

32
src/core/LoardObj.php

@ -40,7 +40,8 @@ class LoardObj
}
/**
* @param string $format 储存 ply模型 的格式
* @param string $path ply模型 保存路径
* @param string $format ply模型 保存格式
*/
public function toPly( string $path = null,string $format = self::TYPE_BINARY)
{
@ -151,9 +152,11 @@ class LoardObj
foreach( $this->objData['meshs'] as $meshs_key => $meshs_value )
{
$ply = [];
$vCount = 0;
$fCount = 0;
$ply['vertex'] = [];
$ply['face'] = [];
$ply['header'] = [];
$indexesList = [];
$vConst = 0;
foreach( $meshs_value as $face_value ) // 生成顶点和面数据
{
@ -178,23 +181,32 @@ class LoardObj
case 0;
$vn = $this->objData['vn'][$v_value-1];
$ply['vertex'][$vCount] = implode(' ',$v).' '.implode(' ',$vn).' '.implode(' ',$vt);
$lineFace[] = $vCount;
$vCount++;
$vnt = implode(' ',$v).' '.implode(' ',$vn).' '.implode(' ',$vt);
$hashVar = hash('md5', $vnt);
if( !array_key_exists($hashVar, $indexesList) )
{
$ply['vertex'][$vConst] = $vnt;
$lineFace[] = $vConst;
$indexesList[$hashVar] = $vConst;
$vConst++;
}
else
{
$lineFace[] = $indexesList[$hashVar];
}
break;
default:
;
}
}
$ply['face'][$fCount] = count($lineFace).' '.implode(' ',$lineFace);
$fCount++;
$ply['face'][] = count($lineFace).' '.implode(' ',$lineFace);
}
$ply['header'][] = 'ply'; // 生成头部数据
$ply['header'][] = "format ";
$ply['header'][] = "comment Created by reaiyunju, source file: ''";
$ply['header'][] = "element vertex ".$vCount;
$ply['header'][] = "element vertex ".$vConst;
$ply['header'][] = "property float x";
$ply['header'][] = "property float y";
$ply['header'][] = "property float z";

Loading…
Cancel
Save