3131import com .ss .editor .annotation .FXThread ;
3232import com .ss .editor .annotation .FromAnyThread ;
3333import com .ss .editor .annotation .JMEThread ;
34+ import com .ss .editor .asset .locator .FolderAssetLocator ;
35+ import com .ss .editor .config .EditorConfig ;
3436import com .ss .editor .executor .impl .JMEThreadExecutor ;
3537import com .ss .editor .model .tool .TangentGenerator ;
3638import com .ss .editor .ui .scene .EditorFXScene ;
@@ -77,6 +79,9 @@ public class JMEFilePreviewManager extends AbstractControl {
7779 @ NotNull
7880 private static final Array <String > AUDIO_FORMATS = ArrayFactory .newArray (String .class );
7981
82+ @ NotNull
83+ private static final EditorConfig EDITOR_CONFIG = EditorConfig .getInstance ();
84+
8085 static {
8186 JME_FORMATS .add (FileExtensions .JME_MATERIAL );
8287 JME_FORMATS .add (FileExtensions .JME_OBJECT );
@@ -287,7 +292,7 @@ public void show(@NotNull final Path file, final int fitWidth, final int fitHeig
287292 final Path assetFile = notNull (getAssetFile (file ), "File can't be null." );
288293 final String path = toAssetPath (assetFile );
289294
290- showPreview (path , getExtension (assetFile ));
295+ showPreview (path , getExtension (assetFile ), false );
291296 }
292297
293298 /**
@@ -301,21 +306,22 @@ public void show(@NotNull final Path file, final int fitWidth, final int fitHeig
301306 public void showExternal (@ NotNull final Path file , final int fitWidth , final int fitHeight ) {
302307 imageView .setFitHeight (fitHeight );
303308 imageView .setFitWidth (fitWidth );
304- showPreview (file .toString (), getExtension (file ));
309+ showPreview (file .toString (), getExtension (file ), true );
305310 }
306311
307312 /**
308313 * Show a preview of the file by the asset path.
309314 *
310315 * @param path the asset path.
311316 * @param extension the extension.
317+ * @param external true if the path is external path.
312318 */
313319 @ FromAnyThread
314- private void showPreview (@ NotNull final String path , @ NotNull final String extension ) {
320+ private void showPreview (@ NotNull final String path , @ NotNull final String extension , final boolean external ) {
315321 if (FileExtensions .JME_MATERIAL .equals (extension )) {
316322 EDITOR_THREAD_EXECUTOR .addToExecute (() -> showMaterial (path ));
317323 } else if (isModelFile (path )) {
318- EDITOR_THREAD_EXECUTOR .addToExecute (() -> showObject (path ));
324+ EDITOR_THREAD_EXECUTOR .addToExecute (() -> showObject (path , external ));
319325 } else {
320326 EDITOR_THREAD_EXECUTOR .addToExecute (this ::clear );
321327 }
@@ -332,31 +338,51 @@ private void showPreview(@NotNull final String path, @NotNull final String exten
332338 public void show (@ NotNull final String assetPath , final int fitWidth , final int fitHeight ) {
333339 imageView .setFitHeight (fitHeight );
334340 imageView .setFitWidth (fitWidth );
335- showPreview (assetPath , getExtension (assetPath ));
341+ showPreview (assetPath , getExtension (assetPath ), false );
336342 }
337343
338344 /**
339345 * Show a j3o object.
340346 *
341- * @param path the path to object.
347+ * @param path the path to object.
348+ * @param external true if the object is external object.
342349 */
343350 @ JMEThread
344- private void showObject (@ NotNull final String path ) {
345- if (processor != null ) processor .setEnabled (true );
346-
347- frame = 0 ;
351+ private void showObject (@ NotNull final String path , final boolean external ) {
352+ prepareProcessor ();
348353
349354 final Editor editor = Editor .getInstance ();
350- final Camera camera = editor .getPreviewCamera ();
351- camera .setLocation (CAMERA_LOCATION );
352- camera .setRotation (CAMERA_ROTATION );
355+ final AssetManager assetManager = editor .getAssetManager ();
356+ final Spatial model ;
353357
354- modelNode .detachAllChildren ();
358+ FolderAssetLocator .setIgnore (external );
359+ try {
360+ model = assetManager .loadModel (path );
355361
356- final AssetManager assetManager = editor .getAssetManager ();
357- final Spatial model = assetManager .loadModel (path );
362+ if (external && EDITOR_CONFIG .isAutoTangentGenerating ()) {
363+ TangentGenerator .useMikktspaceGenerator (model );
364+ }
365+
366+ } finally {
367+ FolderAssetLocator .setIgnore (false );
368+ }
369+
370+ tryToLoad (model );
371+
372+ final Node rootNode = editor .getPreviewNode ();
373+ rootNode .detachChild (modelNode );
374+ }
375+
376+ /**
377+ * Try to load and show the model.
378+ *
379+ * @param model the model.
380+ */
381+ @ JMEThread
382+ private void tryToLoad (@ NotNull final Spatial model ) {
358383 try {
359384
385+ final Editor editor = Editor .getInstance ();
360386 final RenderManager renderManager = editor .getRenderManager ();
361387 renderManager .preloadScene (model );
362388
@@ -365,19 +391,17 @@ private void showObject(@NotNull final String path) {
365391 } catch (final RendererException | AssetNotFoundException | UnsupportedOperationException e ) {
366392 EditorUtil .handleException (LOGGER , this , e );
367393 }
368-
369- final Node rootNode = editor .getPreviewNode ();
370- rootNode .detachChild (modelNode );
371394 }
372395
373396 /**
374- * Show a j3m material.
375- *
376- * @param path the path to material.
397+ * Prepare the processor to render the a preview object.
377398 */
378399 @ JMEThread
379- private void showMaterial (@ NotNull final String path ) {
380- if (processor != null ) processor .setEnabled (true );
400+ private void prepareProcessor () {
401+
402+ if (processor != null ) {
403+ processor .setEnabled (true );
404+ }
381405
382406 frame = 0 ;
383407
@@ -386,22 +410,24 @@ private void showMaterial(@NotNull final String path) {
386410 camera .setLocation (CAMERA_LOCATION );
387411 camera .setRotation (CAMERA_ROTATION );
388412
389- final AssetManager assetManager = editor .getAssetManager ();
390- final Material material = assetManager .loadMaterial (path );
391-
392413 modelNode .detachAllChildren ();
414+ }
393415
394- testBox .setMaterial (material );
395- try {
396-
397- final RenderManager renderManager = editor .getRenderManager ();
398- renderManager .preloadScene (testBox );
416+ /**
417+ * Show a j3m material.
418+ *
419+ * @param path the path to material.
420+ */
421+ @ JMEThread
422+ private void showMaterial (@ NotNull final String path ) {
423+ prepareProcessor ();
399424
400- modelNode .attachChild (testBox );
425+ final Editor editor = Editor .getInstance ();
426+ final AssetManager assetManager = editor .getAssetManager ();
427+ final Material material = assetManager .loadMaterial (path );
401428
402- } catch (final RendererException | AssetNotFoundException | UnsupportedOperationException e ) {
403- EditorUtil .handleException (LOGGER , this , e );
404- }
429+ testBox .setMaterial (material );
430+ tryToLoad (testBox );
405431
406432 final Node rootNode = editor .getPreviewNode ();
407433 rootNode .detachChild (modelNode );
@@ -422,7 +448,9 @@ private void clearImpl() {
422448 final Node rootNode = editor .getPreviewNode ();
423449 rootNode .detachChild (modelNode );
424450
425- if (processor != null ) processor .setEnabled (false );
451+ if (processor != null ) {
452+ processor .setEnabled (false );
453+ }
426454 }
427455
428456 /**
0 commit comments