瀏覽代碼

Refactored loader and added example

Tentone 8 年之前
父節點
當前提交
129a85837f

+ 1 - 0
examples/files.js

@@ -75,6 +75,7 @@ var files = {
 		"webgl_lines_sphere",
 		"webgl_lines_splines",
 		"webgl_loader_3mf",
+		"webgl_loader_3ds",
 		"webgl_loader_amf",
 		"webgl_loader_assimp",
 		"webgl_loader_assimp2json",

+ 19 - 0
examples/js/libs/syncinput.min.js

@@ -0,0 +1,19 @@
+function SyncInput(){}VERSION="SyncInput V1.1";TIMESTAMP="201702041904";function Vector2(a,b){this.x=void 0!==a?a:0;this.y=void 0!==b?b:0}Vector2.prototype.set=function(a,b){this.x=a;this.y=b};function Key(){this.justReleased=this.justPressed=this.pressed=!1}Key.DOWN=-1;Key.UP=1;Key.RESET=0;Key.prototype.update=function(a){this.justReleased=this.justPressed=!1;a===Key.DOWN?(this.pressed||(this.justPressed=!0),this.pressed=!0):a===Key.UP?(this.pressed&&(this.justReleased=!0),this.pressed=!1):a===Key.RESET&&(this.justPressed=this.justReleased=!1)};Key.prototype.set=function(a,b,c){this.pressed=b;this.justPressed=a;this.justReleased=c};
+Key.prototype.reset=function(){this.justReleased=this.justPressed=this.pressed=!1};function Mouse(){this._keys=[];this._position=new Vector2(0,0);this._positionUpdated=!1;this._delta=new Vector2(0,0);this._wheel=0;this._doubleClicked=this._wheelUpdated=!1;this.keys=[];this.position=new Vector2(0,0);this.delta=new Vector2(0,0);this.wheel=0;this.doubleClicked=!1;this.canvas=null;this.events=[];for(var a=0;3>a;a++)this._keys.push(new Key),this.keys.push(new Key);var b=this;void 0!==window.onmousewheel?this.events.push([window,"mousewheel",function(a){b._wheel=a.deltaY;b._wheelUpdated=
+!0}]):void 0!==window.addEventListener?this.events.push([window,"DOMMouseScroll",function(a){b._wheel=30*a.detail;b._wheelUpdated=!0}]):this.events.push([window,"wheel",function(a){b._wheel=a.deltaY;b._wheelUpdated=!0}]);if("ontouchstart"in window||0<navigator.msMaxTouchPoints){var c=new Vector2(0,0);this.events.push([window,"touchstart",function(a){a=a.touches[0];c.set(a.clientX,a.clientY);b.updateKey(Mouse.LEFT,Key.DOWN)}]);this.events.push([window,"touchend",function(a){b.updateKey(Mouse.LEFT,
+Key.UP)}]);this.events.push([window,"touchmove",function(a){a=a.touches[0];if(null!==b.canvas){var e=b.canvas.getBoundingClientRect();b.updatePosition(a.clientX-e.left,a.clientY-e.top,a.clientX-c.x,a.clientY-c.y)}else b.updatePosition(a.clientX,a.clientY,a.clientX-c.x,a.clientY-c.y);c.set(a.clientX,a.clientY)}])}else this.events.push([window,"mousemove",function(a){if(null!==b.canvas){var c=b.canvas.getBoundingClientRect();b.updatePosition(a.clientX-c.left,a.clientY-c.top,a.movementX,a.movementY)}else b.updatePosition(a.clientX,
+a.clientY,a.movementX,a.movementY)}]),this.events.push([window,"mousedown",function(a){b.updateKey(a.which-1,Key.DOWN)}]),this.events.push([window,"mouseup",function(a){b.updateKey(a.which-1,Key.UP)}]);this.events.push([window,"dblclick",function(a){b._doubleClicked=!0}]);for(a=0;a<this.events.length;a++){var d=this.events[a];d[0].addEventListener(d[1],d[2])}}Mouse.LEFT=0;Mouse.MIDDLE=1;Mouse.RIGHT=2;
+Mouse.prototype.setCanvas=function(a){this.canvas=a;a.mouseInside=!1;a.addEventListener("mouseenter",function(){this.mouseInside=!0});a.addEventListener("mouseleave",function(){this.mouseInside=!1})};Mouse.prototype.insideCanvas=function(){return null===this.canvas?!1:this.canvas.mouseInside};
+Mouse.prototype.setLock=function(a){null!==this.canvas&&(a?this.canvas.requestPointerLock?this.canvas.requestPointerLock():this.canvas.mozRequestPointerLock?this.canvas.mozRequestPointerLock():this.canvas.webkitRequestPointerLock&&this.canvas.webkitRequestPointerLock():document.exitPointerLock?document.exitPointerLock():document.mozExitPointerLock?document.mozExitPointerLock():document.webkitExitPointerLock&&document.webkitExitPointerLock())};Mouse.prototype.buttonPressed=function(a){return this.keys[a].pressed};
+Mouse.prototype.buttonDoubleClicked=function(){return this.doubleClicked};Mouse.prototype.buttonJustPressed=function(a){return this.keys[a].justPressed};Mouse.prototype.buttonJustReleased=function(a){return this.keys[a].justReleased};Mouse.prototype.updatePosition=function(a,b,c,d){this._position.set(a,b);this._delta.x+=c;this._delta.y+=d;this._positionUpdated=!0};Mouse.prototype.updateKey=function(a,b){-1<a&&this._keys[a].update(b)};
+Mouse.prototype.update=function(){for(var a=0;a<this._keys.length;a++)this._keys[a].justPressed&&this.keys[a].justPressed&&(this._keys[a].justPressed=!1),this._keys[a].justReleased&&this.keys[a].justReleased&&(this._keys[a].justReleased=!1),this.keys[a].set(this._keys[a].justPressed,this._keys[a].pressed,this._keys[a].justReleased);this._wheelUpdated?(this.wheel=this._wheel,this._wheelUpdated=!1):this.wheel=0;this._doubleClicked?(this.doubleClicked=!0,this._doubleClicked=!1):this.doubleClicked=!1;
+this._positionUpdated?(this.delta.x=this._delta.x,this.delta.y=this._delta.y,this._delta.set(0,0),this.position.x=this._position.x,this.position.y=this._position.y,this._positionUpdated=!1):(this.delta.x=0,this.delta.y=0)};Mouse.prototype.dispose=function(){for(var a=0;a<this.events.length;a++){var b=this.events[a];b[0].removeEventListener(b[1],b[2])}};function Keyboard(){this.keys=[];this.actions=[];for(var a=0;256>a;a++)this.keys.push(new Key);this.events=[];var b=this.actions;this.events.push([window,"keydown",function(a){b.push(a.keyCode);b.push(Key.DOWN)}]);this.events.push([window,"keyup",function(a){b.push(a.keyCode);b.push(Key.UP)}]);for(a=0;a<this.events.length;a++){var c=this.events[a];c[0].addEventListener(c[1],c[2])}}
+Keyboard.prototype.update=function(){for(var a=0;this.actions.length>a;){var b=this.actions.shift(),c=this.actions.shift();this.keys[b].update(c);if(this.keys[b].justReleased||this.keys[b].justPressed)this.actions.push(b),this.actions.push(Key.RESET),a+=2}};Keyboard.prototype.reset=function(){this.actions=[];for(var a=0;a<this.keys.length;a++)this.keys[a].reset()};Keyboard.prototype.keyPressed=function(a){return this.keys[a].pressed};Keyboard.prototype.keyJustPressed=function(a){return this.keys[a].justPressed};
+Keyboard.prototype.keyJustReleased=function(a){return this.keys[a].justReleased};Keyboard.prototype.dispose=function(){for(var a=0;a<this.events.length;a++){var b=this.events[a];b[0].removeEventListener(b[1],b[2])}};Keyboard.TAB=9;Keyboard.ENTER=13;Keyboard.SHIFT=16;Keyboard.CTRL=17;Keyboard.ALT=18;Keyboard.CAPS_LOCK=20;Keyboard.ESC=27;Keyboard.SPACEBAR=32;Keyboard.PAGE_UP=33;Keyboard.PAGE_DOWN=34;Keyboard.END=35;Keyboard.HOME=36;Keyboard.INSERT=45;Keyboard.DEL=46;Keyboard.LEFT=37;
+Keyboard.RIGHT=39;Keyboard.UP=38;Keyboard.DOWN=40;Keyboard.NUM0=48;Keyboard.NUM1=49;Keyboard.NUM2=50;Keyboard.NUM3=51;Keyboard.NUM4=52;Keyboard.NUM5=53;Keyboard.NUM6=54;Keyboard.NUM7=55;Keyboard.NUM8=56;Keyboard.NUM9=57;Keyboard.A=65;Keyboard.B=66;Keyboard.C=67;Keyboard.D=68;Keyboard.E=69;Keyboard.F=70;Keyboard.G=71;Keyboard.H=72;Keyboard.I=73;Keyboard.J=74;Keyboard.K=75;Keyboard.L=76;Keyboard.M=77;Keyboard.N=78;Keyboard.O=79;Keyboard.P=80;Keyboard.Q=81;Keyboard.R=82;Keyboard.S=83;Keyboard.T=84;
+Keyboard.U=85;Keyboard.V=86;Keyboard.W=87;Keyboard.X=88;Keyboard.Y=89;Keyboard.Z=90;Keyboard.F1=112;Keyboard.F2=113;Keyboard.F3=114;Keyboard.F4=115;Keyboard.F5=116;Keyboard.F6=117;Keyboard.F7=118;Keyboard.F8=119;Keyboard.F9=120;Keyboard.F10=121;Keyboard.F11=122;Keyboard.F12=123;function Gamepad(){this.setGamepad(navigator.getGamepads()[Gamepad.gamepads.length]);Gamepad.gamepads.push(this)}Gamepad.prototype.setGamepad=function(a){if(void 0!==a){this.index=a.index;this.gamepad=a;this.buttons=[];for(var b=0;b<a.buttons.length;b++)this.buttons.push(new Key);this.setProductVendor(a);this.connected=!0}else console.warn("SyncInput: No Gamepad found"),this.disconnect()};
+Gamepad.prototype.disconnect=function(){this.product=this.vendor=-1;this.connected=!1;this.gamepad=null;this.buttons=[]};Gamepad.prototype.setProductVendor=function(a){try{var b=a.id.split(":");this.vendor=b[1].split(" ")[1];this.product=b[2].replace(" ","").replace(")","");return}catch(c){}try{b=a.id.split("-"),this.vendor=b[0],this.product=b[1]}catch(c){}};
+Gamepad.prototype.update=function(){this.gamepad=navigator.getGamepads()[this.index];if(void 0!==this.gamepad)for(var a=0;a<this.buttons.length;a++)this.buttons[a].update(this.gamepad.buttons[a].pressed?Key.DOWN:Key.UP)};Gamepad.prototype.getAnalogueButton=function(a){return a>this.buttons.length||0>a?0:this.gamepad.buttons[a].value};Gamepad.prototype.getAxis=function(a){return a>this.gamepad.axes.length||0>a?0:this.gamepad.axes[button].value};
+Gamepad.prototype.buttonPressed=function(a){return a>this.buttons.length?!1:this.buttons[a].pressed};Gamepad.prototype.buttonJustPressed=function(a){return a>this.buttons.length||0>a?!1:this.buttons[a].justPressed};Gamepad.prototype.buttonJustReleased=function(a){return a>this.buttons.length||0>a?!1:this.buttons[a].justReleased};Gamepad.prototype.dispose=function(){var a=Gamepad.gamepads.indexOf(this);-1!==a&&Gamepad.gamepads.splice(a,1)};Gamepad.gamepads=[];Gamepad.getGamepads=function(){return navigator.getGamepads()};
+Gamepad.startListener=function(){window.addEventListener("gamepadconnected",function(a){a=a.gamepad;for(var b=0;b<Gamepad.gamepads.length;b++)Gamepad.gamepads[b].index===a.index&&Gamepad.gamepads[b].setGamepad(a)});window.addEventListener("gamepaddisconnected",function(a){a=a.gamepad;console.warn("SyncInput: Gamepad "+a.id+" disconnected");for(var b=0;b<Gamepad.gamepads.length;b++)Gamepad.gamepads[b].index===a.index&&Gamepad.gamepads[b].disconnect()})};Gamepad.startListener();Gamepad.LEFT=14;
+Gamepad.RIGHT=15;Gamepad.DOWN=13;Gamepad.UP=12;Gamepad.SELECT=8;Gamepad.START=9;Gamepad.HOME=16;Gamepad.LEFT_TRIGGER_A=4;Gamepad.LEFT_TRIGGER_B=6;Gamepad.RIGHT_TRIGGER_A=5;Gamepad.RIGHT_TRIGGER_B=7;Gamepad.A=0;Gamepad.B=1;Gamepad.C=2;Gamepad.D=3;Gamepad.LEFT_ANALOGUE_HOR=0;Gamepad.LEFT_ANALOGUE_VERT=1;Gamepad.RIGHT_ANALOGUE_HOR=2;Gamepad.RIGHT_ANALOGUE_VERT=3;

文件差異過大導致無法顯示
+ 584 - 504
examples/js/loaders/TDSLoader.js


二進制
examples/models/3ds/portalgun/portalgun.3ds


二進制
examples/models/3ds/portalgun/textures/color.jpg


二進制
examples/models/3ds/portalgun/textures/normal.jpg


+ 147 - 0
examples/webgl_loader_3ds.html

@@ -0,0 +1,147 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgl - loaders - OBJ loader</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<style>
+			body {
+				font-family: Monospace;
+				background-color: #000;
+				color: #fff;
+				margin: 0px;
+				overflow: hidden;
+			}
+			#info {
+				color: #fff;
+				position: absolute;
+				top: 10px;
+				width: 100%;
+				text-align: center;
+				z-index: 100;
+				display:block;
+			}
+			#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
+		</style>
+	</head>
+
+	<body>
+		<div id="info">
+			<a href="http://threejs.org" target="_blank">three.js</a> - TDSLoader 3DS loader test
+			<p>Press the mouse right button to rotate the model and use the scroll wheel to zoom.</p>
+		</div>
+
+		<script src="../build/three.js"></script>
+		<script src="js/loaders/TDSLoader.js"></script>
+		<script src="js/libs/syncinput.min.js"></script>
+
+		<script>
+
+			var container;
+			var camera, scene, renderer;
+			var mouse;
+
+			initialize();
+
+			function initialize()
+			{
+				container = document.createElement( 'div' );
+				document.body.appendChild( container );
+
+				// Mouse
+				mouse = new Mouse();
+
+				// Camera
+				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 2000 );
+				camera.position.z = 2;
+
+				// Setup scene
+				scene = new THREE.Scene();
+
+				var ambient = new THREE.AmbientLight( 0x101030 );
+				scene.add( ambient );
+
+				var directionalLight = new THREE.DirectionalLight( 0xffeedd );
+				directionalLight.position.set( 0, 0, 2 );
+				scene.add( directionalLight );
+
+				// Texture
+				var texture = new THREE.Texture();
+
+				var loader = new THREE.ImageLoader();
+				loader.load( 'models/3ds/portalgun/textures/color.jpg', function ( image ) {
+
+					texture.image = image;
+					texture.needsUpdate = true;
+
+				} );
+
+				// Normal texture
+				var normal = new THREE.Texture();
+
+				loader.load( 'models/3ds/portalgun/textures/normal.jpg', function ( image ) {
+
+					normal.image = image;
+					normal.needsUpdate = true;
+
+				} );
+
+				// 3DS Model
+				var loader = new THREE.TDSLoader( );
+				loader.load( 'models/3ds/portalgun/portalgun.3ds', function ( object ) {
+
+					object.traverse( function ( child ) {
+
+						if ( child instanceof THREE.Mesh ) {
+
+							child.material.map = texture;
+							child.material.normalMap = normal;
+						}
+
+					} );
+
+					scene.add( object );
+
+				});
+
+				// Renderer
+				renderer = new THREE.WebGLRenderer();
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				container.appendChild( renderer.domElement );
+
+				window.addEventListener( 'resize', resize, false );
+
+				update();
+			}
+
+			function update() {
+
+				requestAnimationFrame( update );
+
+				mouse.update();
+
+				if ( mouse.buttonPressed( Mouse.LEFT ) ) {
+
+					scene.rotation.y += mouse.delta.x * 0.01;
+
+				}
+				
+				camera.position.z += mouse.wheel * 0.001;
+
+				renderer.render( scene, camera );
+
+			}
+
+			function resize() {
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+		</script>
+
+	</body>
+</html>

部分文件因文件數量過多而無法顯示