简体   繁体   English

HEX 到 Threejs rgb 转换与 THREE.MeshPhongMaterial 不起作用

[英]HEX to Threejs rgb conversion with THREE.MeshPhongMaterial not working

I am using below js code to change color of a part in 3D model我正在使用下面的 js 代码来更改 3D model 中零件的颜色

document.getElementById("custom_color").onchange = function() {
    var custom_color = $("#custom_color").val().split('#').join("0x");
    console.log(custom_color);
    const color = new THREE.MeshPhongMaterial({ color: custom_color, shininess: 10 });
    console.log(color);
    var name = $(".--is-active").data("option");
    initColor(speaker,name,color);
  }

First console.log(custom_color) giving result第一个 console.log(custom_color) 给出结果

0xd04343

Second console.log(color) giving as第二个 console.log(color) 给出为

color object as 
b: 1
g: 1
r: 1

I am unable to understand why THREE.MeshPhongMaterial not converting it.我无法理解为什么THREE.MeshPhongMaterial没有转换它。

This happens because you pass string "0xd04343" instead of a real hex 0xd04343 .发生这种情况是因为您传递了字符串 "0xd04343" 而不是真正的十六进制0xd04343 If you want to pass a string, then leave it as is with # and pass it into color parameter: "#d04343".如果你想传递一个字符串,那么用#保持原样并将其传递给color参数:“#d04343”。 That format of string in CSS-style is valid for color. CSS 样式中的字符串格式对颜色有效。

The docs say:文档说:

It can be a CSS-style string.它可以是 CSS 样式的字符串。 For example:例如:

'rgb(250, 0,0)' 'RGB(250, 0,0)'

'rgb(100%,0%,0%)' 'RGB(100%,0%,0%)'

'hsl(0, 100%, 50%)' 'hsl(0, 100%, 50%)'

'#ff0000' '#ff0000'

'#f00' '#f00'

'red' '红色的'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM