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.

359 lines
11 KiB

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