46
46
import com .jme3 .math .Vector3f ;
47
47
import com .jme3 .renderer .RendererException ;
48
48
import com .jme3 .scene .Geometry ;
49
- import com .jme3 .scene .Spatial ;
50
49
import com .jme3 .scene .shape .Box ;
51
50
import com .jme3 .scene .shape .Quad ;
52
51
import com .jme3 .scene .shape .Sphere ;
59
58
import javax .swing .JLabel ;
60
59
61
60
/**
62
- * Handles rendering of materials in preview widgets of Material and Shader Node editor.
63
- *
61
+ * Handles rendering of materials in preview widgets of Material and Shader Node
62
+ * editor.
63
+ *
64
64
* @author Nehon
65
65
*/
66
66
public class MaterialPreviewRenderer implements SceneListener {
@@ -108,14 +108,14 @@ private void init() {
108
108
quad = new Geometry ("previewQuad" , quadMesh );
109
109
quad .setLocalTranslation (new Vector3f (-2.25f , -2.25f , 0 ));
110
110
MikktspaceTangentGenerator .generate (quad );
111
-
111
+
112
112
teapot = (Geometry ) SceneApplication .getApplication ().getAssetManager ()
113
113
.loadModel ("Models/Teapot/Teapot.obj" );
114
114
teapot .scale (3.5f );
115
115
teapot .rotate (FastMath .PI , -FastMath .QUARTER_PI * 0.5f , -0.0f );
116
116
teapot .setLocalTranslation (new Vector3f (-0.5f , 1.75f , 0 ));
117
117
MikktspaceTangentGenerator .generate (teapot );
118
-
118
+
119
119
currentGeom = sphere ;
120
120
init = true ;
121
121
}
@@ -125,19 +125,14 @@ public void showMaterial(final ProjectAssetManager assetManager, final String ma
125
125
if (!init ) {
126
126
init ();
127
127
}
128
- exec .execute (new Runnable () {
129
-
130
- @ Override
131
- public void run () {
132
- MaterialKey key = new MaterialKey (assetManager .getRelativeAssetPath (materialFileName ));
133
- assetManager .deleteFromCache (key );
134
- Material mat = assetManager .loadAsset (key );
135
- if (mat != null ) {
136
- showMaterial (mat );
137
- }
128
+ exec .execute (() -> {
129
+ MaterialKey key = new MaterialKey (assetManager .getRelativeAssetPath (materialFileName ));
130
+ assetManager .deleteFromCache (key );
131
+ Material mat = assetManager .loadAsset (key );
132
+ if (mat != null ) {
133
+ showMaterial (mat );
138
134
}
139
135
});
140
-
141
136
142
137
}
143
138
@@ -149,46 +144,36 @@ public void showMaterial(final Material m, final String techniqueName) {
149
144
if (!init ) {
150
145
init ();
151
146
}
152
- SceneApplication .getApplication ().enqueue (new Callable <Material >() {
153
-
154
- @ Override
155
- public Material call () throws Exception {
156
- if (techniqueName != null ) {
147
+ SceneApplication .getApplication ().enqueue (() -> {
148
+ if (techniqueName != null ) {
149
+ try {
150
+ m .selectTechnique (techniqueName , SceneApplication .getApplication ().getRenderManager ());
151
+ } catch (Exception e ) {
152
+ //
153
+ }
154
+ }
155
+ final Material mat = reloadMaterial (m );
156
+ if (mat != null ) {
157
+ java .awt .EventQueue .invokeLater (() -> {
158
+ currentMaterial = mat ;
159
+ currentGeom .setMaterial (mat );
157
160
try {
158
- m .selectTechnique (techniqueName , SceneApplication .getApplication ().getRenderManager ());
161
+ if (currentGeom .getMaterial () != null ) {
162
+ PreviewRequest request = new PreviewRequest (MaterialPreviewRenderer .this , currentGeom , label .getWidth (), label .getHeight ());
163
+ request .getCameraRequest ().setLocation (new Vector3f (0 , 0 , 7 ));
164
+ request .getCameraRequest ().setLookAt (new Vector3f (0 , 0 , 0 ), Vector3f .UNIT_Y );
165
+ SceneApplication .getApplication ().createPreview (request );
166
+ }
159
167
} catch (Exception e ) {
160
- //
168
+ java .awt .EventQueue .invokeLater (() -> {
169
+ label .setIcon (Icons .error );
170
+ });
171
+ smartLog ("Error rendering material{0}" , e .getMessage ());
161
172
}
162
- }
163
- final Material mat = reloadMaterial (m );
164
- if (mat != null ) {
165
- java .awt .EventQueue .invokeLater (new Runnable () {
166
- @ Override
167
- public void run () {
168
- currentMaterial = mat ;
169
- currentGeom .setMaterial (mat );
170
- try {
171
- if (currentGeom .getMaterial () != null ) {
172
- PreviewRequest request = new PreviewRequest (MaterialPreviewRenderer .this , currentGeom , label .getWidth (), label .getHeight ());
173
- request .getCameraRequest ().setLocation (new Vector3f (0 , 0 , 7 ));
174
- request .getCameraRequest ().setLookAt (new Vector3f (0 , 0 , 0 ), Vector3f .UNIT_Y );
175
- SceneApplication .getApplication ().createPreview (request );
176
- }
177
- } catch (Exception e) {
178
- java .awt .EventQueue .invokeLater (new Runnable () {
179
- @ Override
180
- public void run () {
181
- label .setIcon (Icons .error );
182
- }
183
- });
184
- smartLog ("Error rendering material{0}" , e .getMessage ());
185
- }
186
- }
187
- });
173
+ });
188
174
189
- }
190
- return mat ;
191
175
}
176
+ return mat ;
192
177
});
193
178
}
194
179
@@ -209,7 +194,7 @@ public Material reloadMaterial(Material mat) {
209
194
210
195
//creating a dummy mat with the mat def of the mat to reload
211
196
dummy = new Material (mat .getMaterialDef ());
212
-
197
+
213
198
for (MatParam matParam : mat .getParams ()) {
214
199
dummy .setParam (matParam .getName (), matParam .getVarType (), matParam .getValue ());
215
200
}
@@ -231,12 +216,7 @@ public Material reloadMaterial(Material mat) {
231
216
//Logger.getLogger(MaterialDebugAppState.class.getName()).log(Level.SEVERE, e.getMessage());
232
217
smartLog ("{0}" , e .getMessage ());
233
218
234
- java .awt .EventQueue .invokeLater (new Runnable () {
235
- @ Override
236
- public void run () {
237
- label .setIcon (Icons .error );
238
- }
239
- });
219
+ java .awt .EventQueue .invokeLater (() -> label .setIcon (Icons .error ));
240
220
return null ;
241
221
} catch (NullPointerException npe ) {
242
222
//utterly bad, but for some reason I get random NPE here and can't figure out why so to avoid bigger issues, I just catch it.
@@ -282,11 +262,8 @@ public void sceneClosed(SceneRequest request) {
282
262
public void previewCreated (PreviewRequest request ) {
283
263
if (request .getRequester () == this )
E377
{
284
264
final ImageIcon icon = new ImageIcon (request .getImage ());
285
- java .awt .EventQueue .invokeLater (new Runnable () {
286
- @ Override
287
- public void run () {
288
- label .setIcon (icon );
289
- }
265
+ java .awt .EventQueue .invokeLater (() -> {
266
+ label .setIcon (icon );
290
267
});
291
268
previewRequested = false ;
292
269
}
@@ -296,15 +273,16 @@ public void cleanUp() {
296
273
SceneApplication .getApplication ().removeSceneListener (this );
297
274
exec .shutdownNow ();
298
275
}
299
-
300
- public boolean isPreviewRequested (){
276
+
277
+ public boolean isPreviewRequested () {
301
278
return previewRequested ;
302
279
}
303
-
280
+
304
281
/**
305
- * A more lightweight refresh than showMaterials that doesn't rebuild the material
282
+ * A more lightweight refresh than showMaterials that doesn't rebuild the
283
+ * material
306
284
*/
307
- public void refreshOnly (){
285
+ public void refreshOnly () {
308
286
previewRequested = true ;
309
287
SceneApplication .getApplication ().enqueue ((Callable <Object >) () -> {
310
288
if (currentGeom .getMaterial () != null ) {
@@ -316,5 +294,5 @@ public void refreshOnly(){
316
294
return null ;
317
295
});
318
296
}
319
-
297
+
320
298
}
0 commit comments