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.

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