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.

404 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\lights\Emission;
  4. use Blobt\Luxcore\scene\lights\env\HdrImage;
  5. use Blobt\Luxcore\scene\materials\Disney;
  6. use Blobt\Luxcore\scene\materials\Matte;
  7. use Blobt\Luxcore\scene\objects\Objects;
  8. use Blobt\Luxcore\scene\render\Batch;
  9. use Blobt\Luxcore\scene\render\effect\BackgroundImg;
  10. use Blobt\Luxcore\scene\render\effect\CammaCorrection;
  11. use Blobt\Luxcore\scene\render\effect\NoiseReducerOIDN;
  12. use Blobt\Luxcore\scene\render\effect\Pretreatment;
  13. use Blobt\Luxcore\scene\render\effect\Synthesis;
  14. use Blobt\Luxcore\scene\render\effect\ToneMapLinear;
  15. use Blobt\Luxcore\scene\render\FileSaver;
  16. use Blobt\Luxcore\scene\render\Film;
  17. use Blobt\Luxcore\scene\render\Filter;
  18. use Blobt\Luxcore\scene\render\Image;
  19. use Blobt\Luxcore\scene\render\LightStrategy;
  20. use Blobt\Luxcore\scene\render\Native;
  21. use Blobt\Luxcore\scene\render\OpenCL;
  22. use Blobt\Luxcore\scene\render\Path;
  23. use Blobt\Luxcore\scene\render\RenderEngine;
  24. use Blobt\Luxcore\scene\render\Sampler;
  25. use Blobt\Luxcore\scene\texture\mapping\Mapping;
  26. use Blobt\Luxcore\scene\texture\procedural\ImageMap;
  27. use Blobt\Luxcore\utils\MatHelper;
  28. use GuzzleHttp\Client;
  29. use GuzzleHttp\RequestOptions;
  30. include dirname(dirname(__FILE__)) . "/vendor/autoload.php";
  31. const OPEN = true | 1;
  32. const CLOSE = false | 0;
  33. /**
  34. * 标准材质长宽
  35. */
  36. const MAT_STANDARD_SIZE = 1000;
  37. const UNIT_CONVERSION_MM_TO_M = 0.001;
  38. $client = new Client();
  39. $response = $client->get("deep3d.backend-api.dev.com/test/render-task");
  40. $resContents = $response->getBody()->getContents();
  41. $result = json_decode($resContents,false);
  42. $taskData = $result->data;
  43. $taskScene = $taskData->scene;
  44. $taskModel = $taskScene->model;
  45. $taskCamera = $taskScene->camera;
  46. /**
  47. * 创建 Luxcore 材质
  48. */
  49. function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $downloadDir, $scene){
  50. switch($matOriginal->renderType){
  51. case 1:
  52. return MatHelper::createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $downloadDir, $scene);
  53. case 2:
  54. return;
  55. case 3:
  56. return;
  57. case 4:
  58. return;
  59. }
  60. }
  61. // 创建一个场景;
  62. $scene = new Scene();
  63. $sceneTemplatePath = dirname(dirname(__FILE__)) . "/sceneTemplate";
  64. // 一、创建光场:
  65. // 1、创建 面片光001
  66. $material = new Matte();
  67. $material->setBaseColor('0 0 0');
  68. $material->setTransparencyShadow('1 1 1');
  69. $material->setEmission('1 1 1');
  70. $scene->registerMaterial($material);
  71. $obj = new Objects();
  72. $obj->ply = './ply/FaceLight001.ply';
  73. $obj->setMaterial($material);
  74. $scene->registerObjects($obj);
  75. // 2、创建 面片光002
  76. $material = new Matte();
  77. $material->setBaseColor('0 0 0');
  78. $material->setTransparencyShadow('1 1 1');
  79. $material->setEmission('0 0 0');
  80. $material->emissionCfg = new Emission([
  81. 'gain' => "30 30 30",
  82. ]);
  83. $scene->registerMaterial($material);
  84. $obj = new Objects();
  85. $obj->ply = './ply/FaceLight002.ply';
  86. $obj->setMaterial($material);
  87. $scene->registerObjects($obj);
  88. // 3、创建 面片光003
  89. $material = new Matte();
  90. $material->setBaseColor('0 0 0');
  91. $material->setTransparencyShadow('1 1 1');
  92. $material->setEmission('1 1 1');
  93. $material->emissionCfg = new Emission([
  94. 'gain' => "50 50 50",
  95. ]);
  96. $scene->registerMaterial($material);
  97. $obj = new Objects();
  98. $obj->ply = './ply/FaceLight003.ply';
  99. $obj->setMaterial($material);
  100. $scene->registerObjects($obj);
  101. // 4、创建 面片光004
  102. $material = new Matte();
  103. $material->setBaseColor('0 0 0');
  104. $material->setTransparencyShadow('1 1 1');
  105. $material->setEmission('0 0 0');
  106. $material->emissionCfg = new Emission([
  107. 'gain' => "4 4 4",
  108. ]);
  109. $scene->registerMaterial($material);
  110. $obj = new Objects();
  111. $obj->ply = './ply/FaceLight004.ply';
  112. $obj->setMaterial($material);
  113. $scene->registerObjects($obj);
  114. // 5、创建 面片光005
  115. $material = new Matte();
  116. $material->setBaseColor('0 0 0');
  117. $material->setTransparencyShadow('1 1 1');
  118. $material->setEmission('1 1 1');
  119. $material->emissionCfg = new Emission([
  120. 'gain' => "10 10 10",
  121. ]);
  122. $scene->registerMaterial($material);
  123. $obj = new Objects();
  124. $obj->ply = './ply/FaceLight005.ply';
  125. $obj->setMaterial($material);
  126. $scene->registerObjects($obj);
  127. // 6、创建 面片光006
  128. $material = new Matte();
  129. $material->setBaseColor('0 0 0');
  130. $material->setTransparencyShadow('1 1 1');
  131. $material->setEmission('1 1 1');
  132. $material->emissionCfg = new Emission([
  133. 'gain' => "8 8 8",
  134. 'mapfile' => "./map/SD-037.exr",
  135. 'gamma' => 1,
  136. 'storage' => "float"
  137. ]);
  138. $scene->registerMaterial($material);
  139. $obj = new Objects();
  140. $obj->ply = './ply/FaceLight006.ply';
  141. $obj->setMaterial($material);
  142. $scene->registerObjects($obj);
  143. // 7、创建 面片光000
  144. $material = new Matte();
  145. $material->setBaseColor('0 0 0');
  146. $material->setTransparencyShadow('1 1 1');
  147. $material->setEmission('1 1 1');
  148. $material->emissionCfg = new Emission([
  149. 'gain' => "3.3 3.3 3.3",
  150. ]);
  151. $scene->registerMaterial($material);
  152. $obj = new Objects();
  153. $obj->ply = './ply/FaceLight000.ply';
  154. $obj->setMaterial($material);
  155. $scene->registerObjects($obj);
  156. // 8、创建环境光
  157. $light = new HdrImage([
  158. 'gain' => "1.25 1.25 1.25",
  159. 'transformation' => "0.1736481 -0.9848078 0 0 -0.9848078 -0.1736481 0 0 0 0 1 0 0 0 0 1",
  160. 'file' => "./map/env.png",
  161. 'gamma' => 1,
  162. 'storage' => "byte"
  163. ]);
  164. $light->id = 0;
  165. $scene->registerLight($light);
  166. // 9、创建地面模型
  167. $mapping = new Mapping();
  168. $mapping->useUVMapping2d(0, "0", "0.4 -0.966", "0.3 0.983");
  169. $texture = new ImageMap([
  170. 'file' => "./map/方形阴影遮照.png",
  171. 'gain' => 0.6,
  172. 'gamma' => 1,
  173. ]);
  174. $texture->mapping = $mapping;
  175. $scene->registerTexture($texture);
  176. $material = new Disney([
  177. 'shadowcatcherEnable' => OPEN,
  178. 'photongiEnable' => CLOSE
  179. ]);
  180. $material->setBaseColor('1 1 1');
  181. $material->setTransparencyFront($texture);
  182. $material->setTransparencyBack($texture);
  183. $scene->registerMaterial($material);
  184. $obj = new Objects();
  185. $obj->ply = './ply/ground.ply';
  186. $obj->setMaterial($material);
  187. $scene->registerObjects($obj);
  188. // 二、添加要渲染的模型
  189. foreach($taskModel->childsParams as $childParams){
  190. $downloadDir = $sceneTemplatePath.'/cacheFiles/';
  191. $material = null;
  192. $matOriginal = (function ($childParams){
  193. if(is_object($childParams->customMat)){
  194. return $childParams->customMat;
  195. }else if(is_object($childParams->fixedMat)){
  196. return $childParams->fixedMat;
  197. }else if(is_object($childParams->defaultMat)){
  198. return $childParams->defaultMat;
  199. }
  200. })($childParams);
  201. $textureScaleU = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->width);
  202. $textureScaleV = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->height);
  203. if(is_object($matOriginal)){
  204. $material = createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $downloadDir, $scene);
  205. }else{
  206. $material = new Disney();
  207. $material->setBaseColor('1 1 1');
  208. }
  209. $scene->registerMaterial($material);
  210. $plyFileSavaPath = $downloadDir.basename($childParams->childPlyFile);
  211. $plyFileHandle = fopen($plyFileSavaPath, "w");
  212. (new Client())->get($childParams->childPlyFile, [RequestOptions::SINK => $plyFileHandle]);
  213. $obj = new Objects();
  214. $obj->ply = $plyFileSavaPath;
  215. $obj->setMaterial($material);
  216. $scene->registerObjects($obj);
  217. }
  218. // 三、创建相机
  219. $position = $taskCamera->position;
  220. $position->x = $position->x * UNIT_CONVERSION_MM_TO_M;
  221. $position->y = $position->y * UNIT_CONVERSION_MM_TO_M;
  222. $position->z = $position->z * UNIT_CONVERSION_MM_TO_M;
  223. $target = $taskCamera->target;
  224. $target->x = $target->x * UNIT_CONVERSION_MM_TO_M;
  225. $target->y = $target->y * UNIT_CONVERSION_MM_TO_M;
  226. $target->z = $target->z * UNIT_CONVERSION_MM_TO_M;
  227. $upVector = $taskCamera->upVector;
  228. $camera = new camera\Perspective(
  229. [
  230. 'autovolumeEnable' => 0,
  231. 'lookatOrig' => "{$position->x} {$position->y} {$position->z}",
  232. 'lookatTarget' => "{$target->x} {$target->y} {$target->z}",
  233. 'up' => "{$upVector->x} {$upVector->y} {$upVector->z}",
  234. 'screenwindow' => "-1 1 -1 1",
  235. 'autofocusEnable' => 0,
  236. 'fieldofview' => $taskCamera->fov,
  237. 'cliphither' => 0.1,
  238. ]
  239. );
  240. $camera->bokeh->blades = 0;
  241. $scene->registerCamera($camera);
  242. // 四、设置渲染参数
  243. // gpu 渲染
  244. $render = '';
  245. $renderEngine = new RenderEngine();
  246. $render .= $renderEngine;
  247. $openCL = new OpenCL();
  248. // $openCL->cpuUse = OpenCL::CLOSE;
  249. // $openCL->nativeThreadsCount = 0;
  250. $render .= $openCL;
  251. $lightStrategy = new LightStrategy();
  252. $render .= $lightStrategy;
  253. $path = new Path();
  254. $render .= $path;
  255. $sampler = new Sampler();
  256. $render .= $sampler;
  257. $filesaver = new FileSaver();
  258. $render .= $filesaver;
  259. $batch = new Batch();
  260. $batch->haltspp = 4000;
  261. // $batch->halttime = 2000;
  262. $render .= $batch;
  263. $sceneCfg = new render\Scene();
  264. $render .= $sceneCfg;
  265. // // cpu 渲染
  266. // $render = '';
  267. // $renderEngine = new RenderEngine();
  268. // $renderEngine->type = RenderEngine::TYPE_PATHCPU;
  269. // $render .= $renderEngine;
  270. // $native = new Native();
  271. // $render .= $native;
  272. // $lightStrategy = new LightStrategy();
  273. // $render .= $lightStrategy;
  274. // $path = new Path();
  275. // $render .= $path;
  276. // $sampler = new Sampler();
  277. // $render .= $sampler;
  278. // $filesaver = new FileSaver();
  279. // $filesaver->renderengineType = FileSaver::TYPE_PATHCPU;
  280. // $render .= $filesaver;
  281. // $batch = new Batch();
  282. // $batch->haltspp = 200;
  283. // // $batch->halttime = 2000;
  284. // $render .= $batch;
  285. // $sceneCfg = new render\Scene();
  286. // $render .= $sceneCfg;
  287. // 图像输出设置
  288. $imageOutPath = $sceneTemplatePath.'/imageOut';
  289. $film = new Film();
  290. $filter = new Filter();
  291. $filter->type = Filter::TYPE_GAUSSIAN;
  292. $filter->gaussianAlpha = 1;
  293. $filter->width = 1;
  294. $film->filter = $filter;
  295. $film->width = 4096;
  296. $film->heigth = 4096;
  297. $img = new Image();
  298. $img->effect = [new Pretreatment(),new ToneMapLinear(),new CammaCorrection()];
  299. $film->addImage($img, $imageOutPath);
  300. $film->addImage(new Image(['type' => Image::TYPE_OBJECT_ID]), $imageOutPath);
  301. $film->addImage(new Image(['type' => Image::TYPE_RGBA]), $imageOutPath);
  302. $img = new Image();
  303. // $img->effect[] = new NoiseReducerOIDN();
  304. $img->effect[] = new Pretreatment();
  305. $img->effect[] = new ToneMapLinear();
  306. $img->effect[] = new Synthesis();
  307. $img->effect[] = new BackgroundImg(
  308. [
  309. 'file' => "./map/纯白242.png"
  310. ]
  311. );
  312. $img->effect[] = new CammaCorrection();
  313. $film->addImage($img, $imageOutPath);
  314. $render .= $film;
  315. // 五、输出场景文件 scene.scn,及渲染文件 render.cfg
  316. echo $scene;
  317. $handle = fopen( $sceneTemplatePath . "/scene.scn",'w+');
  318. fwrite($handle,$scene);
  319. fclose($handle);
  320. echo $render ;
  321. $handle = fopen( $sceneTemplatePath . "/render.cfg",'w+');
  322. fwrite($handle,$render);
  323. fclose($handle);
  324. // 六、启动渲染器
  325. $cfgPath = $sceneTemplatePath . "/render.cfg";
  326. $scenePath = $sceneTemplatePath . "/scene.scn";
  327. $logPath = $sceneTemplatePath . "/log/render.log";
  328. $cmd = "luxcoreconsole -o " . $cfgPath . " -f " . $scenePath . " 1>" . $logPath . " 2>&1";
  329. echo "\n".$cmd."\n";
  330. $output;
  331. $returnVar;
  332. exec($cmd,$output,$returnVar);
  333. if( $returnVar == 127 ){
  334. throw new \Exception("Please install the luxcorerender bin file in the system directory correctly,Or add to the system environment variable");
  335. }
  336. ?>