Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Ablesons
three.js
提交
f1646a21
T
three.js
项目概览
Ablesons
/
three.js
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
three.js
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f1646a21
编写于
1月 11, 2017
作者:
M
Mr.doob
提交者:
GitHub
1月 11, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9928 from erichlof/dev
Optimize SkyShader.js with pre-calculated consts
上级
6511939c
a44f6e6c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
61 addition
and
52 deletion
+61
-52
examples/js/SkyShader.js
examples/js/SkyShader.js
+61
-52
未找到文件。
examples/js/SkyShader.js
浏览文件 @
f1646a21
...
@@ -41,39 +41,41 @@ THREE.ShaderLib[ 'sky' ] = {
...
@@ -41,39 +41,41 @@ THREE.ShaderLib[ 'sky' ] = {
"
varying vec3 vBetaM;
"
,
"
varying vec3 vBetaM;
"
,
"
varying float vSunE;
"
,
"
varying float vSunE;
"
,
"
const vec3 up = vec3(
0.0, 1.0, 0.0
);
"
,
"
const vec3 up = vec3(
0.0, 1.0, 0.0
);
"
,
// constants for atmospheric scattering
// constants for atmospheric scattering
"
const float e = 2.71828182845904523536028747135266249775724709369995957;
"
,
"
const float e = 2.71828182845904523536028747135266249775724709369995957;
"
,
"
const float pi = 3.141592653589793238462643383279502884197169;
"
,
"
const float pi = 3.141592653589793238462643383279502884197169;
"
,
// wavelength of used primaries, according to preetham
"
const vec3 lambda = vec3( 680E-9, 550E-9, 450E-9 );
"
,
// this pre-calcuation replaces older TotalRayleigh(vec3 lambda) function:
// (8.0 * pow(pi, 3.0) * pow(pow(n, 2.0) - 1.0, 2.0) * (6.0 + 3.0 * pn)) / (3.0 * N * pow(lambda, vec3(4.0)) * (6.0 - 7.0 * pn))
"
const vec3 totalRayleigh = vec3( 5.804542996261093E-6, 1.3562911419845635E-5, 3.0265902468824876E-5 );
"
,
// mie stuff
// mie stuff
// K coefficient for the primaries
// K coefficient for the primaries
"
const float v = 4.0;
"
,
"
const float v = 4.0;
"
,
"
const vec3 K = vec3(0.686, 0.678, 0.666);
"
,
"
const vec3 K = vec3( 0.686, 0.678, 0.666 );
"
,
// MieConst = pi * pow( ( 2.0 * pi ) / lambda, vec3( v - 2.0 ) ) * K
// see http://blenderartists.org/forum/showthread.php?321110-Shaders-and-Skybox-madness
"
const vec3 MieConst = vec3( 1.8399918514433978E14, 2.7798023919660528E14, 4.0790479543861094E14 );
"
,
// A simplied version of the total Reayleigh scattering to works on browsers that use ANGLE
"
const vec3 simplifiedRayleigh = 0.0005 / vec3(94, 40, 18);
"
,
// wavelength of used primaries, according to preetham
"
const vec3 lambda = vec3(680E-9, 550E-9, 450E-9);
"
,
// earth shadow hack
// earth shadow hack
"
const float cutoffAngle = pi/1.95;
"
,
// cutoffAngle = pi / 1.95;
"
const float cutoffAngle = 1.6110731556870734;
"
,
"
const float steepness = 1.5;
"
,
"
const float steepness = 1.5;
"
,
"
const float EE = 1000.0;
"
,
"
const float EE = 1000.0;
"
,
"
float sunIntensity(
float zenithAngleCos
)
"
,
"
float sunIntensity(
float zenithAngleCos
)
"
,
"
{
"
,
"
{
"
,
"
zenithAngleCos = clamp(
zenithAngleCos, -1.0, 1.0
);
"
,
"
zenithAngleCos = clamp(
zenithAngleCos, -1.0, 1.0
);
"
,
"
return EE * max(
0.0, 1.0 - pow(e, -((cutoffAngle - acos(zenithAngleCos))/steepness))
);
"
,
"
return EE * max(
0.0, 1.0 - pow( e, -( ( cutoffAngle - acos( zenithAngleCos ) ) / steepness ) )
);
"
,
"
}
"
,
"
}
"
,
"
vec3 totalMie(
vec3 lambda, float T
)
"
,
"
vec3 totalMie(
float T
)
"
,
"
{
"
,
"
{
"
,
"
float c = (0.2 * T ) * 10E-18;
"
,
"
float c = (
0.2 * T ) * 10E-18;
"
,
"
return 0.434 * c *
pi * pow((2.0 * pi) / lambda, vec3(v - 2.0)) * K
;
"
,
"
return 0.434 * c *
MieConst
;
"
,
"
}
"
,
"
}
"
,
"
void main() {
"
,
"
void main() {
"
,
...
@@ -83,20 +85,20 @@ THREE.ShaderLib[ 'sky' ] = {
...
@@ -83,20 +85,20 @@ THREE.ShaderLib[ 'sky' ] = {
"
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
"
,
"
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
"
,
"
vSunDirection = normalize(
sunPosition
);
"
,
"
vSunDirection = normalize(
sunPosition
);
"
,
"
vSunE = sunIntensity(
dot(vSunDirection, up)
);
"
,
"
vSunE = sunIntensity(
dot( vSunDirection, up )
);
"
,
"
vSunfade = 1.0
-clamp(1.0-exp((sunPosition.y/450000.0)),0.0,1.0
);
"
,
"
vSunfade = 1.0
- clamp( 1.0 - exp( ( sunPosition.y / 450000.0 ) ), 0.0, 1.0
);
"
,
"
float rayleighCoefficient = rayleigh - (
1.0 * (1.0-vSunfade)
);
"
,
"
float rayleighCoefficient = rayleigh - (
1.0 * ( 1.0 - vSunfade )
);
"
,
// extinction (absorbtion + out scattering)
// extinction (absorbtion + out scattering)
// rayleigh coefficients
// rayleigh coefficients
"
vBetaR =
simplified
Rayleigh * rayleighCoefficient;
"
,
"
vBetaR =
total
Rayleigh * rayleighCoefficient;
"
,
// mie coefficients
// mie coefficients
"
vBetaM = totalMie(
lambda, turbidity
) * mieCoefficient;
"
,
"
vBetaM = totalMie(
turbidity
) * mieCoefficient;
"
,
"
}
"
"
}
"
...
@@ -114,7 +116,7 @@ THREE.ShaderLib[ 'sky' ] = {
...
@@ -114,7 +116,7 @@ THREE.ShaderLib[ 'sky' ] = {
"
uniform float luminance;
"
,
"
uniform float luminance;
"
,
"
uniform float mieDirectionalG;
"
,
"
uniform float mieDirectionalG;
"
,
"
const vec3 cameraPos = vec3(
0., 0., 0.
);
"
,
"
const vec3 cameraPos = vec3(
0.0, 0.0, 0.0
);
"
,
// constants for atmospheric scattering
// constants for atmospheric scattering
"
const float pi = 3.141592653589793238462643383279502884197169;
"
,
"
const float pi = 3.141592653589793238462643383279502884197169;
"
,
...
@@ -126,19 +128,25 @@ THREE.ShaderLib[ 'sky' ] = {
...
@@ -126,19 +128,25 @@ THREE.ShaderLib[ 'sky' ] = {
// optical length at zenith for molecules
// optical length at zenith for molecules
"
const float rayleighZenithLength = 8.4E3;
"
,
"
const float rayleighZenithLength = 8.4E3;
"
,
"
const float mieZenithLength = 1.25E3;
"
,
"
const float mieZenithLength = 1.25E3;
"
,
"
const vec3 up = vec3(0.0, 1.0, 0.0);
"
,
"
const vec3 up = vec3( 0.0, 1.0, 0.0 );
"
,
"
const float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324;
"
,
// 66 arc seconds -> degrees, and the cosine of that
// 66 arc seconds -> degrees, and the cosine of that
"
const float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324;
"
,
// 3.0 / ( 16.0 * pi )
"
const float THREE_OVER_SIXTEENPI = 0.05968310365946075;
"
,
// 1.0 / ( 4.0 * pi )
"
const float ONE_OVER_FOURPI = 0.07957747154594767;
"
,
"
float rayleighPhase(
float cosTheta
)
"
,
"
float rayleighPhase(
float cosTheta
)
"
,
"
{
"
,
"
{
"
,
"
return
(3.0 / (16.0*pi)) * (1.0 + pow(cosTheta, 2.0)
);
"
,
"
return
THREE_OVER_SIXTEENPI * ( 1.0 + pow( cosTheta, 2.0 )
);
"
,
"
}
"
,
"
}
"
,
"
float hgPhase(
float cosTheta, float g
)
"
,
"
float hgPhase(
float cosTheta, float g
)
"
,
"
{
"
,
"
{
"
,
"
return (1.0 / (4.0*pi)) * ((1.0 - pow(g, 2.0)) / pow(1.0 - 2.0*g*cosTheta + pow(g, 2.0), 1.5));
"
,
"
float g2 = pow( g, 2.0 );
"
,
"
float inverse = 1.0 / pow( 1.0 - 2.0 * g * cosTheta + g2, 1.5 );
"
,
"
return ONE_OVER_FOURPI * ( ( 1.0 - g2 ) * inverse );
"
,
"
}
"
,
"
}
"
,
// Filmic ToneMapping http://filmicgames.com/archives/75
// Filmic ToneMapping http://filmicgames.com/archives/75
...
@@ -151,9 +159,9 @@ THREE.ShaderLib[ 'sky' ] = {
...
@@ -151,9 +159,9 @@ THREE.ShaderLib[ 'sky' ] = {
"
const float whiteScale = 1.0748724675633854;
"
,
// 1.0 / Uncharted2Tonemap(1000.0)
"
const float whiteScale = 1.0748724675633854;
"
,
// 1.0 / Uncharted2Tonemap(1000.0)
"
vec3 Uncharted2Tonemap(
vec3 x
)
"
,
"
vec3 Uncharted2Tonemap(
vec3 x
)
"
,
"
{
"
,
"
{
"
,
"
return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/
F;
"
,
"
return ( ( x * ( A * x + C * B ) + D * E ) / ( x * ( A * x + B ) + D * F ) ) - E /
F;
"
,
"
}
"
,
"
}
"
,
...
@@ -161,42 +169,43 @@ THREE.ShaderLib[ 'sky' ] = {
...
@@ -161,42 +169,43 @@ THREE.ShaderLib[ 'sky' ] = {
"
{
"
,
"
{
"
,
// optical length
// optical length
// cutoff angle at 90 to avoid singularity in next formula.
// cutoff angle at 90 to avoid singularity in next formula.
"
float zenithAngle = acos(max(0.0, dot(up, normalize(vWorldPosition - cameraPos))));
"
,
"
float zenithAngle = acos( max( 0.0, dot( up, normalize( vWorldPosition - cameraPos ) ) ) );
"
,
"
float sR = rayleighZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));
"
,
"
float inverse = 1.0 / ( cos( zenithAngle ) + 0.15 * pow( 93.885 - ( ( zenithAngle * 180.0 ) / pi ), -1.253 ) );
"
,
"
float sM = mieZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));
"
,
"
float sR = rayleighZenithLength * inverse;
"
,
"
float sM = mieZenithLength * inverse;
"
,
// combined extinction factor
// combined extinction factor
"
vec3 Fex = exp(
-(vBetaR * sR + vBetaM * sM)
);
"
,
"
vec3 Fex = exp(
-( vBetaR * sR + vBetaM * sM )
);
"
,
// in scattering
// in scattering
"
float cosTheta = dot(
normalize(vWorldPosition - cameraPos), vSunDirection
);
"
,
"
float cosTheta = dot(
normalize( vWorldPosition - cameraPos ), vSunDirection
);
"
,
"
float rPhase = rayleighPhase(
cosTheta*0.5+0.5
);
"
,
"
float rPhase = rayleighPhase(
cosTheta * 0.5 + 0.5
);
"
,
"
vec3 betaRTheta = vBetaR * rPhase;
"
,
"
vec3 betaRTheta = vBetaR * rPhase;
"
,
"
float mPhase = hgPhase(
cosTheta, mieDirectionalG
);
"
,
"
float mPhase = hgPhase(
cosTheta, mieDirectionalG
);
"
,
"
vec3 betaMTheta = vBetaM * mPhase;
"
,
"
vec3 betaMTheta = vBetaM * mPhase;
"
,
"
vec3 Lin = pow(
vSunE * ((betaRTheta + betaMTheta) / (vBetaR + vBetaM)) * (1.0 - Fex),vec3(1.5)
);
"
,
"
vec3 Lin = pow(
vSunE * ( ( betaRTheta + betaMTheta ) / ( vBetaR + vBetaM ) ) * ( 1.0 - Fex ), vec3( 1.5 )
);
"
,
"
Lin *= mix(
vec3(1.0),pow(vSunE * ((betaRTheta + betaMTheta) / (vBetaR + vBetaM)) * Fex,vec3(1.0/2.0)),clamp(pow(1.0-dot(up, vSunDirection),5.0),0.0,1.0)
);
"
,
"
Lin *= mix(
vec3( 1.0 ), pow( vSunE * ( ( betaRTheta + betaMTheta ) / ( vBetaR + vBetaM ) ) * Fex, vec3( 1.0 / 2.0 ) ), clamp( pow( 1.0 - dot( up, vSunDirection ), 5.0 ), 0.0, 1.0 )
);
"
,
//nightsky
//nightsky
"
vec3 direction = normalize(
vWorldPosition - cameraPos
);
"
,
"
vec3 direction = normalize(
vWorldPosition - cameraPos
);
"
,
"
float theta = acos(
direction.y
); // elevation --> y-axis, [-pi/2, pi/2]
"
,
"
float theta = acos(
direction.y
); // elevation --> y-axis, [-pi/2, pi/2]
"
,
"
float phi = atan(
direction.z, direction.x
); // azimuth --> x-axis [-pi/2, pi/2]
"
,
"
float phi = atan(
direction.z, direction.x
); // azimuth --> x-axis [-pi/2, pi/2]
"
,
"
vec2 uv = vec2(
phi, theta) / vec2(2.0*pi, pi) + vec2(0.5, 0.0
);
"
,
"
vec2 uv = vec2(
phi, theta ) / vec2( 2.0 * pi, pi ) + vec2( 0.5, 0.0
);
"
,
"
vec3 L0 = vec3(
0.1
) * Fex;
"
,
"
vec3 L0 = vec3(
0.1
) * Fex;
"
,
// composition + solar disc
// composition + solar disc
"
float sundisk = smoothstep(
sunAngularDiameterCos,sunAngularDiameterCos+0.00002,cosTheta
);
"
,
"
float sundisk = smoothstep(
sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta
);
"
,
"
L0 += (
vSunE * 19000.0 * Fex)*
sundisk;
"
,
"
L0 += (
vSunE * 19000.0 * Fex ) *
sundisk;
"
,
"
vec3 texColor = (
Lin+L0) * 0.04 + vec3(0.0, 0.0003, 0.00075
);
"
,
"
vec3 texColor = (
Lin + L0 ) * 0.04 + vec3( 0.0, 0.0003, 0.00075
);
"
,
"
vec3 curr = Uncharted2Tonemap(
(log2(2.0/pow(luminance,4.0)))*texColor
);
"
,
"
vec3 curr = Uncharted2Tonemap(
( log2( 2.0 / pow( luminance, 4.0 ) ) ) * texColor
);
"
,
"
vec3 color = curr
*
whiteScale;
"
,
"
vec3 color = curr
*
whiteScale;
"
,
"
vec3 retColor = pow(
color,vec3(1.0/(1.2+(1.2*vSunfade)))
);
"
,
"
vec3 retColor = pow(
color, vec3( 1.0 / ( 1.2 + ( 1.2 * vSunfade ) ) )
);
"
,
"
gl_FragColor.rgb = retColor;
"
,
"
gl_FragColor.rgb = retColor;
"
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录