Browse Source

渲染测式 开发

master
yuanjiajia 1 year ago
parent
commit
16accc0755
  1. 87
      src/utils/MatHelper.php

87
src/utils/MatHelper.php

@ -18,6 +18,9 @@ class MatHelper{
const OPEN = 1 | true; const OPEN = 1 | true;
const CLOSE = 0 | false; const CLOSE = 0 | false;
/** /**
* 十六进制颜色 转换为 浮点数颜色 * 十六进制颜色 转换为 浮点数颜色
*/ */
@ -31,6 +34,9 @@ class MatHelper{
return $floatColor; return $floatColor;
} }
/** /**
* 创建一个 Disney 的材质 * 创建一个 Disney 的材质
*/ */
@ -247,6 +253,8 @@ class MatHelper{
} }
/** /**
* 创建一个 Glass 材质(折射材质) * 创建一个 Glass 材质(折射材质)
*/ */
@ -316,18 +324,21 @@ class MatHelper{
} }
/** /**
* 创建一个半透明材质 * 创建一个半透明材质
*/ */
static function createMatOfTranslucent($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene){ static function createMatOfTranslucent($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene){
$material = new GlossyTranslucent(); $material = new GlossyTranslucent();
$translucencyIntensity = $matOriginal->matParams->translucencyIntensity;
$material->setTransmission( self::hex2floatColor( $material->setTransmission( self::hex2floatColor(
$matOriginal->matParams->translucencyColor, $matOriginal->matParams->translucencyColor,
$matOriginal->matParams->translucencyIntensity
$translucencyIntensity
)); ));
$material->setBaseColor( self::hex2floatColor( $material->setBaseColor( self::hex2floatColor(
$matOriginal->matParams->translucencyColor, $matOriginal->matParams->translucencyColor,
1 - $matOriginal->matParams->translucencyIntensity
1 - $translucencyIntensity
)); ));
$material->setUroughness($matOriginal->matParams->roughness); $material->setUroughness($matOriginal->matParams->roughness);
$material->setVroughness($matOriginal->matParams->roughness); $material->setVroughness($matOriginal->matParams->roughness);
@ -346,20 +357,64 @@ class MatHelper{
"0 0", "0 0",
); );
// // 加载 透射 纹理
// if($matOriginal->matParams->translucencyTexture){
// $textureUrl = $matOriginal->matParams->translucencyTexture;
// $textureSavaPath = $cacheFileDir.'/'.basename($textureUrl);
// $textureSavaHandle = fopen($textureSavaPath, "w");
// (new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]);
// $imageMap = new ImageMap(
// ['file' => $textureSavaPath]
// );
// $imageMap->mapping = $mapping;
// $scene->registerTexture($imageMap);
// $material->setTransmission($imageMap);
// }
// 加载 透射 纹理
if($matOriginal->matParams->translucencyTexture){
$textureUrl = $matOriginal->matParams->translucencyTexture;
$textureSavaPath = $cacheFileDir.'/'.basename($textureUrl);
$textureSavaHandle = fopen($textureSavaPath, "w");
(new Client())->get($textureUrl, [RequestOptions::SINK => $textureSavaHandle]);
$fileName = preg_replace('/(\.[^\.]+)$/','',basename($textureSavaPath));
$extension = preg_replace('/^('.$fileName.')+/','',basename($textureSavaPath));
// 生成 漫反射颜色 纹理
$diffuseReflectionTextureSavaPath = $cacheFileDir.'/'.$fileName.'_diffuseReflection'.$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"] * (1 - $translucencyIntensity));
$g = round($value["g"] * (1 - $translucencyIntensity));
$b = round($value["b"] * (1 - $translucencyIntensity));
$a = $value["a"];
$pixel->setColor("rgba({$r}, {$g}, {$b}, {$a})");
}
$imageIterator->syncIterator();
}
$imagick->setImageCompressionQuality(100);
$imagick->writeImage($diffuseReflectionTextureSavaPath);
$diffuseReflectionTexture = new ImageMap(
['file' => $diffuseReflectionTextureSavaPath]
);
$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);
}
// 加载金属度纹理、粗糙度纹理 // 加载金属度纹理、粗糙度纹理
if($matOriginal->matParams->metallicTexture){ if($matOriginal->matParams->metallicTexture){

Loading…
Cancel
Save