通过 SceneJS 重用 JSON 对象节点

发布于 2024-11-11 01:20:27 字数 2511 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩饰不了的爱 2024-11-18 01:20:27

嘿 Niklas,您发现 SceneJS 解析 JSON 的方式存在错误 - SceneJS 在 DFS 遍历节点对象时将节点对象标记为在地图中已访问。因此,在这种情况下,它在解析“磁盘”节点时标记一次,然后不再解析它。

在这里提出了一个问题:https://github.com/xeolabs/scenejs/issues/99

作为优先事项修复此问题。

同时,您可以使用工厂函数:

函数 newDiskJSON() {
返回 [{
类型:“磁盘”,
半径:3,
内部半径:2}];
};

//...

nodes: [
    {
        type: "translate", 
        z:speedMultiplier*0.8,
        nodes: newDiskJSON()

    // ...

或者使用“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}];
};

//...

nodes: [
    {
        type: "translate", 
        z:speedMultiplier*0.8,
        nodes: newDiskJSON()

    // ...

Or use the "instance" node:

http://scenejs.wikispaces.com/instance

cheers,
LK

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文