瀏覽代碼

Move renderer extension check to internal API.

yellowtailfan 4 年之前
父節點
當前提交
ccfd940ede

+ 15 - 29
examples/jsm/lights/RectAreaLightUniformsLib.js

@@ -84,7 +84,7 @@ function toHalf ( val ) {
 var RectAreaLightUniformsLib = {
 
 	// renderer should be an instance of THREE.WebGLRenderer
-	init: function ( renderer ) {
+	init: function () {
 
 		// source: https://github.com/selfshadow/ltc_code/tree/master/fit/results/ltc.js
 
@@ -94,39 +94,25 @@ var RectAreaLightUniformsLib = {
 
 		// data textures
 
-		var ltc_1 = null;
-		var ltc_2 = null;
+		const ltc_float_1 = new Float32Array( LTC_MAT_1 );
+		const ltc_float_2 = new Float32Array( LTC_MAT_2 );
 
-		if ( renderer.capabilities.isWebGL2 === true || renderer.extensions.get( 'OES_texture_float_linear' ) ) {
+		UniformsLib.LTC_FLOAT_1 = new DataTexture( ltc_float_1, 64, 64, RGBAFormat, FloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
+		UniformsLib.LTC_FLOAT_2 = new DataTexture( ltc_float_2, 64, 64, RGBAFormat, FloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
 
-			const ltc_float_1 = new Float32Array( LTC_MAT_1 );
-			const ltc_float_2 = new Float32Array( LTC_MAT_2 );
+		const ltc_half_1 = new Uint16Array( LTC_MAT_1.length );
+		LTC_MAT_1.forEach( function ( x, index ) {
+			ltc_half_1[index] = toHalf( x );
+		});
 
-			ltc_1 = new DataTexture( ltc_float_1, 64, 64, RGBAFormat, FloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
-			ltc_2 = new DataTexture( ltc_float_2, 64, 64, RGBAFormat, FloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
-		
-		} else if ( renderer.extensions.get( 'OES_texture_half_float_linear' ) ) {
-			
-			const ltc_half_1 = new Uint16Array( LTC_MAT_1.length );
-			LTC_MAT_1.forEach( function ( x, index ) {
-				ltc_half_1[index] = toHalf( x );
-			});
+		const ltc_half_2 = new Uint16Array( LTC_MAT_2.length );
+		LTC_MAT_2.forEach( function ( x, index ) {
+			ltc_half_2[index] = toHalf( x );
+		});
 	
-			const ltc_half_2 = new Uint16Array( LTC_MAT_2.length );
-			LTC_MAT_2.forEach( function ( x, index ) {
-				ltc_half_2[index] = toHalf( x );
-			});
+		UniformsLib.LTC_HALF_1 = new DataTexture( ltc_half_1, 64, 64, RGBAFormat, HalfFloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
+		UniformsLib.LTC_HALF_2 = new DataTexture( ltc_half_2, 64, 64, RGBAFormat, HalfFloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
 		
-			ltc_1 = new DataTexture( ltc_half_1, 64, 64, RGBAFormat, HalfFloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
-			ltc_2 = new DataTexture( ltc_half_2, 64, 64, RGBAFormat, HalfFloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
-			
-		} else {
-			throw 'missing webgl extension';
-		}
-
-		UniformsLib.LTC_1 = ltc_1;
-		UniformsLib.LTC_2 = ltc_2;
-
 	}
 
 };

+ 1 - 1
examples/webgl_lights_rectarealight.html

@@ -54,7 +54,7 @@
 				var ambient = new THREE.AmbientLight( 0xffffff, 0.1 );
 				scene.add( ambient );
 
-				RectAreaLightUniformsLib.init( renderer );
+				RectAreaLightUniformsLib.init( );
 
 				rectLight = new THREE.RectAreaLight( 0xffffff, 1, 10, 10 );
 				rectLight.position.set( 5, 5, 0 );

+ 1 - 1
src/renderers/WebGLRenderer.js

@@ -297,7 +297,7 @@ function WebGLRenderer( parameters ) {
 		programCache = new WebGLPrograms( _this, cubemaps, extensions, capabilities, bindingStates, clipping );
 		materials = new WebGLMaterials( properties );
 		renderLists = new WebGLRenderLists( properties );
-		renderStates = new WebGLRenderStates();
+		renderStates = new WebGLRenderStates( extensions, capabilities );
 		background = new WebGLBackground( _this, cubemaps, state, objects, _premultipliedAlpha );
 
 		bufferRenderer = new WebGLBufferRenderer( _gl, extensions, info, capabilities );

+ 4 - 1
src/renderers/webgl/WebGLLights.d.ts

@@ -1,6 +1,9 @@
+import { WebGLExtensions } from './WebGLExtensions';
+import { WebGLCapabilities } from './WebGLCapabilities';
+
 export class WebGLLights {
 
-	constructor( gl: WebGLRenderingContext, properties: any, info: any );
+	constructor( extensions: WebGLExtensions, capabilities: WebGLCapabilities );
 
 	state: {
 		version: number;

+ 17 - 4
src/renderers/webgl/WebGLLights.js

@@ -150,7 +150,7 @@ function shadowCastingLightsFirst( lightA, lightB ) {
 
 }
 
-function WebGLLights() {
+function WebGLLights( extensions, capabilities ) {
 
 	const cache = new UniformsCache();
 
@@ -404,11 +404,24 @@ function WebGLLights() {
 			}
 
 		}
-
+	
 		if ( rectAreaLength > 0 ) {
 
-			state.rectAreaLTC1 = UniformsLib.LTC_1;
-			state.rectAreaLTC2 = UniformsLib.LTC_2;
+			if ( capabilities.isWebGL2 || extensions.get( 'OES_texture_float_linear' ) ) {
+
+				state.rectAreaLTC1 = UniformsLib.LTC_FLOAT_1;
+				state.rectAreaLTC2 = UniformsLib.LTC_FLOAT_2;
+
+			} else if ( extensions.get( 'OES_texture_half_float_linear' ) ) {
+
+				state.rectAreaLTC1 = UniformsLib.LTC_HALF_1;
+				state.rectAreaLTC2 = UniformsLib.LTC_HALF_2;
+
+			} else {
+
+				throw 'missing webgl extension';
+
+			}
 
 		}
 

+ 4 - 0
src/renderers/webgl/WebGLRenderStates.d.ts

@@ -2,6 +2,8 @@ import { Scene } from '../../scenes/Scene';
 import { Camera } from '../../cameras/Camera';
 import { Light } from '../../lights/Light';
 import { WebGLLights } from './WebGLLights';
+import { WebGLExtensions } from './WebGLExtensions';
+import { WebGLCapabilities } from './WebGLCapabilities';
 
 interface WebGLRenderState {
 
@@ -20,6 +22,8 @@ interface WebGLRenderState {
 
 export class WebGLRenderStates {
 
+	constructor( extensions: WebGLExtensions, capabilities: WebGLCapabilities );
+
 	get( scene: Scene, camera: Camera ): WebGLRenderState;
 	dispose(): void;
 

+ 5 - 5
src/renderers/webgl/WebGLRenderStates.js

@@ -1,8 +1,8 @@
 import { WebGLLights } from './WebGLLights.js';
 
-function WebGLRenderState() {
+function WebGLRenderState( extensions, capabilities ) {
 
-	const lights = new WebGLLights();
+	const lights = new WebGLLights( extensions, capabilities );
 
 	const lightsArray = [];
 	const shadowsArray = [];
@@ -50,7 +50,7 @@ function WebGLRenderState() {
 
 }
 
-function WebGLRenderStates() {
+function WebGLRenderStates( extensions, capabilities ) {
 
 	let renderStates = new WeakMap();
 
@@ -60,7 +60,7 @@ function WebGLRenderStates() {
 
 		if ( renderStates.has( scene ) === false ) {
 
-			renderState = new WebGLRenderState();
+			renderState = new WebGLRenderState( extensions, capabilities );
 			renderStates.set( scene, new WeakMap() );
 			renderStates.get( scene ).set( camera, renderState );
 
@@ -68,7 +68,7 @@ function WebGLRenderStates() {
 
 			if ( renderStates.get( scene ).has( camera ) === false ) {
 
-				renderState = new WebGLRenderState();
+				renderState = new WebGLRenderState( extensions, capabilities );
 				renderStates.get( scene ).set( camera, renderState );
 
 			} else {