Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Ablesons
three.js
提交
b6bfc4e4
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,体验更适合开发者的 AI 搜索 >>
提交
b6bfc4e4
编写于
10月 18, 2016
作者:
M
Mr.doob
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
TTFLoader clean up.
上级
1795ba7d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
176 addition
and
173 deletion
+176
-173
examples/js/loaders/TTFLoader.js
examples/js/loaders/TTFLoader.js
+174
-148
examples/webgl_loader_ttf.html
examples/webgl_loader_ttf.html
+2
-25
未找到文件。
examples/js/loaders/TTFLoader.js
浏览文件 @
b6bfc4e4
...
...
@@ -2,166 +2,192 @@
* @author gero3 / https://github.com/gero3
* @author tentone / https://github.com/tentone
* Requires opentype.js to be included in the project
* Loads TTF files and converts them into typeface JSON that can be used directly to create THREE.Font objects
* Loads TTF files and converts them into typeface JSON that can be used directly
* to create THREE.Font objects
*/
"
use strict
"
;
'
use strict
'
;
THREE
.
TTFLoader
=
function
(
manager
)
{
this
.
manager
=
(
manager
!==
undefined
)
?
manager
:
THREE
.
DefaultLoadingManager
;
THREE
.
TTFLoader
=
function
(
manager
)
{
this
.
manager
=
(
manager
!==
undefined
)
?
manager
:
THREE
.
DefaultLoadingManager
;
this
.
reversed
=
false
;
}
THREE
.
TTFLoader
.
prototype
.
load
=
function
(
url
,
onLoad
,
onProgress
,
onError
)
{
var
self
=
this
;
var
loader
=
new
THREE
.
XHRLoader
(
this
.
manager
);
loader
.
setResponseType
(
"
arraybuffer
"
);
loader
.
load
(
url
,
function
(
buffer
)
{
var
json
=
self
.
parse
(
buffer
);
if
(
onLoad
!==
undefined
)
{
onLoad
(
json
);
};
THREE
.
TTFLoader
.
prototype
.
load
=
function
(
url
,
onLoad
,
onProgress
,
onError
)
{
var
scope
=
this
;
var
loader
=
new
THREE
.
XHRLoader
(
this
.
manager
);
loader
.
setResponseType
(
'
arraybuffer
'
);
loader
.
load
(
url
,
function
(
buffer
)
{
if
(
onLoad
!==
undefined
)
{
onLoad
(
scope
.
parse
(
buffer
)
);
}
},
onProgress
,
onError
);
}
THREE
.
TTFLoader
.
prototype
.
parse
=
function
(
arraybuffer
)
{
if
(
typeof
opentype
===
"
undefined
"
)
{
console
.
warn
(
"
TTFLoader requires opentype.js Make sure it
\
's included before using the loader
"
);
},
onProgress
,
onError
);
};
THREE
.
TTFLoader
.
prototype
.
parse
=
function
(
arraybuffer
)
{
if
(
typeof
opentype
===
'
undefined
'
)
{
console
.
warn
(
'
TTFLoader requires opentype.js Make sure it
\'
s included before using the loader
'
);
return
null
;
}
var
font
=
opentype
.
parse
(
arraybuffer
);
return
THREE
.
TTFLoader
.
convert
(
font
,
this
.
reversed
);
}
THREE
.
TTFLoader
.
convert
=
function
(
font
,
reversed
)
{
var
scale
=
(
100000
)
/
((
font
.
unitsPerEm
||
2048
)
*
72
);
var
result
=
{};
result
.
glyphs
=
{};
for
(
var
i
=
0
;
i
<
font
.
glyphs
.
length
;
i
++
)
{
var
glyph
=
font
.
glyphs
.
glyphs
[
i
];
if
(
glyph
.
unicode
!==
undefined
)
{
var
token
=
{};
token
.
ha
=
Math
.
round
(
glyph
.
advanceWidth
*
scale
);
token
.
x_min
=
Math
.
round
(
glyph
.
xMin
*
scale
);
token
.
x_max
=
Math
.
round
(
glyph
.
xMax
*
scale
);
token
.
o
=
""
if
(
reversed
)
{
glyph
.
path
.
commands
=
TTFLoader
.
reverseCommands
(
glyph
.
path
.
commands
);
}
function
convert
(
font
,
reversed
)
{
var
round
=
Math
.
round
;
var
glyphs
=
{};
var
scale
=
(
100000
)
/
(
(
font
.
unitsPerEm
||
2048
)
*
72
);
for
(
var
i
=
0
;
i
<
font
.
glyphs
.
length
;
i
++
)
{
var
glyph
=
font
.
glyphs
.
glyphs
[
i
];
if
(
glyph
.
unicode
!==
undefined
)
{
var
token
=
{
ha
:
round
(
glyph
.
advanceWidth
*
scale
),
x_min
:
round
(
glyph
.
xMin
*
scale
),
x_max
:
round
(
glyph
.
xMax
*
scale
),
o
:
''
};
if
(
reversed
)
{
glyph
.
path
.
commands
=
reverseCommands
(
glyph
.
path
.
commands
);
glyph
.
path
.
commands
.
forEach
(
function
(
command
,
i
)
{
if
(
command
.
type
.
toLowerCase
()
===
"
c
"
)
{
command
.
type
=
"
b
"
;
}
token
.
o
+=
command
.
type
.
toLowerCase
();
token
.
o
+=
"
"
if
(
command
.
x
!==
undefined
&&
command
.
y
!==
undefined
){
token
.
o
+=
Math
.
round
(
command
.
x
*
scale
);
token
.
o
+=
"
"
token
.
o
+=
Math
.
round
(
command
.
y
*
scale
);
token
.
o
+=
"
"
}
if
(
command
.
x1
!==
undefined
&&
command
.
y1
!==
undefined
)
{
token
.
o
+=
Math
.
round
(
command
.
x1
*
scale
);
token
.
o
+=
"
"
token
.
o
+=
Math
.
round
(
command
.
y1
*
scale
);
token
.
o
+=
"
"
}
if
(
command
.
x2
!==
undefined
&&
command
.
y2
!==
undefined
)
{
token
.
o
+=
Math
.
round
(
command
.
x2
*
scale
);
token
.
o
+=
"
"
token
.
o
+=
Math
.
round
(
command
.
y2
*
scale
);
token
.
o
+=
"
"
}
});
result
.
glyphs
[
String
.
fromCharCode
(
glyph
.
unicode
)]
=
token
;
glyph
.
path
.
commands
.
forEach
(
function
(
command
,
i
)
{
if
(
command
.
type
.
toLowerCase
()
===
'
c
'
)
{
command
.
type
=
'
b
'
;
}
token
.
o
+=
command
.
type
.
toLowerCase
()
+
'
'
;
if
(
command
.
x
!==
undefined
&&
command
.
y
!==
undefined
)
{
token
.
o
+=
round
(
command
.
x
*
scale
)
+
'
'
+
round
(
command
.
y
*
scale
)
+
'
'
;
}
if
(
command
.
x1
!==
undefined
&&
command
.
y1
!==
undefined
)
{
token
.
o
+=
round
(
command
.
x1
*
scale
)
+
'
'
+
round
(
command
.
y1
*
scale
)
+
'
'
;
}
if
(
command
.
x2
!==
undefined
&&
command
.
y2
!==
undefined
)
{
token
.
o
+=
round
(
command
.
x2
*
scale
)
+
'
'
+
round
(
command
.
y2
*
scale
)
+
'
'
;
}
}
);
glyphs
[
String
.
fromCharCode
(
glyph
.
unicode
)
]
=
token
;
}
}
return
{
glyphs
:
glyphs
,
familyName
:
font
.
familyName
,
ascender
:
round
(
font
.
ascender
*
scale
),
descender
:
round
(
font
.
descender
*
scale
),
underlinePosition
:
font
.
tables
.
post
.
underlinePosition
,
underlineThickness
:
font
.
tables
.
post
.
underlineThickness
,
boundingBox
:
{
xMin
:
font
.
tables
.
head
.
xMin
,
xMax
:
font
.
tables
.
head
.
xMax
,
yMin
:
font
.
tables
.
head
.
yMin
,
yMax
:
font
.
tables
.
head
.
yMax
},
resolution
:
1000
,
original_font_information
:
font
.
tables
.
name
};
}
result
.
familyName
=
font
.
familyName
;
result
.
ascender
=
Math
.
round
(
font
.
ascender
*
scale
);
result
.
descender
=
Math
.
round
(
font
.
descender
*
scale
);
result
.
underlinePosition
=
font
.
tables
.
post
.
underlinePosition
;
result
.
underlineThickness
=
font
.
tables
.
post
.
underlineThickness
;
result
.
boundingBox
=
{
"
yMin
"
:
font
.
tables
.
head
.
yMin
,
"
xMin
"
:
font
.
tables
.
head
.
xMin
,
"
yMax
"
:
font
.
tables
.
head
.
yMax
,
"
xMax
"
:
font
.
tables
.
head
.
xMax
};
result
.
resolution
=
1000
;
result
.
original_font_information
=
font
.
tables
.
name
;
return
result
;
}
THREE
.
TTFLoader
.
reverseCommands
=
function
(
commands
)
{
var
paths
=
[];
var
path
;
commands
.
forEach
(
function
(
c
)
{
if
(
c
.
type
.
toLowerCase
()
===
"
m
"
)
{
path
=
[
c
];
paths
.
push
(
path
);
}
else
if
(
c
.
type
.
toLowerCase
()
!==
"
z
"
)
{
path
.
push
(
c
);
}
});
var
reversed
=
[];
paths
.
forEach
(
function
(
p
)
{
var
result
=
{
"
type
"
:
"
m
"
,
"
x
"
:
p
[
p
.
length
-
1
].
x
,
"
y
"
:
p
[
p
.
length
-
1
].
y
};
reversed
.
push
(
result
);
for
(
var
i
=
p
.
length
-
1
;
i
>
0
;
i
--
)
{
var
command
=
p
[
i
];
result
=
{
"
type
"
:
command
.
type
};
if
(
command
.
x2
!==
undefined
&&
command
.
y2
!==
undefined
)
{
result
.
x1
=
command
.
x2
;
result
.
y1
=
command
.
y2
;
result
.
x2
=
command
.
x1
;
result
.
y2
=
command
.
y1
;
function
reverseCommands
(
commands
)
{
var
paths
=
[];
var
path
;
commands
.
forEach
(
function
(
c
)
{
if
(
c
.
type
.
toLowerCase
()
===
'
m
'
)
{
path
=
[
c
];
paths
.
push
(
path
);
}
else
if
(
c
.
type
.
toLowerCase
()
!==
'
z
'
)
{
path
.
push
(
c
);
}
else
if
(
command
.
x1
!==
undefined
&&
command
.
y1
!==
undefined
)
{
result
.
x1
=
command
.
x1
;
result
.
y1
=
command
.
y1
;
}
);
var
reversed
=
[];
paths
.
forEach
(
function
(
p
)
{
var
result
=
{
type
:
'
m
'
,
x
:
p
[
p
.
length
-
1
].
x
,
y
:
p
[
p
.
length
-
1
].
y
};
reversed
.
push
(
result
);
for
(
var
i
=
p
.
length
-
1
;
i
>
0
;
i
--
)
{
var
command
=
p
[
i
];
var
result
=
{
type
:
command
.
type
};
if
(
command
.
x2
!==
undefined
&&
command
.
y2
!==
undefined
)
{
result
.
x1
=
command
.
x2
;
result
.
y1
=
command
.
y2
;
result
.
x2
=
command
.
x1
;
result
.
y2
=
command
.
y1
;
}
else
if
(
command
.
x1
!==
undefined
&&
command
.
y1
!==
undefined
)
{
result
.
x1
=
command
.
x1
;
result
.
y1
=
command
.
y1
;
}
result
.
x
=
p
[
i
-
1
].
x
;
result
.
y
=
p
[
i
-
1
].
y
;
reversed
.
push
(
result
);
}
result
.
x
=
p
[
i
-
1
].
x
;
result
.
y
=
p
[
i
-
1
].
y
;
reversed
.
push
(
result
);
}
});
return
reversed
;
}
}
);
return
reversed
;
}
return
convert
(
opentype
.
parse
(
arraybuffer
),
this
.
reversed
);
};
examples/webgl_loader_ttf.html
浏览文件 @
b6bfc4e4
...
...
@@ -36,13 +36,11 @@
<script
src=
"js/utils/GeometryUtils.js"
></script>
<script
src=
"js/Detector.js"
></script>
<script
src=
"js/loaders/TTFLoader.js"
></script>
<script
src=
"js/libs/stats.min.js"
></script>
<script
src=
"js/libs/opentype.min.js"
></script>
<div
id=
"info"
>
<a
href=
"http://threejs.org"
target=
"_blank"
>
three.js
</a>
- TTFLoader using opentype by gero3
<br/>
type to enter new text, drag to spin the text
<br/><span
class=
"button"
id=
"color"
>
change color
</span>
,
<span
class=
"button"
id=
"weight"
>
change weight
</span>
,
<span
class=
"button"
id=
"bevel"
>
change bevel
</span>
</div>
...
...
@@ -53,7 +51,7 @@
THREE
.
Cache
.
enabled
=
true
;
var
container
,
stats
,
hex
,
color
;
var
container
,
hex
,
color
;
var
camera
,
cameraTarget
,
scene
,
renderer
;
...
...
@@ -151,10 +149,7 @@
}
material
=
new
THREE
.
MultiMaterial
(
[
new
THREE
.
MeshPhongMaterial
(
{
color
:
0xffffff
,
shading
:
THREE
.
FlatShading
}
),
// front
new
THREE
.
MeshPhongMaterial
(
{
color
:
0xffffff
,
shading
:
THREE
.
SmoothShading
}
)
// side
]
);
material
=
new
THREE
.
MeshPhongMaterial
(
{
color
:
0xffffff
,
shading
:
THREE
.
FlatShading
}
);
group
=
new
THREE
.
Group
();
group
.
position
.
y
=
100
;
...
...
@@ -185,11 +180,6 @@
renderer
.
setSize
(
window
.
innerWidth
,
window
.
innerHeight
);
container
.
appendChild
(
renderer
.
domElement
);
// STATS
stats
=
new
Stats
();
//container.appendChild( stats.dom );
// EVENTS
document
.
addEventListener
(
'
mousedown
'
,
onDocumentMouseDown
,
false
);
...
...
@@ -229,12 +219,6 @@
}
function
boolToNum
(
b
)
{
return
b
?
1
:
0
;
}
function
onDocumentKeyDown
(
event
)
{
if
(
firstLetter
)
{
...
...
@@ -454,17 +438,10 @@
requestAnimationFrame
(
animate
);
render
();
stats
.
update
();
}
function
render
()
{
group
.
rotation
.
y
+=
(
targetRotation
-
group
.
rotation
.
y
)
*
0.05
;
camera
.
lookAt
(
cameraTarget
);
renderer
.
clear
();
renderer
.
render
(
scene
,
camera
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录