IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Déplacement d'Obj en WebGL


Sujet :

JavaScript

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2014
    Messages : 14
    Points : 11
    Points
    11
    Par défaut Déplacement d'Obj en WebGL
    Bonjour à tous, je souhaite pouvoir limiter le déplacement de l'OBJ seulement si on se trouve sur la zone d'affichage mais après plusieurs tentatives avec des mouseover mouseout toujours aucun résultat. Voici le code que j'utilise :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
     
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>WebGL 3D model viewer using three.js</title>
    </head>
    <body>
    <script type="text/javascript" src="build/three.js"></script>
     
    <script type="text/javascript" src="examples/js/controls/FirstPersonControls.js"></script>
     
    <script type="text/javascript" src="examples/js/libs/stats.min.js"></script>
     
    <script type="text/javascript" src="examples/js/loaders/OBJLoader.js"></script>
     
    <script type="text/javascript" src="examples/js/wip/TypedGeometry.js"></script>
     
    <script type="text/javascript">
     
     
    var container, stats;
    var camera, controls, scene, renderer;
    var mesh;
    var width, height;
    var woldWidth, worldHeight;
     
    var clock = new THREE.Clock;
     
     
        init();
        animate();
     
     
    function init() {
     
            width = 600;
            height = 600;
            worldWidth = 60;
            worldHeight = 60;
    	container = document.createElement( 'div' );
    	document.body.appendChild(container);
     
     
    	camera = new THREE.PerspectiveCamera( 70, width / height, 1, 10000 );
    	camera.position.y = 100;
            camera.position.x = -50;
     
    	controls = new THREE.FirstPersonControls( camera );
            controls.movementSpeed = 200;
    	controls.lookSpeed = 0.2;
    	controls.lookVertical = true;
            controls.autoForward = false;
            controls.activeLook = true;
     
    	scene = new THREE.Scene();
     
    	scene.add( new THREE.AmbientLight( 0x505050 ) );
     
    	var ambientLight = new THREE.AmbientLight( 0xcccccc );
    	scene.add( ambientLight );
     
    	var directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
    	directionalLight.position.set( 1, 1, 0.5 ).normalize();
    	scene.add( directionalLight );
     
    var geometry = new THREE.TypedGeometry( worldWidth * worldHeight * 2 ); // 2 triangles
    		// texture
     
    		var textureobj = new THREE.Texture();
     
    		var loader = new THREE.ImageLoader();
    		loader.load('examples/textures/bois.jpg', function ( image ) {
     
    		textureobj.image = image;
    		textureobj.needsUpdate = true;
     
    		} );
     
    		// model
     
    		var loader = new THREE.OBJLoader();
    		loader.load('01412.obj', function ( object ) {
     
    		object.traverse( function ( child ) {
     
    		if ( child instanceof THREE.Mesh ) {
     
    		child.material.map = textureobj;
     
    		}
     
    		} );
     
    		object.position.y = 100;
    		scene.add( object );
    		} );
     
     
                    var texture = THREE.ImageUtils.loadTexture( 'examples/textures/patterns/bright_squares256.png' );
    		texture.magFilter = THREE.NearestFilter;
    		texture.minFilter = THREE.LinearMipMapLinearFilter;
    		var mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: texture, ambient: 0xbbbbbb } ) );
    		scene.add( mesh  );
     
    		renderer = new THREE.WebGLRenderer( { antialias: true } );
    		renderer.setClearColor( 0xf0f0f0 );
    		renderer.setSize( width, height );
    		container.appendChild( renderer.domElement );
     
    }
     
    function animate() {
     
    	requestAnimationFrame( animate );
     
    	render();
     
    }
     
    function render() {
     
    	controls.update(clock.getDelta());
     
    	renderer.render( scene, camera );
    }
    </script>                  
     
    </body>
    </html>
    et voici mon FirstPersonControls.js que j'ai modifié pour avoir un déplacement plus approprié :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
     
    /**
     * @author mrdoob / http://mrdoob.com/
     * @author alteredq / http://alteredqualia.com/
     * @author paulirish / http://paulirish.com/
     */
     
    THREE.FirstPersonControls = function ( object, domElement ) {
     
    	this.object = object;
    	this.target = new THREE.Vector3( 0, 0, 0 );
     
    	this.domElement = ( domElement !== undefined ) ? domElement : document;
     
    	this.movementSpeed = 1.0;
    	this.lookSpeed = 0.005;
     
    	this.lookVertical = true;
    	this.autoForward = false;
    	// this.invertVertical = false;
     
    	this.activeLook = true;
     
    	this.heightSpeed = false;
    	this.heightCoef = 1.0;
    	this.heightMin = 0.0;
    	this.heightMax = 1.0;
     
    	this.constrainVertical = false;
    	this.verticalMin = 0;
    	this.verticalMax = Math.PI;
     
    	this.autoSpeedFactor = 0.0;
     
    	this.mouseX = 0;
    	this.mouseY = 0;
     
    	this.lat = 0;
    	this.lon = 0;
    	this.phi = 0;
    	this.theta = 0;
     
    	this.moveForward = false;
    	this.moveBackward = false;
    	this.moveLeft = false;
    	this.moveRight = false;
    	this.freeze = false;
     
    	this.mouseDragOn = false;
     
    	this.viewHalfX = 0;
    	this.viewHalfY = 0;
     
            if ( this.domElement !== document ) {
     
    		this.domElement.setAttribute( 'tabindex', -1 );
     
    	}
     
    	//
     
    	this.handleResize = function () {
     
    		if ( this.domElement === document ) {
     
    			this.viewHalfX = window.innerWidth / 2;
    			this.viewHalfY = window.innerHeight / 2;
     
    		} else {
     
    			this.viewHalfX = this.domElement.offsetWidth / 2;
    			this.viewHalfY = this.domElement.offsetHeight / 2;
     
    		}
     
    	};
     
    	this.onMouseDown = function ( event ) {
     
    		/**if ( this.domElement !== document ) {
     
    			this.domElement.focus();
     
    		}*/
     
    		event.preventDefault();
    		event.stopPropagation();
     
    		/**if ( this.activeLook ) {
                    
                    	switch ( event.button ) {
     
    				case 0: this.moveForward = true; break;
    				case 2: this.moveBackward = true; break;
     
    			}
     
    		}*/
                    if (event.button==0){
                        this.mouseDragOn = true;
                    }
     
    	};
     
    	this.onMouseUp = function ( event ) {
     
    		event.preventDefault();
    		event.stopPropagation();
     
    		/**if ( this.activeLook ) {
     
    			switch ( event.button ) {
     
    				case 0: this.moveForward = false; break;
    				case 2: this.moveBackward = false; break;
     
    			}
     
    		}*/
     
    		this.mouseDragOn = false;
     
    	};
     
    	this.onMouseMove = function ( event ) {
     
                if (this.mouseDragOn==true){
     
    		if ( this.domElement === document ) {
     
    			this.mouseX = event.pageX - this.viewHalfX;
    			this.mouseY = event.pageY - this.viewHalfY;
     
    		}
                }
    	};
     
    	this.onKeyDown = function ( event ) {
     
    		//event.preventDefault();
     
    		switch ( event.keyCode ) {
     
    			case 38: /*up*/
    			case 87: /*W*/ this.moveForward = true; break;
     
    			case 37: /*left*/
    			case 65: /*A*/ this.moveLeft = true; break;
     
    			case 40: /*down*/
    			case 83: /*S*/ this.moveBackward = true; break;
     
    			case 39: /*right*/
    			case 68: /*D*/ this.moveRight = true; break;
     
    			case 82: /*R*/ this.moveUp = true; break;
    			case 70: /*F*/ this.moveDown = true; break;
     
    			case 81: /*Q*/ this.freeze = !this.freeze; break;
     
    		}
     
    	};
     
    	this.onKeyUp = function ( event ) {
     
    		switch( event.keyCode ) {
     
    			case 38: /*up*/
    			case 87: /*W*/ this.moveForward = false; break;
     
    			case 37: /*left*/
    			case 65: /*A*/ this.moveLeft = false; break;
     
    			case 40: /*down*/
    			case 83: /*S*/ this.moveBackward = false; break;
     
    			case 39: /*right*/
    			case 68: /*D*/ this.moveRight = false; break;
     
    			case 82: /*R*/ this.moveUp = false; break;
    			case 70: /*F*/ this.moveDown = false; break;
     
    		}
     
    	};
     
    	this.update = function( delta ) {
     
    		if ( this.freeze ) {
     
    			return;
     
    		}
     
    		if ( this.heightSpeed ) {
     
    			var y = THREE.Math.clamp( this.object.position.y, this.heightMin, this.heightMax );
    			var heightDelta = y - this.heightMin;
     
    			this.autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
     
    		} else {
     
    			this.autoSpeedFactor = 0.0;
     
    		}
     
    		var actualMoveSpeed = delta * this.movementSpeed;
     
    		if ( this.moveForward || ( this.autoForward && !this.moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this.autoSpeedFactor ) );
    		if ( this.moveBackward ) this.object.translateZ( actualMoveSpeed );
     
    		if ( this.moveLeft ) this.object.translateX( - actualMoveSpeed );
    		if ( this.moveRight ) this.object.translateX( actualMoveSpeed );
     
    		if ( this.moveUp ) this.object.translateY( actualMoveSpeed );
    		if ( this.moveDown ) this.object.translateY( - actualMoveSpeed );
     
    		var actualLookSpeed = delta * this.lookSpeed;
     
    		if ( !this.activeLook ) {
     
    			actualLookSpeed = 0;
     
    		}
     
    		var verticalLookRatio = 1;
     
    		if ( this.constrainVertical ) {
     
    			verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
     
    		}
                if (this.mouseDragOn==true){
    		this.lon += this.mouseX * actualLookSpeed;
    		if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
                }
    		this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
    		this.phi = THREE.Math.degToRad( 90 - this.lat );
     
    		this.theta = THREE.Math.degToRad( this.lon );
     
    		if ( this.constrainVertical ) {
     
    			this.phi = THREE.Math.mapLinear( this.phi, 0, Math.PI, this.verticalMin, this.verticalMax );
     
    		}
     
    		var targetPosition = this.target,
    			position = this.object.position;
     
    		targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
    		targetPosition.y = position.y + 100 * Math.cos( this.phi );
    		targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
     
    		this.object.lookAt( targetPosition );
     
    	};
     
     
    	this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
     
    	this.domElement.addEventListener( 'mousemove', bind( this, this.onMouseMove ), false );
    	this.domElement.addEventListener( 'mousedown', bind( this, this.onMouseDown ), false );
    	this.domElement.addEventListener( 'mouseup', bind( this, this.onMouseUp ), false );
     
     
    	window.addEventListener( 'keydown', bind( this, this.onKeyDown ), false );
    	window.addEventListener( 'keyup', bind( this, this.onKeyUp ), false );
     
    	function bind( scope, fn ) {
     
    		return function () {
     
    			fn.apply( scope, arguments );
     
    		};
     
    	};
     
    	this.handleResize();
     
    };
    Si vous n'avez pas d'OBJ vous pouvez en trouver sur http://www.oyonale.com/modeles.php?lang=fr&format=OBJ

    En espérant avoir été clair

  2. #2
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2014
    Messages : 14
    Points : 11
    Points
    11
    Par défaut
    C'est bon j'ai réussi à détecter quand je suis dans la zone et tout marche comme je le souhaite du moins presque ... parce que lorsque je colle la zone d'affichage en haut à droite l'objet ne suis plus vraiment la souris, il tourne en rond je vois pas trop pourquoi . Voici les nouveaux codes que j'utilise :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
     
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>WebGL 3D model viewer using three.js</title>
    </head>
    <body>
    <script type="text/javascript" src="build/three.js"></script>
     
    <script type="text/javascript" src="examples/js/controls/FirstPersonControls.js"></script>
     
    <script type="text/javascript" src="examples/js/libs/stats.min.js"></script>
     
    <script type="text/javascript" src="examples/js/loaders/OBJLoader.js"></script>
     
    <script type="text/javascript" src="examples/js/wip/TypedGeometry.js"></script>
     
    <script type="text/javascript">
     
     
    var container, stats;
    var camera, controls, scene, renderer;
    var mesh;
    var width, height;
    var woldWidth, worldHeight;
     
    var clock = new THREE.Clock;
     
     
        init();
        animate();
     
    function init() {
     
            width = 600;
            height = 600;
            worldWidth = 60;
            worldHeight = 60;
    	container = document.createElement( 'div' );
            container.id = 'container';
            container.style.height = height+1+"px";
            container.style.width = width+1+"px";
    	document.body.appendChild(container);
     
     
    	camera = new THREE.PerspectiveCamera( 70, width / height, 1, 10000 );
    	camera.position.y = 100;
            camera.position.x = -50;
     
    	controls = new THREE.FirstPersonControls( camera, container );
            controls.movementSpeed = 200;
    	controls.lookSpeed = 0.2;
    	controls.lookVertical = true;
            controls.autoForward = false;
            controls.activeLook = true;
     
    	scene = new THREE.Scene();
     
    	scene.add( new THREE.AmbientLight( 0x505050 ) );
     
    	var ambientLight = new THREE.AmbientLight( 0xcccccc );
    	scene.add( ambientLight );
     
    	var directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
    	directionalLight.position.set( 1, 1, 0.5 ).normalize();
    	scene.add( directionalLight );
     
    var geometry = new THREE.TypedGeometry( worldWidth * worldHeight * 2 ); // 2 triangles
    		// texture
     
    		var textureobj = new THREE.Texture();
     
    		var loader = new THREE.ImageLoader();
    		loader.load('examples/textures/bois.jpg', function ( image ) {
     
    		textureobj.image = image;
    		textureobj.needsUpdate = true;
     
    		} );
     
    		// model
     
    		var loader = new THREE.OBJLoader();
    		loader.load('01412.obj', function ( object ) {
     
    		object.traverse( function ( child ) {
     
    		if ( child instanceof THREE.Mesh ) {
     
    		child.material.map = textureobj;
     
    		}
     
    		} );
     
    		object.position.y = 100;
    		scene.add( object );
    		} );
     
     
                    var texture = THREE.ImageUtils.loadTexture( 'examples/textures/patterns/bright_squares256.png' );
    		texture.magFilter = THREE.NearestFilter;
    		texture.minFilter = THREE.LinearMipMapLinearFilter;
    		var mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: texture, ambient: 0xbbbbbb } ) );
    		scene.add( mesh  );
     
    		renderer = new THREE.WebGLRenderer( { antialias: true } );
    		renderer.setClearColor( 0xf0f0f0 );
    		renderer.setSize( width, height );
     
    		container.appendChild( renderer.domElement );
     
     
    }
     
    function animate() {
     
    	requestAnimationFrame( animate );
     
    	render();
     
    }
     
    function render() {
     
    	controls.update(clock.getDelta());
     
    	renderer.render( scene, camera );
    }
    </script>                  
     
    </body>
    </html>
    Pas trop de changement dans cette partie.
    Dans celle-ci un peu plus.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
     
    /**
     * @author mrdoob / http://mrdoob.com/
     * @author alteredq / http://alteredqualia.com/
     * @author paulirish / http://paulirish.com/
     */
     
    THREE.FirstPersonControls = function ( object, domElement ) {
     
    	this.object = object;
    	this.target = new THREE.Vector3( 0, 0, 0 );
     
    	this.domElement = ( domElement !== undefined ) ? domElement : document;
     
    	this.movementSpeed = 1.0;
    	this.lookSpeed = 0.005;
     
    	this.lookVertical = true;
    	this.autoForward = false;
    	// this.invertVertical = false;
     
    	this.activeLook = true;
     
    	this.heightSpeed = false;
    	this.heightCoef = 1.0;
    	this.heightMin = 0.0;
    	this.heightMax = 1.0;
     
    	this.constrainVertical = false;
    	this.verticalMin = 0;
    	this.verticalMax = Math.PI;
     
    	this.autoSpeedFactor = 0.0;
     
    	this.mouseX = 0;
    	this.mouseY = 0;
     
    	this.lat = 0;
    	this.lon = 0;
    	this.phi = 0;
    	this.theta = 0;
     
    	this.moveForward = false;
    	this.moveBackward = false;
    	this.moveLeft = false;
    	this.moveRight = false;
    	this.freeze = false;
     
    	this.mouseDragOn = false;
            this.mouseIn = false;
     
    	this.viewHalfX = 0;
    	this.viewHalfY = 0;
     
            if ( this.domElement !== document ) {
     
    		this.domElement.setAttribute( 'tabindex', -1 );
     
    	}
     
    	//
     
    	this.handleResize = function () {
     
    		if ( this.domElement === document ) {
     
    			this.viewHalfX = window.innerWidth / 2;
    			this.viewHalfY = window.innerHeight / 2;
     
    		} else {
     
    			this.viewHalfX = this.domElement.offsetWidth / 2;
    			this.viewHalfY = this.domElement.offsetHeight / 2;
                            this.domElement.setAttribute( 'tabindex', -1 );
    		}
     
    	};
     
    	this.onMouseDown = function ( event ) {
     
    		/**if ( this.domElement !== document ) {
     
    			this.domElement.focus();
     
    		}*/
     
    		event.preventDefault();
    		event.stopPropagation();
     
    		/**if ( this.activeLook ) {
                    
                    	switch ( event.button ) {
     
    				case 0: this.moveForward = true; break;
    				case 2: this.moveBackward = true; break;
     
    			}
     
    		}*/
                    if (event.button==0){
                        this.mouseDragOn = true;
                    }
     
    	};
     
    	this.onMouseUp = function ( event ) {
     
    		event.preventDefault();
    		event.stopPropagation();
     
    		/**if ( this.activeLook ) {
     
    			switch ( event.button ) {
     
    				case 0: this.moveForward = false; break;
    				case 2: this.moveBackward = false; break;
     
    			}
     
    		}*/
     
    		this.mouseDragOn = false;
     
    	};
     
    	this.onMouseMove = function ( event ) {
     
                if (this.mouseDragOn==true && this.mouseIn==true){
     
    		if ( this.domElement === document ) {
     
    			this.mouseX = event.pageX - this.viewHalfX;
    			this.mouseY = event.pageY - this.viewHalfY;
    		} else {
     
    			this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
    			this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
    		}
                }
    	};
     
            this.onMouseOver = function (event ) {
                this.mouseIn = true;
            }
     
            this.onMouseOut = function (event ) {
                this.mouseIn = false;
            }
     
    	this.onKeyDown = function ( event ) {
     
    		//event.preventDefault();
                if (this.mouseIn==true){
     
    		switch ( event.keyCode ) {
     
    			case 38: /*up*/ this.moveForward = true; event.preventDefault(); break;
     
    			case 37: /*left*/ this.moveLeft = true; event.preventDefault(); break;
     
    			case 40: /*down*/ this.moveBackward = true; event.preventDefault(); break;
     
    			case 39: /*right*/ this.moveRight = true; event.preventDefault(); break;
     
    			case 33: /*page up*/ this.moveUp = true; event.preventDefault(); break;
    			case 34: /*page down*/ this.moveDown = true; event.preventDefault(); break;
     
    			case 19: /*pause*/ this.freeze = !this.freeze; event.preventDefault(); break;
     
    		}
                }
    	};
     
    	this.onKeyUp = function ( event ) {
     
    		switch( event.keyCode ) {
     
    			case 38: /*up*/ this.moveForward = false; break;
     
    			case 37: /*left*/ this.moveLeft = false; break;
     
    			case 40: /*down*/ this.moveBackward = false; break;
     
    			case 39: /*right*/ this.moveRight = false; break;
     
    			case 33: /*page up*/ this.moveUp = false; break;
    			case 34: /*page down*/ this.moveDown = false; break;
     
    		}
     
    	};
     
    	this.update = function( delta ) {
     
    		if ( this.freeze ) {
     
    			return;
     
    		}
     
    		if ( this.heightSpeed ) {
     
    			var y = THREE.Math.clamp( this.object.position.y, this.heightMin, this.heightMax );
    			var heightDelta = y - this.heightMin;
     
    			this.autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
     
    		} else {
     
    			this.autoSpeedFactor = 0.0;
     
    		}
     
    		var actualMoveSpeed = delta * this.movementSpeed;
     
    		if ( this.moveForward || ( this.autoForward && !this.moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this.autoSpeedFactor ) );
    		if ( this.moveBackward ) this.object.translateZ( actualMoveSpeed );
     
    		if ( this.moveLeft ) this.object.translateX( - actualMoveSpeed );
    		if ( this.moveRight ) this.object.translateX( actualMoveSpeed );
     
    		if ( this.moveUp ) this.object.translateY( actualMoveSpeed );
    		if ( this.moveDown ) this.object.translateY( - actualMoveSpeed );
     
    		var actualLookSpeed = delta * this.lookSpeed;
     
    		if ( !this.activeLook ) {
     
    			actualLookSpeed = 0;
     
    		}
     
    		var verticalLookRatio = 1;
     
    		if ( this.constrainVertical ) {
     
    			verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
     
    		}
                if (this.mouseDragOn==true && this.mouseIn==true){
    		this.lon += this.mouseX * actualLookSpeed;
    		if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
                } 
    		this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
    		this.phi = THREE.Math.degToRad( 90 - this.lat );
     
    		this.theta = THREE.Math.degToRad( this.lon );
     
    		if ( this.constrainVertical ) {
     
    			this.phi = THREE.Math.mapLinear( this.phi, 0, Math.PI, this.verticalMin, this.verticalMax );
     
    		}
     
    		var targetPosition = this.target,
    			position = this.object.position;
     
    		targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
    		targetPosition.y = position.y + 100 * Math.cos( this.phi );
    		targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
     
    		this.object.lookAt( targetPosition );
     
    	};
     
     
    	//this.domElement.addEventListener( 'contextmenu', function ( event ){ event.preventDefault(); }, false );
            this.domElement.addEventListener( 'mouseover', bind( this, this.onMouseOver ), false );
            this.domElement.addEventListener( 'mouseout', bind( this, this.onMouseOut ), false );
    	this.domElement.addEventListener( 'mousemove', bind( this, this.onMouseMove ), false );
    	this.domElement.addEventListener( 'mousedown', bind( this, this.onMouseDown ), false );
    	this.domElement.addEventListener( 'mouseup', bind( this, this.onMouseUp ), false );
     
     
    	window.addEventListener( 'keydown', bind( this, this.onKeyDown ), false );
    	window.addEventListener( 'keyup', bind( this, this.onKeyUp ), false );
     
    	function bind( scope, fn ) {
     
    		return function () {
     
    			fn.apply( scope, arguments );
     
    		};
     
    	};
     
    	this.handleResize();
     
    };

  3. #3
    Membre confirmé

    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    311
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2010
    Messages : 311
    Points : 545
    Points
    545
    Par défaut
    Salut, je pense que tu devrais développer ton problème ainsi que ce que tu souhaites obtenir, car ce n’est pas clair et ton code l’est encore moins.
    Pour nous aider à comprendre tu devrais mettre en ligne une demo (sur JSFiddle par exemple)

    Le code que tu poste me semble bien compliqué, pourquoi utilise tu des coordonnées sphériques ? si je me réfère au nom du constructeur FirstPersonControls, je peux qu’en déduire que tu souhaites déplacer la camera comme dans un jeu de FPS, mais quel rapport avec le déplacement d’un model OBJ ?
    ShaderElement : Bénéficier de l’accélération graphique simplement par une nouvelle balise HTML <shader>
    ODE.js : portage JavaScript du célèbre moteur physique 3D Open Dynamics Engine

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Webgl][Three.js] Charger des fichiers .obj
    Par piero5673 dans le forum Général JavaScript
    Réponses: 11
    Dernier message: 18/06/2013, 14h41
  2. Charger un .obj sous WebGL
    Par sylvain230 dans le forum Développement 2D, 3D et Jeux
    Réponses: 0
    Dernier message: 14/04/2011, 09h07
  3. Comment charger un .obj en WebGL ?
    Par sylvain230 dans le forum Développement 2D, 3D et Jeux
    Réponses: 2
    Dernier message: 04/04/2011, 17h43
  4. Déplacement "automatique" du curseur
    Par Amenofis dans le forum Composants VCL
    Réponses: 2
    Dernier message: 08/01/2003, 18h57
  5. Limiter le déplacement de la souris
    Par el_bouleto dans le forum C++Builder
    Réponses: 4
    Dernier message: 08/11/2002, 23h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo