/
ConvertToInPlace.java
401 lines (360 loc) · 14.2 KB
/
ConvertToInPlace.java
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
Copyright (c) 2022-2023, Stephen Gold
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package jme3utilities.test.wes;
import com.jme3.anim.AnimClip;
import com.jme3.anim.AnimComposer;
import com.jme3.anim.SkinningControl;
import com.jme3.app.StatsAppState;
import com.jme3.font.BitmapText;
import com.jme3.font.Rectangle;
import com.jme3.input.CameraInput;
import com.jme3.input.KeyInput;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.shadow.DirectionalLightShadowRenderer;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeSystem;
import com.jme3.system.Platform;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jme3utilities.Heart;
import jme3utilities.InfluenceUtil;
import jme3utilities.MyAsset;
import jme3utilities.MySpatial;
import jme3utilities.MyString;
import jme3utilities.debug.Dumper;
import jme3utilities.debug.SkeletonVisualizer;
import jme3utilities.ui.AcorusDemo;
import jme3utilities.ui.CameraOrbitAppState;
import jme3utilities.ui.InputMode;
import jme3utilities.wes.AnimationEdit;
import org.lwjgl.system.Configuration;
/**
* Demonstrate conversion of a traveling animation into an in-place animation.
*
* @author Stephen Gold sgold@sonic.net
*/
class ConvertToInPlace extends AcorusDemo {
// *************************************************************************
// constants and loggers
/**
* message logger for this class
*/
final static Logger logger
= Logger.getLogger(ConvertToInPlace.class.getName());
/**
* application name (for the title bar of the app's window)
*/
final private static String applicationName
= ConvertToInPlace.class.getSimpleName();
// *************************************************************************
// fields
/**
* animation control of the modified C-G model
*/
private static AnimComposer composer;
/**
* status displayed in the upper-left corner of the GUI node
*/
private static BitmapText statusText;
/**
* collection of all skeleton visualizers in the scene
*/
final private static Collection<SkeletonVisualizer> visualizers
= new ArrayList<>(2);
// *************************************************************************
// new methods exposed
/**
* Main entry point for the ConvertToInPlace application.
*
* @param arguments array of command-line arguments (not null)
*/
public static void main(String[] arguments) {
Platform platform = JmeSystem.getPlatform();
if (platform.getOs() == Platform.Os.MacOS) {
Configuration.GLFW_LIBRARY_NAME.set("glfw_async");
}
String title = applicationName + " " + MyString.join(arguments);
ConvertToInPlace application = new ConvertToInPlace();
Heart.parseAppArgs(application, arguments);
boolean loadDefaults = true;
AppSettings settings = new AppSettings(loadDefaults);
settings.setAudioRenderer(null);
settings.setResizable(true);
settings.setSamples(4); // anti-aliasing
settings.setTitle(title); // Customize the window's title bar.
application.setSettings(settings);
/*
* The AWT settings dialog interferes with LWJGL v3
* on macOS and Raspbian, so don't show it!
*/
application.setShowSettings(false);
application.start();
}
// *************************************************************************
// AcorusDemo methods
/**
* Initialize this application.
*/
@Override
public void acorusInit() {
configureCamera();
// Set the background to light blue.
ColorRGBA backgroundColor = new ColorRGBA(0.2f, 0.2f, 1f, 1f);
viewPort.setBackgroundColor(backgroundColor);
addLighting();
addBox();
attachWorldAxes(1f);
// the unmodified animation in white, the modified one in yellow
composer = addFighter(0.7f, -0.7f, ColorRGBA.Yellow);
AnimComposer control = addFighter(-0.7f, 0.7f, ColorRGBA.White);
// Attach status text to the GUI.
statusText = new BitmapText(guiFont);
guiNode.attachChild(statusText);
super.acorusInit();
/*
* Create an in-place cyclic animation
* based on the "hurricane_kick" clip
* and play it at 1/4 speed.
*/
String originalName = "hurricane_kick";
AnimClip originalClip = composer.getAnimClip(originalName);
String convertedName = "hurricane_kick_in_place";
AnimClip convertedClip
= AnimationEdit.convertToInPlace(originalClip, convertedName);
composer.addAnimClip(convertedClip);
composer.setCurrentAction(convertedName);
composer.setGlobalSpeed(0.25f);
// Play the original animation (also at 1/4 speed) for comparison.
control.setCurrentAction(originalName);
control.setGlobalSpeed(0.25f);
/*
* Start with skeleton visualization enabled
* and the render-statistics overlay hidden.
*/
stateManager.getState(StatsAppState.class).toggleStats();
toggleSkeleton();
}
/**
* Calculate screen bounds for a detailed help node. Meant to be overridden.
*
* @param viewPortWidth (in pixels, >0)
* @param viewPortHeight (in pixels, >0)
* @return a new instance
*/
@Override
public Rectangle detailedHelpBounds(int viewPortWidth, int viewPortHeight) {
// Position help nodes along the top of the viewport.
float margin = 10f; // in pixels
float height = viewPortHeight - (2f * margin)
- 20f; // 20px so as not to cover the status line
float width = viewPortWidth - (2f * margin);
float leftX = margin;
float topY = margin + height;
Rectangle result = new Rectangle(leftX, topY, width, height);
return result;
}
/**
* Add application-specific hotkey bindings and override existing ones.
*/
@Override
public void moreDefaultBindings() {
InputMode dim = getDefaultInputMode();
dim.bind("dump scenes", KeyInput.KEY_P);
dim.bindSignal(CameraInput.FLYCAM_LOWER, KeyInput.KEY_DOWN);
dim.bindSignal(CameraInput.FLYCAM_RISE, KeyInput.KEY_UP);
dim.bindSignal("orbitLeft", KeyInput.KEY_LEFT);
dim.bindSignal("orbitRight", KeyInput.KEY_RIGHT);
dim.bind(asToggleHelp, KeyInput.KEY_H);
dim.bind(asTogglePause, KeyInput.KEY_PAUSE, KeyInput.KEY_PERIOD);
dim.bind("toggle skeleton", KeyInput.KEY_V);
}
/**
* Process an action that wasn't handled by the active InputMode.
*
* @param actionString textual description of the action (not null)
* @param ongoing true if the action is ongoing, otherwise false
* @param tpf time interval between frames (in seconds, ≥0)
*/
@Override
public void onAction(String actionString, boolean ongoing, float tpf) {
if (ongoing) {
switch (actionString) {
case "dump scenes":
dumpScenes();
return;
case "toggle skeleton":
toggleSkeleton();
return;
default:
}
}
super.onAction(actionString, ongoing, tpf);
}
/**
* Update the GUI layout and proposed settings after a resize.
*
* @param newWidth the new width of the framebuffer (in pixels, >0)
* @param newHeight the new height of the framebuffer (in pixels, >0)
*/
@Override
public void onViewPortResize(int newWidth, int newHeight) {
statusText.setLocalTranslation(10f, newHeight, 0f);
super.onViewPortResize(newWidth, newHeight);
}
/**
* Callback invoked once per frame.
*
* @param tpf the time interval between frames (in seconds, ≥0)
*/
@Override
public void simpleUpdate(float tpf) {
super.simpleUpdate(tpf);
// Update the displayed status.
double animationMsec = 1000.0 * composer.getTime();
String text = String.format("animation time = %.0f ms", animationMsec);
statusText.setText(text);
}
// *************************************************************************
// private methods
/**
* Attach a large static Box to the scene, to serve as a platform.
*/
private void addBox() {
float halfExtent = 50f; // mesh units
Mesh mesh = new Box(halfExtent, halfExtent, halfExtent);
Geometry geometry = new Geometry("box", mesh);
rootNode.attachChild(geometry);
geometry.move(0f, -halfExtent, 0f);
ColorRGBA color = new ColorRGBA(0f, 0.2f, 0f, 1f);
Material material = MyAsset.createShadedMaterial(assetManager, color);
geometry.setMaterial(material);
geometry.setShadowMode(RenderQueue.ShadowMode.Receive);
}
/**
* Attach a ninja-fighter model (with visualizer) to the root node and
* return its main AnimComposer.
*
* @param x the desired initial X coordinate
* @param z the desired initial Z coordinate
* @param svColor the desired color for the SkeletonVisualizer (not null)
* @return a new control
*/
private AnimComposer addFighter(float x, float z, ColorRGBA svColor) {
/*
* Temporarily suppress warnings (from the glTF loader)
* regarding animations that use non-linear interpolation.
*/
Logger loaderLogger
= Logger.getLogger("com.jme3.scene.plugins.gltf.GltfLoader");
Level savedLevel = loaderLogger.getLevel();
loaderLogger.setLevel(Level.SEVERE);
String assetPath = "Models/ninja-fighter/ninja.gltf";
Node scene = (Node) assetManager.loadModel(assetPath);
loaderLogger.setLevel(savedLevel);
Spatial ninja = scene.getChild("Ninja");
rootNode.attachChild(ninja);
List<Spatial> list = MySpatial.listSpatials(ninja);
for (Spatial spatial : list) {
spatial.setCullHint(Spatial.CullHint.Never);
spatial.setShadowMode(RenderQueue.ShadowMode.Cast);
}
setCgmHeight(ninja, 2f);
centerCgm(ninja);
ninja.move(x, 0f, z);
// Add a skeleton visualizer.
SkinningControl sc = ninja.getControl(SkinningControl.class);
SkeletonVisualizer sv = new SkeletonVisualizer(assetManager, sc);
InfluenceUtil.hideNonInfluencers(sv, sc);
sv.setLineColor(svColor);
ninja.addControl(sv);
visualizers.add(sv);
AnimComposer result = ninja.getControl(AnimComposer.class);
return result;
}
/**
* Add lighting and shadows to the scene.
*/
private void addLighting() {
ColorRGBA ambientColor = new ColorRGBA(0.2f, 0.2f, 0.2f, 1f);
AmbientLight ambient = new AmbientLight(ambientColor);
rootNode.addLight(ambient);
Vector3f direction = new Vector3f(1f, -2f, -2f).normalizeLocal();
DirectionalLight sun = new DirectionalLight(direction);
rootNode.addLight(sun);
DirectionalLightShadowRenderer dlsr
= new DirectionalLightShadowRenderer(assetManager, 4_096, 3);
dlsr.setLight(sun);
dlsr.setShadowIntensity(0.5f);
viewPort.addProcessor(dlsr);
}
/**
* Configure the Camera during startup.
*/
private void configureCamera() {
flyCam.setDragToRotate(true);
flyCam.setMoveSpeed(2f);
cam.setLocation(new Vector3f(6.7f, 2.9f, 4.4f));
cam.setRotation(new Quaternion(0.0508f, -0.82009f, 0.0731f, 0.56527f));
CameraOrbitAppState orbitState
= new CameraOrbitAppState(cam, "orbitLeft", "orbitRight");
stateManager.attach(orbitState);
}
/**
* Process a "dump scenes" action.
*/
private void dumpScenes() {
Dumper dumper = new Dumper();
//dumper.setDumpBucket(true);
dumper.setDumpCull(true);
//dumper.setDumpMatParam(true);
//dumper.setDumpOverride(true);
dumper.setDumpShadow(true);
dumper.setDumpTransform(true);
//dumper.setDumpUser(true);
dumper.dump(renderManager);
}
/**
* Toggle all the skeleton visualizers on/off.
*/
private void toggleSkeleton() {
for (SkeletonVisualizer sv : visualizers) {
boolean enabled = sv.isEnabled();
sv.setEnabled(!enabled);
}
}
}