原型的基本概念
原型的功能
PROTO nodeTypeName
[
field fieldType fieldName DEFaultValue
exposedField fieldType fieldName DEFaultValue
eventIn fieldType eventInName
eventOut fieldType eventOutName
]
{
nodebody
}
语法格式中,斜体部分是创建者自己定义的。
原型节点的调用:
#VRML V2.0 utf8
PROTO TwoColorTable [
field SFColor legColor 0.8 0.4 0.7
field SFColor topColor 0.6 0.6 0.1
]
{
Transform {
children [
Transform {
translation 0.0 0.6 0.0
children [
Shape {
appearance Appearance {
material Material {
diffuseColor IS topColor
}
}
geometry Box {
size 1.2 0.2 1.2
}
}
]
}
Transform {
translation -0.5 0 -0.5
children [
DEF Leg Shape {
appearance Appearance {
material Material {
diffuseColor IS legColor
}
}
geometry Cylinder {
height 1
radius 0.1
}
}
]
}
Transform { #另一条桌腿
translation 0.5 0 -0.5
children[ USE Leg]
}
Transform { #另一条桌腿
translation -0.5 0 0.5
children[ USE Leg]
}
Transform { #另一条桌腿
translation 0.5 0 0.5
children[ USE Leg]
}
]#根节点Transform的子结束
}#根Transform 结束
}#原型结束
Transform {
translation 1.5 0 0
children [
TwoColorTable{
legColor 0.5 0.6 1
}
]
}
Transform {
translation -1.5 0 0
children [
TwoColorTable{
topColor 1 0.6 0.5
}
]
}
外部原型节点的定义和调用
EXTERPROTO nodeTypeName[
field fieldType fieldName
exposedField fieldType fieldName
eventIn fieldType eventInName
eventOut fieldType eventOutName
]
url orurlList
语法格式中,斜体部分是创建者自己定义的。
#VRML V2.0 utf8
EXTERNPROTO EXTTable [
field SFColor legColor
field SFColor topColor
]
"3-10.wrl#TwoColorTable"
Transform {
translation -1.5 0 0
children [
EXTTable{
legColor 0.5 0.9 0.3
topColor 0.8 0.4 0.3
}
]
}
Transform {
translation 1.5 0 0
children [
EXTTable{
legColor 0.8 0.3 0.6
topColor 0.2 0.6 0.7
}
]
}
IS语句
IS语句语法建立了一种从接口到节点体的一个连接。一旦连接成功,接口或公共域值就能自动地作为节点体的域或外部域值。
与之相同,eventIn事件隐含的接口和公共域自动地按规定路线把事件发送到节点体或隐含的eventIn事件中,eventOut事件隐含的
节点体和公共域自动地按规定的路线用接口和隐含的eventOut事件发送事件。
IS语句的格式:fieldName IS interfaceItem
fieldName可以是PROTO定义的节点体内的任何一个节点接口或Script节点接口的任何一个域、公共域、eventIn事件
和eventOut事件。
interfaceItem可以是PROTO定义的接口中的任何一个接口域、公共域、eventIn事件和eventOut事件。
接口和节点体内的接口数据类型必须是相同的。
eventIn事件接口只能与节点体内的eventIn事件或公共域中隐含的eventIn事件相连接
eventOut事件接口只能与节点体内的eventOut事件或公共域中隐含的eventOut事件相连接。
接口域只能与节点体内的域或公共域相连接,并且接口公共域只能与节点体内的公共域相连接。