[英]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.