Bonjour,

Comme l'indique le titre,
j'aimerais sélectionner, parmi des vignettes images, un objet 3D
et l'afficher sur mon module d'affichage 3D three.js.

Lien de test

Je joins mon code actuel, j'ai tenté plusieurs manières de faire
aussi bien avec des liens type ancre <a>
ou des boutons inclus dans un form.
je n'arrive pas obtenir ce que je souhaite.
à force de test dans tous les sens je n'y vois plus rien.

Si quelqu'un entrevoit une solution, cette dernière sera la bienvenue.


Pour indication, ligne 112 le chargeur de l'objet 3D
ici la variable toto extraite de la fonction SelectSTL par laquelle
je tente via le lien d'envoyer le nom du fichier.
Peut-être que je m'y prends mal...

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
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<head>
		<title>test Three.js app</title>
		<style>
			body { margin: 0; }
			canvas { width: 50%; height: 50% }
		</style>
	</head>
	<body>
	<script src="/JScript/3d/three.min.js"></script>
	<script src="/JScript/3d/STLLoader.js"></script>
	<script src="/JScript/3d/OrbitControls.js"></script>
	<script src="/JScript/3d/TrackballControls.js"></script>
	<script src="/JScript/3d/Detector.js"></script>
 
 
 
	<a href="#"  onclick="SelectSTL('test.stl')"><img src="Images/8to4.png"></a>
	<a href="#"  onclick="SelectSTL('/New_Patin_Y_Moi3D.stl')"><img src="Images/Cap.png"></a>
	<a href="#"  onclick="SelectSTL('test.stl')" id="tester"> <img src="Images/SHose.png"></a>
 
 
 
	<script>
	//***********************************************************************
 
 
	object = "/Models/titre.stl";
 
 
	if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
 
	function SelectSTL(LoadStl) {
		if ( typeof(LoadStl) != 'undefined') {
		object = LoadStl;
		}
		//object = "/Models/test.stl";
		//document.write("LoadStl : " + LoadStl + "<br>");
		return object;
		}
 
	toto = SelectSTL();  //<<<*************************** toto: variable de l'objet à afficher *****************************
//----------------------------------------------------------------
	// ********** Scene
	var scene = new THREE.Scene();
	//*********** Camera
	var camera = new THREE.PerspectiveCamera( 65, window.innerWidth/window.innerHeight, 1, 2000 );
	camera.position.z =5;
	camera.position.y = 1;
	camera.position.x = 6;
	//controls = new THREE.OrbitControls( camera );
	//controls.addEventListener( 'change', render );
	//SelectSTL();
	// ********* Fog
	scene.fog = new THREE.Fog( 0x929292, 10, 30 );
 
	// ******** debug
	document.write("<br>" + "SelectSTL() : " + SelectSTL() + "<br>");
 
 
 
	function CtrlMouse(){
	//************ control par la souris
		//controls = new THREE.TrackballControls( camera );
		controls = new THREE.OrbitControls( camera );
		controls.addEventListener( 'change', render );//*/
		controls.damping = 0.2; //slows down the movement
		//controls.maxPolarAngle = Math.PI/2; //Quaternion rotation
		//controls.minPolarAngle = 1;
		controls.minDistance = 3.5;
		controls.maxDistance = 15;
		}
 
	function addShadowedLight( x, y, z, color, intensity ) {
	// *************Shadowed light
		var directionalLight = new THREE.DirectionalLight( color, intensity );
		directionalLight.position.set( x, y, z );
		scene.add( directionalLight );
		directionalLight.castShadow = true;
		var d = 5;
		directionalLight.shadowCameraLeft = -d;
		directionalLight.shadowCameraRight = d;
		directionalLight.shadowCameraTop = d;
		directionalLight.shadowCameraBottom = -d;
		directionalLight.shadowCameraNear = -1;
		directionalLight.shadowCameraFar = 4;
		directionalLight.shadowMapWidth = 1024;
		directionalLight.shadowMapHeight = 1024;
		directionalLight.shadowBias = -0.005;
		} 
 
	// **************renderer
	var renderer = new THREE.WebGLRenderer({ antialias: true } );
	renderer.shadowMap.enabled = true;
	renderer.shadowMap.cullFace = THREE.CullFaceBack;
	renderer.setClearColor( scene.fog.color );//initialisation du brouillard
	renderer.setSize( window.innerWidth, window.innerHeight );
	document.body.appendChild( renderer.domElement );
	window.addEventListener( 'resize', onWindowResize, false );// rafraichissement après redimensionnement
 
	//**********Light
	var light = new THREE.HemisphereLight( 0xffffbb, 0x080820, 1 );
	scene.add( light );
	addShadowedLight( 0.5, 1, -1, 0xffff99, 1 );
 
	//************ ASCII STL file
	var loader = new THREE.STLLoader();
	//var object = "/Models/test.stl";
 
 
	loader.load( (toto), function ( geometry ) { // <<<*************************** toto: variable de l'objet à afficher *****************************
 
		var material = new THREE.MeshPhongMaterial( { color: 0xff5533, specular: 0x111111, shininess: 200 } );
		var mesh = new THREE.Mesh( geometry, material );
		mesh.position.set( 0, 0, 0);
		mesh.rotation.set( - Math.PI / 2, 0, 0 );
		mesh.scale.set( 0.1,0.1,0.1 );
		mesh.castShadow = true;
		mesh.receiveShadow = true;
 
		scene.add( mesh );
 
			} );
		//*****************
 
	// Grille
	var size = 10;
	var step = 1;
	var gridHelper = new THREE.GridHelper( size, step );
	gridHelper.receiveShadow = true;
	scene.add( gridHelper );
		//
 
 
	function onWindowResize() {
 
		camera.aspect = window.innerWidth / window.innerHeight;
		camera.updateProjectionMatrix();
		renderer.setSize( window.innerWidth, window.innerHeight );
		controls.handleResize();
 
		render();
 
		}
 
	function animate() {
 
		requestAnimationFrame( animate );
		//requestAnimationFrame( render );
		scene.rotation.y  -= .0005;
		//cube.rotation.x += 0.01;
		//cube.rotation.y += 0.01;
		//controls.update();
		render();		
		update();
 
		}
 
	function update(){
		toto = SelectSTL();
 
		controls.update();
		}
 
 
	function render() {
 
		renderer.render(scene, camera);
 
		};
 
 
	CtrlMouse();
	animate();
	//render();
		//*************
 
 
	</script>
 
 
	</body>
</html>
Merci de votre attention.