From 8a33d895d21d16c90200cc6ecccc395495edaad6 Mon Sep 17 00:00:00 2001 From: yuanjiajia <1139393632@qq.com> Date: Mon, 5 Jun 2023 19:03:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=B2=E6=9F=93=E6=B5=8B=E5=BC=8F=20?= =?UTF-8?q?=E6=A1=88=E4=BE=8B=20=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/renderTest.php | 25 ++--- src/scene/texture/mapping/Mapping.php | 9 +- src/scene/texture/transform/NormalMap.php | 2 +- src/utils/MatHelper.php | 106 +++++++++++++++++++++- 4 files changed, 121 insertions(+), 21 deletions(-) diff --git a/examples/renderTest.php b/examples/renderTest.php index 3d85079..26ad4c3 100644 --- a/examples/renderTest.php +++ b/examples/renderTest.php @@ -9,8 +9,12 @@ use GuzzleHttp\Client; include dirname(dirname(__FILE__)) . "/vendor/autoload.php"; -const OPEN = true; -const CLOSE = false; +const OPEN = true | 1; +const CLOSE = false | 0; +/** + * 标准材质长宽 + */ +const MAT_STANDARD_SIZE = 1000; @@ -18,11 +22,9 @@ $client = new Client(); $response = $client->get("deep3d.backend-api.dev.com/test/render-task"); $resContents = $response->getBody()->getContents(); $result = json_decode($resContents,false); - $taskData = $result->data; $taskScene = $taskData->scene; $taskModel = $taskScene->model; -// print_r($taskModel); @@ -30,10 +32,10 @@ $taskModel = $taskScene->model; /** * 创建个 Luxcore 材质 */ -function createLuxcoreMat($matParams){ - switch($matParams->renderType){ +function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $scene){ + switch($matOriginal->renderType){ case 1: - return MatHelper::createMatOfDisney($matParams); + return MatHelper::createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $scene); case 2: return; case 3: @@ -186,9 +188,8 @@ $scene->registerObjects($obj); // 二、添加要渲染的模型 foreach($taskModel->childsParams as $childParams){ - $material = null; - $matParams = (function ($childParams){ + $matOriginal = (function ($childParams){ if(is_object($childParams->customMat)){ return $childParams->customMat; }else if(is_object($childParams->fixedMat)){ @@ -197,8 +198,10 @@ foreach($taskModel->childsParams as $childParams){ return $childParams->defaultMat; } })($childParams); - if(is_object($matParams)){ - $material = createLuxcoreMat($matParams,$scene); + $textureScaleU = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->width); + $textureScaleV = $taskModel->uvScale*(MAT_STANDARD_SIZE/$matOriginal->height); + if(is_object($matOriginal)){ + $material = createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $scene); }else{ $material = new materials\Disney(); $material->setBaseColor('1 1 1'); diff --git a/src/scene/texture/mapping/Mapping.php b/src/scene/texture/mapping/Mapping.php index 3844ef0..a2a7c48 100644 --- a/src/scene/texture/mapping/Mapping.php +++ b/src/scene/texture/mapping/Mapping.php @@ -13,8 +13,8 @@ class Mapping extends BaseCfg const TYPE_UVMAPPING2D = 'uvmapping2d'; // 以模型的UV坐标,调整贴图变换参数的类型 const TYPE_UVRANDOMMAPPING2D = 'uvrandommapping2d'; // 以模型的UV坐标,调整贴图随机变换参数的类型 const TYPE_LOCALMAPPING3D = 'localmapping3d'; // 以模型的本地坐标,调整贴图三维变换参数的类型 - const TYPE_GLOBALMAPPING3D = 'globalmapping3d'; // 以世界坐标,调整贴图三维变换参数的类型 const TYPE_LOCALRANDOMMAPPING3D = 'localrandommapping3d'; // 以模型的本地坐标,调整贴图三维随机变换参数的类型 + const TYPE_GLOBALMAPPING3D = 'globalmapping3d'; // 以世界坐标,调整贴图三维变换参数的类型 const TYPE_UVMAPPING3D = 'uvmapping3d'; // 以模型的UV坐标,调整贴图三维变换参数的类型 @@ -115,14 +115,12 @@ class Mapping extends BaseCfg private $ytranslate; private $ztranslate; - public function useUVMapping2d - ( + public function useUVMapping2d( int $uvindex = 0, string $rotation = '0', string $uvscale = '0', string $uvdelta = '0' - ) - { + ){ $this->type = self::TYPE_UVMAPPING2D; $this->uvindex = $uvindex; $this->rotation = $rotation; @@ -154,7 +152,6 @@ class Mapping extends BaseCfg { // 等完善 } - } ?> diff --git a/src/scene/texture/transform/NormalMap.php b/src/scene/texture/transform/NormalMap.php index 79039e1..27161e1 100644 --- a/src/scene/texture/transform/NormalMap.php +++ b/src/scene/texture/transform/NormalMap.php @@ -5,7 +5,7 @@ use Blobt\Luxcore\scene\texture\TextureBase; use Blobt\Luxcore\scene\Scene; use Blobt\Luxcore\core\Base; -class ImageMap extends TextureBase +class NormalMap extends TextureBase { /** diff --git a/src/utils/MatHelper.php b/src/utils/MatHelper.php index 9ed9335..e92f6e0 100644 --- a/src/utils/MatHelper.php +++ b/src/utils/MatHelper.php @@ -2,16 +2,99 @@ namespace Blobt\Luxcore\utils; +use Blobt\Luxcore\scene\materials\Disney; +use Blobt\Luxcore\scene\texture\mapping\Mapping; +use Blobt\Luxcore\scene\texture\procedural\ImageMap; +use Blobt\Luxcore\scene\texture\transform\NormalMap; + +class MatHelper{ + + const OPEN = 1 | true; + const CLOSE = 0 | false; -class MatHelper -{ /** * 创建一个 Disney 的材质 */ - static function createMatOfDisney($matParams){ + static function createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $scene){ + + $material = new Disney(); + $material->setBaseColor(self::hex2floatColor($matOriginal->matParams->albedoColor)); + $material->setMetallic($matOriginal->matParams->metallic); + $material->setRoughness($matOriginal->matParams->roughness); + if($matOriginal->matParams->isClearCoatEnabled === self::OPEN){ + $material->setClearcoat($matOriginal->matParams->clearCoat); + $material->setClearcoatgloss(1 - $matOriginal->matParams->clearCoat); + } + if($matOriginal->matParams->isFilmEnabled === self::OPEN){ + $material->setFilmamount($matOriginal->matParams->film); + $material->setFilmthickness($matOriginal->matParams->filmthickness); + $material->setFilmior($matOriginal->matParams->filmIor); + } + if($matOriginal->matParams->isSheenEnabled === self::OPEN){ + $material->setSheen(($matOriginal->matParams->sheen)*100); + } + if($matOriginal->matParams->isAnisotropyEnabled === self::OPEN){ + $material->setAnisotropic($matOriginal->matParams->anisotropy); + } + + + // 创建一个铺贴对象 + $mapping = new Mapping(); + // 使用"uvmapping2d"类型的铺贴对象 + $mapping->useUVMapping2d( + // uv通道 + 0, + // 旋转角度 + "0", + // 缩放比例 + `$textureScaleU $textureScaleV`, + // 偏移量 + "0 0", + ); + if($matOriginal->matParams->albedoTexture){ + $albedoTexture = new ImageMap( + ['file' => $matOriginal->matParams->albedoTexture] + ); + $albedoTexture->mapping = $mapping; + $scene->registerTexture($albedoTexture); + $material->setBaseColor($albedoTexture); + } + if($matOriginal->matParams->metallicTexture){ + $metallicTexture = new ImageMap( + ['file' => $matOriginal->matParams->metallicTexture] + ); + $metallicTexture->mapping = $mapping; + $scene->registerTexture($metallicTexture); + $material->setMetallic($metallicTexture); + + $roughnessTexture = new ImageMap( + ['file' => $matOriginal->matParams->metallicTexture] + ); + $roughnessTexture->mapping = $mapping; + $scene->registerTexture($roughnessTexture); + $material->setMetallic($roughnessTexture); + } + if($matOriginal->matParams->bumpTexture){ + $bumpTexture = new ImageMap( + ['file' => $matOriginal->matParams->bumpTexture] + ); + $bumpTexture->mapping = $mapping; + $scene->registerTexture($bumpTexture); + + $normalTexture = new NormalMap(); + $normalTexture->texture = $bumpTexture; + $normalTexture->scale = $matOriginal->matParams->bumpLevel; + $scene->registerTexture($normalTexture); + $material->setBumptex($normalTexture); + } + + //TODO: 更多通道贴图处理,待后续完善 + + return $material; } + /** * 创建一个 Glass 材质(折射材质) */ @@ -19,18 +102,35 @@ class MatHelper } + /** * 创建一个半透明材质 */ static function createMatOfTranslucent(){ } + + /** * 创建一个自发光材质 */ static function createMatOfEmission(){ } + + + /** + * 十六进制颜色 转换为 浮点数颜色 + */ + static function hex2floatColor($hexColor){ + $floatColor = ''; + $hexColor = trim($hexColor); + $r = hexdec(substr($hexColor, 1, 2))/256; + $g = hexdec(substr($hexColor, 3, 2))/256; + $b = hexdec(substr($hexColor, 5, 2))/256; + $floatColor = `$r $g $b`; + return $floatColor; + } } ?>