Browse Source

渲染测式 开发

master
yuanjiajia 1 year ago
parent
commit
01bbe47b52
  1. 32
      examples/renderTest.php
  2. 18
      src/utils/MatHelper.php

32
examples/renderTest.php

@ -60,10 +60,10 @@ $taskCamera = $taskScene->camera;
/**
* 创建 Luxcore 材质
*/
function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $downloadDir, $scene){
function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene){
switch($matOriginal->renderType){
case 1:
return MatHelper::createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $downloadDir, $scene);
return MatHelper::createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
case 2:
return;
case 3:
@ -217,7 +217,7 @@ $scene->registerObjects($obj);
// 二、添加要渲染的模型
foreach($taskModel->childsParams as $childParams){
$downloadDir = $sceneTemplatePath.'/cacheFiles/';
$cacheFileDir = $sceneTemplatePath.'/cacheFiles';
$material = null;
$matOriginal = (function ($childParams){
if(is_object($childParams->customMat)){
@ -232,14 +232,14 @@ foreach($taskModel->childsParams as $childParams){
if(is_object($matOriginal)){
$textureScaleU = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->width);
$textureScaleV = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->height);
$material = createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $downloadDir, $scene);
$material = createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
}else{
$material = new Disney();
$material->setBaseColor('1 1 1');
}
$scene->registerMaterial($material);
$plyFileSavaPath = $downloadDir.basename($childParams->childPlyFile);
$plyFileSavaPath = $cacheFileDir.basename($childParams->childPlyFile);
$plyFileHandle = fopen($plyFileSavaPath, "w");
(new Client())->get($childParams->childPlyFile, [RequestOptions::SINK => $plyFileHandle]);
$obj = new Objects();
@ -339,7 +339,7 @@ $render .= $sceneCfg;
// 图像输出设置
$imageOutPath = $sceneTemplatePath.'/imageOut';
$imageOutDir = $sceneTemplatePath.'/imageOut';
$film = new Film();
$filter = new Filter();
@ -353,10 +353,10 @@ $film->heigth = 4096;
$img = new Image();
$img->effect = [new Pretreatment(),new ToneMapLinear(),new CammaCorrection()];
$film->addImage($img, $imageOutPath);
$film->addImage($img, $imageOutDir);
$film->addImage(new Image(['type' => Image::TYPE_OBJECT_ID]), $imageOutPath);
$film->addImage(new Image(['type' => Image::TYPE_RGBA]), $imageOutPath);
$film->addImage(new Image(['type' => Image::TYPE_OBJECT_ID]), $imageOutDir);
$film->addImage(new Image(['type' => Image::TYPE_RGBA]), $imageOutDir);
$img = new Image();
// $img->effect[] = new NoiseReducerOIDN();
@ -369,18 +369,22 @@ $img->effect[] = new BackgroundImg(
]
);
$img->effect[] = new CammaCorrection();
$film->addImage($img, $imageOutPath);
$film->addImage($img, $imageOutDir);
$render .= $film;
// 五、输出场景文件 scene.scn,及渲染文件 render.cfg
$cfgPath = $sceneTemplatePath . "/render.cfg";
$scenePath = $sceneTemplatePath . "/scene.scn";
$logPath = $sceneTemplatePath . "/log/render.log";
echo $scene;
$handle = fopen( $sceneTemplatePath . "/scene.scn",'w+');
$handle = fopen( $scenePath,'w+');
fwrite($handle,$scene);
fclose($handle);
echo $render ;
$handle = fopen( $sceneTemplatePath . "/render.cfg",'w+');
$handle = fopen( $cfgPath,'w+');
fwrite($handle,$render);
fclose($handle);
@ -388,10 +392,6 @@ fclose($handle);
// 六、启动渲染器
$cfgPath = $sceneTemplatePath . "/render.cfg";
$scenePath = $sceneTemplatePath . "/scene.scn";
$logPath = $sceneTemplatePath . "/log/render.log";
$cmd = "luxcoreconsole -o " . $cfgPath . " -f " . $scenePath . " 1>" . $logPath . " 2>&1";
echo "\n".$cmd."\n";
$output;

18
src/utils/MatHelper.php

@ -18,7 +18,7 @@ class MatHelper{
/**
* 创建一个 Disney 的材质
*/
static function createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $downloadDir, $scene){
static function createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene){
$material = new Disney();
$material->setBaseColor(self::hex2floatColor($matOriginal->matParams->albedoColor));
@ -57,7 +57,7 @@ class MatHelper{
if($matOriginal->matParams->albedoTexture){
$textureUrl = $matOriginal->matParams->albedoTexture;
$textureSavaPath = $downloadDir.basename($textureUrl);
$textureSavaPath = $cacheFileDir.'/'.basename($textureUrl);
$textureHandle = fopen($textureSavaPath, "w");
(new Client())->get($textureUrl, [RequestOptions::SINK => $textureHandle]);
@ -70,14 +70,14 @@ class MatHelper{
}
if($matOriginal->matParams->metallicTexture){
$metallicTextureUrl = $matOriginal->matParams->metallicTexture;
$metallicTextureSavaPath = $downloadDir.basename($metallicTextureUrl);
$metallicTextureSavaPath = $cacheFileDir.'/'.basename($metallicTextureUrl);
$metallicTextureHandle = fopen($metallicTextureSavaPath, "w");
(new Client())->get($metallicTextureUrl, [RequestOptions::SINK => $metallicTextureHandle]);
$fileName = preg_replace('/(\.[^\.]+)$/','',basename($metallicTextureUrl));
$suffixName = preg_replace('/^('.$fileName.')+/','',basename($metallicTextureUrl));
$extension = preg_replace('/^('.$fileName.')+/','',basename($metallicTextureUrl));
// 从金属纹理中 分离 蓝色通道 作为 金属度纹理
$metalnessTextureSavaPath = $downloadDir.$fileName.'_metalness'.$suffixName;
$metalnessTextureSavaPath = $cacheFileDir.'/'.$fileName.'_metalness'.$extension;
$metalnessTexture = new Imagick($metallicTextureSavaPath);
$metalnessTexture->separateImageChannel(Imagick::CHANNEL_BLUE);
$metalnessTexture->writeImage($metalnessTextureSavaPath);
@ -89,7 +89,7 @@ class MatHelper{
$scene->registerTexture($metalnessTexture);
$material->setMetallic($metalnessTexture);
// 从金属纹理中 分离 绿色通道 作为 粗糙度纹理
$roughnessTextureSavaPath = $downloadDir.$fileName.'_roughness'.$suffixName;
$roughnessTextureSavaPath = $cacheFileDir.'/'.$fileName.'_roughness'.$extension;
$roughnessTexture = new Imagick($metallicTextureSavaPath);
$roughnessTexture->separateImageChannel(Imagick::CHANNEL_GREEN);
$roughnessTexture->writeImage($roughnessTextureSavaPath);
@ -103,15 +103,15 @@ class MatHelper{
}
if($matOriginal->matParams->bumpTexture){
$textureUrl = $matOriginal->matParams->bumpTexture;
$textureSavaPath = $downloadDir.basename($textureUrl);
$textureSavaPath = $cacheFileDir.'/'.basename($textureUrl);
$textureHandle = fopen($textureSavaPath, "w");
(new Client())->get($textureUrl, [RequestOptions::SINK => $textureHandle]);
// 因为 LuxCore 使用 opengl 规范的法线纹理格式,与 babylon 使用的 DirectX 不同,
// 所以这里需要反转 G 通道色值,以保证法线纹理应用效果与 babylon 一致。
$fileName = preg_replace('/(\.[^\.]+)$/','',basename($textureSavaPath));
$suffixName = preg_replace('/^('.$fileName.')+/','',basename($textureSavaPath));
$textureInvertGPath = $downloadDir.$fileName.'_InvertG'.$suffixName;
$extension = preg_replace('/^('.$fileName.')+/','',basename($textureSavaPath));
$textureInvertGPath = $cacheFileDir.'/'.$fileName.'_InvertG'.$extension;
$imagick = new \Imagick($textureSavaPath);
$imageIterator = $imagick->getPixelIterator();
foreach ($imageIterator as $row => $pixels) {

Loading…
Cancel
Save