Skip to content

Commit 897b61e

Browse files
committed
implemented import model action, fixed generating of tangents.
1 parent b4e35eb commit 897b61e

File tree

12 files changed

+619
-51
lines changed

12 files changed

+619
-51
lines changed

src/main/java/com/ss/editor/asset/locator/FileSystemAssetLocator.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
import com.jme3.asset.AssetKey;
55
import com.jme3.asset.AssetLocator;
66
import com.jme3.asset.AssetManager;
7+
import com.ss.editor.Editor;
8+
import com.ss.editor.annotation.FromAnyThread;
9+
import com.ss.rlib.util.ArrayUtils;
10+
import com.ss.rlib.util.array.ArrayFactory;
11+
import com.ss.rlib.util.array.ConcurrentArray;
712
import org.jetbrains.annotations.NotNull;
813

914
import java.nio.file.Files;
1015
import java.nio.file.Path;
1116
import java.nio.file.Paths;
17+
import java.util.Collection;
1218

1319
/**
1420
* The implementation of asset locator to use file system as asset folder.
@@ -17,9 +23,29 @@
1723
*/
1824
public class FileSystemAssetLocator implements AssetLocator {
1925

26+
@NotNull
27+
private static final ConcurrentArray<AssetKey<?>> LOCATED_KEYS = ArrayFactory.newConcurrentStampedLockArray(AssetKey.class);
28+
29+
/**
30+
* Clear all located objects from this locator.
31+
*/
32+
@FromAnyThread
33+
public static void clear() {
34+
final long stamp = LOCATED_KEYS.writeLock();
35+
try {
36+
37+
final Editor editor = Editor.getInstance();
38+
final AssetManager assetManager = editor.getAssetManager();
39+
40+
LOCATED_KEYS.forEach(assetManager, (assetKey, manager) -> manager.deleteFromCache(assetKey));
41+
42+
} finally {
43+
LOCATED_KEYS.writeUnlock(stamp);
44+
}
45+
}
46+
2047
@Override
2148
public void setRootPath(@NotNull final String rootPath) {
22-
2349
}
2450

2551
@Override
@@ -28,6 +54,8 @@ public AssetInfo locate(@NotNull final AssetManager manager, @NotNull final Asse
2854
final Path absoluteFile = Paths.get(key.getName());
2955
if (!Files.exists(absoluteFile)) return null;
3056

57+
ArrayUtils.runInWriteLock(LOCATED_KEYS, key, Collection::add);
58+
3159
return new FolderAssetLocator.PathAssetInfo(manager, key, absoluteFile);
3260
}
3361
}

src/main/java/com/ss/editor/asset/locator/FolderAssetLocator.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
*/
2323
public class FolderAssetLocator implements AssetLocator {
2424

25+
@NotNull
26+
private static final ThreadLocal<Boolean> IGNORE_LOCAL = ThreadLocal.withInitial(() -> false);
27+
28+
/**
29+
* Set the flag of ignoring this locator.
30+
*
31+
* @param ignore true if need to ignore this locator.
32+
*/
33+
public static void setIgnore(final boolean ignore) {
34+
IGNORE_LOCAL.set(false);
35+
}
36+
2537
@Override
2638
@JMEThread
2739
public void setRootPath(@NotNull final String rootPath) {
@@ -30,6 +42,7 @@ public void setRootPath(@NotNull final String rootPath) {
3042
@Override
3143
@JMEThread
3244
public AssetInfo locate(@NotNull final AssetManager manager, @NotNull final AssetKey key) {
45+
if (IGNORE_LOCAL.get() == Boolean.TRUE) return null;
3346

3447
final Path absoluteFile = Paths.get(key.getName());
3548
if (Files.exists(absoluteFile)) return null;

src/main/java/com/ss/editor/manager/JMEFilePreviewManager.java

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import com.ss.editor.annotation.FXThread;
3232
import com.ss.editor.annotation.FromAnyThread;
3333
import com.ss.editor.annotation.JMEThread;
34+
import com.ss.editor.asset.locator.FolderAssetLocator;
35+
import com.ss.editor.config.EditorConfig;
3436
import com.ss.editor.executor.impl.JMEThreadExecutor;
3537
import com.ss.editor.model.tool.TangentGenerator;
3638
import 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
/**

src/main/java/com/ss/editor/model/tool/TangentGenerator.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.ss.editor.model.tool;
22

3+
import com.jme3.scene.Mesh;
34
import com.jme3.scene.Spatial;
5+
import com.jme3.scene.VertexBuffer;
46
import com.jme3.util.TangentBinormalGenerator;
57
import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
68
import com.ss.editor.util.EditorUtil;
7-
8-
import org.jetbrains.annotations.NotNull;
9+
import com.ss.editor.util.NodeUtils;
910
import com.ss.rlib.logging.Logger;
1011
import com.ss.rlib.logging.LoggerManager;
12+
import org.jetbrains.annotations.NotNull;
1113

1214
/**
1315
* Tangent generators.
@@ -22,12 +24,22 @@ public class TangentGenerator {
2224
/**
2325
* Generate tangents using a standard algorithm.
2426
*
25-
* @param spatial the spatial
26-
* @param splitMirrored the split mirrored
27+
* @param spatial the spatial.
28+
* @param splitMirrored the split mirrored.
2729
*/
2830
public static void useStandardGenerator(@NotNull final Spatial spatial, final boolean splitMirrored) {
2931
try {
30-
TangentBinormalGenerator.generate(spatial, splitMirrored);
32+
33+
NodeUtils.visitGeometry(spatial, geometry -> {
34+
35+
final Mesh mesh = geometry.getMesh();
36+
final VertexBuffer texCoord = mesh.getBuffer(VertexBuffer.Type.TexCoord);
37+
38+
if (texCoord != null) {
39+
TangentBinormalGenerator.generate(geometry, splitMirrored);
40+
}
41+
});
42+
3143
} catch (final Exception e) {
3244
EditorUtil.handleException(LOGGER, null, e);
3345
}
@@ -36,11 +48,21 @@ public static void useStandardGenerator(@NotNull final Spatial spatial, final bo
3648
/**
3749
* Generate tangents using a Mikktspace algorithm.
3850
*
39-
* @param spatial the spatial
51+
* @param spatial the spatial.
4052
*/
4153
public static void useMikktspaceGenerator(@NotNull final Spatial spatial) {
4254
try {
43-
MikktspaceTangentGenerator.generate(spatial);
55+
56+
NodeUtils.visitGeometry(spatial, geometry -> {
57+
58+
final Mesh mesh = geometry.getMesh();
59+
final VertexBuffer texCoord = mesh.getBuffer(VertexBuffer.Type.TexCoord);
60+
61+
if (texCoord != null) {
62+
MikktspaceTangentGenerator.generate(geometry);
63+
}
64+
});
65+
4466
} catch (final Exception e) {
4567
EditorUtil.handleException(LOGGER, null, e);
4668
}

0 commit comments

Comments
 (0)