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.

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