我使用scenejs框架創建了一個webgl動畫。由於它包含許多相同的元素,因此我希望儘量減少所用代碼的數量並重新使用這些元素。通過SceneJS重用JSON對象節點
首先,我得diskJSON定義如下:
var diskJSON = [{
type: "disk",
radius: 3,
inner_radius: 2}];
當我運行sceneJS下面的代碼,它工作正常。
{
type: "material",
emit: 0,
baseColor: {
r: 0.3,
g: 0.3,
b: 0.9
},
specularColor: {
r: 0.9,
g: 0.9,
b: 0.9
},
specular: 0.9,
shine: 100.0,
nodes: [ {
type: "translate",
x:10,
y:1,
nodes: [
{
type: "translate",
z:speedMultiplier*0.8,
nodes:[{
type: "disk",
radius: 3,
inner_radius: 2
}]
},
{
type: "translate",
z:speedMultiplier*9.8,
nodes:[{
type: "disk",
radius: 3,
inner_radius: 2
}]
},
{
type: "translate",
z:speedMultiplier*11.64,
nodes:[{
type: "disk",
radius: 3,
inner_radius: 2
}]
},
{
type: "translate",
z:speedMultiplier*13.32,
nodes:[{
type: "disk",
radius: 3,
inner_radius: 2
}]
}
]
}
]
}
然而,當我試圖重複使用相同的diskJSON如先前定義的,它僅創建一個節點,而不是4:
{
type: "material",
emit: 0,
baseColor: {
r: 0.3,
g: 0.3,
b: 0.9
},
specularColor: {
r: 0.9,
g: 0.9,
b: 0.9
},
specular: 0.9,
shine: 100.0,
nodes: [ {
type: "translate",
x:10,
y:1,
nodes: [
{
type: "translate",
z:speedMultiplier*0.8,
nodes:diskJSON
},
{
type: "translate",
z:speedMultiplier*9.8,
nodes:diskJSON
},
{
type: "translate",
z:speedMultiplier*11.64,
nodes:diskJSON
},
{
type: "translate",
z:speedMultiplier*13.32,
nodes:diskJSON
}
]
}
]
}
應用程序將有數以千計的節點,所以不必每一次重新定義它似乎都是一種浪費。這是scenejs的問題還是這是按照Javascript/JSON功能的預期工作?
感謝您的提示答案,很高興聽到這不只是我快要瘋了兩種: ) – Niklas 2011-05-28 21:50:39