Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Ablesons
three.js
提交
a28e942f
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,发现更多精彩内容 >>
提交
a28e942f
编写于
2月 27, 2013
作者:
M
Mr.doob
浏览文件
操作
浏览文件
下载
差异文件
Merge @zz85/buffer_attributes into dev
上级
31175e9b
4c2286ca
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
465 addition
and
500 deletion
+465
-500
examples/webgl_buffergeometry_custom_attributes_particles.html
...les/webgl_buffergeometry_custom_attributes_particles.html
+253
-0
src/renderers/WebGLRenderer.js
src/renderers/WebGLRenderer.js
+154
-351
src/renderers/WebGLRenderer2.js
src/renderers/WebGLRenderer2.js
+58
-149
未找到文件。
examples/webgl_buffergeometry_custom_attributes_particles.html
0 → 100644
浏览文件 @
a28e942f
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<title>
three.js webgl - buffer geometry custom attributes [particles]
</title>
<meta
charset=
"utf-8"
>
<meta
name=
"viewport"
content=
"width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
>
<style>
body
{
color
:
#ffffff
;
font-family
:
Monospace
;
font-size
:
13px
;
text-align
:
center
;
font-weight
:
bold
;
background-color
:
#000000
;
margin
:
0px
;
overflow
:
hidden
;
}
#info
{
color
:
#fff
;
background-color
:
rgba
(
0
,
0
,
0
,
0.75
);
position
:
relative
;
top
:
0px
;
width
:
100%
;
padding
:
5px
;
z-index
:
100
;
width
:
33em
;
margin
:
0
auto
-2em
;
}
a
{
color
:
#ff0000
}
</style>
</head>
<body>
<div
id=
"info"
><a
href=
"http://threejs.org"
target=
"_blank"
>
three.js
</a>
- buffergeometry custom attributes example - particles
</div>
<div
id=
"container"
></div>
<script
src=
"../build/three.min.js"
></script>
<script
src=
"../src/renderers/WebGLRenderer.js"
></script>
<script
src=
"js/Detector.js"
></script>
<script
src=
"js/libs/stats.min.js"
></script>
<script
type=
"x-shader/x-vertex"
id=
"vertexshader"
>
uniform
float
amplitude
;
attribute
float
size
;
attribute
vec3
customColor
;
varying
vec3
vColor
;
void
main
()
{
vColor
=
customColor
;
vec4
mvPosition
=
modelViewMatrix
*
vec4
(
position
,
1.0
);
//gl_PointSize = size;
gl_PointSize
=
size
*
(
300.0
/
length
(
mvPosition
.
xyz
)
);
gl_Position
=
projectionMatrix
*
mvPosition
;
}
</script>
<script
type=
"x-shader/x-fragment"
id=
"fragmentshader"
>
uniform
vec3
color
;
uniform
sampler2D
texture
;
varying
vec3
vColor
;
void
main
()
{
gl_FragColor
=
vec4
(
color
*
vColor
,
1.0
);
gl_FragColor
=
gl_FragColor
*
texture2D
(
texture
,
gl_PointCoord
);
}
</script>
<script>
if
(
!
Detector
.
webgl
)
Detector
.
addGetWebGLMessage
();
var
renderer
,
scene
,
camera
,
stats
;
var
sphere
,
uniforms
,
geometry
;
var
noise
=
[];
var
values_size
;
//500000
var
particles
=
100000
;
var
WIDTH
=
window
.
innerWidth
;
var
HEIGHT
=
window
.
innerHeight
;
init
();
animate
();
function
init
()
{
camera
=
new
THREE
.
PerspectiveCamera
(
40
,
WIDTH
/
HEIGHT
,
1
,
10000
);
camera
.
position
.
z
=
300
;
scene
=
new
THREE
.
Scene
();
var
attributes
=
{
size
:
{
type
:
'
f
'
,
value
:
null
},
customColor
:
{
type
:
'
c
'
,
value
:
null
}
};
uniforms
=
{
amplitude
:
{
type
:
"
f
"
,
value
:
1.0
},
color
:
{
type
:
"
c
"
,
value
:
new
THREE
.
Color
(
0xffffff
)
},
texture
:
{
type
:
"
t
"
,
value
:
THREE
.
ImageUtils
.
loadTexture
(
"
textures/sprites/spark1.png
"
)
},
};
var
shaderMaterial
=
new
THREE
.
ShaderMaterial
(
{
uniforms
:
uniforms
,
attributes
:
attributes
,
vertexShader
:
document
.
getElementById
(
'
vertexshader
'
).
textContent
,
fragmentShader
:
document
.
getElementById
(
'
fragmentshader
'
).
textContent
,
blending
:
THREE
.
AdditiveBlending
,
depthTest
:
false
,
transparent
:
true
});
var
radius
=
200
;
geometry
=
new
THREE
.
BufferGeometry
();
// geometry.dynamic = true;
geometry
.
attributes
=
{
position
:
{
itemSize
:
3
,
array
:
new
Float32Array
(
particles
*
3
),
numItems
:
particles
*
3
},
customColor
:
{
itemSize
:
3
,
array
:
new
Float32Array
(
particles
*
3
),
numItems
:
particles
*
3
},
size
:
{
itemSize
:
1
,
array
:
new
Float32Array
(
particles
),
numItems
:
particles
*
1
,
dynamic
:
true
},
}
values_size
=
geometry
.
attributes
.
size
.
array
;
var
positions
=
geometry
.
attributes
.
position
.
array
;
var
values_color
=
geometry
.
attributes
.
customColor
.
array
;
sphere
=
new
THREE
.
ParticleSystem
(
geometry
,
shaderMaterial
);
//sphere.sortParticles = true;
var
color
=
new
THREE
.
Color
(
0xffaa00
);;
for
(
var
v
=
0
;
v
<
particles
;
v
++
)
{
values_size
[
v
]
=
10
;
positions
[
v
*
3
+
0
]
=
(
Math
.
random
()
*
2
-
1
)
*
radius
;
positions
[
v
*
3
+
1
]
=
(
Math
.
random
()
*
2
-
1
)
*
radius
;
positions
[
v
*
3
+
2
]
=
(
Math
.
random
()
*
2
-
1
)
*
radius
;
if
(
positions
[
v
*
3
+
0
]
<
0
)
color
.
setHSL
(
0.5
+
0.1
*
(
v
/
particles
),
0.7
,
0.1
);
else
color
.
setHSL
(
0.0
+
0.1
*
(
v
/
particles
),
0.9
,
0.1
);
values_color
[
v
*
3
+
0
]
=
color
.
r
;
values_color
[
v
*
3
+
1
]
=
color
.
g
;
values_color
[
v
*
3
+
2
]
=
color
.
b
;
}
scene
.
add
(
sphere
);
renderer
=
new
THREE
.
WebGLRenderer
(
{
clearColor
:
0x000000
,
clearAlpha
:
1
}
);
renderer
.
setSize
(
WIDTH
,
HEIGHT
);
var
container
=
document
.
getElementById
(
'
container
'
);
container
.
appendChild
(
renderer
.
domElement
);
stats
=
new
Stats
();
stats
.
domElement
.
style
.
position
=
'
absolute
'
;
stats
.
domElement
.
style
.
top
=
'
0px
'
;
container
.
appendChild
(
stats
.
domElement
);
//
window
.
addEventListener
(
'
resize
'
,
onWindowResize
,
false
);
}
function
onWindowResize
()
{
camera
.
aspect
=
window
.
innerWidth
/
window
.
innerHeight
;
camera
.
updateProjectionMatrix
();
renderer
.
setSize
(
window
.
innerWidth
,
window
.
innerHeight
);
}
function
animate
()
{
requestAnimationFrame
(
animate
);
render
();
stats
.
update
();
}
function
render
()
{
var
time
=
Date
.
now
()
*
0.005
;
sphere
.
rotation
.
z
=
0.01
*
time
;
for
(
var
i
=
0
;
i
<
particles
;
i
++
)
{
values_size
[
i
]
=
14
+
13
*
Math
.
sin
(
0.1
*
i
+
time
);
}
geometry
.
attributes
.
size
.
needsUpdate
=
true
;
renderer
.
render
(
scene
,
camera
);
}
</script>
</body>
</html>
src/renderers/WebGLRenderer.js
浏览文件 @
a28e942f
此差异已折叠。
点击以展开。
src/renderers/WebGLRenderer2.js
浏览文件 @
a28e942f
...
...
@@ -105,7 +105,7 @@ THREE.WebGLRenderer = function ( parameters ) {
_usedTextureUnits
=
0
,
// GL state
// GL state
_viewportX
=
0
,
_viewportY
=
0
,
...
...
@@ -486,54 +486,31 @@ THREE.WebGLRenderer = function ( parameters ) {
var
attributes
=
geometry
.
attributes
;
var
index
=
attributes
[
"
index
"
];
var
position
=
attributes
[
"
position
"
];
var
normal
=
attributes
[
"
normal
"
];
var
uv
=
attributes
[
"
uv
"
];
var
color
=
attributes
[
"
color
"
];
var
tangent
=
attributes
[
"
tangent
"
];
var
attributeName
,
attributeItem
;
if
(
geometry
.
elementsNeedUpdate
&&
index
!==
undefined
)
{
for
(
attributeName
in
attributes
)
{
renderer
.
setDynamicIndexBuffer
(
index
.
buffer
,
index
.
array
)
;
attributeItem
=
attributes
[
attributeName
]
;
}
if
(
geometry
.
verticesNeedUpdate
&&
position
!==
undefined
)
{
renderer
.
setDynamicArrayBuffer
(
position
.
buffer
,
position
.
array
);
}
if
(
geometry
.
normalsNeedUpdate
&&
normal
!==
undefined
)
{
renderer
.
setDynamicArrayBuffer
(
normal
.
buffer
,
normal
.
array
);
}
if
(
attributeItem
.
needsUpdate
)
{
if
(
geometry
.
uvsNeedUpdate
&&
uv
!==
undefined
)
{
if
(
attributeName
===
'
index
'
)
{
renderer
.
setDynamicArrayBuffer
(
uv
.
buffer
,
uv
.
array
);
renderer
.
setDynamicIndexBuffer
(
attributeItem
.
buffer
,
attributeItem
.
array
);
}
if
(
geometry
.
colorsNeedUpdate
&&
color
!==
undefined
)
{
renderer
.
setDynamicArrayBuffer
(
color
.
buffer
,
color
.
array
);
}
}
else
{
if
(
geometry
.
tangentsNeedUpdate
&&
tangent
!==
undefined
)
{
renderer
.
setDynamicArrayBuffer
(
attributeItem
.
buffer
,
attributeItem
.
array
);
renderer
.
setDynamicArrayBuffer
(
tangent
.
buffer
,
tangent
.
array
);
}
}
attributeItem
.
needsUpdate
=
false
;
if
(
dispose
)
{
}
for
(
var
i
in
geometry
.
attributes
)
{
if
(
dispose
&&
!
attributeItem
.
dynamic
)
{
delete
geometry
.
attributes
[
i
]
.
array
;
delete
attributeItem
.
array
;
}
...
...
@@ -601,7 +578,7 @@ THREE.WebGLRenderer = function ( parameters ) {
}
}
renderer
.
setDynamicArrayBuffer
(
object
.
__webglNormalBuffer
,
object
.
normalArray
);
renderer
.
setFloatAttribute
(
program
.
attributes
.
normal
,
object
.
__webglNormalBuffer
,
3
,
0
);
...
...
@@ -630,11 +607,13 @@ THREE.WebGLRenderer = function ( parameters ) {
if
(
material
.
visible
===
false
)
return
;
var
program
,
attributes
,
linewidth
,
primitives
,
a
,
attribute
;
var
program
,
programAttributes
,
linewidth
,
primitives
,
a
,
attribute
,
geometryAttributes
;
var
attributeItem
,
attributeName
,
attributePointer
,
attributeSize
;
program
=
setProgram
(
camera
,
lights
,
fog
,
material
,
object
);
attributes
=
program
.
attributes
;
programAttributes
=
program
.
attributes
;
geometryAttributes
=
geometry
.
attributes
;
var
updateBuffers
=
false
,
wireframeBit
=
material
.
wireframe
?
1
:
0
,
...
...
@@ -657,7 +636,7 @@ THREE.WebGLRenderer = function ( parameters ) {
if
(
object
instanceof
THREE
.
Mesh
)
{
var
index
=
geometry
.
a
ttributes
[
"
index
"
];
var
index
=
geometry
A
ttributes
[
"
index
"
];
// indexed triangles
...
...
@@ -677,60 +656,25 @@ THREE.WebGLRenderer = function ( parameters ) {
if
(
updateBuffers
)
{
// vertices
var
position
=
geometry
.
attributes
[
"
position
"
];
var
positionSize
=
position
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
position
,
position
.
buffer
,
positionSize
,
startIndex
*
positionSize
*
4
);
// normals
var
normal
=
geometry
.
attributes
[
"
normal
"
];
if
(
attributes
.
normal
>=
0
&&
normal
)
{
var
normalSize
=
normal
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
normal
,
normal
.
buffer
,
normalSize
,
startIndex
*
normalSize
*
4
);
}
// uvs
var
uv
=
geometry
.
attributes
[
"
uv
"
];
if
(
attributes
.
uv
>=
0
&&
uv
)
{
var
uvSize
=
uv
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
uv
,
uv
.
buffer
,
uvSize
,
startIndex
*
uvSize
*
4
);
}
// colors
var
color
=
geometry
.
attributes
[
"
color
"
];
if
(
attributes
.
color
>=
0
&&
color
)
{
for
(
attributeName
in
geometryAttributes
)
{
var
colorSize
=
color
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
color
,
color
.
buffer
,
colorSize
,
startIndex
*
colorSize
*
4
);
if
(
attributeName
===
'
index
'
)
continue
;
}
// tangents
attributePointer
=
programAttributes
[
attributeName
];
attributeItem
=
geometryAttributes
[
attributeName
];
attributeSize
=
attributeItem
.
itemSize
;
var
tangent
=
geometry
.
attributes
[
"
tangent
"
];
if
(
attributePointer
>=
0
)
{
if
(
attributes
.
tangent
>=
0
&&
tangent
)
{
renderer
.
setFloatAttribute
(
attributePointer
,
attributeItem
.
buffer
,
attributeSize
,
startIndex
*
attributeSize
*
4
);
var
tangentSize
=
tangent
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
tangent
,
tangent
.
buffer
,
tangentSize
,
startIndex
*
tangentSize
*
4
);
}
}
}
// render indexed triangles
renderer
.
drawTriangleElements
(
index
.
buffer
,
offsets
[
i
].
count
,
offsets
[
i
].
start
*
2
);
_this
.
info
.
render
.
calls
++
;
...
...
@@ -745,59 +689,26 @@ THREE.WebGLRenderer = function ( parameters ) {
if
(
updateBuffers
)
{
// vertices
var
position
=
geometry
.
attributes
[
"
position
"
];
var
positionSize
=
position
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
position
,
position
.
buffer
,
positionSize
,
0
);
// normals
var
normal
=
geometry
.
attributes
[
"
normal
"
];
if
(
attributes
.
normal
>=
0
&&
normal
)
{
var
normalSize
=
normal
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
normal
,
normal
.
buffer
,
normalSize
,
0
);
}
// uvs
var
uv
=
geometry
.
attributes
[
"
uv
"
];
if
(
attributes
.
uv
>=
0
&&
uv
)
{
var
uvSize
=
uv
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
uv
,
uv
.
buffer
,
uvSize
,
0
);
}
// colors
for
(
attributeName
in
geometryAttributes
)
{
var
color
=
geometry
.
attributes
[
"
color
"
];
attributePointer
=
programAttributes
[
attributeName
];
attributeItem
=
geometryAttributes
[
attributeName
];
attributeSize
=
attributeItem
.
itemSize
;
if
(
attributes
.
color
>=
0
&&
color
)
{
if
(
attributePointer
>=
0
)
{
var
colorSize
=
color
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
color
,
color
.
buffer
,
colorSize
,
0
);
renderer
.
setFloatAttribute
(
attributePointer
,
attributeItem
.
buffer
,
attributeSize
,
0
);
}
// tangents
var
tangent
=
geometry
.
attributes
[
"
tangent
"
];
if
(
attributes
.
tangent
>=
0
&&
tangent
)
{
var
tangentSize
=
tangent
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
tangent
,
tangent
.
buffer
,
tangentSize
,
0
);
}
}
}
var
position
=
geometry
.
attributes
[
"
position
"
];
// render non-indexed triangles
renderer
.
drawTriangles
(
position
.
numItems
/
3
)
_this
.
info
.
render
.
calls
++
;
...
...
@@ -812,23 +723,22 @@ THREE.WebGLRenderer = function ( parameters ) {
if
(
updateBuffers
)
{
// vertices
for
(
attributeName
in
geometryAttributes
)
{
var
position
=
geometry
.
attributes
[
"
position
"
];
var
positionSize
=
position
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
position
,
position
.
buffer
,
positionSize
,
0
);
// colors
attributePointer
=
programAttributes
[
attributeName
];
attributeItem
=
geometryAttributes
[
attributeName
];
attributeSize
=
attributeItem
.
itemSize
;
var
color
=
geometry
.
attributes
[
"
color
"
];
if
(
attributePointer
>=
0
)
{
if
(
attributes
.
color
>=
0
&&
color
)
{
renderer
.
setFloatAttribute
(
attributePointer
,
attributeItem
.
buffer
,
attributeSize
,
0
);
var
colorSize
=
color
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
color
,
color
.
buffer
,
colorSize
,
0
);
}
}
var
position
=
geometryAttributes
[
"
position
"
];
// render particles
renderer
.
drawPoints
(
position
.
numItems
/
3
);
...
...
@@ -841,23 +751,22 @@ THREE.WebGLRenderer = function ( parameters ) {
if
(
updateBuffers
)
{
// vertices
for
(
attributeName
in
geometryAttributes
)
{
var
position
=
geometry
.
attributes
[
"
position
"
];
var
positionSize
=
position
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
position
,
position
.
buffer
,
positionSize
,
0
)
;
attributePointer
=
programAttributes
[
attributeName
];
attributeItem
=
geometryAttributes
[
attributeName
]
;
attributeSize
=
attributeItem
.
itemSize
;
// colors
if
(
attributePointer
>=
0
)
{
var
color
=
geometry
.
attributes
[
"
color
"
]
;
renderer
.
setFloatAttribute
(
attributePointer
,
attributeItem
.
buffer
,
attributeSize
,
0
)
;
if
(
attributes
.
color
>=
0
&&
color
)
{
var
colorSize
=
color
.
itemSize
;
renderer
.
setFloatAttribute
(
attributes
.
color
,
color
.
buffer
,
colorSize
,
0
);
}
}
var
position
=
geometryAttributes
[
"
position
"
];
// render lines
renderer
.
setLineWidth
(
material
.
linewidth
);
renderer
.
drawLineStrip
(
position
.
numItems
/
3
);
...
...
@@ -3111,7 +3020,7 @@ THREE.WebGLRenderer = function ( parameters ) {
if
(
!
light
.
visible
)
continue
;
_direction
.
getPositionFromMatrix
(
light
.
matrixWorld
);
_vector3
.
getPositionFromMatrix
(
light
.
target
.
matrixWorld
);
_vector3
.
getPositionFromMatrix
(
light
.
target
.
matrixWorld
);
_direction
.
sub
(
_vector3
);
_direction
.
normalize
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录