Blender渲染
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
11 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <?php
  2. namespace Blobt\Luxcore\scene;
  3. use Blobt\Luxcore\scene\materials\Disney;
  4. use Blobt\Luxcore\scene\objects\Objects;
  5. use Blobt\Luxcore\utils\MatHelper;
  6. use GuzzleHttp\Client;
  7. use GuzzleHttp\RequestOptions;
  8. include dirname(dirname(__FILE__)) . "/vendor/autoload.php";
  9. const OPEN = true | 1;
  10. const CLOSE = false | 0;
  11. /**
  12. * 标准材质长宽
  13. */
  14. const MAT_STANDARD_SIZE = 1000;
  15. $client = new Client();
  16. $response = $client->get("deep3d.backend-api.dev.com/test/render-task");
  17. $resContents = $response->getBody()->getContents();
  18. $result = json_decode($resContents,false);
  19. $taskData = $result->data;
  20. $taskScene = $taskData->scene;
  21. $taskModel = $taskScene->model;
  22. /**
  23. * 创建 Luxcore 材质
  24. */
  25. function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $downloadDir, $scene){
  26. switch($matOriginal->renderType){
  27. case 1:
  28. return MatHelper::createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $downloadDir, $scene);
  29. case 2:
  30. return;
  31. case 3:
  32. return;
  33. case 4:
  34. return;
  35. }
  36. }
  37. // 创建一个场景;
  38. $scene = new Scene();
  39. $sceneTemplatePath = dirname(dirname(__FILE__)) . "/sceneTemplate";
  40. // 一、创建光场:
  41. // 1、创建 面片光001
  42. $material = new materials\Matte();
  43. $material->setBaseColor('0 0 0');
  44. $material->setTransparencyShadow('1 1 1');
  45. $material->setEmission('1 1 1');
  46. $scene->registerMaterial($material);
  47. $obj = new Objects();
  48. $obj->ply = './ply/FaceLight001.ply';
  49. $obj->setMaterial($material);
  50. $scene->registerObjects($obj);
  51. // 2、创建 面片光002
  52. $material = new materials\Matte();
  53. $material->setBaseColor('0 0 0');
  54. $material->setTransparencyShadow('1 1 1');
  55. $material->setEmission('0 0 0');
  56. $material->emissionCfg = new lights\Emission([
  57. 'gain' => "30 30 30",
  58. ]);
  59. $scene->registerMaterial($material);
  60. $obj = new Objects();
  61. $obj->ply = './ply/FaceLight002.ply';
  62. $obj->setMaterial($material);
  63. $scene->registerObjects($obj);
  64. // 3、创建 面片光003
  65. $material = new materials\Matte();
  66. $material->setBaseColor('0 0 0');
  67. $material->setTransparencyShadow('1 1 1');
  68. $material->setEmission('1 1 1');
  69. $material->emissionCfg = new lights\Emission([
  70. 'gain' => "50 50 50",
  71. ]);
  72. $scene->registerMaterial($material);
  73. $obj = new Objects();
  74. $obj->ply = './ply/FaceLight003.ply';
  75. $obj->setMaterial($material);
  76. $scene->registerObjects($obj);
  77. // 4、创建 面片光004
  78. $material = new materials\Matte();
  79. $material->setBaseColor('0 0 0');
  80. $material->setTransparencyShadow('1 1 1');
  81. $material->setEmission('0 0 0');
  82. $material->emissionCfg = new lights\Emission([
  83. 'gain' => "4 4 4",
  84. ]);
  85. $scene->registerMaterial($material);
  86. $obj = new Objects();
  87. $obj->ply = './ply/FaceLight004.ply';
  88. $obj->setMaterial($material);
  89. $scene->registerObjects($obj);
  90. // 5、创建 面片光005
  91. $material = new materials\Matte();
  92. $material->setBaseColor('0 0 0');
  93. $material->setTransparencyShadow('1 1 1');
  94. $material->setEmission('1 1 1');
  95. $material->emissionCfg = new lights\Emission([
  96. 'gain' => "10 10 10",
  97. ]);
  98. $scene->registerMaterial($material);
  99. $obj = new Objects();
  100. $obj->ply = './ply/FaceLight005.ply';
  101. $obj->setMaterial($material);
  102. $scene->registerObjects($obj);
  103. // 6、创建 面片光006
  104. $material = new materials\Matte();
  105. $material->setBaseColor('0 0 0');
  106. $material->setTransparencyShadow('1 1 1');
  107. $material->setEmission('1 1 1');
  108. $material->emissionCfg = new lights\Emission([
  109. 'gain' => "8 8 8",
  110. 'mapfile' => "./map/SD-037.exr",
  111. 'gamma' => 1,
  112. 'storage' => "float"
  113. ]);
  114. $scene->registerMaterial($material);
  115. $obj = new Objects();
  116. $obj->ply = './ply/FaceLight006.ply';
  117. $obj->setMaterial($material);
  118. $scene->registerObjects($obj);
  119. // 7、创建 面片光000
  120. $material = new materials\Matte();
  121. $material->setBaseColor('0 0 0');
  122. $material->setTransparencyShadow('1 1 1');
  123. $material->setEmission('1 1 1');
  124. $material->emissionCfg = new lights\Emission([
  125. 'gain' => "3.3 3.3 3.3",
  126. ]);
  127. $scene->registerMaterial($material);
  128. $obj = new Objects();
  129. $obj->ply = './ply/FaceLight000.ply';
  130. $obj->setMaterial($material);
  131. $scene->registerObjects($obj);
  132. // 8、创建环境光
  133. $light = new lights\env\HdrImage([
  134. 'gain' => "1.25 1.25 1.25",
  135. 'transformation' => "0.1736481 -0.9848078 0 0 -0.9848078 -0.1736481 0 0 0 0 1 0 0 0 0 1",
  136. 'file' => "./map/env.png",
  137. 'gamma' => 1,
  138. 'storage' => "byte"
  139. ]);
  140. $light->id = 0;
  141. $scene->registerLight($light);
  142. // 9、创建地面模型
  143. $mapping = new texture\mapping\Mapping();
  144. $mapping->useUVMapping2d(0, "0", "0.4 -0.966", "0.3 0.983");
  145. $texture = new texture\procedural\ImageMap([
  146. 'file' => "./map/方形阴影遮照.png",
  147. 'gain' => 0.6,
  148. 'gamma' => 1,
  149. ]);
  150. $texture->mapping = $mapping;
  151. $scene->registerTexture($texture);
  152. $material = new Disney([
  153. 'shadowcatcherEnable' => OPEN,
  154. 'photongiEnable' => CLOSE
  155. ]);
  156. $material->setBaseColor('1 1 1');
  157. $material->setTransparencyFront($texture);
  158. $material->setTransparencyBack($texture);
  159. $scene->registerMaterial($material);
  160. $obj = new Objects();
  161. $obj->ply = './ply/ground.ply';
  162. $obj->setMaterial($material);
  163. $scene->registerObjects($obj);
  164. // 二、添加要渲染的模型
  165. foreach($taskModel->childsParams as $childParams){
  166. $downloadDir = $sceneTemplatePath.'/cacheFiles/';
  167. $material = null;
  168. $matOriginal = (function ($childParams){
  169. if(is_object($childParams->customMat)){
  170. return $childParams->customMat;
  171. }else if(is_object($childParams->fixedMat)){
  172. return $childParams->fixedMat;
  173. }else if(is_object($childParams->defaultMat)){
  174. return $childParams->defaultMat;
  175. }
  176. })($childParams);
  177. $textureScaleU = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->width);
  178. $textureScaleV = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->height);
  179. if(is_object($matOriginal)){
  180. $material = createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $downloadDir, $scene);
  181. }else{
  182. $material = new Disney();
  183. $material->setBaseColor('1 1 1');
  184. }
  185. $scene->registerMaterial($material);
  186. $plyFileSavaPath = $downloadDir.basename($childParams->childPlyFile);
  187. $plyFileHandle = fopen($plyFileSavaPath, "w");
  188. (new Client())->get($childParams->childPlyFile, [RequestOptions::SINK => $plyFileHandle]);
  189. $obj = new Objects();
  190. $obj->ply = $plyFileSavaPath;
  191. $obj->setMaterial($material);
  192. $scene->registerObjects($obj);
  193. fclose($plyFileHandle);
  194. }
  195. // 三、创建相机
  196. $camera = new camera\Perspective( // 创建一个相机
  197. [
  198. 'autovolumeEnable' => 0, // 关闭自动体积效果
  199. 'lookatOrig' => "-3.197486 -4.560584 0.8914337", // 视点坐标位置
  200. 'lookatTarget' => "-2.632151 -3.747173 0.7544713", // 目标点坐标位置
  201. 'up' => "0.0781663 0.1124666 0.9905763", // 旋转参数
  202. 'screenwindow' => "-1 1 -1 1", // 视野比例
  203. 'autofocusEnable' => 0, // 关闭自动对焦
  204. 'fieldofview' => 3.40608, // 视野角度大小
  205. 'cliphither' => 0.1 // 剪切成像起始位置,单位跟随系统
  206. ]
  207. );
  208. $camera->bokeh->blades = 0; // 模拟镜头模糊效果的光圈数
  209. $scene->registerCamera($camera); // 将相机注册到 scene 中
  210. // 四、设置渲染参数
  211. $render = '';
  212. $renderEngine = new render\RenderEngine(); // 设置 渲染引擎 的配置参数,默认使用了 PATHOCL,意义着将使用 GPU 设备渲染
  213. $render .= $renderEngine;
  214. $openCL = new render\OpenCL(); // 设置 GPU 渲染参数,这里使用一个默认的参数
  215. $render .= $openCL;
  216. $path = new render\Path(); // 设置 光线跟踪 参数,这里使用一个默认的参数
  217. $render .= $path;
  218. $sampler = new render\Sampler(); // 设置 采样器 配置参数,默认使用 Sobol 类型的自适应采样器
  219. $render .= $sampler;
  220. $lightStrategy = new render\LightStrategy(); // 设置 灯光策略 类型,默认使用 LOG_POWER 类型
  221. $render .= $lightStrategy;
  222. $filesaver = new render\FileSaver(); // 设置 文件储存格式 配置参数
  223. $render .= $filesaver;
  224. $batch = new render\Batch(); // 设置 渲染终止 参数,
  225. $batch->haltspp = 1000; // 设置 每像素 采样达 800 则终止渲染
  226. // $batch->halttime = 2000; // 设置 渲染 超时参数,
  227. $render .= $batch;
  228. $sceneCfg = new render\Scene(); // 设置 场景属性 参数
  229. $render .= $sceneCfg;
  230. $film = new render\Film(); // 添加一个渲染胶片
  231. $film->width = 3000; // 设置渲染胶片宽度,单位像素
  232. $film->heigth = 3000; // 设置渲染胶片宽度,单位像素
  233. $img = new render\Image(); // 添加一个图像输出
  234. $img->effect = [new render\effect\Pretreatment(),new render\effect\ToneMapLinear(),new render\effect\CammaCorrection()];
  235. // 为该图像添加效果管线
  236. $film->addImage($img); // 将该图像输出添加到渲染胶片
  237. $film->addImage(new render\Image(['type' => render\Image::TYPE_OBJECT_ID])); // 为渲染胶片添加一个 通道图类型 的图像输出
  238. $film->addImage(new render\Image(['type' => render\Image::TYPE_RGBA])); // 添加一个 透明底 的图像输出
  239. $img = new render\Image(); // 添加一个图像输出,使用默认参数
  240. $img->effect[] = new render\effect\NoiseReducerOIDN(); // 添加降噪效果管线,使用默认参数
  241. $img->effect[] = new render\effect\Pretreatment(); //
  242. $img->effect[] = new render\effect\ToneMapLinear(); // 添加一亮度效果管线,使用默认参数
  243. $img->effect[] = new render\effect\Synthesis(); // 添加一个合成效果管线,使用默认参数
  244. $img->effect[] = new render\effect\BackgroundImg( // 添加一个背景图像,使用默认参数
  245. [
  246. 'file' => "./map/纯白242.png"
  247. ]
  248. );
  249. $img->effect[] = new render\effect\CammaCorrection(); // 添加一个 gamma 校正,,使用默认参数
  250. $film->addImage($img); // 将该图像输出添加到渲染胶片
  251. $render .= $film;
  252. // 五、输出场景文件 scene.scn,及渲染文件 render.cfg
  253. echo $scene;
  254. $handle = fopen( $sceneTemplatePath . "/scene.scn",'w+');
  255. fwrite($handle,$scene);
  256. fclose($handle);
  257. echo $render ;
  258. $handle = fopen( $sceneTemplatePath . "/render.cfg",'w+');
  259. fwrite($handle,$render);
  260. fclose($handle);
  261. // 六、启动渲染器
  262. $cfgPath = $sceneTemplatePath . "/render.cfg";
  263. $scenePath = $sceneTemplatePath . "/scene.scn";
  264. $logPath = $sceneTemplatePath . "/render.log";
  265. $cmd = "luxcoreconsole -o " . $cfgPath . " -f " . $scenePath . " 1>" . $logPath . " 2>&1";
  266. echo "\n".$cmd."\n";
  267. $output;
  268. $returnVar;
  269. exec($cmd,$output,$returnVar);
  270. if( $returnVar == 127 ){
  271. throw new \Exception("Please install the luxcorerender bin file in the system directory correctly,Or add to the system environment variable");
  272. }
  273. ?>