用 dat.gui 保存参数似乎坏了? [英] Saving parameters with dat.gui seems broken?

查看:44
本文介绍了用 dat.gui 保存参数似乎坏了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 dat.gui 中保存参数似乎有问题,或者我遗漏了一些非常明显的东西..

There seems to be a problem with saving parameters in dat.gui, or I'm missing something really obvious..

单击齿轮图标时会出现问题,该图标应打开带有要保存的 JSON 的弹出窗口.保存到本地存储也对我不起作用.这里有两个 JSFiddle:

The problem shows when clicking the gear icon which should open a popup with the JSON to be saved. Also saving to local storage does not work for me. Here are two JSFiddles:

  • http://jsfiddle.net/navFooh/XZcde/ 与 dat.gui.min.js,单击齿轮会在控制台中引发错误:Uncaught TypeError: Cannot read property 'forEach' of undefined dat.gui.min.js:3

  • http://jsfiddle.net/navFooh/XZcde/ with dat.gui.min.js, clicking the gear throws an error in the console: Uncaught TypeError: Cannot read property 'forEach' of undefined dat.gui.min.js:3

http://jsfiddle.net/navFooh/8Hek6/ 与 dat.gui.js,单击齿轮图标确实显示带有 JSON 的弹出窗口,但参数 x 不显示

http://jsfiddle.net/navFooh/8Hek6/ with dat.gui.js, clicking the gear icon does show the popup with JSON, but the parameter x does not show

他们在线的示例工作正常(请参阅链接的评论).但是他们已经最小化了所有示例代码,包括库本身.所以我什至无法解决他们是否使用最新版本.

The sample they have online works fine (see comment for link). But they have minimized all their sample code, including the lib itself. So I can't even resolve if they're even using the latest build.

任何帮助将不胜感激.

推荐答案

我在调用 gui.remember(obj);gui.add(obj, 'x'); 顺序错误.

I made the error of calling gui.remember(obj); and gui.add(obj, 'x'); in the wrong order.

所以这是解决方案:

var obj = { x: 5 };
var gui = new dat.GUI();
gui.remember(obj);
gui.add(obj, 'x');

当调用 gui.add() 函数时,dat.gui 会生成要记住的对象的内部映射.此映射,gui.__rememberedObjectIndecesToControllers[],在保存值时在内部 getCurrentPreset() 函数中使用.

What happens is that dat.gui makes an internal map of objects to be remembered when calling the gui.add() function. This map, gui.__rememberedObjectIndecesToControllers[], is used in the internal getCurrentPreset() function when saving values.

然而,如果对象已经存储在 gui.__rememberedObjects[] 中,它只会将对象添加到此映射,这就是顺序如此重要的原因.

However it will only add object to this map if they have been stored in gui.__rememberedObjects[] which is why the order is so important.

缩小版本抛出错误的原因是当它试图从 gui.__rememberedObjectIndecesToControllers[] 获取映射值时,它试图遍历一个 undefined 值.

The reason the minified version threw an error is that when it tried to get the mapped value from gui.__rememberedObjectIndecesToControllers[], it tries to loop over an undefined value.

http://workshop.chromeexperiments.com/examples 上的示例/gui/#5--Saving-Values 实际上显示了这个正确的顺序,我只是忽略了它:

The sample on http://workshop.chromeexperiments.com/examples/gui/#5--Saving-Values actually shows this proper order, I just overlooked it:

var fizzyText = new FizzyText();
var gui = new dat.GUI();

gui.remember(fizzyText);

// Add controllers ...

这篇关于用 dat.gui 保存参数似乎坏了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