Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
车家大少爷
three.js
提交
4c037041
T
three.js
项目概览
车家大少爷
/
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,发现更多精彩内容 >>
未验证
提交
4c037041
编写于
2月 26, 2018
作者:
M
Mr.doob
提交者:
GitHub
2月 26, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #13120 from sneha-belkhale/dev
Curvature Estimation Example
上级
15a08147
d2af1656
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
378 addition
and
0 deletion
+378
-0
examples/webgl_curvature_estimation.html
examples/webgl_curvature_estimation.html
+378
-0
未找到文件。
examples/webgl_curvature_estimation.html
0 → 100644
浏览文件 @
4c037041
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<title>
three.js webgl - shader - curvature [ninja]
</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
{
position
:
absolute
;
top
:
0px
;
width
:
100%
;
padding
:
5px
;
}
a
{
color
:
#ffffff
;
}
#oldie
a
{
color
:
#da0
}
</style>
</head>
<body>
<div
id=
"container"
></div>
<div
id=
"info"
><a
href=
"http://threejs.org"
target=
"_blank"
rel=
"noopener"
>
three.js
</a>
- curvature estimation of a geometry by
<a
href=
"http://codercat.club"
target=
"_blank"
rel=
"noopener"
>
CoderCat
</a></div>
<script
src=
"../build/three.js"
></script>
<script
src=
"js/Detector.js"
></script>
<script
src=
"js/controls/OrbitControls.js"
></script>
<script
src=
"js/loaders/OBJLoader.js"
></script>
<script
src=
"js/libs/dat.gui.min.js"
></script>
<script
id=
"vertexShaderRaw"
type=
"x-shader/x-vertex"
>
attribute
float
curvature
;
varying
float
vCurvature
;
void
main
()
{
vec3
p
=
position
;
vec4
modelViewPosition
=
modelViewMatrix
*
vec4
(
p
,
1.0
);
gl_Position
=
projectionMatrix
*
modelViewPosition
;
vCurvature
=
curvature
;
}
</script>
<script
id=
"fragmentShaderRaw"
type=
"x-shader/x-fragment"
>
varying
vec3
vViewPosition
;
varying
float
vCurvature
;
void
main
()
{
gl_FragColor
=
vec4
(
vCurvature
*
2.0
,
0.0
,
0.0
,
0.0
);
}
</script>
<script>
if
(
!
Detector
.
webgl
)
Detector
.
addGetWebGLMessage
();
var
container
;
var
camera
,
scene
,
renderer
;
var
ninjaMeshRaw
,
curvatureAttribute
;
init
();
animate
();
//returns average of elements in a dictionary
function
average
(
dict
)
{
var
sum
=
0
;
var
length
=
0
;
Object
.
keys
(
dict
).
forEach
(
function
(
key
)
{
sum
+=
dict
[
key
];
length
++
;
});
return
sum
/
length
;
}
//clamp a number between min and max
function
clamp
(
number
,
min
,
max
)
{
return
Math
.
max
(
min
,
Math
.
min
(
number
,
max
)
);
}
//filter the curvature array to only show concave values
function
filterConcave
(
curvature
)
{
for
(
var
i
=
0
;
i
<
curvature
.
length
;
i
++
)
{
curvature
[
i
]
=
Math
.
abs
(
clamp
(
curvature
[
i
],
-
1
,
0
)
);
}
}
//filter the curvature array to only show convex values
function
filterConvex
(
curvature
)
{
for
(
var
i
=
0
;
i
<
curvature
.
length
;
i
++
)
{
curvature
[
i
]
=
clamp
(
curvature
[
i
],
0
,
1
);
}
}
//filter the curvature array to show both the concave and convex values
function
filterBoth
(
curvature
)
{
for
(
var
i
=
0
;
i
<
curvature
.
length
;
i
++
)
{
curvature
[
i
]
=
Math
.
abs
(
curvature
[
i
]
);
}
}
//initialize the scene
function
init
()
{
scene
=
new
THREE
.
Scene
();
camera
=
new
THREE
.
PerspectiveCamera
(
75
,
window
.
innerWidth
/
window
.
innerHeight
,
0.1
,
1000
);
camera
.
position
.
x
=
-
23
;
camera
.
position
.
y
=
2
;
camera
.
position
.
z
=
24
;
controls
=
new
THREE
.
OrbitControls
(
camera
);
renderer
=
new
THREE
.
WebGLRenderer
();
renderer
.
setSize
(
window
.
innerWidth
,
window
.
innerHeight
);
renderer
.
autoClear
=
false
;
document
.
body
.
appendChild
(
renderer
.
domElement
);
var
loader
=
new
THREE
.
OBJLoader
();
//load the obj
loader
.
load
(
'
models/obj/ninja/ninjaHead_Low.obj
'
,
function
(
object
)
{
object
.
traverse
(
function
(
child
)
{
if
(
child
instanceof
THREE
.
Mesh
)
{
bufferGeo
=
child
.
geometry
;
bufferGeo
.
center
();
var
dict
=
{};
for
(
var
i
=
0
;
i
<
bufferGeo
.
attributes
.
position
.
count
;
i
+=
3
)
{
//create a dictionary of every position, and its neighboring positions
var
array
=
bufferGeo
.
attributes
.
position
.
array
;
var
normArray
=
bufferGeo
.
attributes
.
normal
.
array
;
var
posA
=
new
THREE
.
Vector3
(
array
[
3
*
i
],
array
[
3
*
i
+
1
],
array
[
3
*
i
+
2
]);
var
posB
=
new
THREE
.
Vector3
(
array
[
3
*
(
i
+
1
)
],
array
[
3
*
(
i
+
1
)
+
1
],
array
[
3
*
(
i
+
1
)
+
2
]);
var
posC
=
new
THREE
.
Vector3
(
array
[
3
*
(
i
+
2
)
],
array
[
3
*
(
i
+
2
)
+
1
],
array
[
3
*
(
i
+
2
)
+
2
]);
var
normA
=
new
THREE
.
Vector3
(
normArray
[
3
*
i
],
normArray
[
3
*
i
+
1
],
normArray
[
3
*
i
+
2
]).
normalize
();
var
normB
=
new
THREE
.
Vector3
(
normArray
[
3
*
(
i
+
1
)
],
normArray
[
3
*
(
i
+
1
)
+
1
],
normArray
[
3
*
(
i
+
1
)
+
2
]).
normalize
();
var
normC
=
new
THREE
.
Vector3
(
normArray
[
3
*
(
i
+
2
)
],
normArray
[
3
*
(
i
+
2
)
+
1
],
normArray
[
3
*
(
i
+
2
)
+
2
]).
normalize
();
var
strA
=
posA
.
toArray
().
toString
();
var
strB
=
posB
.
toArray
().
toString
();
var
strC
=
posC
.
toArray
().
toString
();
var
posB_A
=
new
THREE
.
Vector3
().
subVectors
(
posB
,
posA
);
var
posB_C
=
new
THREE
.
Vector3
().
subVectors
(
posB
,
posC
);
var
posC_A
=
new
THREE
.
Vector3
().
subVectors
(
posC
,
posA
);
var
normB_A
=
new
THREE
.
Vector3
().
subVectors
(
normB
,
normA
);
var
normB_C
=
new
THREE
.
Vector3
().
subVectors
(
normB
,
normC
);
var
normC_A
=
new
THREE
.
Vector3
().
subVectors
(
normC
,
normA
);
var
b2a
=
normB
.
dot
(
posB_A
.
normalize
()
);
var
b2c
=
normB
.
dot
(
posB_C
.
normalize
()
);
var
c2a
=
normC
.
dot
(
posC_A
.
normalize
()
);
var
a2b
=
-
normA
.
dot
(
posB_A
.
normalize
()
);
var
c2b
=
-
normC
.
dot
(
posB_C
.
normalize
()
);
var
a2c
=
-
normA
.
dot
(
posC_A
.
normalize
()
);
if
(
dict
[
strA
]
===
undefined
)
{
dict
[
strA
]
=
{};
}
if
(
dict
[
strB
]
===
undefined
)
{
dict
[
strB
]
=
{};
}
if
(
dict
[
strC
]
===
undefined
)
{
dict
[
strC
]
=
{};
}
dict
[
strA
][
strB
]
=
a2b
;
dict
[
strA
][
strC
]
=
a2c
;
dict
[
strB
][
strA
]
=
b2a
;
dict
[
strB
][
strC
]
=
b2c
;
dict
[
strC
][
strA
]
=
c2a
;
dict
[
strC
][
strB
]
=
c2b
;
}
curvatureDict
=
{};
var
min
=
10
,
max
=
0
;
Object
.
keys
(
dict
).
forEach
(
function
(
key
)
{
curvatureDict
[
key
]
=
average
(
dict
[
key
]
);
});
//smoothing
var
smoothCurvatureDict
=
Object
.
create
(
curvatureDict
);
Object
.
keys
(
dict
).
forEach
(
function
(
key
)
{
var
count
=
0
;
var
sum
=
0
;
Object
.
keys
(
dict
[
key
]
).
forEach
(
function
(
key2
)
{
sum
+=
smoothCurvatureDict
[
key2
];
count
++
;
});
smoothCurvatureDict
[
key
]
=
sum
/
count
;
});
curvatureDict
=
smoothCurvatureDict
;
// fit values to 0 and 1
Object
.
keys
(
curvatureDict
).
forEach
(
function
(
key
)
{
val
=
Math
.
abs
(
curvatureDict
[
key
]
);
if
(
val
<
min
)
min
=
val
;
if
(
val
>
max
)
max
=
val
;
});
var
range
=
(
max
-
min
);
Object
.
keys
(
curvatureDict
).
forEach
(
function
(
key
)
{
val
=
Math
.
abs
(
curvatureDict
[
key
]
);
if
(
curvatureDict
[
key
]
<
0
)
{
curvatureDict
[
key
]
=
(
min
-
val
)
/
range
}
else
{
curvatureDict
[
key
]
=
(
val
-
min
)
/
range
;
}
});
curvatureAttribute
=
new
Float32Array
(
bufferGeo
.
attributes
.
position
.
count
);
for
(
var
i
=
0
;
i
<
bufferGeo
.
attributes
.
position
.
count
;
i
++
)
{
array
=
bufferGeo
.
attributes
.
position
.
array
;
var
pos
=
new
THREE
.
Vector3
(
array
[
3
*
i
],
array
[
3
*
i
+
1
],
array
[
3
*
i
+
2
]
);
var
str
=
pos
.
toArray
().
toString
();
curvatureAttribute
[
i
]
=
curvatureDict
[
str
];
}
bufferGeo
.
addAttribute
(
'
curvature
'
,
new
THREE
.
BufferAttribute
(
curvatureAttribute
,
1
)
);
//starting filter is to show both concave and convex
var
curvatureFiltered
=
new
Float32Array
(
curvatureAttribute
);
filterBoth
(
curvatureFiltered
);
var
materialRaw
=
new
THREE
.
ShaderMaterial
({
vertexShader
:
document
.
getElementById
(
'
vertexShaderRaw
'
).
textContent
,
fragmentShader
:
document
.
getElementById
(
'
fragmentShaderRaw
'
).
textContent
}
);
ninjaMeshRaw
=
new
THREE
.
Mesh
(
bufferGeo
,
materialRaw
);
}
}
);
scene
.
add
(
ninjaMeshRaw
);
}
);
//init GUI
var
params
=
{
filterConvex
:
function
()
{
var
curvatureFiltered
=
new
Float32Array
(
curvatureAttribute
);
filterConvex
(
curvatureFiltered
);
bufferGeo
.
attributes
.
curvature
.
array
=
curvatureFiltered
;
bufferGeo
.
attributes
.
curvature
.
needsUpdate
=
true
;
},
filterConcave
:
function
()
{
var
curvatureFiltered
=
new
Float32Array
(
curvatureAttribute
);
filterConcave
(
curvatureFiltered
);
bufferGeo
.
attributes
.
curvature
.
array
=
curvatureFiltered
;
bufferGeo
.
attributes
.
curvature
.
needsUpdate
=
true
;
},
filterBoth
:
function
()
{
var
curvatureFiltered
=
new
Float32Array
(
curvatureAttribute
);
filterBoth
(
curvatureFiltered
);
bufferGeo
.
attributes
.
curvature
.
array
=
curvatureFiltered
;
bufferGeo
.
attributes
.
curvature
.
needsUpdate
=
true
;
}
};
var
gui
=
new
dat
.
GUI
();
topologyFolder
=
gui
.
addFolder
(
'
Topology
'
);
topologyFolder
.
add
(
params
,
'
filterConvex
'
);
topologyFolder
.
add
(
params
,
'
filterConcave
'
);
topologyFolder
.
add
(
params
,
'
filterBoth
'
);
topologyFolder
.
open
()
onWindowResize
();
window
.
addEventListener
(
'
resize
'
,
onWindowResize
,
false
);
}
function
onWindowResize
(
event
)
{
renderer
.
setSize
(
window
.
innerWidth
,
window
.
innerHeight
);
camera
.
aspect
=
window
.
innerWidth
/
window
.
innerHeight
;
camera
.
updateProjectionMatrix
();
}
function
animate
()
{
requestAnimationFrame
(
animate
);
render
();
}
function
render
()
{
renderer
.
render
(
scene
,
camera
);
}
</script>
</body>
</html>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录