22
33import com .jme3 .app .FlyCamAppState ;
44import com .jme3 .app .SimpleApplication ;
5- import com .jme3 .math .Matrix4f ;
6- import com .jme3 .math .Quaternion ;
75import com .jme3 .math .Transform ;
86import com .jme3 .math .Vector3f ;
97import com .jme3 .scene .Geometry ;
1715import com .jme3 .vulkan .Format ;
1816import com .jme3 .vulkan .VulkanInstance ;
1917import com .jme3 .vulkan .buffers .BufferUsage ;
20- import com .jme3 .vulkan .buffers .GpuBuffer ;
2118import com .jme3 .vulkan .buffers .PersistentBuffer ;
2219import com .jme3 .vulkan .buffers .StageableBuffer ;
2320import com .jme3 .vulkan .commands .CommandBuffer ;
3128import com .jme3 .vulkan .frames .UpdateFrame ;
3229import com .jme3 .vulkan .frames .UpdateFrameManager ;
3330import com .jme3 .vulkan .images .*;
31+ import com .jme3 .vulkan .material .MatrixTransformMaterial ;
3432import com .jme3 .vulkan .material .TestMaterial ;
3533import com .jme3 .vulkan .memory .MemoryProp ;
3634import com .jme3 .vulkan .memory .MemorySize ;
@@ -79,32 +77,6 @@ public class VulkanHelperTest extends SimpleApplication implements SwapchainUpda
7977 private boolean swapchainResizeFlag = false ;
8078 private boolean applicationStopped = false ;
8179
82- // mesh
83- private NewMesh mesh ;
84- private final FloatBuffer vertexData = BufferUtils .createFloatBuffer (
85- -0.5f , -0.5f , 0f , 1f , 0f , 0f , 1f , 0f ,
86- 0.5f , -0.5f , 0f , 0f , 1f , 0f , 0f , 0f ,
87- 0.5f , 0.5f , 0f , 0f , 0f , 1f , 0f , 1f ,
88- -0.5f , 0.5f , 0f , 1f , 1f , 1f , 1f , 1f ,
89-
90- -0.5f , -0.5f , -0.5f , 1f , 0f , 0f , 1f , 0f ,
91- 0.5f , -0.5f , -0.5f , 0f , 1f , 0f , 0f , 0f ,
92- 0.5f , 0.5f , -0.5f , 0f , 0f , 1f , 0f , 1f ,
93- -0.5f , 0.5f , -0.5f , 1f , 1f , 1f , 1f , 1f
94- );
95- private final IntBuffer indexData = BufferUtils .createIntBuffer (
96- 0 , 1 , 2 , 2 , 3 , 0 ,
97- 4 , 5 , 6 , 6 , 7 , 4 );
98-
99- // material
100- private TestMaterial material ;
101-
102- // geometry
103- private final Transform modelTransform = new Transform ();
104-
105- // material
106- private Texture texture ;
107-
10880 // framebuffer
10981 private ImageView depthView ;
11082
@@ -261,7 +233,8 @@ public void simpleInitApp() {
261233 try (ImageView .Builder i = imgView .build ()) {
262234 i .setAspect (VulkanImage .Aspect .Color );
263235 }
264- texture = new Texture (device , imgView );
236+ // material
237+ Texture texture = new Texture (device , imgView );
265238 try (Sampler .Builder t = texture .build ()) {
266239 t .setMinMagFilters (Filter .Linear , Filter .Linear );
267240 t .setEdgeModes (AddressMode .Repeat );
@@ -273,14 +246,17 @@ public void simpleInitApp() {
273246 sharedData = new BasicCommandBatch ();
274247 sharedDataFence = new Fence (device , true );
275248
276- // mesh
277- mesh = new MyCustomMesh (device , frames , meshDesc , sharedData ,
278- Vector3f .UNIT_Z , Vector3f .UNIT_Y , 1f , 1f , 0.5f , 0.5f );
249+ TestMaterial material = new TestMaterial (descriptorPool );
250+ material .getBaseColorMap ().setResource (new SingleResource <>(texture ));
279251
280- material = new TestMaterial (descriptorPool );
281- material .getMatrices ().setResource (frames .perFrame (n ->
252+ NewMesh m = new MyCustomMesh (device , frames , meshDesc , sharedData ,
253+ Vector3f .UNIT_Z , Vector3f .UNIT_Y , 1f , 1f , 0.5f , 0.5f );
254+ MatrixTransformMaterial t = new MatrixTransformMaterial (descriptorPool );
255+ t .getTransforms ().setResource (frames .perFrame (n ->
282256 new PersistentBuffer (device , MemorySize .floats (16 ), BufferUsage .Uniform , false )));
283- material .getBaseColorMap ().setResource (new SingleResource <>(texture ));
257+ Geometry geometry = new Geometry ("geometry" , m , t );
258+ geometry .setMaterial (material );
259+ rootNode .attachChild (geometry );
284260
285261 }
286262
@@ -382,15 +358,10 @@ public void update(UpdateFrameManager frames, float tpf) {
382358 renderManager .setCamera (cam , false );
383359
384360 // update matrix uniform (geometry)
385- {
386- // compute geometry states
387- modelTransform .getRotation ().multLocal (new Quaternion ().fromAngleAxis (tpf , Vector3f .UNIT_Y ));
388- Matrix4f worldViewProjection = cam .getViewProjectionMatrix ().mult (modelTransform .toTransformMatrix ());
389-
390- // update material uniform
391- GpuBuffer matrixBuffer = material .getMatrices ().getResource ().get ();
392- worldViewProjection .fillFloatBuffer (matrixBuffer .mapFloats (), true );
393- matrixBuffer .unmap ();
361+ for (Spatial s : rootNode ) {
362+ if (s instanceof Geometry ) {
363+ ((Geometry )s ).updateTransformMaterial (cam );
364+ }
394365 }
395366
396367 // update shared data
@@ -436,25 +407,20 @@ public void update(UpdateFrameManager frames, float tpf) {
436407 // run graphics commands via CommandBatch
437408 graphics .run (graphicsCommands , frames .getCurrentFrame ());
438409
439- // material.bind(graphicsCommands, pipeline);
440- // mesh.bind(graphicsCommands);
441- // mesh.draw(graphicsCommands);
442-
443410 // draw all geometries in the rootNode
444411 for (Spatial s : rootNode ) {
445412 if (s instanceof Geometry ) {
446413 Geometry g = (Geometry )s ;
447- g .getMaterial ().bind (graphicsCommands , pipeline );
448- g .getMesh ().bind (graphicsCommands );
449- g .getMesh ().draw (graphicsCommands );
414+ g .draw (graphicsCommands , pipeline );
450415 }
451416 }
452417
453418 // material
454419 renderPass .end (graphicsCommands );
455420
456421 // render manager
457- graphicsCommands .endAndSubmit (new SyncGroup (new Semaphore [] {imageAvailable , perFrameDataFinished , sharedDataFinished }, renderFinished , inFlight ));
422+ graphicsCommands .endAndSubmit (new SyncGroup (new Semaphore []
423+ {imageAvailable , perFrameDataFinished , sharedDataFinished }, renderFinished , inFlight ));
458424 swapchain .present (device .getPhysicalDevice ().getPresent (), image , renderFinished .toGroupWait ());
459425
460426 }
0 commit comments