Skip to content

Commit 43dc36a

Browse files
CodyJasonBennettRuthySheffi
authored andcommitted
Texture: Add setValues. (mrdoob#31087)
* Texture: Add setValues. * RenderTarget3D: Set texture options. * Tests: Add coverage for render targets. * RenderTarget: Respect wrapR option.
1 parent 4b337d6 commit 43dc36a

12 files changed

+200
-9
lines changed

src/core/RenderTarget.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,7 @@ class RenderTarget extends EventDispatcher {
126126

127127
const image = { width: width, height: height, depth: options.depth };
128128

129-
const texture = new Texture( image, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace );
130-
131-
texture.flipY = false;
132-
texture.generateMipmaps = options.generateMipmaps;
133-
texture.internalFormat = options.internalFormat;
129+
const texture = new Texture( image );
134130

135131
/**
136132
* An array of textures. Each color attachment is represented as a separate texture.
@@ -149,6 +145,8 @@ class RenderTarget extends EventDispatcher {
149145

150146
}
151147

148+
this._setTextureOptions( options );
149+
152150
/**
153151
* Whether to allocate a depth buffer or not.
154152
*
@@ -204,6 +202,38 @@ class RenderTarget extends EventDispatcher {
204202

205203
}
206204

205+
_setTextureOptions( options = {} ) {
206+
207+
const values = {
208+
minFilter: LinearFilter,
209+
generateMipmaps: false,
210+
flipY: false,
211+
internalFormat: null
212+
};
213+
214+
if ( options.mapping !== undefined ) values.mapping = options.mapping;
215+
if ( options.wrapS !== undefined ) values.wrapS = options.wrapS;
216+
if ( options.wrapT !== undefined ) values.wrapT = options.wrapT;
217+
if ( options.wrapR !== undefined ) values.wrapR = options.wrapR;
218+
if ( options.magFilter !== undefined ) values.magFilter = options.magFilter;
219+
if ( options.minFilter !== undefined ) values.minFilter = options.minFilter;
220+
if ( options.format !== undefined ) values.format = options.format;
221+
if ( options.type !== undefined ) values.type = options.type;
222+
if ( options.anisotropy !== undefined ) values.anisotropy = options.anisotropy;
223+
if ( options.colorSpace !== undefined ) values.colorSpace = options.colorSpace;
224+
if ( options.flipY !== undefined ) values.flipY = options.flipY;
225+
if ( options.generateMipmaps !== undefined ) values.generateMipmaps = options.generateMipmaps;
226+
if ( options.internalFormat !== undefined ) values.internalFormat = options.internalFormat;
227+
228+
for ( let i = 0; i < this.textures.length; i ++ ) {
229+
230+
const texture = this.textures[ i ];
231+
texture.setValues( values );
232+
233+
}
234+
235+
}
236+
207237
/**
208238
* The texture representing the default color attachment.
209239
*

src/core/RenderTarget3D.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class RenderTarget3D extends RenderTarget {
3737
* @type {Data3DTexture}
3838
*/
3939
this.texture = new Data3DTexture( null, width, height, depth );
40+
this._setTextureOptions( options );
4041

4142
this.texture.isRenderTargetTexture = true;
4243

src/renderers/WebGL3DRenderTarget.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class WebGL3DRenderTarget extends WebGLRenderTarget {
3737
* @type {Data3DTexture}
3838
*/
3939
this.texture = new Data3DTexture( null, width, height, depth );
40+
this._setTextureOptions( options );
4041

4142
this.texture.isRenderTargetTexture = true;
4243

src/renderers/WebGLArrayRenderTarget.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class WebGLArrayRenderTarget extends WebGLRenderTarget {
3737
* @type {DataArrayTexture}
3838
*/
3939
this.texture = new DataArrayTexture( null, width, height, depth );
40+
this._setTextureOptions( options );
4041

4142
this.texture.isRenderTargetTexture = true;
4243

src/renderers/WebGLCubeRenderTarget.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class WebGLCubeRenderTarget extends WebGLRenderTarget {
4141
*
4242
* @type {DataArrayTexture}
4343
*/
44-
this.texture = new CubeTexture( images, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace );
44+
this.texture = new CubeTexture( images );
45+
this._setTextureOptions( options );
4546

4647
// By convention -- likely based on the RenderMan spec from the 1990's -- cube maps are specified by WebGL (and three.js)
4748
// 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 {
5354

5455
this.texture.isRenderTargetTexture = true;
5556

56-
this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false;
57-
this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter;
58-
5957
}
6058

6159
/**

src/textures/Texture.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,54 @@ class Texture extends EventDispatcher {
509509

510510
}
511511

512+
/**
513+
* Sets this texture's properties based on `values`.
514+
* @param {Object} values - A container with texture parameters.
515+
*/
516+
setValues( values ) {
517+
518+
for ( const key in values ) {
519+
520+
const newValue = values[ key ];
521+
522+
if ( newValue === undefined ) {
523+
524+
console.warn( `THREE.Texture.setValues(): parameter '${ key }' has value of undefined.` );
525+
continue;
526+
527+
}
528+
529+
const currentValue = this[ key ];
530+
531+
if ( currentValue === undefined ) {
532+
533+
console.warn( `THREE.Texture.setValues(): property '${ key }' does not exist.` );
534+
continue;
535+
536+
}
537+
538+
if ( ( currentValue && newValue ) && ( currentValue.isVector2 && newValue.isVector2 ) ) {
539+
540+
currentValue.copy( newValue );
541+
542+
} else if ( ( currentValue && newValue ) && ( currentValue.isVector3 && newValue.isVector3 ) ) {
543+
544+
currentValue.copy( newValue );
545+
546+
} else if ( ( currentValue && newValue ) && ( currentValue.isMatrix3 && newValue.isMatrix3 ) ) {
547+
548+
currentValue.copy( newValue );
549+
550+
} else {
551+
552+
this[ key ] = newValue;
553+
554+
}
555+
556+
}
557+
558+
}
559+
512560
/**
513561
* Serializes the texture into JSON.
514562
*
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* global QUnit */
2+
3+
import { NearestFilter } from '../../../../src/constants.js';
4+
import { RenderTarget } from '../../../../src/core/RenderTarget.js';
5+
6+
export default QUnit.module( 'Core', () => {
7+
8+
QUnit.module( 'RenderTarget', () => {
9+
10+
// Constructor options
11+
QUnit.test( 'Constructor', ( assert ) => {
12+
13+
const empty = new RenderTarget();
14+
assert.ok( empty.width != null && empty.height != null && empty.textures.length === 1, 'Can instantiate a RenderTarget with no arguments.' );
15+
16+
const sized = new RenderTarget( 1, 1 );
17+
assert.ok( sized.width === 1 && sized.height === 1 && sized.textures.length === 1, 'Can instantiate a RenderTarget with custom size.' );
18+
19+
const mrt = new RenderTarget( 1, 1, { count: 2 } );
20+
assert.ok( mrt.width === 1 && mrt.height === 1 && mrt.textures.length === 2, 'Can instantiate a RenderTarget with custom count (MRT).' );
21+
22+
const options = new RenderTarget( 1, 1, { magFilter: NearestFilter } );
23+
assert.ok( options.width === 1 && options.height === 1 && options.texture.magFilter === NearestFilter, 'Can instantiate a RenderTarget with texture options.' );
24+
25+
} );
26+
27+
// PROPERTIES
28+
QUnit.todo( 'texture', ( assert ) => {
29+
30+
assert.ok( false, 'everything\'s gonna be alright' );
31+
32+
} );
33+
34+
QUnit.todo( 'depthTexture', ( assert ) => {
35+
36+
assert.ok( false, 'everything\'s gonna be alright' );
37+
38+
} );
39+
40+
// PUBLIC
41+
QUnit.test( 'setSize', ( assert ) => {
42+
43+
const renderTarget = new RenderTarget();
44+
renderTarget.setSize( 128, 128 );
45+
assert.ok( renderTarget.width === 128 && renderTarget.height === 128, 'Sets a size with width and height' );
46+
assert.ok( renderTarget.texture.image.width === 128 && renderTarget.texture.image.height === 128, 'Texture image is updated on resize' );
47+
assert.ok( renderTarget.viewport.width === 128 && renderTarget.viewport.height === 128, 'Viewport is updated on resize' );
48+
assert.ok( renderTarget.scissor.width === 128 && renderTarget.scissor.height === 128, 'Scissor is updated on resize' );
49+
50+
const mrt = new RenderTarget( 0, 0, { count: 2 } );
51+
mrt.setSize( 128, 128 );
52+
assert.ok( mrt.width === 128 && mrt.height === 128, 'Sets a size with width and height' );
53+
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' );
54+
assert.ok( mrt.viewport.width === 128 && mrt.viewport.height === 128, 'Viewport is updated on resize' );
55+
assert.ok( mrt.scissor.width === 128 && mrt.scissor.height === 128, 'Scissor is updated on resize' );
56+
57+
const renderTarget3D = new RenderTarget();
58+
renderTarget3D.setSize( 128, 128, 16 );
59+
assert.ok( renderTarget3D.width === 128 && renderTarget3D.height === 128 && renderTarget3D.depth === 16, 'Sets a size with width, height, and depth' );
60+
assert.ok( renderTarget3D.texture.image.width === 128 && renderTarget3D.texture.image.height === 128 && renderTarget3D.texture.image.depth === 16, 'Texture image is updated on resize' );
61+
assert.ok( renderTarget3D.viewport.width === 128 && renderTarget3D.viewport.height === 128, 'Viewport is updated on resize' );
62+
assert.ok( renderTarget3D.scissor.width === 128 && renderTarget3D.scissor.height === 128, 'Scissor is updated on resize' );
63+
64+
} );
65+
66+
} );
67+
68+
} );
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* global QUnit */
2+
3+
import { RenderTarget3D } from '../../../../src/core/RenderTarget3D.js';
4+
5+
import { EventDispatcher } from '../../../../src/core/EventDispatcher.js';
6+
import { NearestFilter, RepeatWrapping } from '../../../../src/constants.js';
7+
8+
export default QUnit.module( 'Core', () => {
9+
10+
QUnit.module( 'RenderTarget3D', () => {
11+
12+
// INHERITANCE
13+
QUnit.test( 'Extending', ( assert ) => {
14+
15+
const object = new RenderTarget3D();
16+
assert.strictEqual(
17+
object instanceof EventDispatcher, true,
18+
'RenderTarget3D extends from EventDispatcher'
19+
);
20+
21+
const options = new RenderTarget3D( 1, 1, 1, { magFilter: NearestFilter, wrapR: RepeatWrapping } );
22+
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.' );
23+
24+
} );
25+
26+
} );
27+
28+
} );

test/unit/src/renderers/WebGL3DRenderTarget.tests.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* global QUnit */
22

3+
import { NearestFilter } from '../../../../src/constants.js';
34
import { WebGL3DRenderTarget } from '../../../../src/renderers/WebGL3DRenderTarget.js';
45

56
import { WebGLRenderTarget } from '../../../../src/renderers/WebGLRenderTarget.js';
@@ -17,6 +18,9 @@ export default QUnit.module( 'Renderers', () => {
1718
'WebGL3DRenderTarget extends from WebGLRenderTarget'
1819
);
1920

21+
const options = new WebGL3DRenderTarget( 1, 1, 1, { magFilter: NearestFilter } );
22+
assert.ok( options.width === 1 && options.height === 1 && options.depth === 1 && options.texture.magFilter === NearestFilter, 'Can instantiate a WebGL3DRenderTarget with texture options.' );
23+
2024
} );
2125

2226
// INSTANCING

test/unit/src/renderers/WebGLArrayRenderTarget.tests.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* global QUnit */
22

3+
import { NearestFilter } from '../../../../src/constants.js';
34
import { WebGLArrayRenderTarget } from '../../../../src/renderers/WebGLArrayRenderTarget.js';
45

56
import { WebGLRenderTarget } from '../../../../src/renderers/WebGLRenderTarget.js';
@@ -17,6 +18,9 @@ export default QUnit.module( 'Renderers', () => {
1718
'WebGLArrayRenderTarget extends from WebGLRenderTarget'
1819
);
1920

21+
const options = new WebGLArrayRenderTarget( 1, 1, 1, { magFilter: NearestFilter } );
22+
assert.ok( options.width === 1 && options.height === 1 && options.depth === 1 && options.texture.magFilter === NearestFilter, 'Can instantiate a WebGLArrayRenderTarget with texture options.' );
23+
2024
} );
2125

2226
// INSTANCING

0 commit comments

Comments
 (0)