From 8dfe72967a83fa6892ea8e4d2344b887152c98ae Mon Sep 17 00:00:00 2001 From: yuanjiajia <1139393632@qq.com> Date: Fri, 21 Jul 2023 16:17:18 +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 --- src/utils/MatHelper.php | 73 ++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/src/utils/MatHelper.php b/src/utils/MatHelper.php index 0bdd6ef..b2f46af 100644 --- a/src/utils/MatHelper.php +++ b/src/utils/MatHelper.php @@ -14,7 +14,8 @@ use GuzzleHttp\RequestOptions; use Imagick; use OzdemirBurak\Iris\Color\Factory; use OzdemirBurak\Iris\Color\Hsv; - +use OzdemirBurak\Iris\Color\Rgb; +use OzdemirBurak\Iris\Color\Rgba; class MatHelper{ @@ -60,6 +61,32 @@ class MatHelper{ return $hexColor; } + /** + * rgb色 转 十六进制颜色 + */ + static function rgbToHexColor($rgbColor, $scale = 1){ + $color = Factory::init(trim($rgbColor)); + $r = round($color->values()[0] * $scale); + $g = round($color->values()[1] * $scale); + $b = round($color->values()[2] * $scale); + $hexColor = (new Rgb("rgb({$r}, {$g}, {$b})"))->toHex(); + return trim($hexColor); + } + + /** + * hexa颜色 转 rgba色 + */ + static function hexaToRgbaColor($hexaColor, $scale = 1, $alphaScale = 1){ + $color = Factory::init(trim($hexaColor)); + $rgbaColor = $color->toRgba(); + $r = round($rgbaColor->values()[0] * $scale); + $g = round($rgbaColor->values()[1] * $scale); + $b = round($rgbaColor->values()[2] * $scale); + $a = $rgbaColor->values()[3] * $alphaScale; + $rgbaColor = new Rgba("rgba({$r}, {$g}, {$b}, {$a})"); + return trim($rgbaColor); + } + /** * 创建一个 Disney 的材质 @@ -475,9 +502,17 @@ class MatHelper{ $textureSavaHandle = fopen($textureSavaPath, "w"); (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]); + // 生成 透射 纹理 + $transmissionTexture = new ImageMap( + ['file' => $textureSavaPath] + ); + $transmissionTexture->mapping = $mapping; + $scene->registerTexture($transmissionTexture); + $material->setTransmission($transmissionTexture); + + // 生成 漫反射颜色 纹理 $fileName = preg_replace('/(\.[^\.]+)$/','',basename($textureSavaPath)); $extension = preg_replace('/^('.$fileName.')+/','',basename($textureSavaPath)); - // 生成 漫反射颜色 纹理 $diffuseReflectionTextureSavaPath = $cacheFileDir.'/'.$fileName.'_diffuseReflection'.$extension; $imagick = new Imagick($textureSavaPath); $imageIterator = $imagick->getPixelIterator(); @@ -485,11 +520,11 @@ class MatHelper{ foreach ($pixels as $column => $pixel) { // 获取每个像素色值; $value = $pixel->getColor(); - $r = round($value["r"] * (1 - $translucencyIntensity)); - $g = round($value["g"] * (1 - $translucencyIntensity)); - $b = round($value["b"] * (1 - $translucencyIntensity)); - $a = $value["a"]; - $pixel->setColor("rgba({$r}, {$g}, {$b}, {$a})"); + $hexColor = self::rgbToHexColor("rgb({$value["r"]}, {$value["g"]}, {$value["b"]})"); + $hexColor = self::colorMaxBrightness($hexColor,$translucencyIntensity ** (1/2.2)); + $hexaColor = $hexColor . dechex(round($value["a"] * 255)); + $rgbaColor = self::hexaToRgbaColor($hexaColor, 1 - $translucencyIntensity); + $pixel->setColor($rgbaColor); } $imageIterator->syncIterator(); } @@ -501,30 +536,6 @@ class MatHelper{ $diffuseReflectionTexture->mapping = $mapping; $scene->registerTexture($diffuseReflectionTexture); $material->setBaseColor($diffuseReflectionTexture); - // 生成 透射 纹理 - $transmissionTextureSavaPath = $cacheFileDir.'/'.$fileName.'_transmission'.$extension; - $imagick = new Imagick($textureSavaPath); - $imageIterator = $imagick->getPixelIterator(); - foreach ($imageIterator as $row => $pixels) { - foreach ($pixels as $column => $pixel) { - // 获取每个像素色值; - $value = $pixel->getColor(); - $r = round($value["r"] * $translucencyIntensity); - $g = round($value["g"] * $translucencyIntensity); - $b = round($value["b"] * $translucencyIntensity); - $a = $value["a"]; - $pixel->setColor("rgba({$r}, {$g}, {$b}, {$a})"); - } - $imageIterator->syncIterator(); - } - $imagick->setImageCompressionQuality(100); - $imagick->writeImage($transmissionTextureSavaPath); - $transmissionTexture = new ImageMap( - ['file' => $transmissionTextureSavaPath] - ); - $transmissionTexture->mapping = $mapping; - $scene->registerTexture($transmissionTexture); - $material->setTransmission($transmissionTexture); } // 加载 粗糙度 纹理