033-局部纹理刷新-texSubImage2D

参考:
在线体验
代码出处
更新原理: texSubImage2D
参考教程

UpdatableTexture.js

import * as THREE from './lib/three.module.js'

export default class UpdatableTexture extends THREE.Texture {

	isUpdatableTexture = true

	constructor(format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, encoding) {
		super(null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding)
		var canvas = document.createElement('canvas');
		canvas.width = 1;
		canvas.height = 1;
		var ctx = canvas.getContext('2d');
		var imageData = ctx.createImageData(1, 1);

		this.image = imageData;

		this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
		this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;

		this.generateMipmaps = true;
		this.flipY = true;
		this.unpackAlignment = 1;
		this.needsUpdate = true;
	}

	setRenderer(renderer) {
		this.renderer = renderer;
		this.gl = this.renderer.getContext()
		this.utils = THREE.WebGLUtils(this.gl, this.renderer.extensions, { isWebGL2: false })
	}

	setSize(width, height) {
		if (width === this.width && height === this.height) return;
		var textureProperties = this.renderer.properties.get(this);
		if (!textureProperties.__webglTexture) return;

		this.width = width;
		this.height = height;

		var activeTexture = this.gl.getParameter(this.gl.TEXTURE_BINDING_2D);
		this.gl.bindTexture(this.gl.TEXTURE_2D, textureProperties.__webglTexture);
		if (!textureProperties.__webglTexture) this.width = null;
		this.gl.texImage2D(
			this.gl.TEXTURE_2D,
			0,
			this.utils.convert(this.format),
			width,
			height,
			0,
			this.utils.convert(this.format),
			this.utils.convert(this.type),
			null
		);
		this.gl.bindTexture(this.gl.TEXTURE_2D, activeTexture);

	}

	update(src, x, y) {
		var textureProperties = this.renderer.properties.get(this);
		if (!textureProperties.__webglTexture) return;

		var activeTexture = this.gl.getParameter(this.gl.TEXTURE_BINDING_2D);
		this.gl.bindTexture(this.gl.TEXTURE_2D, textureProperties.__webglTexture);
		this.gl.texSubImage2D(
			this.gl.TEXTURE_2D,
			0,
			x,
			this.height - y - src.height,
			this.utils.convert(this.format),
			this.utils.convert(this.type),
			src
		);
		this.gl.generateMipmap(this.gl.TEXTURE_2D);
		this.gl.bindTexture(this.gl.TEXTURE_2D, activeTexture);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值