Browse Source

渲染测式 开发

master
yuanjiajia 1 year ago
parent
commit
8dfe72967a
  1. 73
      src/utils/MatHelper.php

73
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);
}
// 加载 粗糙度 纹理

Loading…
Cancel
Save