阅读(4686) (0)

three.js DataTexture

2022-12-26 14:27:19 更新

从原始数据(raw data)、宽(width)、高(height)来直接创建一个纹理贴图。

构造函数

DataTexture( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy )

data 参数必须是一个 ArrayBufferView 。 其他参数对应于继承自 Texture 的属性,其中 magFilter 与 minFilter 默认为 THREE.NearestFilter。

数据的解释取决于type与format: 如果类型为 THREE.UnsignedByteType,则 Uint8Array 可用于寻址纹素数据。如果格式为 THREE.RGBAFormat,则数据需要为一个纹素提供四个值;红色、绿色、蓝色和 Alpha(通常是不透明度)。对于打包类型,THREE.UnsignedShort4444Type 和 THREE.UnsignedShort5551Type,一个纹素的所有颜色分量都可以作为 Uint16Array 的整数元素中的位域来寻址。为了使用这些类型THREE.FloatType 和 THREE.HalfFloatType,WebGL 实现必须支持各自的扩展 OES_texture_float 和 OES_texture_half_float。为了将 THREE.LinearFilter 用于基于这些类型的纹素的分量双线性插值,还必须存在 WebGL 扩展 OES_texture_float_linear 或 OES_texture_half_float_linear。

代码示例

// create a buffer with color data

const width = 512;
const height = 512;

const size = width * height;
const data = new Uint8Array( 4 * size );
const color = new THREE.Color( 0xffffff );

const r = Math.floor( color.r * 255 );
const g = Math.floor( color.g * 255 );
const b = Math.floor( color.b * 255 );

for ( let i = 0; i < size; i ++ ) {

	const stride = i * 4;

	data[ stride ] = r;
	data[ stride + 1 ] = g;
	data[ stride + 2 ] = b;
	data[ stride + 3 ] = 255;

}

// used the buffer to create a DataTexture

const texture = new THREE.DataTexture( data, width, height );
texture.needsUpdate = true;

属性

请参阅基本 Texture 类以了解通用属性。

.flipY : Boolean

如果设置为 true,纹理在上传到 GPU 时沿垂直轴翻转。默认为假。

.generateMipmaps : Boolean

是否为纹理生成 mipmap(如果可能)。默认为假。

.image : Image

用保存数据、宽度和高度的记录类型覆盖。

.isDataTexture : Boolean

用于检查给定对象是否为 DataTexture 类型的只读标志。

.unpackAlignment : number

默认为 1。指定内存中每个像素行开始的对齐要求。允许的值为 1(字节对齐)、2(行对齐到偶数字节)、4(字对齐)和 8(行从双字边界开始)。有关详细信息,请参阅 glPixelStorei。

方法

有关常用方法,请参见基 Texture 类。

源码

src/textures/DataTexture.js