我正在尝试向 perlin 单纯形噪声函数添加 asm.js 注释:
"use strict";
// Ported from Stefan Gustavson's java implementation
// http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
// Read Stefan's excellent paper for details on how this code works.
//
// Sean McCullough banksean@gmail.com
function SimplexNoise(stdlib,foreign,heap) {
"use asm";
// heap should be at least 1024*4+12*3*4 bytes long
var floor = stdlib.Math.floor,
sqrt = stdlib.Math.sqrt,
random = new stdlib.Float32Array(heap),
buf = new stdlib.Int32Array(heap),
F2 = 0.0, F3 = 0.0,
G2 = 0.0, G3 = 0.0;
F2 = (sqrt(3.0)-1.0)*0.5; F3 = 1.0/3.0;
G2 = (3.0-sqrt(3.0))/6.0; G3 = 1.0/6.0;
function init() {
// before calling this function, set the first 256 floats in the heap to be random numbers between 0..1
// the heap gets rewritten, but if you record the IV separately, the noise is reproducable
for(var i=0; i<256; i++) {
buf[i] = buf[i+256] = floor(random[i]*256.0) & 0xff;
buf[i+512] = buf[i+512+256] = (buf[i] % 12) * 3 + 1024;
}
i = 1024;
buf[i++] = 1; buf[i++] = 1; buf[i++] = 0;
buf[i++] = -1; buf[i++] = 1; buf[i++] = 0;
buf[i++] = 1; buf[i++] = -1; buf[i++] = 0;
buf[i++] = -1; buf[i++] = -1; buf[i++] = 0;
buf[i++] = 1; buf[i++] = 0; buf[i++] = 1;
buf[i++] = -1; buf[i++] = 0; buf[i++] = 1;
buf[i++] = 1; buf[i++] = 0; buf[i++] = -1;
buf[i++] = -1; buf[i++] = 0; buf[i++] = -1;
buf[i++] = 0; buf[i++] = 1; buf[i++] = 1;
buf[i++] = 0; buf[i++] = -1; buf[i++] = 1;
buf[i++] = 0; buf[i++] = 1; buf[i++] = -1;
buf[i++] = 0; buf[i++] = -1; buf[i++] = -1;
}
function noise2D(xin,yin) {
xin = +xin; yin = +yin;
var s = 0.0, i = 0, j = 0,
t = 0.0,
X0 = 0.0, Y0 = 0.0,
x0 = 0.0, y0 = 0.0,
i1 = 0, j1 = 0,
x1 = 0.0, y1 = 0.0,
x2 = 0.0, y2 = 0.0,
ii = 0, jj = 0,
gi0 = 0, gi1 = 0, gi2 = 0,
t0 = 0.0, t1 = 0.0, t2 = 0.0,
n0 = 0.0, n1 = 0.0, n2 = 0.0;
// Skew the input space to determine which simplex cell we're in
s = (xin+yin)*F2; // Hairy factor for 2D
i = floor(xin+s); j = floor(yin+s);
t = (i+j)*G2;
X0 = i-t; Y0 = j-t; // Unskew the cell origin back to (x;y) space
x0 = xin-X0; y0 = yin-Y0; // The x;y distances from the cell origin
// For the 2D case; the simplex shape is an equilateral triangle.
// Determine which simplex we are in.
i1 = (x0>y0?1:0); j1 = (x0>y0?0:1); // Offsets for second (middle) corner of simplex in (i;j) coords
// A step of (1;0) in (i;j) means a step of (1-c;-c) in (x;y); and
// a step of (0;1) in (i;j) means a step of (-c;1-c) in (x;y); where
// c = (3-sqrt(3))/6
x1 = x0-i1+G2; y1 = y0-j1+G2; // Offsets for middle corner in (x;y) unskewed coords
x2 = x0-1+2*G2; y2 = y0-1+2*G2; // Offsets for last corner in (x;y) unskewed coords
// Work out the hashed gradient indices of the three simplex corners
ii = i & 255; jj = j & 255;
gi0 = buf[ii+buf[jj]+512];
gi1 = buf[ii+i1+buf[jj+j1]+512];
gi2 = buf[ii+1+buf[jj+1]+512];
// Calculate the contribution from the three corners
t0 = 0.5-x0*x0-y0*y0; t1 = 0.5-x1*x1-y1*y1; t2 = 0.5-x2*x2-y2*y2;
n0 = t0<0.0? 0.0: t0*t0*t0*t0*(buf[gi0]*x0+buf[gi0+1]*y0); // (x;y) of buf used for 2D gradient
n1 = t1<0.0? 0.0: t1*t1*t1*t1*(buf[gi1]*x1+buf[gi1+1]*y1);
n2 = t2<0.0? 0.0: t2*t2*t2*t2*(buf[gi2]*x2+buf[gi2+1]*y2);
// Add contributions from each corner to get the final noise value.
// The result is scaled to return values in the interval [-1,1].
return 70.0 * (n0 + n1 + n2);
}
return {
init: init,
noise2D: noise2D
};
};
Firefox Javascript 错误控制台仅显示:
Warning: TypeError: asm.js type error: asm.js must end with a return export statement Source File: perlin_simplex.js Line: 9
我已经搜索了有关错误可能是什么的帮助,但只找到了 https://github.com/zbjornson/human-asmjs这通常非常有用,但在这种特定情况下没有帮助。
我做错了什么?这个 perlin 噪声函数如何由人类 asm.js 处理?
最佳答案
问题出在以下几行:
F2 = (sqrt(3.0)-1.0)*0.5; F3 = 1.0/3.0;
G2 = (3.0-sqrt(3.0))/6.0; G3 = 1.0/6.0;
在 asm.js 模块的顶级范围内只允许声明。不允许在顶级范围内定义变量。
根据spec您只能将文字值分配给全局变量(以及其他导入语句)。因此,您也不能执行以下操作,因为 F2、F3、G2 和 G3 未被分配文字值(value)观:
var floor = stdlib.Math.floor,
sqrt = stdlib.Math.sqrt,
random = new stdlib.Float32Array(heap),
buf = new stdlib.Int32Array(heap),
F2 = (sqrt(3.0)-1.0)*0.5, F3 = 1.0/3.0,
G2 = (3.0-sqrt(3.0))/6.0, G3 = 1.0/6.0;
因此,您需要将 F2、F3、G2 和 G3 的定义移动到一个函数中(可能是 init 函数)。因此你的代码应该是这样的:
function SimplexNoise(stdlib,foreign,heap) {
"use asm";
var floor = stdlib.Math.floor,
sqrt = stdlib.Math.sqrt,
random = new stdlib.Float32Array(heap),
buf = new stdlib.Int32Array(heap),
F2 = 0.0, F3 = 0.0,
G2 = 0.0, G3 = 0.0;
function init() {
F2 = (sqrt(3.0)-1.0)*0.5; F3 = 1.0/3.0;
G2 = (3.0-sqrt(3.0))/6.0; G3 = 1.0/6.0;
// rest of init
}
function noise2D(xin,yin) {
// function body
}
return {
init: init,
noise2D: noise2D
};
};
Human asm.js 确实告诉你这个问题,但他们给你的例子是一个数组而不是一个简单的变量:
Note that these typed arrays cannot be modified outside of a function:
function MyModule(stdlib, foreign, heap) {
var arr = new stdlib.Int8Array(heap);
arr[0] = 1; // "asm.js must end with a return export statement"
// ...
}
Instead do something like this:
function MyModule(stdlib, foreign, heap) {
var arr = new stdlib.Int8Array(heap);
function init() {
arr[0] = 1;
}
return {
init: init
};
}
给你。 asm.js 不是很痛苦吗?
关于javascript - 手写 asm.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17778006/
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我开始了一个新的Rails3.2.5项目,Assets管道不再工作了。CSS和Javascript文件不再编译。这是尝试生成Assets时日志的输出:StartedGET"/assets/application.css?body=1"for127.0.0.1at2012-06-1623:59:11-0700Servedasset/application.css-200OK(0ms)[2012-06-1623:59:11]ERRORNoMethodError:undefinedmethod`each'fornil:NilClass/Users/greg/.rbenv/versions/1
rails新手。只是想了解\assests目录中的这两个文件。例如,application.js文件有如下行://=requirejquery//=requirejquery_ujs//=require_tree.我理解require_tree。只是将所有JS文件添加到当前目录中。根据上下文,我可以看出requirejquery添加了jQuery库。但是它从哪里得到这些jQuery库呢?我没有在我的Assets文件夹中看到任何jquery.js文件——或者直接在我的整个应用程序中没有看到任何jquery.js文件?同样,我正在按照一些说明安装TwitterBootstrap(http:
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan
我有一个包含多个组件的存储库,其中大部分是用JavaScript(Node.js)编写的,一个是用Ruby(RubyonRails)编写的。我想要一个.travis.yml文件来触发一个运行每个组件的所有测试的构建。根据thisTravisCIGoogleGroupthread,目前还没有官方支持。我的目录结构是这样的:.├──构建服务器├──核心├──扩展├──网络应用├──流浪文件├──package.json├──.travis.yml└──生成文件我希望能够运行特定版本的Ruby(2.2.2)和Node.js(0.12.2)。我已经有了一个make目标,所以maketest在每
我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文
我有一个用Rails3编写的站点。我的帖子模型有一个名为“内容”的文本列。在帖子面板中,html表单使用tinymce将“content”列设置为textarea字段。在首页,因为使用了tinymce,post.html.erb的代码需要用这样的原始方法来实现。.好的,现在如果我关闭浏览器javascript,这个文本区域可以在没有tinymce的情况下输入,也许用户会输入任何xss,比如alert('xss');.我的前台会显示那个警告框。我尝试sanitize(@post.content)在posts_controller中,但sanitize方法将相互过滤tinymce样式。例如
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。多年来,我一直在使用多种语言进行编程,并且认为自己总体上相当擅长。但是,我从未编写过任何自动化测试:没有单元测试,没有TDD,没有BDD,什么都没有。我已经尝试开始为我的项目编写适当的测试套件。我可以看到在进行任何更改后能够自动测试项目中所有代码的理论值(value)。我可以看到像RSpec和Mocha这样的测试框架应该如何使设置和运行所述测试变得相当容易
出于某种原因,我必须为Firefox禁用javascript(手动,我们按照提到的步骤执行http://support.mozilla.org/en-US/kb/javascript-settings-for-interactive-web-pages#w_enabling-and-disabling-javascript)。使用Ruby的SeleniumWebDriver如何实现这一点? 最佳答案 是的,这是可能的。而是另一种方式。您首先需要查看链接Selenium::WebDriver::Firefox::Profile#[]=
我需要一些指导来了解如何将Angular整合到rails中。选择Rails的原因:我喜欢他们偏执的做事方式。还有迁移,gem真的很酷。使用angular的原因:我正在研究和寻找最适合SPA的框架。Backbone似乎太抽象了。我不得不在Angular和Ember之间做出选择。我首先开始阅读Angular,它对我来说很有意义。所以我从来没有去读过关于ember的文章。使用Angular和Rails的原因:我研究并尝试使用小型框架,例如grape、slim(是的,我也使用php)。但我觉得需要坚持项目的长期范围。我个人喜欢用Rails的方式做事。这就是我需要帮助的地方,我在Rails4中有