Преглед на файлове

Merge pull request #17697 from Mugen87/dev34

WebGLRenderer: Added .initTexture().
Michael Herzog преди 5 години
родител
ревизия
d769e95bd4

+ 3 - 0
docs/api/en/renderers/WebGLRenderer.html

@@ -394,6 +394,9 @@
 		Returns the viewport.
 		</p>
 
+		<h3>[method:null initTexture]( [param:Texture texture] )</h3>
+		<p> Initializes the given texture. Useful for preloading a texture rather than waiting until first render (which can cause noticeable lags due to decode and GPU upload overhead).</p>
+
 		<h3>[method:null resetGLState]( )</h3>
 		<p>Reset the GL state to default. Called internally if the WebGL context is lost.</p>
 

+ 4 - 1
docs/api/zh/renderers/WebGLRenderer.html

@@ -46,7 +46,7 @@
 		[page:String powerPreference] - 提示用户代理怎样的配置更适用于当前WebGL环境。 可能是*"high-performance"*, *"low-power"* 或 *"default"*。默认是*"default"*.
 		详见[link:https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.12 WebGL spec]<br />
 
-		
+
 		[page:Boolean failIfMajorPerformanceCaveat] - whether the renderer creation will fail upon low perfomance is detected. Default is *false*.
 		See [link:https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.12 WebGL spec] for details.<br />
 
@@ -352,6 +352,9 @@
 		<h3>[method:Object getSize]()</h3>
 		<p>返回包含渲染器输出canvas的宽度和高度(单位像素)的对象。</p>
 
+		<h3>[method:null initTexture]( [param:Texture texture] )</h3>
+		<p> Initializes the given texture. Useful for preloading a texture rather than waiting until first render (which can cause noticeable lags due to decode and GPU upload overhead).</p>
+
 		<h3>[method:null resetGLState]( )</h3>
 		<p>将GL状态重置为默认值。WebGL环境丢失时会内部调用</p>
 

+ 7 - 0
src/renderers/WebGLRenderer.d.ts

@@ -417,6 +417,13 @@ export class WebGLRenderer implements Renderer {
 	 */
 	copyTextureToTexture( position: Vector2, srcTexture: Texture, dstTexture: Texture, level?: number ): void;
 
+	/**
+	 * Initializes the given texture. Can be used to preload a texture rather than waiting until first render (which can cause noticeable lags due to decode and GPU upload overhead).
+	 *
+	 * @param texture The texture to Initialize.
+	 */
+	initTexture( texture: Texture ): void;
+
 	/**
 	 * @deprecated
 	 */

+ 12 - 0
src/renderers/WebGLRenderer.js

@@ -2730,6 +2730,8 @@ function WebGLRenderer( parameters ) {
 
 		_gl.copyTexImage2D( _gl.TEXTURE_2D, level, glFormat, position.x, position.y, width, height, 0 );
 
+		state.unbindTexture();
+
 	};
 
 	this.copyTextureToTexture = function ( position, srcTexture, dstTexture, level ) {
@@ -2751,6 +2753,16 @@ function WebGLRenderer( parameters ) {
 
 		}
 
+		state.unbindTexture();
+
+	};
+
+	this.initTexture = function ( texture ) {
+
+		textures.setTexture2D( texture, 0 );
+
+		state.unbindTexture();
+
 	};
 
 	if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {

+ 1 - 0
src/renderers/webgl/WebGLState.d.ts

@@ -78,6 +78,7 @@ export class WebGLState {
 	setScissorTest( scissorTest: boolean ): void;
 	activeTexture( webglSlot: number ): void;
 	bindTexture( webglType: number, webglTexture: any ): void;
+	unbindTexture(): void;
 	// Same interface as https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/compressedTexImage2D
 	compressedTexImage2D(
 		target: number,

+ 16 - 0
src/renderers/webgl/WebGLState.js

@@ -845,6 +845,21 @@ function WebGLState( gl, extensions, utils, capabilities ) {
 
 	}
 
+	function unbindTexture() {
+
+		var boundTexture = currentBoundTextures[ currentTextureSlot ];
+
+		if ( boundTexture !== undefined && boundTexture.type !== undefined ) {
+
+			gl.bindTexture( boundTexture.type, null );
+
+			boundTexture.type = undefined;
+			boundTexture.texture = undefined;
+
+		}
+
+	}
+
 	function compressedTexImage2D() {
 
 		try {
@@ -977,6 +992,7 @@ function WebGLState( gl, extensions, utils, capabilities ) {
 
 		activeTexture: activeTexture,
 		bindTexture: bindTexture,
+		unbindTexture: unbindTexture,
 		compressedTexImage2D: compressedTexImage2D,
 		texImage2D: texImage2D,
 		texImage3D: texImage3D,