Browse Source

addd WebGLUtils

Jaume Sanchez 8 years ago
parent
commit
615e25b578
2 changed files with 152 additions and 0 deletions
  1. 1 0
      src/Three.js
  2. 151 0
      src/extras/WebGLUtils.js

+ 1 - 0
src/Three.js

@@ -156,5 +156,6 @@ export { CurvePath } from './extras/core/CurvePath.js';
 export { Curve } from './extras/core/Curve.js';
 export { ShapeUtils } from './extras/ShapeUtils.js';
 export { SceneUtils } from './extras/SceneUtils.js';
+export { WebGLUtils } from './extras/WebGLUtils.js';
 export * from './constants.js';
 export * from './Three.Legacy.js';

+ 151 - 0
src/extras/WebGLUtils.js

@@ -0,0 +1,151 @@
+/**
+ * @author thespite / http://www.twitter.com/thespite
+ */
+
+import { REVISION, MaxEquation, MinEquation, RGB_ETC1_Format, RGBA_PVRTC_2BPPV1_Format, RGBA_PVRTC_4BPPV1_Format, RGB_PVRTC_2BPPV1_Format, RGB_PVRTC_4BPPV1_Format, RGBA_S3TC_DXT5_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT1_Format, RGB_S3TC_DXT1_Format, SrcAlphaSaturateFactor, OneMinusDstColorFactor, DstColorFactor, OneMinusDstAlphaFactor, DstAlphaFactor, OneMinusSrcAlphaFactor, SrcAlphaFactor, OneMinusSrcColorFactor, SrcColorFactor, OneFactor, ZeroFactor, ReverseSubtractEquation, SubtractEquation, AddEquation, DepthFormat, DepthStencilFormat, LuminanceAlphaFormat, LuminanceFormat, RGBAFormat, RGBFormat, AlphaFormat, HalfFloatType, FloatType, UnsignedIntType, IntType, UnsignedShortType, ShortType, ByteType, UnsignedInt248Type, UnsignedShort565Type, UnsignedShort5551Type, UnsignedShort4444Type, UnsignedByteType, LinearMipMapLinearFilter, LinearMipMapNearestFilter, LinearFilter, NearestMipMapLinearFilter, NearestMipMapNearestFilter, NearestFilter, MirroredRepeatWrapping, ClampToEdgeWrapping, RepeatWrapping, FrontFaceDirectionCW, NoBlending, TriangleFanDrawMode, TriangleStripDrawMode, TrianglesDrawMode, NoColors, LinearToneMapping } from '../constants';
+import { WebGLExtensions } from '../renderers/webgl/WebGLExtensions';
+
+var canvas = document.createElement( 'canvas' );
+var _gl = canvas.getContext( 'webgl' );
+
+var extensions = new WebGLExtensions( _gl );
+extensions.get( 'WEBGL_depth_texture' );
+extensions.get( 'OES_texture_float' );
+extensions.get( 'OES_texture_float_linear' );
+extensions.get( 'OES_texture_half_float' );
+extensions.get( 'OES_texture_half_float_linear' );
+extensions.get( 'OES_standard_derivatives' );
+extensions.get( 'ANGLE_instanced_arrays' );
+
+var WebGLUtils = {
+
+	convert: function ( p ) {
+
+		var extension;
+
+		if ( p === RepeatWrapping ) return _gl.REPEAT;
+		if ( p === ClampToEdgeWrapping ) return _gl.CLAMP_TO_EDGE;
+		if ( p === MirroredRepeatWrapping ) return _gl.MIRRORED_REPEAT;
+
+		if ( p === NearestFilter ) return _gl.NEAREST;
+		if ( p === NearestMipMapNearestFilter ) return _gl.NEAREST_MIPMAP_NEAREST;
+		if ( p === NearestMipMapLinearFilter ) return _gl.NEAREST_MIPMAP_LINEAR;
+
+		if ( p === LinearFilter ) return _gl.LINEAR;
+		if ( p === LinearMipMapNearestFilter ) return _gl.LINEAR_MIPMAP_NEAREST;
+		if ( p === LinearMipMapLinearFilter ) return _gl.LINEAR_MIPMAP_LINEAR;
+
+		if ( p === UnsignedByteType ) return _gl.UNSIGNED_BYTE;
+		if ( p === UnsignedShort4444Type ) return _gl.UNSIGNED_SHORT_4_4_4_4;
+		if ( p === UnsignedShort5551Type ) return _gl.UNSIGNED_SHORT_5_5_5_1;
+		if ( p === UnsignedShort565Type ) return _gl.UNSIGNED_SHORT_5_6_5;
+
+		if ( p === ByteType ) return _gl.BYTE;
+		if ( p === ShortType ) return _gl.SHORT;
+		if ( p === UnsignedShortType ) return _gl.UNSIGNED_SHORT;
+		if ( p === IntType ) return _gl.INT;
+		if ( p === UnsignedIntType ) return _gl.UNSIGNED_INT;
+		if ( p === FloatType ) return _gl.FLOAT;
+
+		if ( p === HalfFloatType ) {
+
+			extension = extensions.get( 'OES_texture_half_float' );
+
+			if ( extension !== null ) return extension.HALF_FLOAT_OES;
+
+		}
+
+		if ( p === AlphaFormat ) return _gl.ALPHA;
+		if ( p === RGBFormat ) return _gl.RGB;
+		if ( p === RGBAFormat ) return _gl.RGBA;
+		if ( p === LuminanceFormat ) return _gl.LUMINANCE;
+		if ( p === LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA;
+		if ( p === DepthFormat ) return _gl.DEPTH_COMPONENT;
+		if ( p === DepthStencilFormat ) return _gl.DEPTH_STENCIL;
+
+		if ( p === AddEquation ) return _gl.FUNC_ADD;
+		if ( p === SubtractEquation ) return _gl.FUNC_SUBTRACT;
+		if ( p === ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT;
+
+		if ( p === ZeroFactor ) return _gl.ZERO;
+		if ( p === OneFactor ) return _gl.ONE;
+		if ( p === SrcColorFactor ) return _gl.SRC_COLOR;
+		if ( p === OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR;
+		if ( p === SrcAlphaFactor ) return _gl.SRC_ALPHA;
+		if ( p === OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA;
+		if ( p === DstAlphaFactor ) return _gl.DST_ALPHA;
+		if ( p === OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA;
+
+		if ( p === DstColorFactor ) return _gl.DST_COLOR;
+		if ( p === OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR;
+		if ( p === SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE;
+
+		if ( p === RGB_S3TC_DXT1_Format || p === RGBA_S3TC_DXT1_Format ||
+			p === RGBA_S3TC_DXT3_Format || p === RGBA_S3TC_DXT5_Format ) {
+
+			extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
+
+			if ( extension !== null ) {
+
+				if ( p === RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT;
+				if ( p === RGBA_S3TC_DXT1_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT;
+				if ( p === RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT;
+				if ( p === RGBA_S3TC_DXT5_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT;
+
+			}
+
+		}
+
+		if ( p === RGB_PVRTC_4BPPV1_Format || p === RGB_PVRTC_2BPPV1_Format ||
+			p === RGBA_PVRTC_4BPPV1_Format || p === RGBA_PVRTC_2BPPV1_Format ) {
+
+			extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );
+
+			if ( extension !== null ) {
+
+				if ( p === RGB_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
+				if ( p === RGB_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
+				if ( p === RGBA_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
+				if ( p === RGBA_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
+
+			}
+
+		}
+
+		if ( p === RGB_ETC1_Format ) {
+
+			extension = extensions.get( 'WEBGL_compressed_texture_etc1' );
+
+			if ( extension !== null ) return extension.COMPRESSED_RGB_ETC1_WEBGL;
+
+		}
+
+		if ( p === MinEquation || p === MaxEquation ) {
+
+			extension = extensions.get( 'EXT_blend_minmax' );
+
+			if ( extension !== null ) {
+
+				if ( p === MinEquation ) return extension.MIN_EXT;
+				if ( p === MaxEquation ) return extension.MAX_EXT;
+
+			}
+
+		}
+
+		if ( p === UnsignedInt248Type ) {
+
+			extension = extensions.get( 'WEBGL_depth_texture' );
+
+			if ( extension !== null ) return extension.UNSIGNED_INT_24_8_WEBGL;
+
+		}
+
+		return 0;
+
+	}
+
+};
+
+
+export { WebGLUtils };