|
|
<?php
namespace Blobt\Luxcore\scene;
use Blobt\Luxcore\utils\MatHelper; use GuzzleHttp\Client;
include dirname(dirname(__FILE__)) . "/vendor/autoload.php";
const OPEN = true | 1; const CLOSE = false | 0; /** * 标准材质长宽 */ const MAT_STANDARD_SIZE = 1000;
$client = new Client(); $response = $client->get("deep3d.backend-api.dev.com/test/render-task"); $resContents = $response->getBody()->getContents(); $result = json_decode($resContents,false); $taskData = $result->data; $taskScene = $taskData->scene; $taskModel = $taskScene->model;
/** * 创建个 Luxcore 材质 */ function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $scene){ switch($matOriginal->renderType){ case 1: return MatHelper::createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $scene); case 2: return; case 3: return; case 4: return; } }
// 创建一个场景;
$scene = new Scene();
// 一、创建光场:
// 1、创建 面片光001
$material = new materials\Matte(); $material->setBaseColor('0 0 0'); $material->setTransparencyShadow('1 1 1'); $material->setEmission('1 1 1'); $scene->registerMaterial($material); $obj = new objects\Objects(); $obj->ply = './ply/FaceLight001.ply'; $obj->setMaterial($material); $scene->registerObjects($obj);
// 2、创建 面片光002
$material = new materials\Matte(); $material->setBaseColor('0 0 0'); $material->setTransparencyShadow('1 1 1'); $material->setEmission('0 0 0'); $material->emissionCfg = new lights\Emission([ 'gain' => "30 30 30", ]); $scene->registerMaterial($material); $obj = new objects\Objects(); $obj->ply = './ply/FaceLight002.ply'; $obj->setMaterial($material); $scene->registerObjects($obj);
// 3、创建 面片光003
$material = new materials\Matte(); $material->setBaseColor('0 0 0'); $material->setTransparencyShadow('1 1 1'); $material->setEmission('1 1 1'); $material->emissionCfg = new lights\Emission([ 'gain' => "50 50 50", ]); $scene->registerMaterial($material); $obj = new objects\Objects(); $obj->ply = './ply/FaceLight003.ply'; $obj->setMaterial($material); $scene->registerObjects($obj);
// 4、创建 面片光004
$material = new materials\Matte(); $material->setBaseColor('0 0 0'); $material->setTransparencyShadow('1 1 1'); $material->setEmission('0 0 0'); $material->emissionCfg = new lights\Emission([ 'gain' => "4 4 4", ]); $scene->registerMaterial($material); $obj = new objects\Objects(); $obj->ply = './ply/FaceLight004.ply'; $obj->setMaterial($material); $scene->registerObjects($obj);
// 5、创建 面片光005
$material = new materials\Matte(); $material->setBaseColor('0 0 0'); $material->setTransparencyShadow('1 1 1'); $material->setEmission('1 1 1'); $material->emissionCfg = new lights\Emission([ 'gain' => "10 10 10", ]); $scene->registerMaterial($material); $obj = new objects\Objects(); $obj->ply = './ply/FaceLight005.ply'; $obj->setMaterial($material); $scene->registerObjects($obj);
// 6、创建 面片光006
$material = new materials\Matte(); $material->setBaseColor('0 0 0'); $material->setTransparencyShadow('1 1 1'); $material->setEmission('1 1 1'); $material->emissionCfg = new lights\Emission([ 'gain' => "8 8 8", 'mapfile' => "./map/SD-037.exr", 'gamma' => 1, 'storage' => "float" ]); $scene->registerMaterial($material); $obj = new objects\Objects(); $obj->ply = './ply/FaceLight006.ply'; $obj->setMaterial($material); $scene->registerObjects($obj);
// 7、创建 面片光000
$material = new materials\Matte(); $material->setBaseColor('0 0 0'); $material->setTransparencyShadow('1 1 1'); $material->setEmission('1 1 1'); $material->emissionCfg = new lights\Emission([ 'gain' => "3.3 3.3 3.3", ]); $scene->registerMaterial($material); $obj = new objects\Objects(); $obj->ply = './ply/FaceLight000.ply'; $obj->setMaterial($material); $scene->registerObjects($obj);
// 8、创建环境光
$light = new lights\env\HdrImage([ 'gain' => "1.25 1.25 1.25", 'transformation' => "0.1736481 -0.9848078 0 0 -0.9848078 -0.1736481 0 0 0 0 1 0 0 0 0 1", 'file' => "./map/env.png", 'gamma' => 1, 'storage' => "byte" ]); $light->id = 0; $scene->registerLight($light);
// 9、创建地面模型
$mapping = new texture\mapping\Mapping(); $mapping->useUVMapping2d(0, "0", "0.4 -0.966", "0.3 0.983"); $texture = new texture\procedural\ImageMap([ 'file' => "./map/方形阴影遮照.png", 'gain' => 0.6, 'gamma' => 1, ]); $texture->mapping = $mapping; $scene->registerTexture($texture); $material = new materials\Disney([ 'shadowcatcherEnable' => OPEN, 'photongiEnable' => CLOSE ]); $material->setBaseColor('1 1 1'); $material->setTransparencyFront($texture); $material->setTransparencyBack($texture); $scene->registerMaterial($material); $obj = new objects\Objects(); $obj->ply = './ply/ground.ply'; $obj->setMaterial($material); $scene->registerObjects($obj);
// 二、添加要渲染的模型
foreach($taskModel->childsParams as $childParams){ $material = null; $matOriginal = (function ($childParams){ if(is_object($childParams->customMat)){ return $childParams->customMat; }else if(is_object($childParams->fixedMat)){ return $childParams->fixedMat; }else if(is_object($childParams->defaultMat)){ return $childParams->defaultMat; } })($childParams); $textureScaleU = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->width); $textureScaleV = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->height); if(is_object($matOriginal)){ $material = createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $scene); }else{ $material = new materials\Disney(); $material->setBaseColor('1 1 1'); } $scene->registerMaterial($material);
$obj = new objects\Objects(); $obj->ply = $childParams->childPlyFile; $obj->setMaterial($material); $scene->registerObjects($obj); }
// 三、创建相机
$camera = new camera\Perspective( // 创建一个相机
[ 'autovolumeEnable' => 0, // 关闭自动体积效果
'lookatOrig' => "-3.197486 -4.560584 0.8914337", // 视点坐标位置
'lookatTarget' => "-2.632151 -3.747173 0.7544713", // 目标点坐标位置
'up' => "0.0781663 0.1124666 0.9905763", // 旋转参数
'screenwindow' => "-1 1 -1 1", // 视野比例
'autofocusEnable' => 0, // 关闭自动对焦
'fieldofview' => 3.40608, // 视野角度大小
'cliphither' => 0.1 // 剪切成像起始位置,单位跟随系统
] ); $camera->bokeh->blades = 0; // 模拟镜头模糊效果的光圈数
$scene->registerCamera($camera); // 将相机注册到 scene 中
// 四、设置渲染参数
$render = ''; $renderEngine = new render\RenderEngine(); // 设置 渲染引擎 的配置参数,默认使用了 PATHOCL,意义着将使用 GPU 设备渲染
$render .= $renderEngine;
$openCL = new render\OpenCL(); // 设置 GPU 渲染参数,这里使用一个默认的参数
$render .= $openCL; $path = new render\Path(); // 设置 光线跟踪 参数,这里使用一个默认的参数
$render .= $path;
$sampler = new render\Sampler(); // 设置 采样器 配置参数,默认使用 Sobol 类型的自适应采样器
$render .= $sampler;
$lightStrategy = new render\LightStrategy(); // 设置 灯光策略 类型,默认使用 LOG_POWER 类型
$render .= $lightStrategy;
$filesaver = new render\FileSaver(); // 设置 文件储存格式 配置参数
$render .= $filesaver;
$batch = new render\Batch(); // 设置 渲染终止 参数,
$batch->haltspp = 1000; // 设置 每像素 采样达 800 则终止渲染
// $batch->halttime = 2000; // 设置 渲染 超时参数,
$render .= $batch;
$sceneCfg = new render\Scene(); // 设置 场景属性 参数
$render .= $sceneCfg;
$film = new render\Film(); // 添加一个渲染胶片
$film->width = 3000; // 设置渲染胶片宽度,单位像素
$film->heigth = 3000; // 设置渲染胶片宽度,单位像素
$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' => render\Image::TYPE_OBJECT_ID])); // 为渲染胶片添加一个 通道图类型 的图像输出
$film->addImage(new render\Image(['type' => render\Image::TYPE_RGBA])); // 添加一个 透明底 的图像输出
$img = new render\Image(); // 添加一个图像输出,使用默认参数
$img->effect[] = new render\effect\NoiseReducerOIDN(); // 添加降噪效果管线,使用默认参数
$img->effect[] = new render\effect\Pretreatment(); //
$img->effect[] = new render\effect\ToneMapLinear(); // 添加一亮度效果管线,使用默认参数
$img->effect[] = new render\effect\Synthesis(); // 添加一个合成效果管线,使用默认参数
$img->effect[] = new render\effect\BackgroundImg( // 添加一个背景图像,使用默认参数
[ 'file' => "./map/纯白242.png" ] ); $img->effect[] = new render\effect\CammaCorrection(); // 添加一个 gamma 校正,,使用默认参数
$film->addImage($img); // 将该图像输出添加到渲染胶片
$render .= $film;
// 五、输出场景文件 scene.scn,及渲染文件 render.cfg
echo $scene; $handle = fopen( "./DemoScene/scene.scn",'w+'); fwrite($handle,$scene); fclose($handle);
echo $render ; $handle = fopen( "./DemoScene/render.cfg",'w+'); fwrite($handle,$render); fclose($handle);
// // 六、启动渲染器
// $cmd = "luxcoreconsole -o ./DemoScene/render.cfg -f ./DemoScene/scene.scn 1>./DemoScene/render.log 2>&1";
// $output;
// $returnVar;
// exec($cmd,$output,$returnVar);
// if( $returnVar == 127 ){
// throw new \Exception("Please install the luxcorerender bin file in the system directory correctly,Or add to the system environment variable");
// }
?>
|