threejs glsl(math)

common

#define PI 3.14159265359
//PI*2
#define PI2 6.28318530718
// PI/2
#define PI_HALF 1.5707963267949
// 1/PI
#define RECIPROCAL_PI 0.31830988618
// 1/(2*PI)
#define RECIPROCAL_PI2 0.15915494
//LOG2E
#define LOG2 1.442695
//精度
#define EPSILON 1e-6
// 限定a取值0~1
#define saturate(a) clamp( a, 0.0, 1.0 )
// 限定a取值0~1 并返回1-a
#define whiteCompliment(a) ( 1.0 - saturate( a ) )
//平方
float pow2( const in float x ) {
    return x*x; }
//3次方
float pow3( const in float x ) {
    return x*x*x; }
//4次方
float pow4( const in float x ) {
    float x2 = x*x; return x2*x2; }
//vec3各组件平均值
float average( const in vec3 color ) {
    return dot( color, vec3( 0.3333 ) ); }
//随机数
highp float rand( const in vec2 uv ) {
   
	const highp float a = 12.9898, b = 78.233, c = 43758.5453;
	highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );
	return fract(sin(sn) * c);
}
//入射光照
struct IncidentLight {
   
	vec3 color;
	vec3 direction;
	bool visible;
};
//反射光
struct ReflectedLight {
   
	vec3 directDiffuse;
	vec3 directSpecular;
	vec3 indirectDiffuse;
	vec3 indirectSpecular;
};
//几何体上下文
struct GeometricContext {
   
	vec3 position;
	vec3 normal;
	vec3 viewDir;
};
//根据矩阵变换向量方向
vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
   
	return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
}
//根据矩阵反方向变换向量方向
vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
   
	return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
}
//点在平面的投影
vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
   
	float distance = dot( planeNormal, point - pointOnPlane );
	return - distance * planeNormal + point;
}
//return 点在平面正面? 1 : -1
float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
   
	return sign( dot( point - pointOnPlane, planeNormal ) );
}
//线和平面的交点
vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {
   
	return lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;
}
//转置矩阵
mat3 transposeMat3( const in mat3 m ) {
   
	mat3 tmp;
	tmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );
	tmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );
	tmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );
	return tmp;
}
//相对亮度(可做灰度图颜色值)
float linearToRelativeLuminance( const in vec3 color ) {
   
	vec3 weights = vec3( 0.2126, 0.7152, 0.0722 );
	return dot( weights, color.rgb );
}

bsdfs

/* 获取灯光衰减后的辐照强度
 * @param {float } 灯光与物体表面距离
 * @param {float } 灯光最远照射距离
 * @param {float } 衰减因子
 * @returns {void} 
*/
float punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {
   
	#if defined ( PHYSICALLY_CORRECT_LIGHTS )
	float distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );
	if( cutoffDistance > 0.0 ) {
   
		distanceFalloff *= pow2( saturate( 1.0 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值