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.

311 lines
11 KiB

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