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.

314 lines
11 KiB

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\utils\MatHelper;
  4. use GuzzleHttp\Client;
  5. include dirname(dirname(__FILE__)) . "/vendor/autoload.php";
  6. const OPEN = true | 1;
  7. const CLOSE = false | 0;
  8. /**
  9. * 标准材质长宽
  10. */
  11. const MAT_STANDARD_SIZE = 1000;
  12. $client = new Client();
  13. $response = $client->get("deep3d.backend-api.dev.com/test/render-task");
  14. $resContents = $response->getBody()->getContents();
  15. $result = json_decode($resContents,false);
  16. $taskData = $result->data;
  17. $taskScene = $taskData->scene;
  18. $taskModel = $taskScene->model;
  19. /**
  20. * 创建个 Luxcore 材质
  21. */
  22. function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $scene){
  23. switch($matOriginal->renderType){
  24. case 1:
  25. return MatHelper::createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $scene);
  26. case 2:
  27. return;
  28. case 3:
  29. return;
  30. case 4:
  31. return;
  32. }
  33. }
  34. // 创建一个场景;
  35. $scene = new Scene();
  36. // 一、创建光场:
  37. // 1、创建 面片光001
  38. $material = new materials\Matte();
  39. $material->setBaseColor('0 0 0');
  40. $material->setTransparencyShadow('1 1 1');
  41. $material->setEmission('1 1 1');
  42. $scene->registerMaterial($material);
  43. $obj = new objects\Objects();
  44. $obj->ply = './ply/FaceLight001.ply';
  45. $obj->setMaterial($material);
  46. $scene->registerObjects($obj);
  47. // 2、创建 面片光002
  48. $material = new materials\Matte();
  49. $material->setBaseColor('0 0 0');
  50. $material->setTransparencyShadow('1 1 1');
  51. $material->setEmission('0 0 0');
  52. $material->emissionCfg = new lights\Emission([
  53. 'gain' => "30 30 30",
  54. ]);
  55. $scene->registerMaterial($material);
  56. $obj = new objects\Objects();
  57. $obj->ply = './ply/FaceLight002.ply';
  58. $obj->setMaterial($material);
  59. $scene->registerObjects($obj);
  60. // 3、创建 面片光003
  61. $material = new materials\Matte();
  62. $material->setBaseColor('0 0 0');
  63. $material->setTransparencyShadow('1 1 1');
  64. $material->setEmission('1 1 1');
  65. $material->emissionCfg = new lights\Emission([
  66. 'gain' => "50 50 50",
  67. ]);
  68. $scene->registerMaterial($material);
  69. $obj = new objects\Objects();
  70. $obj->ply = './ply/FaceLight003.ply';
  71. $obj->setMaterial($material);
  72. $scene->registerObjects($obj);
  73. // 4、创建 面片光004
  74. $material = new materials\Matte();
  75. $material->setBaseColor('0 0 0');
  76. $material->setTransparencyShadow('1 1 1');
  77. $material->setEmission('0 0 0');
  78. $material->emissionCfg = new lights\Emission([
  79. 'gain' => "4 4 4",
  80. ]);
  81. $scene->registerMaterial($material);
  82. $obj = new objects\Objects();
  83. $obj->ply = './ply/FaceLight004.ply';
  84. $obj->setMaterial($material);
  85. $scene->registerObjects($obj);
  86. // 5、创建 面片光005
  87. $material = new materials\Matte();
  88. $material->setBaseColor('0 0 0');
  89. $material->setTransparencyShadow('1 1 1');
  90. $material->setEmission('1 1 1');
  91. $material->emissionCfg = new lights\Emission([
  92. 'gain' => "10 10 10",
  93. ]);
  94. $scene->registerMaterial($material);
  95. $obj = new objects\Objects();
  96. $obj->ply = './ply/FaceLight005.ply';
  97. $obj->setMaterial($material);
  98. $scene->registerObjects($obj);
  99. // 6、创建 面片光006
  100. $material = new materials\Matte();
  101. $material->setBaseColor('0 0 0');
  102. $material->setTransparencyShadow('1 1 1');
  103. $material->setEmission('1 1 1');
  104. $material->emissionCfg = new lights\Emission([
  105. 'gain' => "8 8 8",
  106. 'mapfile' => "./map/SD-037.exr",
  107. 'gamma' => 1,
  108. 'storage' => "float"
  109. ]);
  110. $scene->registerMaterial($material);
  111. $obj = new objects\Objects();
  112. $obj->ply = './ply/FaceLight006.ply';
  113. $obj->setMaterial($material);
  114. $scene->registerObjects($obj);
  115. // 7、创建 面片光000
  116. $material = new materials\Matte();
  117. $material->setBaseColor('0 0 0');
  118. $material->setTransparencyShadow('1 1 1');
  119. $material->setEmission('1 1 1');
  120. $material->emissionCfg = new lights\Emission([
  121. 'gain' => "3.3 3.3 3.3",
  122. ]);
  123. $scene->registerMaterial($material);
  124. $obj = new objects\Objects();
  125. $obj->ply = './ply/FaceLight000.ply';
  126. $obj->setMaterial($material);
  127. $scene->registerObjects($obj);
  128. // 8、创建环境光
  129. $light = new lights\env\HdrImage([
  130. 'gain' => "1.25 1.25 1.25",
  131. 'transformation' => "0.1736481 -0.9848078 0 0 -0.9848078 -0.1736481 0 0 0 0 1 0 0 0 0 1",
  132. 'file' => "./map/env.png",
  133. 'gamma' => 1,
  134. 'storage' => "byte"
  135. ]);
  136. $light->id = 0;
  137. $scene->registerLight($light);
  138. // 9、创建地面模型
  139. $mapping = new texture\mapping\Mapping();
  140. $mapping->useUVMapping2d(0, "0", "0.4 -0.966", "0.3 0.983");
  141. $texture = new texture\procedural\ImageMap([
  142. 'file' => "./map/方形阴影遮照.png",
  143. 'gain' => 0.6,
  144. 'gamma' => 1,
  145. ]);
  146. $texture->mapping = $mapping;
  147. $scene->registerTexture($texture);
  148. $material = new materials\Disney([
  149. 'shadowcatcherEnable' => OPEN,
  150. 'photongiEnable' => CLOSE
  151. ]);
  152. $material->setBaseColor('1 1 1');
  153. $material->setTransparencyFront($texture);
  154. $material->setTransparencyBack($texture);
  155. $scene->registerMaterial($material);
  156. $obj = new objects\Objects();
  157. $obj->ply = './ply/ground.ply';
  158. $obj->setMaterial($material);
  159. $scene->registerObjects($obj);
  160. // 二、添加要渲染的模型
  161. foreach($taskModel->childsParams as $childParams){
  162. $material = null;
  163. $matOriginal = (function ($childParams){
  164. if(is_object($childParams->customMat)){
  165. return $childParams->customMat;
  166. }else if(is_object($childParams->fixedMat)){
  167. return $childParams->fixedMat;
  168. }else if(is_object($childParams->defaultMat)){
  169. return $childParams->defaultMat;
  170. }
  171. })($childParams);
  172. $textureScaleU = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->width);
  173. $textureScaleV = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->height);
  174. if(is_object($matOriginal)){
  175. $material = createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $scene);
  176. }else{
  177. $material = new materials\Disney();
  178. $material->setBaseColor('1 1 1');
  179. }
  180. $scene->registerMaterial($material);
  181. $obj = new objects\Objects();
  182. $obj->ply = $childParams->childPlyFile;
  183. $obj->setMaterial($material);
  184. $scene->registerObjects($obj);
  185. }
  186. // 三、创建相机
  187. $camera = new camera\Perspective( // 创建一个相机
  188. [
  189. 'autovolumeEnable' => 0, // 关闭自动体积效果
  190. 'lookatOrig' => "-3.197486 -4.560584 0.8914337", // 视点坐标位置
  191. 'lookatTarget' => "-2.632151 -3.747173 0.7544713", // 目标点坐标位置
  192. 'up' => "0.0781663 0.1124666 0.9905763", // 旋转参数
  193. 'screenwindow' => "-1 1 -1 1", // 视野比例
  194. 'autofocusEnable' => 0, // 关闭自动对焦
  195. 'fieldofview' => 3.40608, // 视野角度大小
  196. 'cliphither' => 0.1 // 剪切成像起始位置,单位跟随系统
  197. ]
  198. );
  199. $camera->bokeh->blades = 0; // 模拟镜头模糊效果的光圈数
  200. $scene->registerCamera($camera); // 将相机注册到 scene 中
  201. // 四、设置渲染参数
  202. $render = '';
  203. $renderEngine = new render\RenderEngine(); // 设置 渲染引擎 的配置参数,默认使用了 PATHOCL,意义着将使用 GPU 设备渲染
  204. $render .= $renderEngine;
  205. $openCL = new render\OpenCL(); // 设置 GPU 渲染参数,这里使用一个默认的参数
  206. $render .= $openCL;
  207. $path = new render\Path(); // 设置 光线跟踪 参数,这里使用一个默认的参数
  208. $render .= $path;
  209. $sampler = new render\Sampler(); // 设置 采样器 配置参数,默认使用 Sobol 类型的自适应采样器
  210. $render .= $sampler;
  211. $lightStrategy = new render\LightStrategy(); // 设置 灯光策略 类型,默认使用 LOG_POWER 类型
  212. $render .= $lightStrategy;
  213. $filesaver = new render\FileSaver(); // 设置 文件储存格式 配置参数
  214. $render .= $filesaver;
  215. $batch = new render\Batch(); // 设置 渲染终止 参数,
  216. $batch->haltspp = 1000; // 设置 每像素 采样达 800 则终止渲染
  217. // $batch->halttime = 2000; // 设置 渲染 超时参数,
  218. $render .= $batch;
  219. $sceneCfg = new render\Scene(); // 设置 场景属性 参数
  220. $render .= $sceneCfg;
  221. $film = new render\Film(); // 添加一个渲染胶片
  222. $film->width = 3000; // 设置渲染胶片宽度,单位像素
  223. $film->heigth = 3000; // 设置渲染胶片宽度,单位像素
  224. $img = new render\Image(); // 添加一个图像输出
  225. $img->effect = [new render\effect\Pretreatment(),new render\effect\ToneMapLinear(),new render\effect\CammaCorrection()];
  226. // 为该图像添加效果管线
  227. $film->addImage($img); // 将该图像输出添加到渲染胶片
  228. $film->addImage(new render\Image(['type' => render\Image::TYPE_OBJECT_ID])); // 为渲染胶片添加一个 通道图类型 的图像输出
  229. $film->addImage(new render\Image(['type' => render\Image::TYPE_RGBA])); // 添加一个 透明底 的图像输出
  230. $img = new render\Image(); // 添加一个图像输出,使用默认参数
  231. $img->effect[] = new render\effect\NoiseReducerOIDN(); // 添加降噪效果管线,使用默认参数
  232. $img->effect[] = new render\effect\Pretreatment(); //
  233. $img->effect[] = new render\effect\ToneMapLinear(); // 添加一亮度效果管线,使用默认参数
  234. $img->effect[] = new render\effect\Synthesis(); // 添加一个合成效果管线,使用默认参数
  235. $img->effect[] = new render\effect\BackgroundImg( // 添加一个背景图像,使用默认参数
  236. [
  237. 'file' => "./map/纯白242.png"
  238. ]
  239. );
  240. $img->effect[] = new render\effect\CammaCorrection(); // 添加一个 gamma 校正,,使用默认参数
  241. $film->addImage($img); // 将该图像输出添加到渲染胶片
  242. $render .= $film;
  243. // 五、输出场景文件 scene.scn,及渲染文件 render.cfg
  244. echo $scene;
  245. $handle = fopen( "./DemoScene/scene.scn",'w+');
  246. fwrite($handle,$scene);
  247. fclose($handle);
  248. echo $render ;
  249. $handle = fopen( "./DemoScene/render.cfg",'w+');
  250. fwrite($handle,$render);
  251. fclose($handle);
  252. // // 六、启动渲染器
  253. // $cmd = "luxcoreconsole -o ./DemoScene/render.cfg -f ./DemoScene/scene.scn 1>./DemoScene/render.log 2>&1";
  254. // $output;
  255. // $returnVar;
  256. // exec($cmd,$output,$returnVar);
  257. // if( $returnVar == 127 ){
  258. // throw new \Exception("Please install the luxcorerender bin file in the system directory correctly,Or add to the system environment variable");
  259. // }
  260. ?>