【问题标题】:Transforming simplex noise value to color将单纯形噪声值转换为颜色
【发布时间】:2021-09-18 22:27:01
【问题描述】:

我正在尝试使用单纯形噪声创建 256x256 高度图。噪声函数返回一个介于 -1 和 1 之间的值,这是我目前将该值转换为灰度值的尝试。

    import { SimplexNoise } from "three/examples/jsm/math/SimplexNoise";

    const ctx = document.createElement("canvas").getContext("2d");
    ctx.canvas.width = 256;
    ctx.canvas.height = 256;

    const simplex = new SimplexNoise();
    for(let y = 0; y < ctx.canvas.width; y++) {
        for(let x = 0; x < ctx.canvas.width; x++) {
            let noise = simplex.noise(x, y);
            noise = (noise + 1) / 2;
            ctx.fillStyle = `rgba(0, 0, 0, ${noise})`;
            ctx.fillRect(x, y, 1, 1)
        }
    }

这不起作用,我不知道如何将噪声值转换为有效颜色以绘制到画布上。任何帮助将不胜感激

【问题讨论】:

    标签: javascript three.js noise simplex-noise


    【解决方案1】:

    您正在尝试设置黑色的不透明度,您应该做的是通过将 RG 和 B 分量设置为从 0 到 255 的值,通过将噪声值视为百分比来将噪声转换为灰度,例如通过获取它的绝对值并将其乘以 255,同时将其不透明度设置为 1:

    import { SimplexNoise } from "three/examples/jsm/math/SimplexNoise";
    
    const ctx = document.createElement("canvas").getContext("2d");
    ctx.canvas.width = 256;
    ctx.canvas.height = 256;
    
    const simplex = new SimplexNoise();
    for(let y = 0; y < ctx.canvas.width; y++) {
        for(let x = 0; x < ctx.canvas.width; x++) {
            let noise = simplex.noise(x, y);
            noise = (noise + 1) / 2;
            let color = Math.abs(noise) * 255;
            ctx.fillStyle = `rgba(${color}, ${color}, ${color}, 1)`;          
            ctx.fillRect(x, y, 1, 1)
        }
    }
    

    【讨论】:

    • 感谢这项工作!尽管您在 fillstyle 上打了一点错字,但它应该是颜色而不是噪点。
    • @Ropro 是的,我很着急,谢谢指点!
    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 2011-09-20
    • 2013-08-22
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    相关资源
    最近更新 更多