【问题标题】:Using a HEX value from a color picker for three.js as a string使用来自 three.js 的颜色选择器的 HEX 值作为字符串
【发布时间】:2020-04-07 23:32:51
【问题描述】:

所以我有一个颜色选择器,它以这种格式输出颜色:FFA6A6。它是来自 jscolor.com 的插件。当我用颜色选择器改变它时,我正在使用 three.js 来改变它的颜色。然而,three.js 需要 0xFFA6A6,不仅如此。最重要的是,我不能将 0x 连接到 FFA6A6,这将使它成为一个字符串。此外,它将颜色输出为字符串,我似乎无法将其从字符串中删除

例如:“FFA6A6”到 FFA6A6

如何将 FFA6A6 更改为可以识别为 three.js 颜色的东西?这就是我所拥有的:

function updateNoseColor(){
    scene.remove(nose);
    var geometry = new THREE.ConeGeometry( .4, 1, 32 );

    var material = new THREE.MeshBasicMaterial( {color: document.getElementById("nosecolor").value} );
    //three.js cannot take a string as a value, so im not sure what to do
    var nose = new THREE.Mesh( geometry, material );
    scene.add(nose);
}

感谢您抽出宝贵时间,可能有一个我不知道的非常简单的解决方案。同样,问题是删除引号,并将其添加到 0x 谢谢!

编辑:我尝试使用 ParseInt 转换为小数,three.js 不接受它

【问题讨论】:

  • 你可以使用parseInt,只要确保你在前面加上0x0xFFF === parseInt('0xFFF') 将评估为 true。
  • @DrewSnow 那也不行
  • 当我在控制台中找到它时它会返回:Color {r: 0.4196078431372549, g: 1, b: 0.7529411764705882}
  • @UnityDude 您在粘贴箱中添加了 JavaScript 代码。您也可以添加您的 HTML 代码吗?也许使用jsfiddle.net

标签: javascript colors three.js


【解决方案1】:

您可以使用 three.js 中的 Color 类 (reference)。例如:

var color = new THREE.Color("#FFA6A6"); // "FFA6A6" won't work!
color.getHex(); // 0xFFA6A6

也就是说,用给定的代码替换您的以下代码

var material = new THREE.MeshBasicMaterial( {color: document.getElementById("nosecolor").value} );

var material = new THREE.MeshBasicMaterial( {color: new Color(document.getElementById("nosecolor").value)} );

如果 document.getElementById("nosecolor").value = "#FFA6A6"

还有

var material = new THREE.MeshBasicMaterial( {color: new Color("#" + document.getElementById("nosecolor").value)} );

if document.getElementById("nosecolor").value = "FFA6A6"(开头没有#)

例如,运行下面的sn-p(灵感来自一个three.js example)。

function createSquare() {
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    var geometry = new THREE.BoxGeometry(1, 1, 1);
    
    /* START - EXACT ANS */
    var a = "#"+document.getElementById("nosecolor").value;
    var material = new THREE.MeshBasicMaterial({
        color: (new THREE.Color(a))
    });
    /* STOP - EXACT ANS */
    
    var cube = new THREE.Mesh(geometry, material);
    scene.add(cube);

    camera.position.z = 5;

    function animate() {
        requestAnimationFrame(animate);
        renderer.render(scene, camera);
    }
    animate();
}

createSquare();
<input type="text" id="nosecolor" value="FFA6A6">

<script src="http://threejs.org/build/three.min.js"></script>

【讨论】:

  • 这不起作用。我需要将其放入材质中。
  • @UnityDude 请检查我更新的答案 - 它有效!尝试打印出document.getElementById("nosecolor").value,我认为它只是“FFA6A6”。如果是,则必须使用"#" + document.getElementById("nosecolor").value 来初始化Color
  • @UnityDude 检查更新的答案。如果还是不行,打印出document.getElementById("nosecolor").value的值,并在此处添加输出。
  • 很抱歉,仍然不适合我。 var a = "#"+document.getElementById("nosecolor").value; material = new THREE.MeshBasicMaterial( { color: (new THREE.Color(a)).getHex() });
  • @UnityDude document.getElementById("nosecolor").value 是什么(打印出来)?
猜你喜欢
  • 2013-04-17
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
  • 2011-07-28
  • 2018-06-16
  • 2011-06-28
  • 1970-01-01
  • 2011-05-26
相关资源
最近更新 更多