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.

318 lines
9.0 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 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. // 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. // 如果某个模型节点不可见,则不添加到场景渲染
  101. if($childParams->isVisible !== 1){
  102. continue;
  103. }
  104. $cacheFileDir = $sceneTemplatePath.'/cacheFiles';
  105. $material = null;
  106. $matOriginal = (function ($childParams){
  107. if(is_object($childParams->customMat)){
  108. return $childParams->customMat;
  109. }else if(is_object($childParams->fixedMat)){
  110. return $childParams->fixedMat;
  111. }else if(is_object($childParams->defaultMat)){
  112. return $childParams->defaultMat;
  113. }
  114. })($childParams);
  115. if(is_object($matOriginal)){
  116. $textureScaleU = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->width);
  117. $textureScaleV = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->height)*-1;
  118. $material = createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
  119. }else{
  120. $material = new Matte();
  121. $material->setBaseColor('0.5');
  122. }
  123. $scene->registerMaterial($material);
  124. $plyFileSavaPath = $cacheFileDir.'/'.basename($childParams->childPlyFile);
  125. $plyFileHandle = fopen($plyFileSavaPath, "w");
  126. (new Client())->get($childParams->childPlyFile, [RequestOptions::SINK => $plyFileHandle]);
  127. $obj = new Objects();
  128. $obj->ply = $plyFileSavaPath;
  129. $obj->setMaterial($material);
  130. $scene->registerObjects($obj);
  131. }
  132. // 三、创建相机
  133. $position = $taskCamera->position;
  134. $position->x = $position->x * UNIT_CONVERSION_MM_TO_M;
  135. $position->y = $position->y * UNIT_CONVERSION_MM_TO_M;
  136. $position->z = $position->z * UNIT_CONVERSION_MM_TO_M;
  137. $target = $taskCamera->target;
  138. $target->x = $target->x * UNIT_CONVERSION_MM_TO_M;
  139. $target->y = $target->y * UNIT_CONVERSION_MM_TO_M;
  140. $target->z = $target->z * UNIT_CONVERSION_MM_TO_M;
  141. $upVector = $taskCamera->upVector;
  142. $camera = new camera\Perspective(
  143. [
  144. 'autovolumeEnable' => 0,
  145. 'lookatOrig' => "{$position->x} {$position->y} {$position->z}",
  146. 'lookatTarget' => "{$target->x} {$target->y} {$target->z}",
  147. 'up' => "{$upVector->x} {$upVector->y} {$upVector->z}",
  148. 'screenwindow' => "-1 1 -1 1",
  149. 'autofocusEnable' => 0,
  150. 'fieldofview' => $taskCamera->fov,
  151. 'cliphither' => 0.1,
  152. ]
  153. );
  154. $camera->bokeh->blades = 0;
  155. $scene->registerCamera($camera);
  156. // 四、设置渲染参数
  157. // gpu 渲染
  158. $render = '';
  159. $renderEngine = new RenderEngine();
  160. $render .= $renderEngine;
  161. $openCL = new OpenCL();
  162. // $openCL->cpuUse = OpenCL::CLOSE;
  163. // $openCL->nativeThreadsCount = 0;
  164. $render .= $openCL;
  165. $lightStrategy = new LightStrategy();
  166. $render .= $lightStrategy;
  167. $hybridBackforWard = new HybridBackforWard();
  168. $hybridBackforWard->partition = 1;
  169. $path = new Path([],null,$hybridBackforWard);
  170. $render .= $path;
  171. $sampler = new Sampler();
  172. $render .= $sampler;
  173. $filesaver = new FileSaver();
  174. $render .= $filesaver;
  175. $batch = new Batch();
  176. $batch->haltspp = 4000;
  177. // $batch->halttime = 2000;
  178. $render .= $batch;
  179. $sceneCfg = new render\Scene();
  180. $render .= $sceneCfg;
  181. // // cpu 渲染
  182. // $render = '';
  183. // $renderEngine = new RenderEngine();
  184. // $renderEngine->type = RenderEngine::TYPE_PATHCPU;
  185. // $render .= $renderEngine;
  186. // $native = new Native();
  187. // $render .= $native;
  188. // $lightStrategy = new LightStrategy();
  189. // $render .= $lightStrategy;
  190. // $hybridBackforWard = new HybridBackforWard();
  191. // $hybridBackforWard->partition = 0.8;
  192. // $path = new Path([],null,$hybridBackforWard);
  193. // $render .= $path;
  194. // $sampler = new Sampler();
  195. // $render .= $sampler;
  196. // $filesaver = new FileSaver();
  197. // $filesaver->renderengineType = FileSaver::TYPE_PATHCPU;
  198. // $render .= $filesaver;
  199. // $batch = new Batch();
  200. // $batch->haltspp = 4000;
  201. // // $batch->halttime = 2000;
  202. // $render .= $batch;
  203. // $sceneCfg = new render\Scene();
  204. // $render .= $sceneCfg;
  205. // 图像输出设置
  206. $imageOutDir = $sceneTemplatePath.'/imageOut';
  207. $film = new Film();
  208. $filter = new Filter();
  209. $filter->type = Filter::TYPE_GAUSSIAN;
  210. $filter->gaussianAlpha = 1;
  211. $filter->width = 1;
  212. $film->filter = $filter;
  213. $film->width = 4096;
  214. $film->heigth = 4096;
  215. $img = new Image();
  216. $img->effect = [new Pretreatment(),new ToneMapLinear(),new CammaCorrection()];
  217. $film->addImage($img, $imageOutDir);
  218. $film->addImage(new Image(['type' => Image::TYPE_OBJECT_ID]), $imageOutDir);
  219. $film->addImage(new Image(['type' => Image::TYPE_RGBA]), $imageOutDir);
  220. $img = new Image();
  221. // $img->effect[] = new NoiseReducerOIDN();
  222. $img->effect[] = new Pretreatment();
  223. $img->effect[] = new ToneMapLinear();
  224. $img->effect[] = new Synthesis();
  225. $img->effect[] = new BackgroundImg(
  226. [
  227. 'file' => "./map/纯白242.png"
  228. ]
  229. );
  230. $img->effect[] = new CammaCorrection();
  231. $film->addImage($img, $imageOutDir);
  232. $render .= $film;
  233. // 五、输出场景文件 scene.scn,及渲染文件 render.cfg
  234. $cfgPath = $sceneTemplatePath . "/render.cfg";
  235. $scenePath = $sceneTemplatePath . "/scene.scn";
  236. $logPath = $sceneTemplatePath . "/log/render.log";
  237. echo $scene;
  238. $handle = fopen( $scenePath,'w+');
  239. fwrite($handle,$scene);
  240. fclose($handle);
  241. echo $render ;
  242. $handle = fopen( $cfgPath,'w+');
  243. fwrite($handle,$render);
  244. fclose($handle);
  245. // 六、启动渲染器
  246. $cmd = "luxcoreconsole -o " . $cfgPath . " -f " . $scenePath . " 1>" . $logPath . " 2>&1";
  247. echo "\n".$cmd."\n";
  248. $output;
  249. $returnVar;
  250. exec($cmd,$output,$returnVar);
  251. if( $returnVar == 127 ){
  252. throw new \Exception("Please install the luxcorerender bin file in the system directory correctly,Or add to the system environment variable");
  253. }
  254. ?>