浏览代码

Merge pull request #16958 from Mugen87/dev36

JSM: Move WebGL.js into THREE namespace and modularize.
Michael Herzog 6 年之前
父节点
当前提交
b96f4e005d

+ 2 - 2
docs/manual/en/introduction/How-to-use-WebGL2.html

@@ -22,11 +22,11 @@
 
 	<p>
 		Since WebGL 2 is not supported by all devices that support WebGL 1, it's important to check the respective availability.
-		To do so, please include [link:https://github.com/mrdoob/three.js/blob/master/examples/js/WebGL.js WebGL.js] into your project.
+		To do so, please include [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/WebGL.js WebGL.js] into your project.
 	</p>
 
 	<code>
-&lt;script src="/path/to/WebGL.js"&gt;&lt;/script&gt;
+import { WEBGL } from 'three/examples/jsm/WebGL.js';
 	</code>
 
 	<p>

+ 1 - 1
docs/manual/en/introduction/WebGL-compatibility-check.html

@@ -15,7 +15,7 @@
 		</p>
 
 		<p>
-			Add	[link:https://github.com/mrdoob/three.js/blob/master/examples/js/WebGL.js]
+			Add	[link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/WebGL.js]
 			to your javascript and run the following before attempting to render anything.
 		</p>
 

+ 2 - 2
docs/manual/zh/introduction/How-to-use-WebGL2.html

@@ -21,11 +21,11 @@
 
 	<p>
 		由于WebGL 2并不被所有支持WebGL 1的设备所支持,因此检查各种设备上WebGL 2的可用性是非常重要的。
-		要对其可用性进行检查,请将[link:https://github.com/mrdoob/three.js/blob/master/examples/js/WebGL.js WebGL.js]包含到你的项目中。
+		要对其可用性进行检查,请将[link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/WebGL.js WebGL.js]包含到你的项目中。
 	</p>
 
 	<code>
-&lt;script src="/path/to/WebGL.js"&gt;&lt;/script&gt;
+import { WEBGL } from 'three/examples/jsm/WebGL.js';
 	</code>
 
 	<p>

+ 1 - 1
docs/manual/zh/introduction/WebGL-compatibility-check.html

@@ -14,7 +14,7 @@
 		</p>
 
 		<p>
-			请将[link:https://github.com/mrdoob/three.js/blob/master/examples/js/WebGL.js]引入到你的文件,并在尝试开始渲染之前先运行该文件。
+			请将[link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/WebGL.js]引入到你的文件,并在尝试开始渲染之前先运行该文件。
         	</p>
 
 <code>

+ 1 - 1
examples/js/WebGL.js

@@ -3,7 +3,7 @@
  * @author mr.doob / http://mrdoob.com/
  */
 
-var WEBGL = {
+THREE.WEBGL = {
 
 	isWebGLAvailable: function () {
 

+ 9 - 0
examples/jsm/WebGL.d.ts

@@ -0,0 +1,9 @@
+export namespace WEBGL {
+
+  export function isWebGLAvailable(): boolean;
+  export function isWebGL2Available(): boolean;
+  export function getWebGLErrorMessage(): HTMLElement;
+  export function getWebGL2ErrorMessage(): HTMLElement;
+  export function getErrorMessage(version: number): HTMLElement;
+
+}

+ 98 - 0
examples/jsm/WebGL.js

@@ -0,0 +1,98 @@
+/**
+ * @author alteredq / http://alteredqualia.com/
+ * @author mr.doob / http://mrdoob.com/
+ */
+
+
+
+var WEBGL = {
+
+	isWebGLAvailable: function () {
+
+		try {
+
+			var canvas = document.createElement( 'canvas' );
+			return !! ( window.WebGLRenderingContext && ( canvas.getContext( 'webgl' ) || canvas.getContext( 'experimental-webgl' ) ) );
+
+		} catch ( e ) {
+
+			return false;
+
+		}
+
+	},
+
+	isWebGL2Available: function () {
+
+		try {
+
+			var canvas = document.createElement( 'canvas' );
+			return !! ( window.WebGL2RenderingContext && canvas.getContext( 'webgl2' ) );
+
+		} catch ( e ) {
+
+			return false;
+
+		}
+
+	},
+
+	getWebGLErrorMessage: function () {
+
+		return this.getErrorMessage( 1 );
+
+	},
+
+	getWebGL2ErrorMessage: function () {
+
+		return this.getErrorMessage( 2 );
+
+	},
+
+	getErrorMessage: function ( version ) {
+
+		var names = {
+			1: 'WebGL',
+			2: 'WebGL 2'
+		};
+
+		var contexts = {
+			1: window.WebGLRenderingContext,
+			2: window.WebGL2RenderingContext
+		};
+
+		var message = 'Your $0 does not seem to support <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation" style="color:#000">$1</a>';
+
+		var element = document.createElement( 'div' );
+		element.id = 'webglmessage';
+		element.style.fontFamily = 'monospace';
+		element.style.fontSize = '13px';
+		element.style.fontWeight = 'normal';
+		element.style.textAlign = 'center';
+		element.style.background = '#fff';
+		element.style.color = '#000';
+		element.style.padding = '1.5em';
+		element.style.width = '400px';
+		element.style.margin = '5em auto 0';
+
+		if ( contexts[ version ] ) {
+
+			message = message.replace( '$0', 'graphics card' );
+
+		} else {
+
+			message = message.replace( '$0', 'browser' );
+
+		}
+
+		message = message.replace( '$1', names[ version ] );
+
+		element.innerHTML = message;
+
+		return element;
+
+	}
+
+};
+
+export { WEBGL };

+ 1 - 1
examples/webgl2_materials_texture2darray.html

@@ -55,13 +55,13 @@
 		</div>
 
 		<script src="js/libs/jszip.min.js"></script>
-		<script src="js/WebGL.js"></script>
 
 		<script type="module">
 
 			import * as THREE from '../build/three.module.js';
 
 			import Stats from './jsm/libs/stats.module.js';
+			import { WEBGL } from './jsm/WebGL.js';
 
 			if ( WEBGL.isWebGL2Available() === false ) {
 

+ 1 - 2
examples/webgl2_materials_texture3d.html

@@ -13,8 +13,6 @@
 	</div>
 	<div id="inset"></div>
 
-	<script src="js/WebGL.js"></script>
-
 	<script type="module">
 		import * as THREE from '../build/three.module.js';
 
@@ -22,6 +20,7 @@
 		import { OrbitControls } from './jsm/controls/OrbitControls.js';
 		import { NRRDLoader } from './jsm/loaders/NRRDLoader.js';
 		import { VolumeRenderShader1 } from './jsm/shaders/VolumeShader.js';
+		import { WEBGL } from './jsm/WebGL.js';
 
 		if ( WEBGL.isWebGL2Available() === false ) {
 

+ 1 - 2
examples/webgl2_multisampled_renderbuffers.html

@@ -33,8 +33,6 @@
 		<div id="container">
 		</div>
 
-		<script src="js/WebGL.js"></script>
-
 		<script type="module">
 
 			import * as THREE from '../build/three.module.js';
@@ -43,6 +41,7 @@
 			import { RenderPass } from './jsm/postprocessing/RenderPass.js';
 			import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
 			import { CopyShader } from './jsm/shaders/CopyShader.js';
+			import { WEBGL } from './jsm/WebGL.js';
 
 			if ( WEBGL.isWebGL2Available() === false ) {
 

+ 1 - 2
examples/webgl2_sandbox.html

@@ -10,13 +10,12 @@
 	<body>
 		<div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl2 sandbox</div>
 
-		<script src="js/WebGL.js"></script>
-
 		<script type="module">
 
 			import * as THREE from '../build/three.module.js';
 
 			import { OrbitControls } from './jsm/controls/OrbitControls.js';
+			import { WEBGL } from './jsm/WebGL.js';
 
 			if ( WEBGL.isWebGL2Available() === false ) {
 

+ 2 - 0
utils/modularize.js

@@ -255,6 +255,8 @@ var files = [
 	{ path: 'vr/deprecated/GearVRController.js', dependencies: [], ignoreList: [] },
 	{ path: 'vr/PaintViveController.js', dependencies: [ { name: 'ViveController', path: 'vr/ViveController.js' } ], ignoreList: [] },
 	{ path: 'vr/ViveController.js', dependencies: [], ignoreList: [] },
+
+	{ path: 'WebGL.js', dependencies: [], ignoreList: [] },
 ];
 
 for ( var i = 0; i < files.length; i ++ ) {