Browse Source

Texture: Add setValues. (#31087)

* Texture: Add setValues.

* RenderTarget3D: Set texture options.

* Tests: Add coverage for render targets.

* RenderTarget: Respect wrapR option.
Cody Bennett 9 months ago
parent
commit
7703433fca

+ 35 - 5
src/core/RenderTarget.js

@@ -126,11 +126,7 @@ class RenderTarget extends EventDispatcher {
 
 		const image = { width: width, height: height, depth: options.depth };
 
-		const texture = new Texture( image, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace );
-
-		texture.flipY = false;
-		texture.generateMipmaps = options.generateMipmaps;
-		texture.internalFormat = options.internalFormat;
+		const texture = new Texture( image );
 
 		/**
 		 * An array of textures. Each color attachment is represented as a separate texture.
@@ -149,6 +145,8 @@ class RenderTarget extends EventDispatcher {
 
 		}
 
+		this._setTextureOptions( options );
+
 		/**
 		 * Whether to allocate a depth buffer or not.
 		 *
@@ -204,6 +202,38 @@ class RenderTarget extends EventDispatcher {
 
 	}
 
+	_setTextureOptions( options = {} ) {
+
+		const values = {
+			minFilter: LinearFilter,
+			generateMipmaps: false,
+			flipY: false,
+			internalFormat: null
+		};
+
+		if ( options.mapping !== undefined ) values.mapping = options.mapping;
+		if ( options.wrapS !== undefined ) values.wrapS = options.wrapS;
+		if ( options.wrapT !== undefined ) values.wrapT = options.wrapT;
+		if ( options.wrapR !== undefined ) values.wrapR = options.wrapR;
+		if ( options.magFilter !== undefined ) values.magFilter = options.magFilter;
+		if ( options.minFilter !== undefined ) values.minFilter = options.minFilter;
+		if ( options.format !== undefined ) values.format = options.format;
+		if ( options.type !== undefined ) values.type = options.type;
+		if ( options.anisotropy !== undefined ) values.anisotropy = options.anisotropy;
+		if ( options.colorSpace !== undefined ) values.colorSpace = options.colorSpace;
+		if ( options.flipY !== undefined ) values.flipY = options.flipY;
+		if ( options.generateMipmaps !== undefined ) values.generateMipmaps = options.generateMipmaps;
+		if ( options.internalFormat !== undefined ) values.internalFormat = options.internalFormat;
+
+		for ( let i = 0; i < this.textures.length; i ++ ) {
+
+			const texture = this.textures[ i ];
+			texture.setValues( values );
+
+		}
+
+	}
+
 	/**
 	 * The texture representing the default color attachment.
 	 *

+ 1 - 0
src/core/RenderTarget3D.js

@@ -37,6 +37,7 @@ class RenderTarget3D extends RenderTarget {
 		 * @type {Data3DTexture}
 		 */
 		this.texture = new Data3DTexture( null, width, height, depth );
+		this._setTextureOptions( options );
 
 		this.texture.isRenderTargetTexture = true;
 

+ 1 - 0
src/renderers/WebGL3DRenderTarget.js

@@ -37,6 +37,7 @@ class WebGL3DRenderTarget extends WebGLRenderTarget {
 		 * @type {Data3DTexture}
 		 */
 		this.texture = new Data3DTexture( null, width, height, depth );
+		this._setTextureOptions( options );
 
 		this.texture.isRenderTargetTexture = true;
 

+ 1 - 0
src/renderers/WebGLArrayRenderTarget.js

@@ -37,6 +37,7 @@ class WebGLArrayRenderTarget extends WebGLRenderTarget {
 		 * @type {DataArrayTexture}
 		 */
 		this.texture = new DataArrayTexture( null, width, height, depth );
+		this._setTextureOptions( options );
 
 		this.texture.isRenderTargetTexture = true;
 

+ 2 - 4
src/renderers/WebGLCubeRenderTarget.js

@@ -41,7 +41,8 @@ class WebGLCubeRenderTarget extends WebGLRenderTarget {
 		 *
 		 * @type {DataArrayTexture}
 		 */
-		this.texture = new CubeTexture( images, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace );
+		this.texture = new CubeTexture( images );
+		this._setTextureOptions( options );
 
 		// By convention -- likely based on the RenderMan spec from the 1990's -- cube maps are specified by WebGL (and three.js)
 		// in a coordinate system in which positive-x is to the right when looking up the positive-z axis -- in other words,
@@ -53,9 +54,6 @@ class WebGLCubeRenderTarget extends WebGLRenderTarget {
 
 		this.texture.isRenderTargetTexture = true;
 
-		this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false;
-		this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter;
-
 	}
 
 	/**

+ 48 - 0
src/textures/Texture.js

@@ -509,6 +509,54 @@ class Texture extends EventDispatcher {
 
 	}
 
+	/**
+	 * Sets this texture's properties based on `values`.
+	 * @param {Object} values - A container with texture parameters.
+	 */
+	setValues( values ) {
+
+		for ( const key in values ) {
+
+			const newValue = values[ key ];
+
+			if ( newValue === undefined ) {
+
+				console.warn( `THREE.Texture.setValues(): parameter '${ key }' has value of undefined.` );
+				continue;
+
+			}
+
+			const currentValue = this[ key ];
+
+			if ( currentValue === undefined ) {
+
+				console.warn( `THREE.Texture.setValues(): property '${ key }' does not exist.` );
+				continue;
+
+			}
+
+			if ( ( currentValue && newValue ) && ( currentValue.isVector2 && newValue.isVector2 ) ) {
+
+				currentValue.copy( newValue );
+
+			} else if ( ( currentValue && newValue ) && ( currentValue.isVector3 && newValue.isVector3 ) ) {
+
+				currentValue.copy( newValue );
+
+			} else if ( ( currentValue && newValue ) && ( currentValue.isMatrix3 && newValue.isMatrix3 ) ) {
+
+				currentValue.copy( newValue );
+
+			} else {
+
+				this[ key ] = newValue;
+
+			}
+
+		}
+
+	}
+
 	/**
 	 * Serializes the texture into JSON.
 	 *

+ 68 - 0
test/unit/src/core/RenderTarget.tests.js

@@ -0,0 +1,68 @@
+/* global QUnit */
+
+import { NearestFilter } from '../../../../src/constants.js';
+import { RenderTarget } from '../../../../src/core/RenderTarget.js';
+
+export default QUnit.module( 'Core', () => {
+
+	QUnit.module( 'RenderTarget', () => {
+
+		// Constructor options
+		QUnit.test( 'Constructor', ( assert ) => {
+
+			const empty = new RenderTarget();
+			assert.ok( empty.width != null && empty.height != null && empty.textures.length === 1, 'Can instantiate a RenderTarget with no arguments.' );
+
+			const sized = new RenderTarget( 1, 1 );
+			assert.ok( sized.width === 1 && sized.height === 1 && sized.textures.length === 1, 'Can instantiate a RenderTarget with custom size.' );
+
+			const mrt = new RenderTarget( 1, 1, { count: 2 } );
+			assert.ok( mrt.width === 1 && mrt.height === 1 && mrt.textures.length === 2, 'Can instantiate a RenderTarget with custom count (MRT).' );
+
+			const options = new RenderTarget( 1, 1, { magFilter: NearestFilter } );
+			assert.ok( options.width === 1 && options.height === 1 && options.texture.magFilter === NearestFilter, 'Can instantiate a RenderTarget with texture options.' );
+
+		} );
+
+		// PROPERTIES
+		QUnit.todo( 'texture', ( assert ) => {
+
+			assert.ok( false, 'everything\'s gonna be alright' );
+
+		} );
+
+		QUnit.todo( 'depthTexture', ( assert ) => {
+
+			assert.ok( false, 'everything\'s gonna be alright' );
+
+		} );
+
+		// PUBLIC
+		QUnit.test( 'setSize', ( assert ) => {
+
+			const renderTarget = new RenderTarget();
+			renderTarget.setSize( 128, 128 );
+			assert.ok( renderTarget.width === 128 && renderTarget.height === 128, 'Sets a size with width and height' );
+			assert.ok( renderTarget.texture.image.width === 128 && renderTarget.texture.image.height === 128, 'Texture image is updated on resize' );
+			assert.ok( renderTarget.viewport.width === 128 && renderTarget.viewport.height === 128, 'Viewport is updated on resize' );
+			assert.ok( renderTarget.scissor.width === 128 && renderTarget.scissor.height === 128, 'Scissor is updated on resize' );
+
+			const mrt = new RenderTarget( 0, 0, { count: 2 } );
+			mrt.setSize( 128, 128 );
+			assert.ok( mrt.width === 128 && mrt.height === 128, 'Sets a size with width and height' );
+			assert.ok( mrt.textures[ 0 ].image.width === 128 && mrt.textures[ 0 ].image.height === 128 && mrt.textures[ 1 ].image.width === 128 && mrt.textures[ 1 ].image.height === 128, 'Texture images are updated on resize' );
+			assert.ok( mrt.viewport.width === 128 && mrt.viewport.height === 128, 'Viewport is updated on resize' );
+			assert.ok( mrt.scissor.width === 128 && mrt.scissor.height === 128, 'Scissor is updated on resize' );
+
+			const renderTarget3D = new RenderTarget();
+			renderTarget3D.setSize( 128, 128, 16 );
+			assert.ok( renderTarget3D.width === 128 && renderTarget3D.height === 128 && renderTarget3D.depth === 16, 'Sets a size with width, height, and depth' );
+			assert.ok( renderTarget3D.texture.image.width === 128 && renderTarget3D.texture.image.height === 128 && renderTarget3D.texture.image.depth === 16, 'Texture image is updated on resize' );
+			assert.ok( renderTarget3D.viewport.width === 128 && renderTarget3D.viewport.height === 128, 'Viewport is updated on resize' );
+			assert.ok( renderTarget3D.scissor.width === 128 && renderTarget3D.scissor.height === 128, 'Scissor is updated on resize' );
+
+		} );
+
+	} );
+
+} );

+ 28 - 0
test/unit/src/core/RenderTarget3D.tests.js

@@ -0,0 +1,28 @@
+/* global QUnit */
+
+import { RenderTarget3D } from '../../../../src/core/RenderTarget3D.js';
+
+import { EventDispatcher } from '../../../../src/core/EventDispatcher.js';
+import { NearestFilter, RepeatWrapping } from '../../../../src/constants.js';
+
+export default QUnit.module( 'Core', () => {
+
+	QUnit.module( 'RenderTarget3D', () => {
+
+		// INHERITANCE
+		QUnit.test( 'Extending', ( assert ) => {
+
+			const object = new RenderTarget3D();
+			assert.strictEqual(
+				object instanceof EventDispatcher, true,
+				'RenderTarget3D extends from EventDispatcher'
+			);
+
+			const options = new RenderTarget3D( 1, 1, 1, { magFilter: NearestFilter, wrapR: RepeatWrapping } );
+			assert.ok( options.width === 1 && options.height === 1 && options.depth === 1 && options.texture.magFilter === NearestFilter && options.texture.wrapR === RepeatWrapping, 'Can instantiate a RenderTarget3D with texture options.' );
+
+		} );
+
+	} );
+
+} );

+ 4 - 0
test/unit/src/renderers/WebGL3DRenderTarget.tests.js

@@ -1,5 +1,6 @@
 /* global QUnit */
 
+import { NearestFilter } from '../../../../src/constants.js';
 import { WebGL3DRenderTarget } from '../../../../src/renderers/WebGL3DRenderTarget.js';
 
 import { WebGLRenderTarget } from '../../../../src/renderers/WebGLRenderTarget.js';
@@ -17,6 +18,9 @@ export default QUnit.module( 'Renderers', () => {
 				'WebGL3DRenderTarget extends from WebGLRenderTarget'
 			);
 
+			const options = new WebGL3DRenderTarget( 1, 1, 1, { magFilter: NearestFilter } );
+			assert.ok( options.width === 1 && options.height === 1 && options.depth === 1 && options.texture.magFilter === NearestFilter, 'Can instantiate a WebGL3DRenderTarget with texture options.' );
+
 		} );
 
 		// INSTANCING

+ 4 - 0
test/unit/src/renderers/WebGLArrayRenderTarget.tests.js

@@ -1,5 +1,6 @@
 /* global QUnit */
 
+import { NearestFilter } from '../../../../src/constants.js';
 import { WebGLArrayRenderTarget } from '../../../../src/renderers/WebGLArrayRenderTarget.js';
 
 import { WebGLRenderTarget } from '../../../../src/renderers/WebGLRenderTarget.js';
@@ -17,6 +18,9 @@ export default QUnit.module( 'Renderers', () => {
 				'WebGLArrayRenderTarget extends from WebGLRenderTarget'
 			);
 
+			const options = new WebGLArrayRenderTarget( 1, 1, 1, { magFilter: NearestFilter } );
+			assert.ok( options.width === 1 && options.height === 1 && options.depth === 1 && options.texture.magFilter === NearestFilter, 'Can instantiate a WebGLArrayRenderTarget with texture options.' );
+
 		} );
 
 		// INSTANCING

+ 4 - 0
test/unit/src/renderers/WebGLCubeRenderTarget.tests.js

@@ -1,5 +1,6 @@
 /* global QUnit */
 
+import { NearestFilter } from '../../../../src/constants.js';
 import { WebGLCubeRenderTarget } from '../../../../src/renderers/WebGLCubeRenderTarget.js';
 
 import { WebGLRenderTarget } from '../../../../src/renderers/WebGLRenderTarget.js';
@@ -17,6 +18,9 @@ export default QUnit.module( 'Renderers', () => {
 				'WebGLCubeRenderTarget extends from WebGLRenderTarget'
 			);
 
+			const options = new WebGLCubeRenderTarget( 1, { magFilter: NearestFilter } );
+			assert.ok( options.width === 1 && options.height === 1 && options.texture.magFilter === NearestFilter, 'Can instantiate a WebGLCubeRenderTarget with texture options.' );
+
 		} );
 
 		// INSTANCING

+ 4 - 0
test/unit/src/renderers/WebGLRenderTarget.tests.js

@@ -3,6 +3,7 @@
 import { WebGLRenderTarget } from '../../../../src/renderers/WebGLRenderTarget.js';
 
 import { EventDispatcher } from '../../../../src/core/EventDispatcher.js';
+import { NearestFilter } from '../../../../src/constants.js';
 
 export default QUnit.module( 'Renderers', () => {
 
@@ -17,6 +18,9 @@ export default QUnit.module( 'Renderers', () => {
 				'WebGLRenderTarget extends from EventDispatcher'
 			);
 
+			const options = new WebGLRenderTarget( 1, 1, { magFilter: NearestFilter } );
+			assert.ok( options.width === 1 && options.height === 1 && options.texture.magFilter === NearestFilter, 'Can instantiate a WebGLRenderTarget with texture options.' );
+
 		} );
 
 		// INSTANCING

粤ICP备19079148号