通过 SceneJS 重用 JSON 对象节点
我使用 scenejs 框架创建了一个 webgl 动画。由于它将包含许多相同的元素,因此我希望最大限度地减少使用的代码量并尽可能地重复使用这些元素。
首先,我将 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 功能是否按预期工作?
I've created a webgl animation using the scenejs framework. As it'll contain a lot of identical elements, I want to minimize the amount of code used and re-use the elements as much as possible.
Firstly, I've got diskJSON defined as following:
var diskJSON = [{
type: "disk",
radius: 3,
inner_radius: 2}];
When I run the following code with sceneJS, it works fine.
{
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
}]
}
]
}
]
}
However, when I try to reuse the same diskJSON as defined previously, it only creates one node, instead of 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
}
]
}
]
}
The application will have thousands of these nodes, so having to redefine it every single time seems quite a waste. Is this a problem with scenejs or is this working as intended in regards to Javascript/JSON functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嘿 Niklas,您发现 SceneJS 解析 JSON 的方式存在错误 - SceneJS 在 DFS 遍历节点对象时将节点对象标记为在地图中已访问。因此,在这种情况下,它在解析“磁盘”节点时标记一次,然后不再解析它。
在这里提出了一个问题:https://github.com/xeolabs/scenejs/issues/99
作为优先事项修复此问题。
同时,您可以使用工厂函数:
函数 newDiskJSON() {
返回 [{
类型:“磁盘”,
半径:3,
内部半径:2}];
};
或者使用“instance”节点:
http://scenejs.wikispaces.com/instance
干杯,
力康
Hey Niklas, you've found a bug in the way SceneJS is parsing the JSON - SceneJS is marking the node objects as visited in a map while it DFS traverses them. So in this case it's marking your "disk" node once as it parses it, then never parsing it again.
Raised an issue here: https://github.com/xeolabs/scenejs/issues/99
Fixing this one as priority.
In the meantime, you could use a factory function:
function newDiskJSON() {
return [{
type: "disk",
radius: 3,
inner_radius: 2}];
};
Or use the "instance" node:
http://scenejs.wikispaces.com/instance
cheers,
LK