From 9b1a6143959fa6a3a7bbfa388d388da8e01ef732 Mon Sep 17 00:00:00 2001 From: yuanjiajia <1139393632@qq.com> Date: Thu, 13 Jul 2023 09:31:03 +0800 Subject: [PATCH] =?UTF-8?q?MatHelper.php=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/MatHelper.php | 130 ++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/src/utils/MatHelper.php b/src/utils/MatHelper.php index 4f3c88b..7cd2966 100644 --- a/src/utils/MatHelper.php +++ b/src/utils/MatHelper.php @@ -18,6 +18,19 @@ class MatHelper{ const OPEN = 1 | true; const CLOSE = 0 | false; + /** + * 十六进制颜色 转换为 浮点数颜色 + */ + static function hex2floatColor($hexColor, $scale = 1){ + $floatColor = ''; + $hexColor = trim($hexColor); + $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; + } + /** * 创建一个 Disney 的材质 */ @@ -64,8 +77,8 @@ class MatHelper{ if($matOriginal->matParams->albedoTexture){ $textureUrl = $matOriginal->matParams->albedoTexture; $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl); - $textureHandle = fopen($textureSavaPath, "w"); - (new Client())->get($textureUrl, [RequestOptions::SINK => $textureHandle]); + $textureSavaHandle = fopen($textureSavaPath, "w"); + (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]); $albedoTexture = new ImageMap( ['file' => $textureSavaPath] @@ -77,19 +90,19 @@ class MatHelper{ // 加载金属度纹理、粗糙度纹理 if($matOriginal->matParams->metallicTexture){ - $metallicTextureUrl = $matOriginal->matParams->metallicTexture; - $metallicTextureSavaPath = $cacheFileDir.'/'.basename($metallicTextureUrl); - $metallicTextureHandle = fopen($metallicTextureSavaPath, "w"); - (new Client())->get($metallicTextureUrl, [RequestOptions::SINK => $metallicTextureHandle]); + $textureUrl = $matOriginal->matParams->metallicTexture; + $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl); + $textureSavaHandle = fopen($textureSavaPath, "w"); + (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]); - $fileName = preg_replace('/(\.[^\.]+)$/','',basename($metallicTextureUrl)); - $extension = preg_replace('/^('.$fileName.')+/','',basename($metallicTextureUrl)); + $fileName = preg_replace('/(\.[^\.]+)$/','',basename($textureSavaPath)); + $extension = preg_replace('/^('.$fileName.')+/','',basename($textureSavaPath)); // 从金属纹理中 分离 蓝色通道 作为 金属度纹理 $metalnessTextureSavaPath = $cacheFileDir.'/'.$fileName.'_metalness'.$extension; - $metalnessTexture = new Imagick($metallicTextureSavaPath); - $metalnessTexture->separateImageChannel(Imagick::CHANNEL_BLUE); - $metalnessTexture->setImageCompressionQuality(100); - $metalnessTexture->writeImage($metalnessTextureSavaPath); + $imagick = new Imagick($textureSavaPath); + $imagick->separateImageChannel(Imagick::CHANNEL_BLUE); + $imagick->setImageCompressionQuality(100); + $imagick->writeImage($metalnessTextureSavaPath); $metalnessTexture = new ImageMap( ['file' => $metalnessTextureSavaPath] ); @@ -99,10 +112,10 @@ class MatHelper{ $material->setMetallic($metalnessTexture); // 从金属纹理中 分离 绿色通道 作为 粗糙度纹理 $roughnessTextureSavaPath = $cacheFileDir.'/'.$fileName.'_roughness'.$extension; - $roughnessTexture = new Imagick($metallicTextureSavaPath); - $roughnessTexture->separateImageChannel(Imagick::CHANNEL_GREEN); - $roughnessTexture->setImageCompressionQuality(100); - $roughnessTexture->writeImage($roughnessTextureSavaPath); + $imagick = new Imagick($textureSavaPath); + $imagick->separateImageChannel(Imagick::CHANNEL_GREEN); + $imagick->setImageCompressionQuality(100); + $imagick->writeImage($roughnessTextureSavaPath); $roughnessTexture = new ImageMap( ['file' => $roughnessTextureSavaPath] ); @@ -116,14 +129,14 @@ class MatHelper{ if($matOriginal->matParams->bumpTexture){ $textureUrl = $matOriginal->matParams->bumpTexture; $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl); - $textureHandle = fopen($textureSavaPath, "w"); - (new Client())->get($textureUrl, [RequestOptions::SINK => $textureHandle]); + $textureSavaHandle = fopen($textureSavaPath, "w"); + (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]); // 因为 LuxCore 使用 opengl 规范的法线纹理格式,与 babylon 使用的 DirectX 不同, // 所以这里需要反转 G 通道色值,以保证法线纹理应用效果与 babylon 一致。 $fileName = preg_replace('/(\.[^\.]+)$/','',basename($textureSavaPath)); $extension = preg_replace('/^('.$fileName.')+/','',basename($textureSavaPath)); - $textureInvertGPath = $cacheFileDir.'/'.$fileName.'_InvertG'.$extension; + $textureInvertSavaGPath = $cacheFileDir.'/'.$fileName.'_InvertG'.$extension; $imagick = new Imagick($textureSavaPath); $imageIterator = $imagick->getPixelIterator(); foreach ($imageIterator as $row => $pixels) { @@ -140,10 +153,10 @@ class MatHelper{ $imageIterator->syncIterator(); } $imagick->setImageCompressionQuality(100); - $imagick->writeImage($textureInvertGPath); + $imagick->writeImage($textureInvertSavaGPath); $bumpTexture = new ImageMap( - ['file' => $textureInvertGPath] + ['file' => $textureInvertSavaGPath] ); $bumpTexture->gamma = 1; $bumpTexture->mapping = $mapping; @@ -159,15 +172,15 @@ class MatHelper{ // 加载清漆涂层纹理 if($matOriginal->matParams->isClearCoatEnabled === self::OPEN && $matOriginal->matParams->clearCoatTexture){ $textureUrl = $matOriginal->matParams->clearCoatTexture; - $savaTexturePath = $cacheFileDir.'/'.basename($textureUrl); - $savaTextureHandle = fopen($savaTexturePath, "w"); - (new Client())->get($textureUrl, [RequestOptions::SINK => $savaTextureHandle]); + $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl); + $textureSavaHandle = fopen($textureSavaPath, "w"); + (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]); - $fileName = preg_replace('/(\.[^\.]+)$/','',basename($savaTexturePath)); - $extension = preg_replace('/^('.$fileName.')+/','',basename($savaTexturePath)); + $fileName = preg_replace('/(\.[^\.]+)$/','',basename($textureSavaPath)); + $extension = preg_replace('/^('.$fileName.')+/','',basename($textureSavaPath)); // 从清漆涂层纹理中 分离 红色通道 作为 清漆涂层强度纹理 $coatIntensityTextureSavaPath = $cacheFileDir.'/'.$fileName.'_coatIntensity'.$extension; - $imagick = new Imagick($savaTexturePath); + $imagick = new Imagick($textureSavaPath); $imagick->separateImageChannel(Imagick::CHANNEL_RED); $imagick->setImageCompressionQuality(100); $imagick->writeImage($coatIntensityTextureSavaPath); @@ -180,7 +193,7 @@ class MatHelper{ $material->setClearcoat($coatIntensityTexture); // 从清漆涂层纹理中 分离 绿色通道 作为 清漆涂层光泽度纹理 $coatGlossTextureSavaPath = $cacheFileDir.'/'.$fileName.'_coatGloss'.$extension; - $imagick = new Imagick($savaTexturePath); + $imagick = new Imagick($textureSavaPath); $imagick->separateImageChannel(Imagick::CHANNEL_GREEN); $imagick->setImageCompressionQuality(100); $imagick->writeImage($coatGlossTextureSavaPath); @@ -201,15 +214,15 @@ class MatHelper{ // 加载镀膜材质纹理 if($matOriginal->matParams->isFilmEnabled === self::OPEN && $matOriginal->matParams->filmTexture){ $textureUrl = $matOriginal->matParams->filmTexture; - $savaTexturePath = $cacheFileDir.'/'.basename($textureUrl); - $savaTextureHandle = fopen($savaTexturePath, "w"); - (new Client())->get($textureUrl, [RequestOptions::SINK => $savaTextureHandle]); + $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl); + $textureSavaHandle = fopen($textureSavaPath, "w"); + (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]); - $fileName = preg_replace('/(\.[^\.]+)$/','',basename($savaTexturePath)); - $extension = preg_replace('/^('.$fileName.')+/','',basename($savaTexturePath)); + $fileName = preg_replace('/(\.[^\.]+)$/','',basename($textureSavaPath)); + $extension = preg_replace('/^('.$fileName.')+/','',basename($textureSavaPath)); // 从镀膜材质纹理中 分离 红色通道 作为 镀膜材质强度纹理 $filmIntensityTextureSavaPath = $cacheFileDir.'/'.$fileName.'_filmIntensity'.$extension; - $imagick = new Imagick($savaTexturePath); + $imagick = new Imagick($textureSavaPath); $imagick->separateImageChannel(Imagick::CHANNEL_RED); $imagick->setImageCompressionQuality(100); $imagick->writeImage($filmIntensityTextureSavaPath); @@ -262,8 +275,8 @@ class MatHelper{ if($matOriginal->matParams->refractivityTexture){ $textureUrl = $matOriginal->matParams->refractivityTexture; $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl); - $textureHandle = fopen($textureSavaPath, "w"); - (new Client())->get($textureUrl, [RequestOptions::SINK => $textureHandle]); + $textureSavaHandle = fopen($textureSavaPath, "w"); + (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]); $imageMap = new ImageMap( ['file' => $textureSavaPath] @@ -275,16 +288,16 @@ class MatHelper{ // 加载金属度纹理、粗糙度纹理 if($matOriginal->matParams->metallicTexture){ - $metallicTextureUrl = $matOriginal->matParams->metallicTexture; - $metallicTextureSavaPath = $cacheFileDir.'/'.basename($metallicTextureUrl); - $metallicTextureHandle = fopen($metallicTextureSavaPath, "w"); - (new Client())->get($metallicTextureUrl, [RequestOptions::SINK => $metallicTextureHandle]); + $textureUrl = $matOriginal->matParams->metallicTexture; + $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl); + $textureSavaHandle = fopen($textureSavaPath, "w"); + (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]); - $fileName = preg_replace('/(\.[^\.]+)$/','',basename($metallicTextureSavaPath)); - $extension = preg_replace('/^('.$fileName.')+/','',basename($metallicTextureSavaPath)); + $fileName = preg_replace('/(\.[^\.]+)$/','',basename($textureSavaPath)); + $extension = preg_replace('/^('.$fileName.')+/','',basename($textureSavaPath)); // 从金属纹理中 分离 绿色通道 作为 粗糙度纹理 $roughnessTextureSavaPath = $cacheFileDir.'/'.$fileName.'_roughness'.$extension; - $imagick = new Imagick($metallicTextureSavaPath); + $imagick = new Imagick($textureSavaPath); $imagick->separateImageChannel(Imagick::CHANNEL_GREEN); $imagick->setImageCompressionQuality(100); $imagick->writeImage($roughnessTextureSavaPath); @@ -337,8 +350,8 @@ class MatHelper{ // if($matOriginal->matParams->translucencyTexture){ // $textureUrl = $matOriginal->matParams->translucencyTexture; // $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl); - // $textureHandle = fopen($textureSavaPath, "w"); - // (new Client())->get($textureUrl, [RequestOptions::SINK => $textureHandle]); + // $textureSavaHandle = fopen($textureSavaPath, "w"); + // (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]); // $imageMap = new ImageMap( // ['file' => $textureSavaPath] @@ -350,16 +363,16 @@ class MatHelper{ // 加载金属度纹理、粗糙度纹理 if($matOriginal->matParams->metallicTexture){ - $metallicTextureUrl = $matOriginal->matParams->metallicTexture; - $metallicTextureSavaPath = $cacheFileDir.'/'.basename($metallicTextureUrl); - $metallicTextureHandle = fopen($metallicTextureSavaPath, "w"); - (new Client())->get($metallicTextureUrl, [RequestOptions::SINK => $metallicTextureHandle]); + $textureUrl = $matOriginal->matParams->metallicTexture; + $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl); + $textureSavaHandle = fopen($textureSavaPath, "w"); + (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]); - $fileName = preg_replace('/(\.[^\.]+)$/','',basename($metallicTextureSavaPath)); - $extension = preg_replace('/^('.$fileName.')+/','',basename($metallicTextureSavaPath)); + $fileName = preg_replace('/(\.[^\.]+)$/','',basename($textureSavaPath)); + $extension = preg_replace('/^('.$fileName.')+/','',basename($textureSavaPath)); // 从金属纹理中 分离 绿色通道 作为 粗糙度纹理 $roughnessTextureSavaPath = $cacheFileDir.'/'.$fileName.'_roughness'.$extension; - $imagick = new Imagick($metallicTextureSavaPath); + $imagick = new Imagick($textureSavaPath); $imagick->separateImageChannel(Imagick::CHANNEL_GREEN); $imagick->setImageCompressionQuality(100); $imagick->writeImage($roughnessTextureSavaPath); @@ -385,19 +398,6 @@ class MatHelper{ } - - /** - * 十六进制颜色 转换为 浮点数颜色 - */ - static function hex2floatColor($hexColor, $scale = 1){ - $floatColor = ''; - $hexColor = trim($hexColor); - $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; - } } ?>