diff --git a/examples/matRenderTest.php b/examples/matRenderTest.php index 2591020..aca1058 100644 --- a/examples/matRenderTest.php +++ b/examples/matRenderTest.php @@ -66,6 +66,7 @@ function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $cacheFi case 1: return MatHelper::createMatOfDisney($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene); case 2: + return MatHelper::createMatOfGlass($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene); return; case 3: return; diff --git a/src/utils/MatHelper.php b/src/utils/MatHelper.php index 7baed13..edd17b9 100644 --- a/src/utils/MatHelper.php +++ b/src/utils/MatHelper.php @@ -3,6 +3,7 @@ namespace Blobt\Luxcore\utils; use Blobt\Luxcore\scene\materials\Disney; +use Blobt\Luxcore\scene\materials\GlassRough; use Blobt\Luxcore\scene\texture\composite\Subtract; use Blobt\Luxcore\scene\texture\mapping\Mapping; use Blobt\Luxcore\scene\texture\procedural\ImageMap; @@ -228,7 +229,6 @@ class MatHelper{ } - //TODO: 更多贴图通道处理,待后续完善 return $material; } @@ -236,8 +236,66 @@ class MatHelper{ /** * 创建一个 Glass 材质(折射材质) */ - static function createMatOfGlass(){ + static function createMatOfGlass($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene){ + $material = new GlassRough(); + $material->setTransmission(self::hex2floatColor($matOriginal->matParams->refractivityColor)); + $material->setInteriorIor($matOriginal->matParams->refractionIor); + $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->albedoTexture){ + $textureUrl = $matOriginal->matParams->albedoTexture; + $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); + } }