From 8f34e8a62ad3a50aa65d47600b074fc23ed438e0 Mon Sep 17 00:00:00 2001 From: yuanjiajia <1139393632@qq.com> Date: Wed, 12 Jul 2023 18:12:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=B2=E6=9F=93=E6=B5=8B=E5=BC=8F=20?= =?UTF-8?q?=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/matRenderTest.php | 2 +- examples/renderTest.php | 2 +- src/utils/MatHelper.php | 78 +++++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/examples/matRenderTest.php b/examples/matRenderTest.php index ed46ef0..6c15015 100644 --- a/examples/matRenderTest.php +++ b/examples/matRenderTest.php @@ -68,7 +68,7 @@ function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $cacheFi case 2: return MatHelper::createMatOfGlass($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene); case 3: - return; + return MatHelper::createMatOfTranslucent($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene); case 4: return; } diff --git a/examples/renderTest.php b/examples/renderTest.php index 0015649..54126c8 100644 --- a/examples/renderTest.php +++ b/examples/renderTest.php @@ -68,7 +68,7 @@ function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $cacheFi case 2: return MatHelper::createMatOfGlass($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene); case 3: - return; + return MatHelper::createMatOfTranslucent($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene); case 4: return; } diff --git a/src/utils/MatHelper.php b/src/utils/MatHelper.php index 57f723d..dcaab9c 100644 --- a/src/utils/MatHelper.php +++ b/src/utils/MatHelper.php @@ -4,6 +4,7 @@ namespace Blobt\Luxcore\utils; use Blobt\Luxcore\scene\materials\Disney; use Blobt\Luxcore\scene\materials\GlassRough; +use Blobt\Luxcore\scene\materials\GlossyTranslucent; use Blobt\Luxcore\scene\texture\composite\Subtract; use Blobt\Luxcore\scene\texture\mapping\Mapping; use Blobt\Luxcore\scene\texture\procedural\ImageMap; @@ -305,8 +306,75 @@ class MatHelper{ /** * 创建一个半透明材质 */ - static function createMatOfTranslucent(){ + static function createMatOfTranslucent($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene){ + $material = new GlossyTranslucent(); + $material->setTransmission( + self::hex2floatColor($matOriginal->matParams->translucencyColor), + $matOriginal->matParams->translucencyIntensity + ); + $material->setBaseColor( + self::hex2floatColor($matOriginal->matParams->translucencyColor), + 1 - $matOriginal->matParams->translucencyIntensity + ); + $material->setUroughness($matOriginal->matParams->roughness); + $material->setVroughness($matOriginal->matParams->roughness); + + + //创建一个铺贴对象,使用"uvmapping2d"类型的铺贴对象 + $mapping = new Mapping(); + $mapping->useUVMapping2d( + // uv通道 + 0, + // 旋转角度 + "0", + // 缩放比例 + "{$textureScaleU} {$textureScaleV}", + // 偏移量 + "0 0", + ); + + // // 加载 透射 纹理 + // if($matOriginal->matParams->translucencyTexture){ + // $textureUrl = $matOriginal->matParams->translucencyTexture; + // $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl); + // $textureHandle = fopen($textureSavaPath, "w"); + // (new Client())->get($textureUrl, [RequestOptions::SINK => $textureHandle]); + + // $imageMap = new ImageMap( + // ['file' => $textureSavaPath] + // ); + // $imageMap->mapping = $mapping; + // $scene->registerTexture($imageMap); + // $material->setTransmission($imageMap); + // } + + // 加载金属度纹理、粗糙度纹理 + if($matOriginal->matParams->metallicTexture){ + $metallicTextureUrl = $matOriginal->matParams->metallicTexture; + $metallicTextureSavaPath = $cacheFileDir.'/'.basename($metallicTextureUrl); + $metallicTextureHandle = fopen($metallicTextureSavaPath, "w"); + (new Client())->get($metallicTextureUrl, [RequestOptions::SINK => $metallicTextureHandle]); + $fileName = preg_replace('/(\.[^\.]+)$/','',basename($metallicTextureSavaPath)); + $extension = preg_replace('/^('.$fileName.')+/','',basename($metallicTextureSavaPath)); + // 从金属纹理中 分离 绿色通道 作为 粗糙度纹理 + $roughnessTextureSavaPath = $cacheFileDir.'/'.$fileName.'_roughness'.$extension; + $imagick = new Imagick($metallicTextureSavaPath); + $imagick->separateImageChannel(Imagick::CHANNEL_GREEN); + $imagick->setImageCompressionQuality(100); + $imagick->writeImage($roughnessTextureSavaPath); + $roughnessTexture = new ImageMap( + ['file' => $roughnessTextureSavaPath] + ); + $roughnessTexture->gamma = 1; + $roughnessTexture->mapping = $mapping; + $scene->registerTexture($roughnessTexture); + $material->setUroughness($roughnessTexture); + $material->setVroughness($roughnessTexture); + } + + + return $material; } @@ -321,12 +389,12 @@ class MatHelper{ /** * 十六进制颜色 转换为 浮点数颜色 */ - static function hex2floatColor($hexColor){ + static function hex2floatColor($hexColor, $scale = 1){ $floatColor = ''; $hexColor = trim($hexColor); - $r = (hexdec(substr($hexColor, 1, 2))/255) ** 2.2; - $g = (hexdec(substr($hexColor, 3, 2))/255) ** 2.2; - $b = (hexdec(substr($hexColor, 5, 2))/255) ** 2.2; + $r = (hexdec(substr($hexColor, 1, 2))/255) ** 2.2 * $scale; + $g = (hexdec(substr($hexColor, 3, 2))/255) ** 2.2 * $scale; + $b = (hexdec(substr($hexColor, 5, 2))/255) ** 2.2 * $scale; $floatColor = "$r $g $b"; return $floatColor; }