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.

313 lines
8.7 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\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. // 8、创建环境光
  67. $light = new HdrImage([
  68. 'gain' => "1 1 1",
  69. 'transformation' => "-1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1",
  70. 'file' => "./map/shendoEnv.exr",
  71. 'gamma' => 1,
  72. 'storage' => "float"
  73. ]);
  74. $light->id = 0;
  75. $scene->registerLight($light);
  76. // 9、创建地面模型
  77. $mapping = new Mapping();
  78. $mapping->useUVMapping2d(0, "0", "0.4 -0.966", "0.3 0.983");
  79. $texture = new ImageMap([
  80. 'file' => "./map/方形阴影遮照.png",
  81. 'gain' => 0.6,
  82. 'gamma' => 1,
  83. ]);
  84. $texture->mapping = $mapping;
  85. $scene->registerTexture($texture);
  86. $material = new Disney([
  87. 'shadowcatcherEnable' => OPEN,
  88. 'photongiEnable' => CLOSE
  89. ]);
  90. $material->setBaseColor('1 1 1');
  91. $material->setTransparencyFront($texture);
  92. $material->setTransparencyBack($texture);
  93. $scene->registerMaterial($material);
  94. $obj = new Objects();
  95. $obj->ply = './ply/ground.ply';
  96. $obj->setMaterial($material);
  97. $scene->registerObjects($obj);
  98. // 二、添加要渲染的模型
  99. foreach($taskModel->childsParams as $childParams){
  100. $cacheFileDir = $sceneTemplatePath.'/cacheFiles';
  101. $material = null;
  102. $matOriginal = (function ($childParams){
  103. if(is_object($childParams->customMat)){
  104. return $childParams->customMat;
  105. }else if(is_object($childParams->fixedMat)){
  106. return $childParams->fixedMat;
  107. }else if(is_object($childParams->defaultMat)){
  108. return $childParams->defaultMat;
  109. }
  110. })($childParams);
  111. if(is_object($matOriginal)){
  112. $textureScaleU = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->width);
  113. $textureScaleV = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->height)*-1;
  114. $material = createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
  115. }else{
  116. $material = new Disney();
  117. $material->setBaseColor('1 1 1');
  118. }
  119. $scene->registerMaterial($material);
  120. $plyFileSavaPath = $cacheFileDir.'/'.basename($childParams->childPlyFile);
  121. $plyFileHandle = fopen($plyFileSavaPath, "w");
  122. (new Client())->get($childParams->childPlyFile, [RequestOptions::SINK => $plyFileHandle]);
  123. $obj = new Objects();
  124. $obj->ply = $plyFileSavaPath;
  125. $obj->setMaterial($material);
  126. $scene->registerObjects($obj);
  127. }
  128. // 三、创建相机
  129. $position = $taskCamera->position;
  130. $position->x = $position->x * UNIT_CONVERSION_MM_TO_M;
  131. $position->y = $position->y * UNIT_CONVERSION_MM_TO_M;
  132. $position->z = $position->z * UNIT_CONVERSION_MM_TO_M;
  133. $target = $taskCamera->target;
  134. $target->x = $target->x * UNIT_CONVERSION_MM_TO_M;
  135. $target->y = $target->y * UNIT_CONVERSION_MM_TO_M;
  136. $target->z = $target->z * UNIT_CONVERSION_MM_TO_M;
  137. $upVector = $taskCamera->upVector;
  138. $camera = new camera\Perspective(
  139. [
  140. 'autovolumeEnable' => 0,
  141. 'lookatOrig' => "{$position->x} {$position->y} {$position->z}",
  142. 'lookatTarget' => "{$target->x} {$target->y} {$target->z}",
  143. 'up' => "{$upVector->x} {$upVector->y} {$upVector->z}",
  144. 'screenwindow' => "-1 1 -1 1",
  145. 'autofocusEnable' => 0,
  146. 'fieldofview' => $taskCamera->fov,
  147. 'cliphither' => 0.1,
  148. ]
  149. );
  150. $camera->bokeh->blades = 0;
  151. $scene->registerCamera($camera);
  152. // 四、设置渲染参数
  153. // // gpu 渲染
  154. // $render = '';
  155. // $renderEngine = new RenderEngine();
  156. // $render .= $renderEngine;
  157. // $openCL = new OpenCL();
  158. // // $openCL->cpuUse = OpenCL::CLOSE;
  159. // // $openCL->nativeThreadsCount = 0;
  160. // $render .= $openCL;
  161. // $lightStrategy = new LightStrategy();
  162. // $render .= $lightStrategy;
  163. // $hybridBackforWard = new HybridBackforWard();
  164. // $hybridBackforWard->partition = 1;
  165. // $path = new Path([],null,$hybridBackforWard);
  166. // $render .= $path;
  167. // $sampler = new Sampler();
  168. // $render .= $sampler;
  169. // $filesaver = new FileSaver();
  170. // $render .= $filesaver;
  171. // $batch = new Batch();
  172. // $batch->haltspp = 4000;
  173. // // $batch->halttime = 2000;
  174. // $render .= $batch;
  175. // $sceneCfg = new render\Scene();
  176. // $render .= $sceneCfg;
  177. // cpu 渲染
  178. $render = '';
  179. $renderEngine = new RenderEngine();
  180. $renderEngine->type = RenderEngine::TYPE_PATHCPU;
  181. $render .= $renderEngine;
  182. $native = new Native();
  183. $render .= $native;
  184. $lightStrategy = new LightStrategy();
  185. $render .= $lightStrategy;
  186. $hybridBackforWard = new HybridBackforWard();
  187. $hybridBackforWard->partition = 0.8;
  188. $path = new Path([],null,$hybridBackforWard);
  189. $render .= $path;
  190. $sampler = new Sampler();
  191. $render .= $sampler;
  192. $filesaver = new FileSaver();
  193. $filesaver->renderengineType = FileSaver::TYPE_PATHCPU;
  194. $render .= $filesaver;
  195. $batch = new Batch();
  196. $batch->haltspp = 2000;
  197. // $batch->halttime = 2000;
  198. $render .= $batch;
  199. $sceneCfg = new render\Scene();
  200. $render .= $sceneCfg;
  201. // 图像输出设置
  202. $imageOutDir = $sceneTemplatePath.'/imageOut';
  203. $film = new Film();
  204. $filter = new Filter();
  205. $filter->type = Filter::TYPE_GAUSSIAN;
  206. $filter->gaussianAlpha = 1;
  207. $filter->width = 1;
  208. $film->filter = $filter;
  209. $film->width = 4096;
  210. $film->heigth = 4096;
  211. $img = new Image();
  212. $img->effect = [new Pretreatment(),new ToneMapLinear(),new CammaCorrection()];
  213. $film->addImage($img, $imageOutDir);
  214. $film->addImage(new Image(['type' => Image::TYPE_OBJECT_ID]), $imageOutDir);
  215. $film->addImage(new Image(['type' => Image::TYPE_RGBA]), $imageOutDir);
  216. $img = new Image();
  217. // $img->effect[] = new NoiseReducerOIDN();
  218. $img->effect[] = new Pretreatment();
  219. $img->effect[] = new ToneMapLinear();
  220. $img->effect[] = new Synthesis();
  221. $img->effect[] = new BackgroundImg(
  222. [
  223. 'file' => "./map/纯白242.png"
  224. ]
  225. );
  226. $img->effect[] = new CammaCorrection();
  227. $film->addImage($img, $imageOutDir);
  228. $render .= $film;
  229. // 五、输出场景文件 scene.scn,及渲染文件 render.cfg
  230. $cfgPath = $sceneTemplatePath . "/render.cfg";
  231. $scenePath = $sceneTemplatePath . "/scene.scn";
  232. $logPath = $sceneTemplatePath . "/log/render.log";
  233. echo $scene;
  234. $handle = fopen( $scenePath,'w+');
  235. fwrite($handle,$scene);
  236. fclose($handle);
  237. echo $render ;
  238. $handle = fopen( $cfgPath,'w+');
  239. fwrite($handle,$render);
  240. fclose($handle);
  241. // 六、启动渲染器
  242. $cmd = "luxcoreconsole -o " . $cfgPath . " -f " . $scenePath . " 1>" . $logPath . " 2>&1";
  243. echo "\n".$cmd."\n";
  244. $output;
  245. $returnVar;
  246. exec($cmd,$output,$returnVar);
  247. if( $returnVar == 127 ){
  248. throw new \Exception("Please install the luxcorerender bin file in the system directory correctly,Or add to the system environment variable");
  249. }
  250. ?>