Skip to content

Commit 640f046

Browse files
authored
Merge pull request #16069 from mrdoob/webgluniforms
WebGLUniforms: Remove WebGLRenderer dependency.
2 parents de338cf + 08ce26f commit 640f046

File tree

5 files changed

+134
-175
lines changed

5 files changed

+134
-175
lines changed

src/renderers/WebGLRenderer.js

Lines changed: 4 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ function WebGLRenderer( parameters ) {
145145

146146
//
147147

148-
_usedTextureUnits = 0,
149-
150-
//
151-
152148
_width = _canvas.width,
153149
_height = _canvas.height,
154150

@@ -1635,7 +1631,7 @@ function WebGLRenderer( parameters ) {
16351631

16361632
function setProgram( camera, fog, material, object ) {
16371633

1638-
_usedTextureUnits = 0;
1634+
textures.resetTextureUnits();
16391635

16401636
var materialProperties = properties.get( material );
16411637
var lights = currentRenderState.state.lights;
@@ -1822,7 +1818,7 @@ function WebGLRenderer( parameters ) {
18221818

18231819
}
18241820

1825-
p_uniforms.setValue( _gl, 'boneTexture', skeleton.boneTexture );
1821+
p_uniforms.setValue( _gl, 'boneTexture', skeleton.boneTexture, textures );
18261822
p_uniforms.setValue( _gl, 'boneTextureSize', skeleton.boneTextureSize );
18271823

18281824
} else {
@@ -1952,13 +1948,13 @@ function WebGLRenderer( parameters ) {
19521948
if ( m_uniforms.ltc_1 !== undefined ) m_uniforms.ltc_1.value = UniformsLib.LTC_1;
19531949
if ( m_uniforms.ltc_2 !== undefined ) m_uniforms.ltc_2.value = UniformsLib.LTC_2;
19541950

1955-
WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, _this, textures );
1951+
WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures );
19561952

19571953
}
19581954

19591955
if ( material.isShaderMaterial && material.uniformsNeedUpdate === true ) {
19601956

1961-
WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, _this, textures );
1957+
WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures );
19621958
material.uniformsNeedUpdate = false;
19631959

19641960
}
@@ -2421,114 +2417,6 @@ function WebGLRenderer( parameters ) {
24212417

24222418
}
24232419

