本文主要是介绍three.js各向异性shader实现记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- `WebGLMaterials`
- `ShaderLib`
- `lights_physical_fragment.glsl`
- `lights_fragment_maps.glsl`
- `lights_physical_pars_fragment.glsl`
- `lights_fragment_begin.glsl`
WebGLMaterials
if ( material.anisotropy > 0 ) {uniforms.anisotropyVector.value.set( material.anisotropy * Math.cos( material.anisotropyRotation ), material.anisotropy * Math.sin( material.anisotropyRotation ) );if ( material.anisotropyMap ) {uniforms.anisotropyMap.value = material.anisotropyMap;refreshTransformUniform( material.anisotropyMap, uniforms.anisotropyMapTransform );}}
ShaderLib
anisotropyVector: { value: /*@__PURE__*/ new Vector2() },
anisotropyMap: { value: null },
anisotropyMapTransform: { value: /*@__PURE__*/ new Matrix3() },
meshphysical.glsl
#ifdef USE_ANISOTROPYuniform vec2 anisotropyVector;#ifdef USE_ANISOTROPYMAPuniform sampler2D anisotropyMap;#endif
#endif
lights_physical_fragment.glsl
#ifdef USE_ANISOTROPY#ifdef USE_ANISOTROPYMAPmat2 anisotropyMat = mat2( anisotropyVector.x, anisotropyVector.y, - anisotropyVector.y, anisotropyVector.x );vec3 anisotropyPolar = texture2D( anisotropyMap, vAnisotropyMapUv ).rgb;vec2 anisotropyV = anisotropyMat * normalize( 2.0 * anisotropyPolar.rg - vec2( 1.0 ) ) * anisotropyPolar.b;#elsevec2 anisotropyV = anisotropyVector;#endifmaterial.anisotropy = length( anisotropyV );if( material.anisotropy == 0.0 ) {anisotropyV = vec2( 1.0, 0.0 );} else {anisotropyV /= material.anisotropy;material.anisotropy = saturate( material.anisotropy );}// Roughness along the anisotropy bitangent is the material roughness, while the tangent roughness increases with anisotropy.material.alphaT = mix( pow2( material.roughness ), 1.0, pow2( material.anisotropy ) );material.anisotropyT = tbn[ 0 ] * anisotropyV.x + tbn[ 1 ] * anisotropyV.y;material.anisotropyB = tbn[ 1 ] * anisotropyV.x - tbn[ 0 ] * anisotropyV.y;#endif
lights_fragment_maps.glsl
#if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular )#ifdef USE_ANISOTROPYradiance += getIBLAnisotropyRadiance( geometryViewDir, geometryNormal, material.roughness, material.anisotropyB, material.anisotropy );#else
#endif
lights_physical_pars_fragment.glsl
vec3 BRDF_GGX( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, const in PhysicalMaterial material ) {vec3 f0 = material.specularColor;float f90 = material.specularF90;float roughness = material.roughness;float alpha = pow2( roughness ); // UE4's roughnessvec3 halfDir = normalize( lightDir + viewDir );float dotNL = saturate( dot( normal, lightDir ) );float dotNV = saturate( dot( normal, viewDir ) );float dotNH = saturate( dot( normal, halfDir ) );float dotVH = saturate( dot( viewDir, halfDir ) );vec3 F = F_Schlick( f0, f90, dotVH );#ifdef USE_IRIDESCENCEF = mix( F, material.iridescenceFresnel, material.iridescence );#endif#ifdef USE_ANISOTROPY// 这里用的 anisotropyT 和 anisotropyB 就是各向异性处理中增加的属性// material.anisotropyT = tbn[ 0 ] * anisotropyV.x + tbn[ 1 ] * anisotropyV.y;// material.anisotropyB = tbn[ 1 ] * anisotropyV.x - tbn[ 0 ] * anisotropyV.y;float dotTL = dot( material.anisotropyT, lightDir );float dotTV = dot( material.anisotropyT, viewDir );float dotTH = dot( material.anisotropyT, halfDir );float dotBL = dot( material.anisotropyB, lightDir );float dotBV = dot( material.anisotropyB, viewDir );float dotBH = dot( material.anisotropyB, halfDir );float V = V_GGX_SmithCorrelated_Anisotropic( material.alphaT, alpha, dotTV, dotBV, dotTL, dotBL, dotNV, dotNL );float D = D_GGX_Anisotropic( material.alphaT, alpha, dotNH, dotTH, dotBH );#elsefloat V = V_GGX_SmithCorrelated( alpha, dotNL, dotNV );float D = D_GGX( alpha, dotNH );#endifreturn F * ( V * D );}
void RE_Direct_Physical( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {//直接镜面反射reflectedLight.directSpecular += irradiance * BRDF_GGX( directLight.direction, geometryViewDir, geometryNormal, material );
}
将RE_Direct_Physical 方法 定义成 RE_Direct 使用
#define RE_Direct RE_Direct_Physical
lights_fragment_begin.glsl
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight )
NUM_POINT_LIGHTS
NUM_SPOT_LIGHTS
NUM_DIR_LIGHTS
点光源,聚光灯,方向光 这三种 进行每个类型灯光遍历执行大致如下 点光源例
IncidentLight directLight;#if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )PointLight pointLight;#if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0PointLightShadow pointLightShadow;#endif#pragma unroll_loop_startfor ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {pointLight = pointLights[ i ];getPointLightInfo( pointLight, geometryPosition, directLight );#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )pointLightShadow = pointLightShadows[ i ];directLight.color *= ( directLight.visible && receiveShadow ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;#endifRE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );}#pragma unroll_loop_end#endif
这篇关于three.js各向异性shader实现记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!