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.

415 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 MatHelper::createMatOfGlass($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
  56. case 3:
  57. return MatHelper::createMatOfTranslucent($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
  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. // 如果某个模型节点不可见,则不添加到场景渲染
  192. if($childParams->isVisible !== 1){
  193. continue;
  194. }
  195. $cacheFileDir = $sceneTemplatePath.'/cacheFiles';
  196. $material = null;
  197. $matOriginal = (function ($childParams){
  198. if(is_object($childParams->customMat)){
  199. return $childParams->customMat;
  200. }else if(is_object($childParams->fixedMat)){
  201. return $childParams->fixedMat;
  202. }else if(is_object($childParams->defaultMat)){
  203. return $childParams->defaultMat;
  204. }
  205. })($childParams);
  206. if(is_object($matOriginal)){
  207. $textureScaleU = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->width);
  208. $textureScaleV = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->height)*-1;
  209. $material = createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
  210. }else{
  211. $material = new Matte();
  212. $material->setBaseColor('0.5');
  213. }
  214. $scene->registerMaterial($material);
  215. $plyFileSavaPath = $cacheFileDir.'/'.basename($childParams->childPlyFile);
  216. $plyFileHandle = fopen($plyFileSavaPath, "w");
  217. (new Client())->get($childParams->childPlyFile, [RequestOptions::SINK => $plyFileHandle]);
  218. $obj = new Objects();
  219. $obj->ply = $plyFileSavaPath;
  220. $obj->setMaterial($material);
  221. $scene->registerObjects($obj);
  222. }
  223. // 三、创建相机
  224. $position = $taskCamera->position;
  225. $position->x = $position->x * UNIT_CONVERSION_MM_TO_M;
  226. $position->y = $position->y * UNIT_CONVERSION_MM_TO_M;
  227. $position->z = $position->z * UNIT_CONVERSION_MM_TO_M;
  228. $target = $taskCamera->target;
  229. $target->x = $target->x * UNIT_CONVERSION_MM_TO_M;
  230. $target->y = $target->y * UNIT_CONVERSION_MM_TO_M;
  231. $target->z = $target->z * UNIT_CONVERSION_MM_TO_M;
  232. $upVector = $taskCamera->upVector;
  233. $camera = new camera\Perspective(
  234. [
  235. 'autovolumeEnable' => 0,
  236. 'lookatOrig' => "{$position->x} {$position->y} {$position->z}",
  237. 'lookatTarget' => "{$target->x} {$target->y} {$target->z}",
  238. 'up' => "{$upVector->x} {$upVector->y} {$upVector->z}",
  239. 'screenwindow' => "-1 1 -1 1",
  240. 'autofocusEnable' => 0,
  241. 'fieldofview' => $taskCamera->fov,
  242. 'cliphither' => 0.1,
  243. ]
  244. );
  245. $camera->bokeh->blades = 0;
  246. $scene->registerCamera($camera);
  247. // 四、设置渲染参数
  248. // gpu 渲染
  249. $render = '';
  250. $renderEngine = new RenderEngine();
  251. $render .= $renderEngine;
  252. $openCL = new OpenCL();
  253. // $openCL->cpuUse = OpenCL::CLOSE;
  254. // $openCL->nativeThreadsCount = 0;
  255. $render .= $openCL;
  256. $lightStrategy = new LightStrategy();
  257. $render .= $lightStrategy;
  258. $hybridBackforWard = new HybridBackforWard();
  259. $hybridBackforWard->partition = 1;
  260. $path = new Path([],null,$hybridBackforWard);
  261. $render .= $path;
  262. $sampler = new Sampler();
  263. $render .= $sampler;
  264. $filesaver = new FileSaver();
  265. $render .= $filesaver;
  266. $batch = new Batch();
  267. $batch->haltspp = 4000;
  268. // $batch->halttime = 2000;
  269. $render .= $batch;
  270. $sceneCfg = new render\Scene();
  271. $render .= $sceneCfg;
  272. // // cpu 渲染
  273. // $render = '';
  274. // $renderEngine = new RenderEngine();
  275. // $renderEngine->type = RenderEngine::TYPE_PATHCPU;
  276. // $render .= $renderEngine;
  277. // $native = new Native();
  278. // $render .= $native;
  279. // $lightStrategy = new LightStrategy();
  280. // $render .= $lightStrategy;
  281. // $hybridBackforWard = new HybridBackforWard();
  282. // $hybridBackforWard->partition = 0.8;
  283. // $path = new Path([],null,$hybridBackforWard);
  284. // $render .= $path;
  285. // $sampler = new Sampler();
  286. // $render .= $sampler;
  287. // $filesaver = new FileSaver();
  288. // $filesaver->renderengineType = FileSaver::TYPE_PATHCPU;
  289. // $render .= $filesaver;
  290. // $batch = new Batch();
  291. // $batch->haltspp = 4000;
  292. // // $batch->halttime = 2000;
  293. // $render .= $batch;
  294. // $sceneCfg = new render\Scene();
  295. // $render .= $sceneCfg;
  296. // 图像输出设置
  297. $imageOutDir = $sceneTemplatePath.'/imageOut';
  298. $film = new Film();
  299. $filter = new Filter();
  300. $filter->type = Filter::TYPE_GAUSSIAN;
  301. $filter->gaussianAlpha = 1;
  302. $filter->width = 1;
  303. $film->filter = $filter;
  304. $film->width = 4096;
  305. $film->heigth = 4096;
  306. $img = new Image();
  307. $img->effect = [new Pretreatment(),new ToneMapLinear(),new CammaCorrection()];
  308. $film->addImage($img, $imageOutDir);
  309. $film->addImage(new Image(['type' => Image::TYPE_OBJECT_ID]), $imageOutDir);
  310. $film->addImage(new Image(['type' => Image::TYPE_RGBA]), $imageOutDir);
  311. $img = new Image();
  312. // $img->effect[] = new NoiseReducerOIDN();
  313. $img->effect[] = new Pretreatment();
  314. $img->effect[] = new ToneMapLinear();
  315. $img->effect[] = new Synthesis();
  316. $img->effect[] = new BackgroundImg(
  317. [
  318. 'file' => "./map/纯白242.png"
  319. ]
  320. );
  321. $img->effect[] = new CammaCorrection();
  322. $film->addImage($img, $imageOutDir);
  323. $render .= $film;
  324. // 五、输出场景文件 scene.scn,及渲染文件 render.cfg
  325. $cfgPath = $sceneTemplatePath . "/render.cfg";
  326. $scenePath = $sceneTemplatePath . "/scene.scn";
  327. $logPath = $sceneTemplatePath . "/log/render.log";
  328. echo $scene;
  329. $handle = fopen( $scenePath,'w+');
  330. fwrite($handle,$scene);
  331. fclose($handle);
  332. echo $render ;
  333. $handle = fopen( $cfgPath,'w+');
  334. fwrite($handle,$render);
  335. fclose($handle);
  336. // 六、启动渲染器
  337. $cmd = "luxcoreconsole -o " . $cfgPath . " -f " . $scenePath . " 1>" . $logPath . " 2>&1";
  338. echo "\n".$cmd."\n";
  339. $output;
  340. $returnVar;
  341. exec($cmd,$output,$returnVar);
  342. if( $returnVar == 127 ){
  343. throw new \Exception("Please install the luxcorerender bin file in the system directory correctly,Or add to the system environment variable");
  344. }
  345. ?>