2424-
// Textures
2425-
2426-
function allocTextureUnit() {
2427-
2428-
var textureUnit = _usedTextureUnits;
2429-
2430-
if ( textureUnit >= capabilities.maxTextures ) {
2431-
2432-
console.warn( 'THREE.WebGLRenderer: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + capabilities.maxTextures );
2433-
2434-
}
2435-
2436-
_usedTextureUnits += 1;
2437-
2438-
return textureUnit;
2439-
2440-
}
2441-
2442-
this.allocTextureUnit = allocTextureUnit;
2443-
2444-
// this.setTexture2D = setTexture2D;
2445-
this.setTexture2D = ( function () {
2446-
2447-
var warned = false;
2448-
2449-
// backwards compatibility: peel texture.texture
2450-
return function setTexture2D( texture, slot ) {
2451-
2452-
if ( texture && texture.isWebGLRenderTarget ) {
2453-
2454-
if ( ! warned ) {
2455-
2456-
console.warn( "THREE.WebGLRenderer.setTexture2D: don't use render targets as textures. Use their .texture property instead." );
2457-
warned = true;
2458-
2459-
}
2460-
2461-
texture = texture.texture;
2462-
2463-
}
2464-
2465-
textures.setTexture2D( texture, slot );
2466-
2467-
};
2468-
2469-
}() );
2470-
2471-
this.setTexture = ( function () {
2472-
2473-
var warned = false;
2474-
2475-
return function setTexture( texture, slot ) {
2476-
2477-
if ( ! warned ) {
2478-
2479-
console.warn( "THREE.WebGLRenderer: .setTexture is deprecated, use setTexture2D instead." );
2480-
warned = true;
2481-
2482-
}
2483-
2484-
textures.setTexture2D( texture, slot );
2485-
2486-
};
2487-
2488-
}() );
2489-
2490-
this.setTextureCube = ( function () {
2491-
2492-
var warned = false;
2493-
2494-
return function setTextureCube( texture, slot ) {
2495-
2496-
// backwards compatibility: peel texture.texture
2497-
if ( texture && texture.isWebGLRenderTargetCube ) {
2498-
2499-
if ( ! warned ) {
2500-
2501-
console.warn( "THREE.WebGLRenderer.setTextureCube: don't use cube render targets as textures. Use their .texture property instead." );
2502-
warned = true;
2503-
2504-
}
2505-
2506-
texture = texture.texture;
2507-
2508-
}
2509-
2510-
// currently relying on the fact that WebGLRenderTargetCube.texture is a Texture and NOT a CubeTexture
2511-
// TODO: unify these code paths
2512-
if ( ( texture && texture.isCubeTexture ) ||
2513-
( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {
2514-
2515-
// CompressedTexture can have Array in image :/
2516-
2517-
// this function alone should take care of cube textures
2518-
textures.setTextureCube( texture, slot );
2519-
2520-
} else {
2521-
2522-
// assumed: texture property of THREE.WebGLRenderTargetCube
2523-
2524-
textures.setTextureCubeDynamic( texture, slot );
2525-
2526-
}
2527-
2528-
};
2529-
2530-
}() );
2531-
25322420
//
25332421

25342422
this.setFramebuffer = function ( value ) {

src/renderers/webgl/WebGLProgram.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ function WebGLProgram( renderer, extensions, code, material, shader, parameters,
670670

671671
if ( cachedUniforms === undefined ) {
672672

673-
cachedUniforms = new WebGLUniforms( gl, program, renderer, textures );
673+
cachedUniforms = new WebGLUniforms( gl, program, textures );
674674

675675
}
676676

src/renderers/webgl/WebGLTextures.js

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,31 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
265265

266266
//
267267

268+
var textureUnits = 0;
268269

270+
function resetTextureUnits() {
271+
272+
textureUnits = 0;
273+
274+
}
275+
276+
function allocateTextureUnit() {
277+
278+
var textureUnit = textureUnits;
279+
280+
if ( textureUnit >= capabilities.maxTextures ) {
281+
282+
console.warn( 'THREE.WebGLTextures: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + capabilities.maxTextures );
283+
284+
}
285+
286+
textureUnits += 1;
287+
288+
return textureUnit;
289+
290+
}
291+
292+
//
269293

270294
function setTexture2D( texture, slot ) {
271295

@@ -1077,6 +1101,69 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
10771101

10781102
}
10791103

1104+
// backwards compatibility
1105+
1106+
var warnedTexture2D = false;
1107+
var warnedTextureCube = false;
1108+
1109+
function safeSetTexture2D( texture, slot ) {
1110+
1111+
if ( texture && texture.isWebGLRenderTarget ) {
1112+
1113+
if ( warnedTexture2D === false ) {
1114+
1115+
console.warn( "THREE.WebGLTextures.safeSetTexture2D: don't use render targets as textures. Use their .texture property instead." );
1116+
warnedTexture2D = true;
1117+
1118+
}
1119+
1120+
texture = texture.texture;
1121+
1122+
}
1123+
1124+
setTexture2D( texture, slot );
1125+
1126+
}
1127+
1128+
function safeSetTextureCube( texture, slot ) {
1129+
1130+
if ( texture && texture.isWebGLRenderTargetCube ) {
1131+
1132+
if ( warnedTextureCube === false ) {
1133+
1134+
console.warn( "THREE.WebGLTextures.safeSetTextureCube: don't use cube render targets as textures. Use their .texture property instead." );
1135+
warnedTextureCube = true;
1136+
1137+
}
1138+
1139+
texture = texture.texture;
1140+
1141+
}
1142+
1143+
// currently relying on the fact that WebGLRenderTargetCube.texture is a Texture and NOT a CubeTexture
1144+
// TODO: unify these code paths
1145+
if ( ( texture && texture.isCubeTexture ) ||
1146+
( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {
1147+
1148+
// CompressedTexture can have Array in image :/
1149+
1150+
// this function alone should take care of cube textures
1151+
setTextureCube( texture, slot );
1152+
1153+
} else {
1154+
1155+
// assumed: texture property of THREE.WebGLRenderTargetCube
1156+
setTextureCubeDynamic( texture, slot );
1157+
1158+
}
1159+
1160+
}
1161+
1162+
//
1163+
1164+
this.allocateTextureUnit = allocateTextureUnit;
1165+
this.resetTextureUnits = resetTextureUnits;
1166+
10801167
this.setTexture2D = setTexture2D;
10811168
this.setTexture2DArray = setTexture2DArray;
10821169
this.setTexture3D = setTexture3D;
@@ -1086,7 +1173,9 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
10861173
this.updateRenderTargetMipmap = updateRenderTargetMipmap;
10871174
this.updateMultisampleRenderTarget = updateMultisampleRenderTarget;
10881175

1089-
}
1176+
this.safeSetTexture2D = safeSetTexture2D;
1177+
this.safeSetTextureCube = safeSetTextureCube;
10901178

1179+
}
10911180

10921181
export { WebGLTextures };

src/renderers/webgl/WebGLUniforms.d.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { WebGLProgram } from './WebGLProgram';
2-
import { WebGLRenderer } from './../WebGLRenderer';
32
import { WebGLTextures } from './WebGLTextures';
43

54
export class WebGLUniforms {
6-
constructor(gl: any, program: WebGLProgram, renderer: WebGLRenderer, textures: WebGLTextures);
5+
constructor(gl: any, program: WebGLProgram);
76

8-
renderer: WebGLRenderer;
9-
10-
setValue(gl: any, name: string, value: any): void;
11-
set(gl: any, object: any, name: string): void;
7+
setValue(gl: any, name: string, value: any, textures: WebGLTextures): void;
128
setOptional(gl: any, object: any, name: string): void;
139

14-
static upload(gl: any, seq: any, values: any[], renderer: WebGLRenderer, textures: WebGLTextures): void;
10+
static upload(gl: any, seq: any, values: any[], textures: WebGLTextures): void;
1511
static seqWithValue(seq: any, values: any[]): any[];
1612
static splitDynamic(seq: any, values: any[]): any[];
1713
static evalDynamic(seq: any, values: any[], object: any, camera: any): any[];

0 commit comments

Comments
 (0)