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.

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