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.

392 lines
11 KiB

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