[英]Threejs Geometry color
我用十六进制值设置几何颜色
RichGeometry.prototype = new THREE.Geometry();
RichGeometry.prototype.constructor = RichGeometry;
function RichGeometry(c) {
c = typeof c !== 'undefined' ? c : 0x00C020; // color
THREE.Geometry.call(this);
this.color = c;
}
但是当我从创建的对象中获取值时,它返回了一个像这样的RGB值16711680。为什么? 如何获得十六进制值?
var geometry0 = new RichGeometry(0xff0000);
console.log(geometry0.color);// it returns rgb value like this 16711680
您可以将颜色存储为THREE.Color
的实例:
function RichGeometry(c) {
c = (typeof c !== 'undefined') ? c : 0x00C020;
THREE.Geometry.call(this);
this.color = new THREE.Color(c);
}
然后,您可以使用THREE.Color
上的方法检索颜色的不同表示THREE.Color
。 对于您的示例,请尝试:
var geometry0 = new RichGeometry(0xff0000);
console.log('0x' + geometry0.color.getHexString());
使用0xff0000时,它表示的是整数(即16711680),与使用字符串“ 0xff0000”时不同。 因此,取决于您要做什么。
> foo = 16711680
16711680
> '0x'+foo.toString(16)
'0xff0000'
将数字转换为十六进制字符串,并在前面加上“ 0x”
您可能希望将其包装在一个类似的吸气剂中:
this.__defineGetter__('color', function(){
return '0x'+this.color.toString(16)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.