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.

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