Browse Source

渲染测式 开发

master
yuanjiajia 1 year ago
parent
commit
8f34e8a62a
  1. 2
      examples/matRenderTest.php
  2. 2
      examples/renderTest.php
  3. 78
      src/utils/MatHelper.php

2
examples/matRenderTest.php

@ -68,7 +68,7 @@ function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $cacheFi
case 2:
return MatHelper::createMatOfGlass($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
case 3:
return;
return MatHelper::createMatOfTranslucent($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
case 4:
return;
}

2
examples/renderTest.php

@ -68,7 +68,7 @@ function createLuxcoreMat($matOriginal, $textureScaleU, $textureScaleV, $cacheFi
case 2:
return MatHelper::createMatOfGlass($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
case 3:
return;
return MatHelper::createMatOfTranslucent($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene);
case 4:
return;
}

78
src/utils/MatHelper.php

@ -4,6 +4,7 @@ namespace Blobt\Luxcore\utils;
use Blobt\Luxcore\scene\materials\Disney;
use Blobt\Luxcore\scene\materials\GlassRough;
use Blobt\Luxcore\scene\materials\GlossyTranslucent;
use Blobt\Luxcore\scene\texture\composite\Subtract;
use Blobt\Luxcore\scene\texture\mapping\Mapping;
use Blobt\Luxcore\scene\texture\procedural\ImageMap;
@ -305,8 +306,75 @@ class MatHelper{
/**
* 创建一个半透明材质
*/
static function createMatOfTranslucent(){
static function createMatOfTranslucent($matOriginal, $textureScaleU, $textureScaleV, $cacheFileDir, $scene){
$material = new GlossyTranslucent();
$material->setTransmission(
self::hex2floatColor($matOriginal->matParams->translucencyColor),
$matOriginal->matParams->translucencyIntensity
);
$material->setBaseColor(
self::hex2floatColor($matOriginal->matParams->translucencyColor),
1 - $matOriginal->matParams->translucencyIntensity
);
$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->translucencyTexture){
// $textureUrl = $matOriginal->matParams->translucencyTexture;
// $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);
}
return $material;
}
@ -321,12 +389,12 @@ class MatHelper{
/**
* 十六进制颜色 转换为 浮点数颜色
*/
static function hex2floatColor($hexColor){
static function hex2floatColor($hexColor, $scale = 1){
$floatColor = '';
$hexColor = trim($hexColor);
$r = (hexdec(substr($hexColor, 1, 2))/255) ** 2.2;
$g = (hexdec(substr($hexColor, 3, 2))/255) ** 2.2;
$b = (hexdec(substr($hexColor, 5, 2))/255) ** 2.2;
$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;
}

Loading…
Cancel
Save