【发布时间】:2016-10-02 08:55:30
【问题描述】:
编辑(改写的问题): 我将如何使用提供的 smoothstep 函数在相邻的二维数组之间创建渐变?每个数组的大小相同,包含范围在 0 和 1 之间的值,通过单纯形噪声从边缘到边缘平滑过渡。因此,我希望相邻数组值之间的差异最大为 0.04
function smoothstep (min, max, value) {
var x = Math.max(0, Math.min(1, (value-min)/(max-min)));
return x*x*(3 - 2*x);
};
我有 6 个二维数组,其中包含 0 到 1 之间的值来表示球体表面的高度。要遍历数组的所有值,我有这个:
for (var i = 0; i < cube.faces.length; i++) {
for (var x = 0; x < cube.faces[i].heightMap.length; x++) {
for (var z = 0; z < cube.faces[i].heightMap.length; z++) {
if (x == 0 || x == cube.faces[i].heightMap.length - 1 || z == 0 || z == cube.faces[i].heightMap.length - 1) {
switch (i) {
case 0:
if (x == 0) {
//match left of face 1 to top of face 4
} else if (z == 0) {
//match top of face 1 to top of face 6
} else if (z == cube.faces[i].heightMap.length - 1) {
//match bottom of face 1 to top of face 5
} else {
//match right of face 1 to top of face 3
}
break;
case 1:
if (x == 0) {
//match left of face 2 to bottom of face 3
} else if (z == 0) {
//match top of face 2 to bottom of face 6
} else if (z == cube.faces[i].heightMap.length - 1) {
//match bottom of face 2 to bottom of face 5
} else {
//match right of face 2 to bottom of face 4
}
break;
case 2:
if (x == 0) {
//match left of face 3 to right of face 5
} else if (z == 0) {
//~~match top of face 3 to right of face 1~~
} else if (z == cube.faces[i].heightMap.length - 1) {
//~~match bottom of face 3 to left of face 2~~
} else {
//match right of face 3 to left of face 6
}
break;
case 3:
if (x == 0) {
//match left of face 4 to right of face 6
} else if (z == 0) {
//~~match top of face 4 to left of face 1~~
} else if (z == cube.faces[i].heightMap.length - 1) {
//~~match bottom of face 4 to right of face 2~~
} else {
//match right of face 4 to left of face 5
}
break;
case 4:
break;
case 5:
break;
default:
break;
}
}
}
}
}
但是,我在让面孔匹配时遇到了一些麻烦。调查这个我发现了一个名为“smoothstep”的函数,这似乎正是我所需要的。我不知道如何实现它,我还没有找到对我有用的解释。
function smoothstep(min, max, value) {
var x = Math.max(0, Math.min(1, (value - min) / (max - min)));
return x * x * (3 - 2 * x);
};
以下页面是我学习此方法的地方,但我无法理解要说的内容。如果有人有时间,您能解释一下我如何将其实施到我的情况中吗? Link to related question
【问题讨论】:
-
示例输入、预期输出、实际输出,解释问题所在。不要只是发布一大段代码并说它不起作用。
-
问题是如何对两个包含 0 到 1 值的二维数组实现平滑步长算法,以使它们当前不无缝的边缘变得无缝。如果你真的需要一个例子,我可以给出一个,但是任何符合这些参数的输入都可以。
-
那么想要的结果是什么?它有什么作用?你有参考吗?你已经提供了一个实现,那么有什么问题呢?请输入和输出。
-
输入量很大,但总是一个包含 0 和 1 之间值的二维数组。任何介于 0 和 1 之间的二维数组。所有数组的大小都相同。预期输出是相邻数组边缘上的值大致相等,而不会影响整个原始数组的斜率。我现在得到的输出没有,因为我要求解释这个平滑步骤,以便我可以开始处理这个问题。我对这个主题的研究使我相信smoothstep 是正确的算法,但是我无法掌握它,这是我的全部问题。也许您误解了要问的内容。
标签: javascript arrays algorithm math simplex