Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Ablesons
three.js
提交
84d1b582
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,发现更多精彩内容 >>
提交
84d1b582
编写于
2月 02, 2011
作者:
A
alteredq
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Moved textured quad screen shader into ShaderUtils.
上级
418a6da7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
118 addition
and
78 deletion
+118
-78
build/ThreeExtras.js
build/ThreeExtras.js
+7
-6
examples/postprocessing.html
examples/postprocessing.html
+17
-27
src/extras/ShaderUtils.js
src/extras/ShaderUtils.js
+94
-45
未找到文件。
build/ThreeExtras.js
浏览文件 @
84d1b582
...
...
@@ -223,17 +223,18 @@ e,g,b,l,j,h,o){c=new THREE.Mesh(c,o);c.scale.x=c.scale.y=c.scale.z=d;c.position.
[];
e
.
push
(
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
0
])}));
e
.
push
(
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
1
])}));
e
.
push
(
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
2
])}));
e
.
push
(
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
3
])}));
e
.
push
(
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
4
])}));
e
.
push
(
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
5
])}));
c
=
new
THREE
.
Mesh
(
new
Cube
(
c
,
c
,
c
,
1
,
1
,
e
,
true
),
new
THREE
.
MeshFaceMaterial
);
a
.
addObject
(
c
);
return
c
},
addPanoramaCubePlanes
:
function
(
a
,
c
,
d
){
var
e
=
c
/
2
;
c
=
new
Plane
(
c
,
c
);
var
g
=
Math
.
PI
/
2
,
b
=
Math
.
PI
;
SceneUtils
.
addMesh
(
a
,
c
,
1
,
0
,
0
,
-
e
,
0
,
0
,
0
,
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
5
])}));
SceneUtils
.
addMesh
(
a
,
c
,
1
,
-
e
,
0
,
0
,
0
,
g
,
0
,
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
0
])}));
SceneUtils
.
addMesh
(
a
,
c
,
1
,
e
,
0
,
0
,
0
,
-
g
,
0
,
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
1
])}));
SceneUtils
.
addMesh
(
a
,
c
,
1
,
0
,
e
,
0
,
g
,
0
,
b
,
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
2
])}));
SceneUtils
.
addMesh
(
a
,
c
,
1
,
0
,
-
e
,
0
,
-
g
,
0
,
b
,
new
THREE
.
MeshBasicMaterial
({
map
:
new
THREE
.
Texture
(
d
[
3
])}))}},
ShaderUtils
=
{
lib
:{
fresnel
:{
uniforms
:{
mRefractionRatio
:{
type
:
"
f
"
,
value
:
1.02
},
mFresnelBias
:{
type
:
"
f
"
,
value
:
0.1
},
mFresnelPower
:{
type
:
"
f
"
,
value
:
2
},
mFresnelScale
:{
type
:
"
f
"
,
value
:
1
},
tCube
:{
type
:
"
t
"
,
value
:
1
,
texture
:
null
}},
fragment_shader
:
"
uniform samplerCube tCube;
\n
varying vec3 vReflect;
\n
varying vec3 vRefract[3];
\n
varying float vReflectionFactor;
\n
void main() {
\n
vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );
\n
vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );
\n
refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;
\n
refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;
\n
refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;
\n
refractedColor.a = 1.0;
\n
gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );
\n
}
"
,
vertex_shader
:
"
uniform float mRefractionRatio;
\n
uniform float mFresnelBias;
\n
uniform float mFresnelScale;
\n
uniform float mFresnelPower;
\n
varying vec3 vReflect;
\n
varying vec3 vRefract[3];
\n
varying float vReflectionFactor;
\n
void main(
void
) {
\n
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
\n
vec4 mPosition = objectMatrix * vec4( position, 1.0 );
\n
vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );
\n
vec3 I = mPosition.xyz - cameraPosition;
\n
vReflect = reflect( I, nWorld );
\n
vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );
\n
vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );
\n
vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );
\n
vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );
\n
gl_Position = projectionMatrix * mvPosition;
\n
}
"
},
vertex_shader
:
"
uniform float mRefractionRatio;
\n
uniform float mFresnelBias;
\n
uniform float mFresnelScale;
\n
uniform float mFresnelPower;
\n
varying vec3 vReflect;
\n
varying vec3 vRefract[3];
\n
varying float vReflectionFactor;
\n
void main() {
\n
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
\n
vec4 mPosition = objectMatrix * vec4( position, 1.0 );
\n
vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );
\n
vec3 I = mPosition.xyz - cameraPosition;
\n
vReflect = reflect( I, nWorld );
\n
vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );
\n
vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );
\n
vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );
\n
vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );
\n
gl_Position = projectionMatrix * mvPosition;
\n
}
"
},
normal
:{
uniforms
:{
enableAO
:{
type
:
"
i
"
,
value
:
0
},
enableDiffuse
:{
type
:
"
i
"
,
value
:
0
},
tDiffuse
:{
type
:
"
t
"
,
value
:
0
,
texture
:
null
},
tNormal
:{
type
:
"
t
"
,
value
:
2
,
texture
:
null
},
tAO
:{
type
:
"
t
"
,
value
:
3
,
texture
:
null
},
uNormalScale
:{
type
:
"
f
"
,
value
:
1
},
tDisplacement
:{
type
:
"
t
"
,
value
:
4
,
texture
:
null
},
uDisplacementBias
:{
type
:
"
f
"
,
value
:
-
0.5
},
uDisplacementScale
:{
type
:
"
f
"
,
value
:
2.5
},
uPointLightPos
:{
type
:
"
v3
"
,
value
:
new
THREE
.
Vector3
},
uPointLightColor
:{
type
:
"
c
"
,
value
:
new
THREE
.
Color
(
15658734
)},
uDirLightPos
:{
type
:
"
v3
"
,
value
:
new
THREE
.
Vector3
},
uDirLightColor
:{
type
:
"
c
"
,
value
:
new
THREE
.
Color
(
15658734
)},
uAmbientLightColor
:{
type
:
"
c
"
,
value
:
new
THREE
.
Color
(
328965
)},
uDiffuseColor
:{
type
:
"
c
"
,
value
:
new
THREE
.
Color
(
15658734
)},
uSpecularColor
:{
type
:
"
c
"
,
value
:
new
THREE
.
Color
(
1118481
)},
uAmbientColor
:{
type
:
"
c
"
,
value
:
new
THREE
.
Color
(
328965
)},
uShininess
:{
type
:
"
f
"
,
value
:
30
}},
fragment_shader
:
"
uniform vec3 uDirLightPos;
\n
uniform vec3 uAmbientLightColor;
\n
uniform vec3 uDirLightColor;
\n
uniform vec3 uPointLightColor;
\n
uniform vec3 uAmbientColor;
\n
uniform vec3 uDiffuseColor;
\n
uniform vec3 uSpecularColor;
\n
uniform float uShininess;
\n
uniform bool enableDiffuse;
\n
uniform bool enableAO;
\n
uniform sampler2D tDiffuse;
\n
uniform sampler2D tNormal;
\n
uniform sampler2D tAO;
\n
uniform float uNormalScale;
\n
varying vec3 vTangent;
\n
varying vec3 vBinormal;
\n
varying vec3 vNormal;
\n
varying vec2 vUv;
\n
varying vec3 vPointLightVector;
\n
varying vec3 vViewPosition;
\n
void main() {
\n
vec3 diffuseTex = vec3( 1.0, 1.0, 1.0 );
\n
vec3 aoTex = vec3( 1.0, 1.0, 1.0 );
\n
vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;
\n
normalTex.xy *= uNormalScale;
\n
normalTex = normalize( normalTex );
\n
if( enableDiffuse )
\n
diffuseTex = texture2D( tDiffuse, vUv ).xyz;
\n
if( enableAO )
\n
aoTex = texture2D( tAO, vUv ).xyz;
\n
mat3 tsb = mat3( vTangent, vBinormal, vNormal );
\n
vec3 finalNormal = tsb * normalTex;
\n
vec3 normal = normalize( finalNormal );
\n
vec3 viewPosition = normalize( vViewPosition );
\n
vec4 pointDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );
\n
vec4 pointSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );
\n
vec3 pointVector = normalize( vPointLightVector );
\n
vec3 pointHalfVector = normalize( vPointLightVector + vViewPosition );
\n
float pointDotNormalHalf = dot( normal, pointHalfVector );
\n
float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );
\n
float pointSpecularWeight = 0.0;
\n
if ( pointDotNormalHalf >= 0.0 )
\n
pointSpecularWeight = pow( pointDotNormalHalf, uShininess );
\n
pointDiffuse += vec4( uDiffuseColor, 1.0 ) * pointDiffuseWeight;
\n
pointSpecular += vec4( uSpecularColor, 1.0 ) * pointSpecularWeight;
\n
vec4 dirDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );
\n
vec4 dirSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );
\n
vec4 lDirection = viewMatrix * vec4( uDirLightPos, 0.0 );
\n
vec3 dirVector = normalize( lDirection.xyz );
\n
vec3 dirHalfVector = normalize( lDirection.xyz + vViewPosition );
\n
float dirDotNormalHalf = dot( normal, dirHalfVector );
\n
float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );
\n
float dirSpecularWeight = 0.0;
\n
if ( dirDotNormalHalf >= 0.0 )
\n
dirSpecularWeight = pow( dirDotNormalHalf, uShininess );
\n
dirDiffuse += vec4( uDiffuseColor, 1.0 ) * dirDiffuseWeight;
\n
dirSpecular += vec4( uSpecularColor, 1.0 ) * dirSpecularWeight;
\n
vec4 totalLight = vec4( uAmbientLightColor * uAmbientColor, 1.0 );
\n
totalLight += vec4( uDirLightColor, 1.0 ) * ( dirDiffuse + dirSpecular );
\n
totalLight += vec4( uPointLightColor, 1.0 ) * ( pointDiffuse + pointSpecular );
\n
gl_FragColor = vec4( totalLight.xyz * aoTex * diffuseTex, 1.0 );
\n
}
"
,
vertex_shader
:
"
attribute vec4 tangent;
\n
uniform vec3 uPointLightPos;
\n
#ifdef VERTEX_TEXTURES
\n
uniform sampler2D tDisplacement;
\n
uniform float uDisplacementScale;
\n
uniform float uDisplacementBias;
\n
#endif
\n
varying vec3 vTangent;
\n
varying vec3 vBinormal;
\n
varying vec3 vNormal;
\n
varying vec2 vUv;
\n
varying vec3 vPointLightVector;
\n
varying vec3 vViewPosition;
\n
void main() {
\n
vec4 mPosition = objectMatrix * vec4( position, 1.0 );
\n
vViewPosition = cameraPosition - mPosition.xyz;
\n
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
\n
vNormal = normalize( normalMatrix * normal );
\n
vTangent = normalize( normalMatrix * tangent.xyz );
\n
vBinormal = cross( vNormal, vTangent ) * tangent.w;
\n
vBinormal = normalize( vBinormal );
\n
vUv = uv;
\n
vec4 lPosition = viewMatrix * vec4( uPointLightPos, 1.0 );
\n
vPointLightVector = normalize( lPosition.xyz - mvPosition.xyz );
\n
#ifdef VERTEX_TEXTURES
\n
vec3 dv = texture2D( tDisplacement, uv ).xyz;
\n
float df = uDisplacementScale * dv.x + uDisplacementBias;
\n
vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;
\n
gl_Position = projectionMatrix * displacedPosition;
\n
#else
\n
gl_Position = projectionMatrix * mvPosition;
\n
#endif
\n
}
"
},
cube
:{
uniforms
:{
tCube
:{
type
:
"
t
"
,
value
:
1
,
texture
:
null
}},
vertex_shader
:
"
varying vec3 vViewPosition;
\n
void main() {
\n
vec4 mPosition = objectMatrix * vec4( position, 1.0 );
\n
vViewPosition = cameraPosition - mPosition.xyz;
\n
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
\n
}
"
,
fragment_shader
:
"
uniform samplerCube tCube;
\n
varying vec3 vViewPosition;
\n
void main() {
\n
vec3 wPos = cameraPosition - vViewPosition;
\n
gl_FragColor = textureCube( tCube, vec3( - wPos.x, wPos.yz ) );
\n
}
"
},
convolution
:{
uniforms
:{
tDiffuse
:{
type
:
"
t
"
,
value
:
0
,
texture
:
null
},
uImageIncrement
:{
type
:
"
v2
"
,
value
:
new
THREE
.
Vector2
(
0.001953125
,
0
)},
cKernel
:{
type
:
"
fv1
"
,
value
:[]}},
vertex_shader
:
"
varying vec2 vUv;
\n
uniform vec2 uImageIncrement;
\n
#define KERNEL_SIZE 25.0
\n
void main(void) {
\n
vUv = uv - ((KERNEL_SIZE - 1.0) / 2.0) * uImageIncrement;
\n
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
\n
}
"
,
fragment_shader
:
"
varying vec2 vUv;
\n
uniform sampler2D tDiffuse;
\n
uniform vec2 uImageIncrement;
\n
#define KERNEL_SIZE 25
\n
uniform float cKernel[KERNEL_SIZE];
\n
void main(void) {
\n
vec2 imageCoord = vUv;
\n
vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );
\n
for( int i=0; i<KERNEL_SIZE; ++i ) {
\n
sum += texture2D( tDiffuse, imageCoord ) * cKernel[i];
\n
imageCoord += uImageIncrement;
\n
}
\n
gl_FragColor = sum;
\n
}
"
},
basic
:{
uniforms
:{},
vertex_shader
:
"
void main() {
\n
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
\n
}
"
,
fragment_shader
:
"
void main() {
\n
gl_FragColor = vec4(1.0, 0.0, 0.0, 0.5);
\n
}
"
}},
buildKernel
:
function
(
a
){
var
c
,
d
,
e
,
g
,
b
=
2
*
Math
.
ceil
(
a
*
3
)
+
1
;
if
(
b
>
25
)
b
=
25
;
g
=
(
b
-
1
)
*
0.5
;
d
=
Array
(
b
);
for
(
c
=
e
=
0
;
c
<
b
;
++
c
){
d
[
c
]
=
Math
.
exp
(
-
((
c
-
g
)
*
(
c
-
g
))
/
(
2
*
a
*
a
));
e
+=
d
[
c
]}
for
(
c
=
0
;
c
<
b
;
++
c
)
d
[
c
]
/=
e
;
return
d
}},
Cube
=
function
(
a
,
c
,
d
,
e
,
g
,
b
,
l
,
j
){
function
h
(
B
,
G
,
t
,
k
,
f
,
m
,
n
,
C
){
var
w
,
p
,
F
=
e
||
1
,
y
=
g
||
1
,
J
=
F
+
1
,
E
=
y
+
1
,
A
=
f
/
2
,
M
=
m
/
2
;
f
=
f
/
F
;
var
aa
=
m
/
y
,
N
=
o
.
vertices
.
length
;
if
(
B
==
"
x
"
&&
G
==
"
y
"
||
B
==
"
y
"
&&
G
==
"
x
"
)
w
=
"
z
"
;
else
if
(
B
==
"
x
"
&&
G
==
"
z
"
||
B
==
"
z
"
&&
G
==
"
x
"
)
w
=
"
y
"
;
else
if
(
B
==
"
z
"
&&
G
==
"
y
"
||
B
==
"
y
"
&&
G
==
"
z
"
)
w
=
"
x
"
;
for
(
p
=
0
;
p
<
E
;
p
++
)
for
(
m
=
0
;
m
<
J
;
m
++
){
var
U
=
new
THREE
.
Vector3
;
U
[
B
]
=
(
m
*
f
-
A
)
*
t
;
U
[
G
]
=
(
p
*
aa
-
M
)
*
k
;
U
[
w
]
=
n
;
o
.
vertices
.
push
(
new
THREE
.
Vertex
(
U
))}
for
(
p
=
0
;
p
<
y
;
p
++
)
for
(
m
=
0
;
m
<
F
;
m
++
){
o
.
faces
.
push
(
new
THREE
.
Face4
(
m
+
J
*
p
+
N
,
m
+
J
*
(
p
+
1
)
+
N
,
m
+
1
+
J
*
(
p
+
1
)
+
N
,
m
+
1
+
J
*
p
+
N
,
null
,
C
));
o
.
uvs
.
push
([
new
THREE
.
UV
(
m
/
F
,
p
/
y
),
new
THREE
.
UV
(
m
/
F
,(
p
+
1
)
/
y
),
new
THREE
.
UV
((
m
+
1
)
/
F
,(
p
+
1
)
/
y
),
new
THREE
.
UV
((
m
+
1
)
/
F
,
p
/
y
)])}}
THREE
.
Geometry
.
call
(
this
);
var
o
=
this
,
v
=
a
/
2
,
z
=
c
/
2
,
u
=
d
/
2
;
l
=
l
?
-
1
:
1
;
if
(
b
!==
undefined
)
if
(
b
instanceof
Array
)
this
.
materials
=
b
;
else
{
this
.
materials
=
[];
for
(
var
q
=
0
;
q
<
6
;
q
++
)
this
.
materials
.
push
([
b
])}
else
this
.
materials
=
[];
this
.
sides
=
{
px
:
true
,
nx
:
true
,
py
:
true
,
ny
:
true
,
pz
:
true
,
nz
:
true
};
if
(
j
!=
undefined
)
for
(
var
x
in
j
)
if
(
this
.
sides
[
x
]
!=
undefined
)
this
.
sides
[
x
]
=
j
[
x
];
this
.
sides
.
px
&&
h
(
"
z
"
,
"
y
"
,
1
*
l
,
-
1
,
d
,
c
,
-
v
,
this
.
materials
[
0
]);
this
.
sides
.
nx
&&
h
(
"
z
"
,
"
y
"
,
-
1
*
l
,
-
1
,
d
,
c
,
v
,
this
.
materials
[
1
]);
this
.
sides
.
py
&&
h
(
"
x
"
,
"
z
"
,
1
*
l
,
1
,
a
,
d
,
z
,
this
.
materials
[
2
]);
this
.
sides
.
ny
&&
h
(
"
x
"
,
"
z
"
,
1
*
l
,
-
1
,
a
,
d
,
-
z
,
this
.
materials
[
3
]);
this
.
sides
.
pz
&&
h
(
"
x
"
,
"
y
"
,
1
*
l
,
-
1
,
a
,
c
,
u
,
this
.
materials
[
4
]);
this
.
sides
.
nz
&&
h
(
"
x
"
,
"
y
"
,
-
1
*
l
,
-
1
,
a
,
c
,
-
u
,
this
.
materials
[
5
]);(
function
(){
for
(
var
B
=
[],
G
=
[],
t
=
0
,
k
=
o
.
vertices
.
length
;
t
<
k
;
t
++
){
for
(
var
f
=
o
.
vertices
[
t
],
m
=
false
,
n
=
0
,
C
=
B
.
length
;
n
<
C
;
n
++
){
var
w
=
B
[
n
];
if
(
f
.
position
.
x
==
w
.
position
.
x
&&
f
.
position
.
y
==
w
.
position
.
y
&&
f
.
position
.
z
==
w
.
position
.
z
){
G
[
t
]
=
n
;
m
=
true
;
break
}}
if
(
!
m
){
G
[
t
]
=
B
.
length
;
B
.
push
(
new
THREE
.
Vertex
(
f
.
position
.
clone
()))}}
t
=
0
;
for
(
k
=
o
.
faces
.
length
;
t
<
k
;
t
++
){
f
=
o
.
faces
[
t
];
f
.
a
=
G
[
f
.
a
];
f
.
b
=
G
[
f
.
b
];
f
.
c
=
G
[
f
.
c
];
f
.
d
=
G
[
f
.
d
]}
o
.
vertices
=
B
})();
this
.
computeCentroids
();
this
.
computeFaceNormals
();
this
.
sortFacesByMaterial
()};
Cube
.
prototype
=
new
THREE
.
Geometry
;
Cube
.
prototype
.
constructor
=
Cube
;
screen
:{
uniforms
:{
tDiffuse
:{
type
:
"
t
"
,
value
:
0
,
texture
:
null
},
opacity
:{
type
:
"
f
"
,
value
:
1
}},
vertex_shader
:
"
varying vec2 vUv;
\n
void main() {
\n
vUv = vec2( uv.x, 1.0 - uv.y );
\n
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
\n
}
"
,
fragment_shader
:
"
varying vec2 vUv;
\n
uniform sampler2D tDiffuse;
\n
uniform float opacity;
\n
void main() {
\n
vec4 texel = texture2D( tDiffuse, vUv );
\n
gl_FragColor = opacity * texel;
\n
}
"
},
basic
:{
uniforms
:{},
vertex_shader
:
"
void main() {
\n
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
\n
}
"
,
fragment_shader
:
"
void main() {
\n
gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );
\n
}
"
}},
buildKernel
:
function
(
a
){
var
c
,
d
,
e
,
g
,
b
=
2
*
Math
.
ceil
(
a
*
3
)
+
1
;
if
(
b
>
25
)
b
=
25
;
g
=
(
b
-
1
)
*
0.5
;
d
=
Array
(
b
);
for
(
c
=
e
=
0
;
c
<
b
;
++
c
){
d
[
c
]
=
Math
.
exp
(
-
((
c
-
g
)
*
(
c
-
g
))
/
(
2
*
a
*
a
));
e
+=
d
[
c
]}
for
(
c
=
0
;
c
<
b
;
++
c
)
d
[
c
]
/=
e
;
return
d
}},
Cube
=
function
(
a
,
c
,
d
,
e
,
g
,
b
,
l
,
j
){
function
h
(
B
,
G
,
t
,
k
,
f
,
m
,
n
,
C
){
var
w
,
p
,
F
=
e
||
1
,
y
=
g
||
1
,
J
=
F
+
1
,
E
=
y
+
1
,
A
=
f
/
2
,
M
=
m
/
2
;
f
=
f
/
F
;
var
aa
=
m
/
y
,
N
=
o
.
vertices
.
length
;
if
(
B
==
"
x
"
&&
G
==
"
y
"
||
B
==
"
y
"
&&
G
==
"
x
"
)
w
=
"
z
"
;
else
if
(
B
==
"
x
"
&&
G
==
"
z
"
||
B
==
"
z
"
&&
G
==
"
x
"
)
w
=
"
y
"
;
else
if
(
B
==
"
z
"
&&
G
==
"
y
"
||
B
==
"
y
"
&&
G
==
"
z
"
)
w
=
"
x
"
;
for
(
p
=
0
;
p
<
E
;
p
++
)
for
(
m
=
0
;
m
<
J
;
m
++
){
var
U
=
new
THREE
.
Vector3
;
U
[
B
]
=
(
m
*
f
-
A
)
*
t
;
U
[
G
]
=
(
p
*
aa
-
M
)
*
k
;
U
[
w
]
=
n
;
o
.
vertices
.
push
(
new
THREE
.
Vertex
(
U
))}
for
(
p
=
0
;
p
<
y
;
p
++
)
for
(
m
=
0
;
m
<
F
;
m
++
){
o
.
faces
.
push
(
new
THREE
.
Face4
(
m
+
J
*
p
+
N
,
m
+
J
*
(
p
+
1
)
+
N
,
m
+
1
+
J
*
(
p
+
1
)
+
N
,
m
+
1
+
J
*
p
+
N
,
null
,
C
));
o
.
uvs
.
push
([
new
THREE
.
UV
(
m
/
F
,
p
/
y
),
new
THREE
.
UV
(
m
/
F
,(
p
+
1
)
/
y
),
new
THREE
.
UV
((
m
+
1
)
/
F
,(
p
+
1
)
/
y
),
new
THREE
.
UV
((
m
+
1
)
/
F
,
p
/
y
)])}}
THREE
.
Geometry
.
call
(
this
);
var
o
=
this
,
v
=
a
/
2
,
z
=
c
/
2
,
u
=
d
/
2
;
l
=
l
?
-
1
:
1
;
if
(
b
!==
undefined
)
if
(
b
instanceof
Array
)
this
.
materials
=
b
;
else
{
this
.
materials
=
[];
for
(
var
q
=
0
;
q
<
6
;
q
++
)
this
.
materials
.
push
([
b
])}
else
this
.
materials
=
[];
this
.
sides
=
{
px
:
true
,
nx
:
true
,
py
:
true
,
ny
:
true
,
pz
:
true
,
nz
:
true
};
if
(
j
!=
undefined
)
for
(
var
x
in
j
)
if
(
this
.
sides
[
x
]
!=
undefined
)
this
.
sides
[
x
]
=
j
[
x
];
this
.
sides
.
px
&&
h
(
"
z
"
,
"
y
"
,
1
*
l
,
-
1
,
d
,
c
,
-
v
,
this
.
materials
[
0
]);
this
.
sides
.
nx
&&
h
(
"
z
"
,
"
y
"
,
-
1
*
l
,
-
1
,
d
,
c
,
v
,
this
.
materials
[
1
]);
this
.
sides
.
py
&&
h
(
"
x
"
,
"
z
"
,
1
*
l
,
1
,
a
,
d
,
z
,
this
.
materials
[
2
]);
this
.
sides
.
ny
&&
h
(
"
x
"
,
"
z
"
,
1
*
l
,
-
1
,
a
,
d
,
-
z
,
this
.
materials
[
3
]);
this
.
sides
.
pz
&&
h
(
"
x
"
,
"
y
"
,
1
*
l
,
-
1
,
a
,
c
,
u
,
this
.
materials
[
4
]);
this
.
sides
.
nz
&&
h
(
"
x
"
,
"
y
"
,
-
1
*
l
,
-
1
,
a
,
c
,
-
u
,
this
.
materials
[
5
]);(
function
(){
for
(
var
B
=
[],
G
=
[],
t
=
0
,
k
=
o
.
vertices
.
length
;
t
<
k
;
t
++
){
for
(
var
f
=
o
.
vertices
[
t
],
m
=
false
,
n
=
0
,
C
=
B
.
length
;
n
<
C
;
n
++
){
var
w
=
B
[
n
];
if
(
f
.
position
.
x
==
w
.
position
.
x
&&
f
.
position
.
y
==
w
.
position
.
y
&&
f
.
position
.
z
==
w
.
position
.
z
){
G
[
t
]
=
n
;
m
=
true
;
break
}}
if
(
!
m
){
G
[
t
]
=
B
.
length
;
B
.
push
(
new
THREE
.
Vertex
(
f
.
position
.
clone
()))}}
t
=
0
;
for
(
k
=
o
.
faces
.
length
;
t
<
k
;
t
++
){
f
=
o
.
faces
[
t
];
f
.
a
=
G
[
f
.
a
];
f
.
b
=
G
[
f
.
b
];
f
.
c
=
G
[
f
.
c
];
f
.
d
=
G
[
f
.
d
]}
o
.
vertices
=
B
})();
this
.
computeCentroids
();
this
.
computeFaceNormals
();
this
.
sortFacesByMaterial
()};
Cube
.
prototype
=
new
THREE
.
Geometry
;
Cube
.
prototype
.
constructor
=
Cube
;
var
Cylinder
=
function
(
a
,
c
,
d
,
e
,
g
){
function
b
(
o
,
v
,
z
){
l
.
vertices
.
push
(
new
THREE
.
Vertex
(
new
THREE
.
Vector3
(
o
,
v
,
z
)))}
THREE
.
Geometry
.
call
(
this
);
var
l
=
this
,
j
=
Math
.
PI
,
h
;
for
(
h
=
0
;
h
<
a
;
h
++
)
b
(
Math
.
sin
(
2
*
j
*
h
/
a
)
*
c
,
Math
.
cos
(
2
*
j
*
h
/
a
)
*
c
,
0
);
for
(
h
=
0
;
h
<
a
;
h
++
)
b
(
Math
.
sin
(
2
*
j
*
h
/
a
)
*
d
,
Math
.
cos
(
2
*
j
*
h
/
a
)
*
d
,
e
);
for
(
h
=
0
;
h
<
a
;
h
++
)
l
.
faces
.
push
(
new
THREE
.
Face4
(
h
,
h
+
a
,
a
+
(
h
+
1
)
%
a
,(
h
+
1
)
%
a
));
if
(
d
!=
0
){
b
(
0
,
0
,
-
g
);
for
(
h
=
a
;
h
<
a
+
a
/
2
;
h
++
)
l
.
faces
.
push
(
new
THREE
.
Face4
(
2
*
a
,(
2
*
h
-
2
*
a
)
%
a
,(
2
*
h
-
2
*
a
+
1
)
%
a
,(
2
*
h
-
2
*
a
+
2
)
%
a
))}
if
(
c
!=
0
){
b
(
0
,
0
,
e
+
g
);
for
(
h
=
a
+
a
/
2
;
h
<
2
*
a
;
h
++
)
l
.
faces
.
push
(
new
THREE
.
Face4
((
2
*
h
-
2
*
a
+
2
)
%
a
+
a
,(
2
*
h
-
2
*
a
+
1
)
%
a
+
a
,(
2
*
h
-
2
*
a
)
%
a
+
a
,
2
*
a
+
1
))}
this
.
computeCentroids
();
this
.
computeFaceNormals
();
this
.
sortFacesByMaterial
()};
Cylinder
.
prototype
=
new
THREE
.
Geometry
;
Cylinder
.
prototype
.
constructor
=
Cylinder
;
var
Plane
=
function
(
a
,
c
,
d
,
e
){
THREE
.
Geometry
.
call
(
this
);
var
g
,
b
=
a
/
2
,
l
=
c
/
2
;
d
=
d
||
1
;
e
=
e
||
1
;
var
j
=
d
+
1
,
h
=
e
+
1
;
a
=
a
/
d
;
var
o
=
c
/
e
;
for
(
g
=
0
;
g
<
h
;
g
++
)
for
(
c
=
0
;
c
<
j
;
c
++
)
this
.
vertices
.
push
(
new
THREE
.
Vertex
(
new
THREE
.
Vector3
(
c
*
a
-
b
,
-
(
g
*
o
-
l
),
0
)));
for
(
g
=
0
;
g
<
e
;
g
++
)
for
(
c
=
0
;
c
<
d
;
c
++
){
this
.
faces
.
push
(
new
THREE
.
Face4
(
c
+
j
*
g
,
c
+
j
*
(
g
+
1
),
c
+
1
+
j
*
(
g
+
1
),
c
+
1
+
j
*
g
));
this
.
uvs
.
push
([
new
THREE
.
UV
(
c
/
d
,
g
/
e
),
new
THREE
.
UV
(
c
/
d
,(
g
+
1
)
/
e
),
new
THREE
.
UV
((
c
+
1
)
/
d
,(
g
+
1
)
/
e
),
new
THREE
.
UV
((
c
+
1
)
/
d
,
g
/
e
)])}
this
.
computeCentroids
();
this
.
computeFaceNormals
();
this
.
sortFacesByMaterial
()};
...
...
examples/postprocessing.html
浏览文件 @
84d1b582
...
...
@@ -127,20 +127,6 @@
gl_FragColor
=
vec4
(
cResult
,
cTextureScreen
.
a
);
}
</script>
<!-- Render parameter modulated texture fragment shader -->
<script
id=
"fs-screen"
type=
"x-shader/x-fragment"
>
varying
vec2
vUv
;
uniform
sampler2D
tDiffuse
;
uniform
float
opacity
;
void
main
(
void
)
{
vec4
texel
=
texture2D
(
tDiffuse
,
vUv
);
gl_FragColor
=
opacity
*
texel
;
}
</script>
<!-- Time modulated procedural color fragment shader -->
<script
id=
"fs-colors"
type=
"x-shader/x-fragment"
>
...
...
@@ -219,13 +205,17 @@
}
);
var
screen_shader
=
ShaderUtils
.
lib
[
"
screen
"
];
var
screen_uniforms
=
Uniforms
.
clone
(
screen_shader
.
uniforms
);
screen_uniforms
[
"
tDiffuse
"
].
texture
=
rtTexture1
;
screen_uniforms
[
"
opacity
"
].
value
=
0.4
;
materialScreen
=
new
THREE
.
MeshShaderMaterial
(
{
uniforms
:
{
tDiffuse
:
{
type
:
"
t
"
,
value
:
0
,
texture
:
rtTexture1
},
opacity
:
{
type
:
"
f
"
,
value
:
0.4
}
},
vertex_shader
:
getText
(
'
vs-generic
'
),
fragment_shader
:
getText
(
'
fs-screen
'
),
uniforms
:
screen_uniforms
,
vertex_shader
:
screen_shader
.
vertex_shader
,
fragment_shader
:
screen_shader
.
fragment_shader
,
blending
:
THREE
.
AdditiveBlending
}
);
...
...
@@ -245,18 +235,18 @@
blurx
=
new
THREE
.
Vector2
(
increment
,
0.0
),
blury
=
new
THREE
.
Vector2
(
0.0
,
increment
);
var
shader
=
ShaderUtils
.
lib
[
"
convolution
"
];
var
uniforms
=
Uniforms
.
clone
(
shader
.
uniforms
);
var
convolution_
shader
=
ShaderUtils
.
lib
[
"
convolution
"
];
var
convolution_uniforms
=
Uniforms
.
clone
(
convolution_
shader
.
uniforms
);
uniforms
[
"
tDiffuse
"
].
texture
=
rtTexture1
;
uniforms
[
"
uImageIncrement
"
].
value
=
blurx
;
uniforms
[
"
cKernel
"
].
value
=
ShaderUtils
.
buildKernel
(
4.0
);
convolution_
uniforms
[
"
tDiffuse
"
].
texture
=
rtTexture1
;
convolution_
uniforms
[
"
uImageIncrement
"
].
value
=
blurx
;
convolution_
uniforms
[
"
cKernel
"
].
value
=
ShaderUtils
.
buildKernel
(
4.0
);
materialConvolution
=
new
THREE
.
MeshShaderMaterial
(
{
uniforms
:
uniforms
,
vertex_shader
:
shader
.
vertex_shader
,
fragment_shader
:
shader
.
fragment_shader
uniforms
:
convolution_
uniforms
,
vertex_shader
:
convolution_
shader
.
vertex_shader
,
fragment_shader
:
convolution_
shader
.
fragment_shader
}
);
...
...
src/extras/ShaderUtils.js
浏览文件 @
84d1b582
...
...
@@ -7,69 +7,73 @@ var ShaderUtils = {
- based on Nvidia Cg tutorial
------------------------------------------------------------------------- */
'
fresnel
'
:
{
'
fresnel
'
:
{
uniforms
:
{
uniforms
:
{
"
mRefractionRatio
"
:
{
type
:
"
f
"
,
value
:
1.02
},
"
mFresnelBias
"
:
{
type
:
"
f
"
,
value
:
0.1
},
"
mFresnelPower
"
:
{
type
:
"
f
"
,
value
:
2.0
},
"
mFresnelScale
"
:
{
type
:
"
f
"
,
value
:
1.0
},
"
tCube
"
:
{
type
:
"
t
"
,
value
:
1
,
texture
:
null
}
"
mRefractionRatio
"
:
{
type
:
"
f
"
,
value
:
1.02
},
"
mFresnelBias
"
:
{
type
:
"
f
"
,
value
:
0.1
},
"
mFresnelPower
"
:
{
type
:
"
f
"
,
value
:
2.0
},
"
mFresnelScale
"
:
{
type
:
"
f
"
,
value
:
1.0
},
"
tCube
"
:
{
type
:
"
t
"
,
value
:
1
,
texture
:
null
}
},
},
fragment_shader
:
[
fragment_shader
:
[
"
uniform samplerCube tCube;
"
,
"
uniform samplerCube tCube;
"
,
"
varying vec3 vReflect;
"
,
"
varying vec3 vRefract[3];
"
,
"
varying float vReflectionFactor;
"
,
"
varying vec3 vReflect;
"
,
"
varying vec3 vRefract[3];
"
,
"
varying float vReflectionFactor;
"
,
"
void main() {
"
,
"
vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );
"
,
"
vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );
"
,
"
void main() {
"
,
"
vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );
"
,
"
vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );
"
,
"
refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;
"
,
"
refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;
"
,
"
refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;
"
,
"
refractedColor.a = 1.0;
"
,
"
refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;
"
,
"
refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;
"
,
"
refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;
"
,
"
refractedColor.a = 1.0;
"
,
"
gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );
"
,
"
}
"
"
gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );
"
,
"
}
"
].
join
(
"
\n
"
),
].
join
(
"
\n
"
),
vertex_shader
:
[
vertex_shader
:
[
"
uniform float mRefractionRatio;
"
,
"
uniform float mFresnelBias;
"
,
"
uniform float mFresnelScale;
"
,
"
uniform float mFresnelPower;
"
,
"
uniform float mRefractionRatio;
"
,
"
uniform float mFresnelBias;
"
,
"
uniform float mFresnelScale;
"
,
"
uniform float mFresnelPower;
"
,
"
varying vec3 vReflect;
"
,
"
varying vec3 vRefract[3];
"
,
"
varying float vReflectionFactor;
"
,
"
varying vec3 vReflect;
"
,
"
varying vec3 vRefract[3];
"
,
"
varying float vReflectionFactor;
"
,
"
void main(void) {
"
,
"
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
"
,
"
vec4 mPosition = objectMatrix * vec4( position, 1.0 );
"
,
"
void main() {
"
,
"
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
"
,
"
vec4 mPosition = objectMatrix * vec4( position, 1.0 );
"
,
"
vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );
"
,
"
vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );
"
,
"
vec3 I = mPosition.xyz - cameraPosition;
"
,
"
vec3 I = mPosition.xyz - cameraPosition;
"
,
"
vReflect = reflect( I, nWorld );
"
,
"
vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );
"
,
"
vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );
"
,
"
vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );
"
,
"
vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );
"
,
"
vReflect = reflect( I, nWorld );
"
,
"
vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );
"
,
"
vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );
"
,
"
vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );
"
,
"
vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );
"
,
"
gl_Position = projectionMatrix * mvPosition;
"
,
"
}
"
"
gl_Position = projectionMatrix * mvPosition;
"
,
"
}
"
].
join
(
"
\n
"
)
].
join
(
"
\n
"
)
},
...
...
@@ -340,8 +344,10 @@ var ShaderUtils = {
"
#define KERNEL_SIZE 25.0
"
,
"
void main(void) {
"
,
"
vUv = uv - ((KERNEL_SIZE - 1.0) / 2.0) * uImageIncrement;
"
,
"
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
"
,
"
}
"
].
join
(
"
\n
"
),
...
...
@@ -357,6 +363,7 @@ var ShaderUtils = {
"
uniform float cKernel[KERNEL_SIZE];
"
,
"
void main(void) {
"
,
"
vec2 imageCoord = vUv;
"
,
"
vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );
"
,
"
for( int i=0; i<KERNEL_SIZE; ++i ) {
"
,
...
...
@@ -364,13 +371,55 @@ var ShaderUtils = {
"
imageCoord += uImageIncrement;
"
,
"
}
"
,
"
gl_FragColor = sum;
"
,
"
}
"
].
join
(
"
\n
"
)
},
/* -------------------------------------------------------------------------
Full-screen textured quad shader
------------------------------------------------------------------------- */
'
screen
'
:
{
uniforms
:
{
tDiffuse
:
{
type
:
"
t
"
,
value
:
0
,
texture
:
null
},
opacity
:
{
type
:
"
f
"
,
value
:
1.0
}
},
vertex_shader
:
[
"
varying vec2 vUv;
"
,
"
void main() {
"
,
"
vUv = vec2( uv.x, 1.0 - uv.y );
"
,
"
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
"
,
"
}
"
].
join
(
"
\n
"
),
fragment_shader
:
[
"
varying vec2 vUv;
"
,
"
uniform sampler2D tDiffuse;
"
,
"
uniform float opacity;
"
,
"
void main() {
"
,
"
vec4 texel = texture2D( tDiffuse, vUv );
"
,
"
gl_FragColor = opacity * texel;
"
,
"
}
"
].
join
(
"
\n
"
)
},
/* -------------------------------------------------------------------------
Simple test shader
------------------------------------------------------------------------- */
...
...
@@ -393,7 +442,7 @@ var ShaderUtils = {
"
void main() {
"
,
"
gl_FragColor = vec4(
1.0, 0.0, 0.0, 0.5
);
"
,
"
gl_FragColor = vec4(
1.0, 0.0, 0.0, 0.5
);
"
,
"
}
"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录