-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockView3dGUI.java
More file actions
679 lines (569 loc) · 29.8 KB
/
BlockView3dGUI.java
File metadata and controls
679 lines (569 loc) · 29.8 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//This file is by Jacob Mobin
import javax.imageio.ImageIO;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Text2D;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class BlockView3dGUI {
private static Block block2;
private static Map<String, BufferedImage> imageCache = new HashMap<>();
public static void blockView3dGUI(Block block) {
block2 = block;
JFrame frame = new JFrame("3D Canvas Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(1080, 720);
// Create panels
JPanel centerPanel = new JPanel(new BorderLayout()); // For 3D content
// Set layout manager (BorderLayout)
frame.setLayout(new BorderLayout());
// Set size for center panel
centerPanel.setPreferredSize(new Dimension(1080, 620));
// Add panels to the frame
frame.add(centerPanel, BorderLayout.CENTER);
try {
// Initialize Java 3D canvas and universe
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
if (config == null) {
throw new RuntimeException("Preferred graphics configuration is null.");
}
Canvas3D canvas3D = new Canvas3D(config);
centerPanel.add(canvas3D, BorderLayout.CENTER);
SimpleUniverse universe = new SimpleUniverse(canvas3D);
universe.getViewingPlatform().setNominalViewingTransform(); // Reset the viewing platform
// Create a textured cube
BranchGroup root = new BranchGroup();
TransformGroup cubeTransformGroup = createTexturedCube();
root.addChild(cubeTransformGroup);
// Add lighting
addLight(root);
// Add floor and wall
addFloor(root);
addBackgroundWall(root);
// Add mouse rotate behavior
MouseRotate mouseRotate = new MouseRotate();
mouseRotate.setTransformGroup(cubeTransformGroup);
mouseRotate.setSchedulingBounds(new BoundingSphere(new Point3d(0, 0, 0), 100));
root.addChild(mouseRotate);
// Add additional smaller cubes in a grid pattern
addGridBackgroundCubes(root, 10, 6, 0.45f); // 5x5 grid with 2.0f spacing
addOverlay(root);
// Add the root branch group to the universe
universe.addBranchGraph(root);
// Show the frame
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "An error occurred while initializing the 3D canvas: " + e.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
/*
* CUBE LOGIC METHODS BELOW
* Rendering main cube, side panel ovelay cubes and background cubes
* */
private static TransformGroup createTexturedCube() {
// Create a transform group for the cube
TransformGroup transformGroup = new TransformGroup();
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
// Create textured cube
Box box = new Box(0.3f, 0.3f, 0.3f, Box.GENERATE_TEXTURE_COORDS, new Appearance());
// Apply textures to each side
setTexture(box, block2.getName());
// Apply rotation
Transform3D rotation = new Transform3D();
rotation.rotZ(Math.PI / 2); // 90 degrees rotation around x-axis
TransformGroup rotationGroup = new TransformGroup(rotation);
rotationGroup.addChild(box);
// Add the rotated cube to the transform group
transformGroup.addChild(rotationGroup);
return transformGroup;
}
// New method to create specific overlay cubes with different textures
private static TransformGroup createSpecificOverlayCube(String name) {
// Create a transform group for the overlay cube
TransformGroup transformGroup = new TransformGroup();
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
// Create a smaller textured cube for the overlay
Box box = new Box(0.05f, 0.05f, 0.05f, Box.GENERATE_TEXTURE_COORDS, new Appearance());
// Apply different textures to each side
setTexture(box, name);
// Add the cube to the transform group
transformGroup.addChild(box);
return transformGroup;
}
//Add a grid of background cubes
private static void addGridBackgroundCubes(BranchGroup root, int rows, int cols, float spacing) {
String[] avalableTextures = {
"Stone",
"Cherry Log",
"Dirt",
"Birch Log",
"Diorite",
"Oak Log",
"Deepslate Tiles",
"Prismarine",
"Cobblestone",
"Piston",
"Bedrock",
"Sculk",
"Sponge",
"End stone",
"Obsidian",
"Polished Andesite",
"Wool",
"Concrete",
"Leaves",
"Basalt",
"Granite",
"Shulker box",
"Hay bale",
"Redstone Lamp",
"Sea lantern",
"Soul Sand",
"Calcite",
"Command block",
"Lodestone",
"Magma block",
"Crafting Table",
"Juke Box",
"Note Block",
"Observer",
"Block of Diamond",
"Block of Gold",
"Block of Emerald",
"Block of Iron",
"Iron ore",
"Diamond ore",
"Gold ore",
"Emerald ore",
"Bricks",
"Bookshelf",
"Furnace",
"Ice",
"Packed Ice",
"Redstone ore",
"Block of Redstone",
"Acacia plank",
"Barrel",
"Cactus",
"Dark Prismarine",
"Gilded Blackstone",
"Fire Coral Block",
"Grass Block",
"Jack o'Lantern",
"Shroomlight",
"Slime Block",
"Podzol",
"Dried Kelp Block",
"TNT",
"Pumpkin",
"Farmland",
"Gravel",
"Blast Furnace",
"Rooted Dirt",
"Horn Coral Block",
"Coarse Dirt",
"Infested Cobblestone",
"Sand",
"Sandstone",
"Smithing Table",
"Smoker",
"Coal Ore",
"Purpur Block",
"Cracked Stone Brick",
"Terracotta",
"Honeycomb Block",
"Reinforced Deepslate",
"Smooth Red Sandstone",
"Brown Mushroom Block",
"Red Mushroom Block",
"Stem Mushroom Block",
"Concrete Powder",
"Tuff",
"Target",
"Nylium",
"Mud",
"Mycelium",
"Muddy Mangrove Roots",
"Nether Bricks",
"Mud Bricks",
"Nether Gold Ore",
"Nether Wart Block",
"Warped Wart Block",
"Waxed Block of Copper",
"Packed Mud",
"Pearlescent",
"Dropper"
};
String[] avalableTextures2 = {"TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT",
"TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT",
"TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT", "TNT"};
int x = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.println(avalableTextures[x]);
TransformGroup cubeTransformGroup = createRotatingBackgroundCube(avalableTextures[x]); //scrapped becuase it was too much for school pc to handle as is
Transform3D transform = new Transform3D();
// Position the cubes in a grid pattern
transform.setTranslation(new Vector3f(i * spacing - (rows - 1) * spacing / 6 - 1.3f, j * spacing - (cols - 1) * spacing / 6 -1f, -2.7f));
cubeTransformGroup.setTransform(transform);
root.addChild(cubeTransformGroup);
x++;
}
}
}
//This method creates the background cubes, these are intended to slowly rotate (If time permits)
private static TransformGroup createRotatingBackgroundCube(String texture) {
// Create a transform group for the background cube
TransformGroup transformGroup = new TransformGroup();
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
// Create smaller textured cube
Box box = new Box(0.1f, 0.1f, 0.1f, Box.GENERATE_TEXTURE_COORDS, new Appearance());
// Apply textures to each side
setTexture(box, texture);
// Create a rotation interpolator
Transform3D rotation = new Transform3D();
rotation.rotZ(Math.PI / 2); // 90 degrees rotation around x-axis
TransformGroup rotationGroup = new TransformGroup(rotation);
rotationGroup.addChild(box);
// Add the rotated cube to the transform group
transformGroup.addChild(rotationGroup);
return transformGroup;
}
//This method rotates the front back left and right images to line up with the 3d cube
private static BufferedImage rotateImage(BufferedImage originalImage, double angle) {
int width = originalImage.getWidth();
int height = originalImage.getHeight();
BufferedImage rotatedImage = new BufferedImage(height, width, originalImage.getType());
AffineTransform transform = new AffineTransform();
transform.translate((height - width) / 2, (width - height) / 2);
transform.rotate(Math.toRadians(angle), width / 2, height / 2);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
op.filter(originalImage, rotatedImage);
return rotatedImage;
}
//This method rotates the front back left and right images to line up with the 3d cube
private static Texture loadAndRotateTexture(String imagePath, double angle) {
TextureLoader loader = new TextureLoader(imagePath, null);
ImageComponent2D originalImage = loader.getImage();
BufferedImage rotatedImage = rotateImage(originalImage.getImage(), angle);
return new TextureLoader(rotatedImage, (String) null).getTexture();
}
//This method sets the textures on each face of the cube, and uses the rotate image method to fix texture orientation
private static void setTexture(Box box, String name) {
// Load textures for each side
Texture textureFront = loadAndRotateTexture("object/front.jpg.jpg", 90);
Texture textureBack = loadAndRotateTexture("object/back.jpg.jpg", 270);
Texture textureBottom = new TextureLoader("object/bottom.jpg.jpg", null).getTexture(); // No rotation needed
Texture textureTop = new TextureLoader("object/top.jpg.jpg", null).getTexture(); // No rotation needed
Texture textureLeft = loadAndRotateTexture("object/left.jpg.jpg", 90);
Texture textureRight = loadAndRotateTexture("object/right.jpg.jpg", 90);
BufferedImage buffer = null;
try {
buffer = loadImage("object/" + name + "/front.jpg.jpg");
if (buffer != null) {
textureFront = loadAndRotateTexture("object/" + name + "/front.jpg.jpg", 90);
textureBack = loadAndRotateTexture("object/" + name + "/back.jpg.jpg", 270);
textureBottom = new TextureLoader("object/" + name + "/bottom.jpg.jpg", null).getTexture(); // No rotation needed
textureTop = new TextureLoader("object/" + name + "/top.jpg.jpg", null).getTexture(); // No rotation needed
textureLeft = loadAndRotateTexture("object/" + name + "/left.jpg.jpg", 90);
textureRight = loadAndRotateTexture("object/" + name + "/right.jpg.jpg", 90);
}
} catch (Exception e) {
//e.printStackTrace();
}
//System.out.println(name);
Appearance front = new Appearance();
front.setTexture(textureFront);
Appearance back = new Appearance();
back.setTexture(textureBack);
Appearance top = new Appearance();
top.setTexture(textureTop);
Appearance bottom = new Appearance();
bottom.setTexture(textureBottom);
Appearance left = new Appearance();
left.setTexture(textureLeft);
Appearance right = new Appearance();
right.setTexture(textureRight);
// Set textures to each side
for (int i = 0; i < box.numChildren(); i++) {
Shape3D face = (Shape3D) box.getChild(i);
switch (i) {
case 0:
face.setAppearance(front);
break;
case 1:
face.setAppearance(back);
break;
case 2:
face.setAppearance(top);
break;
case 3:
face.setAppearance(bottom);
break;
case 4:
face.setAppearance(left);
break;
case 5:
face.setAppearance(right);
break;
}
}
}
/*
* SCENE METHODS
* These methods build the actually scene
* */
//use Colors and Vectors to create a directional light with a range of 100f for the scenes lighting
private static void addLight(BranchGroup root) {
Color3f lightColor = new Color3f(1.0f, 1.0f, 1.0f); // White light
Vector3f lightDirection = new Vector3f(-1.0f, -1.0f, -1.0f); // Directional light from top-left
DirectionalLight light = new DirectionalLight(lightColor, lightDirection);
light.setInfluencingBounds(new BoundingSphere(new Point3d(0, 0, 0), 100));
root.addChild(light);
}
//Add a floor to the scene using floor and texture co-ordinates
private static void addFloor(BranchGroup root) {
// Load the texture image
TextureLoader loader = new TextureLoader("object/ground.jpg", null);
Texture floorTexture = loader.getTexture();
// Create a textured appearance for the floor
Appearance floorApp = new Appearance();
floorApp.setTexture(floorTexture);
// Create texture attributes and set the texture mode to modulate
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
floorApp.setTextureAttributes(texAttr);
// Create the geometry for the floor
QuadArray floorGeometry = new QuadArray(4, QuadArray.COORDINATES | QuadArray.TEXTURE_COORDINATE_2);
floorGeometry.setCoordinate(0, new Point3f(-10.0f, -1.5f, 5.0f));
floorGeometry.setCoordinate(1, new Point3f(10.0f, -1.5f, 5.0f));
floorGeometry.setCoordinate(2, new Point3f(10.0f, -1.5f, -15.0f));
floorGeometry.setCoordinate(3, new Point3f(-10.0f, -1.5f, -15.0f));
// Set the texture coordinates for the floor
floorGeometry.setTextureCoordinate(0, 0, new TexCoord2f(0.0f, 0.0f));
floorGeometry.setTextureCoordinate(0, 1, new TexCoord2f(1.0f, 0.0f));
floorGeometry.setTextureCoordinate(0, 2, new TexCoord2f(1.0f, 1.0f));
floorGeometry.setTextureCoordinate(0, 3, new TexCoord2f(0.0f, 1.0f));
// Create the floor shape and set its appearance
Shape3D floor = new Shape3D(floorGeometry, floorApp);
root.addChild(floor);
}
//Add a background wall to the scene using the same logic and texture as the ground method
private static void addBackgroundWall(BranchGroup root) {
// Load the texture image for the wall
TextureLoader loader = new TextureLoader("object/ground.jpg", null);
Texture wallTexture = loader.getTexture();
// Create a textured appearance for the wall
Appearance wallApp = new Appearance();
wallApp.setTexture(wallTexture);
// Create texture attributes and set the texture mode to modulate
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
wallApp.setTextureAttributes(texAttr);
// Create the geometry for the wall
QuadArray wallGeometry = new QuadArray(4, QuadArray.COORDINATES | QuadArray.TEXTURE_COORDINATE_2);
wallGeometry.setCoordinate(0, new Point3f(-10.0f, -1.5f, -15.0f)); // Bottom-left
wallGeometry.setCoordinate(1, new Point3f(10.0f, -1.5f, -15.0f)); // Bottom-right
wallGeometry.setCoordinate(2, new Point3f(10.0f, 10f, -15.0f)); // Top-right
wallGeometry.setCoordinate(3, new Point3f(-10.0f, 10f, -15.0f)); // Top-left
// Set the texture coordinates for the wall
wallGeometry.setTextureCoordinate(0, 0, new TexCoord2f(0.0f, 0.0f));
wallGeometry.setTextureCoordinate(0, 1, new TexCoord2f(1.0f, 0.0f));
wallGeometry.setTextureCoordinate(0, 2, new TexCoord2f(1.0f, 1.0f));
wallGeometry.setTextureCoordinate(0, 3, new TexCoord2f(0.0f, 1.0f));
// Create the wall shape and set its appearance
Shape3D wall = new Shape3D(wallGeometry, wallApp);
// Add the wall to the root branch group
root.addChild(wall);
}
/*
* OVERLAY METHODS
* methods that controll construction of the overlay
* */
//The overlay controller that runs the other function, made this way so its easier for CHU
private static void addOverlay(BranchGroup root) {
addSpecificOverlayCubes(root, 0.13f); // add sidebar cube icons
addOverlayRectFar(root);
addOverlayRectTop(root);
addOverlayRect(root); // add layer for 3d cubes and text.
addText3D(root, block2.getName(), -0.9f, 0.42f, 0.2f,40 ); //String text, float x, float y, float z, float fontSize
addText3D(root, "BlastRes: " + block2.getBlastres(), -0.75f, 0.27f, 0f, 15 );
addText3D(root, "Hardness: " + block2.getHardness(), -0.75f, 0.14f, 0f, 15 );
addText3D(root, "Renewable: " + block2.getRenewability(), -0.75f, 0.01f, 0f, 15 );
addText3D(root, "Flammable: " + block2.getFlammable(), -0.75f, -0.12f, 0f, 15 );
addText3D(root, "Dimension: " + block2.getDimension(), -0.75f, -0.25f, 0f, 15 );
addText3D(root, "Craftable: " + " Yes", -0.75f, -0.38f, 0f, 15 );
addText3D(root, "Luminious: " + block2.getLuminous(), -0.75f, -0.51f, 0f, 15 );
addCraftingRecipe(root, block2.getName());
// add sidebar LEFT text
}
//Add a 3d text to the scene with root x y z and font size (Font size is absouloute and is not modified (note to self by jacob)
private static void addText3D(BranchGroup root, String text, float x, float y, float z, float fontSize) {
// Create a Text2D object with the specified text and font size
Text2D text2D = new Text2D(text, new Color3f(0f, 0f, 0f), "Arial", (int) (fontSize), Font.PLAIN); //111 is white
// Create a Transform3D to position the text
Transform3D transform = new Transform3D();
transform.setTranslation(new Vector3f(x, y, z+0.01f));
// Create a TransformGroup and add the Transform3D to it
TransformGroup textTransformGroup = new TransformGroup(transform);
textTransformGroup.addChild(text2D);
// Add the text TransformGroup to the root BranchGroup
root.addChild(textTransformGroup);
}
private static void addCraftingRecipe(BranchGroup root, String name) {
// Load the texture image for the wall
TextureLoader loader = new TextureLoader("object/Notcraftable.jpg", null); //just in case shit fucks itself
BufferedImage unselected = loadImage("object/Recipes/" + block2.getName() + "/" + block2.getName() + ".jpg");
if (unselected != null) {
loader = new TextureLoader("object/Recipes/" + block2.getName() + "/" + block2.getName() + ".jpg", null);
}
Texture wallTexture = loader.getTexture();
// Create a textured appearance for the wall
Appearance wallApp = new Appearance();
wallApp.setTexture(wallTexture);
// Create texture attributes and set the texture mode to modulate
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
wallApp.setTextureAttributes(texAttr);
// Create the geometry for the wall
QuadArray wallGeometry = new QuadArray(4, QuadArray.COORDINATES | QuadArray.TEXTURE_COORDINATE_2);
wallGeometry.setCoordinate(0, new Point3f(0.4f, -0.4f, 0f)); // Bottom-left
wallGeometry.setCoordinate(1, new Point3f(1f, -0.4f, 0f)); // Bottom-right
wallGeometry.setCoordinate(2, new Point3f(1f, 0.2f, 0f)); // Top-right
wallGeometry.setCoordinate(3, new Point3f(0.4f, 0.2f, 0f)); // Top-left
// Set the texture coordinates for the wall
wallGeometry.setTextureCoordinate(0, 0, new TexCoord2f(0.0f, 0.0f));
wallGeometry.setTextureCoordinate(0, 1, new TexCoord2f(1.0f, 0.0f));
wallGeometry.setTextureCoordinate(0, 2, new TexCoord2f(1.0f, 1.0f));
wallGeometry.setTextureCoordinate(0, 3, new TexCoord2f(0.0f, 1.0f));
// Create the wall shape and set its appearance
Shape3D wall = new Shape3D(wallGeometry, wallApp);
// Add the wall to the root branch group
root.addChild(wall);
}
// adds specific 3d cubes tot he 2d overlay on the 3d scene
private static void addSpecificOverlayCubes(BranchGroup root, float spacing) {
String[] name = {"TNT", "Bedrock", "Leaves", "Lava", "Dimension", "Crafting Table", "Glowstone"};
TransformGroup overlayTransformGroup = new TransformGroup();
for (int i = 0; i < name.length; i++) {
TransformGroup cubeTransformGroup = createSpecificOverlayCube(name[i]);
Transform3D transform = new Transform3D();
transform.setTranslation(new Vector3f(-0.82f, 0.3f - i * spacing, 0.1f)); // Position the cubes vertically
// Create a new Transform3D for rotation
Transform3D rotation = new Transform3D();
rotation.rotZ(Math.PI / 2); // 90 degrees rotation around x-axis
// Combine the rotation and translation transforms
transform.mul(rotation);
cubeTransformGroup.setTransform(transform);
overlayTransformGroup.addChild(cubeTransformGroup);
}
root.addChild(overlayTransformGroup);
}
// same logic as the wall and ground methods however has no texture and the blending attributes give it transparecy
//same with all methods below this
public static void addOverlayRect(BranchGroup root) {
// Create an appearance with semi-transparent grey color
Appearance overlayAppearance = new Appearance();
ColoringAttributes coloringAttributes = new ColoringAttributes();
coloringAttributes.setColor(new Color3f(0.5f, 0.5f, 0.5f));
overlayAppearance.setColoringAttributes(coloringAttributes);
TransparencyAttributes transparencyAttributes = new TransparencyAttributes();
transparencyAttributes.setTransparencyMode(TransparencyAttributes.NONE);
transparencyAttributes.setTransparency(0.3f); // For example
overlayAppearance.setTransparencyAttributes(transparencyAttributes);
// Create the geometry for the rectangle
QuadArray overlayGeometry = new QuadArray(4, QuadArray.COORDINATES);
overlayGeometry.setCoordinate(0, new Point3f(-0.95f, -0.6f, 0f));
overlayGeometry.setCoordinate(1, new Point3f(-0.3f, -0.6f, 0f));
overlayGeometry.setCoordinate(2, new Point3f(-0.3f, 0.4f, 0f));
overlayGeometry.setCoordinate(3, new Point3f(-0.95f, 0.4f, 0f));
// Create the shape with the appearance
Shape3D overlay = new Shape3D(overlayGeometry, overlayAppearance);
// Create a transform group for positioning the rectangle
TransformGroup overlayTransformGroup = new TransformGroup();
Transform3D transform = new Transform3D();
transform.setTranslation(new Vector3f(0.0f, 0.0f, -0.1f)); // Position the rectangle
overlayTransformGroup.setTransform(transform);
// Add the rectangle to the transform group and the root
overlayTransformGroup.addChild(overlay);
root.addChild(overlayTransformGroup);
}
public static void addOverlayRectTop(BranchGroup root) {
// Create an appearance with semi-transparent grey color
Appearance overlayAppearance = new Appearance();
ColoringAttributes coloringAttributes = new ColoringAttributes();
coloringAttributes.setColor(new Color3f(0.5f, 0.5f, 0.5f));
overlayAppearance.setColoringAttributes(coloringAttributes);
TransparencyAttributes transparencyAttributes = new TransparencyAttributes(TransparencyAttributes.NONE, 0.05f);
overlayAppearance.setTransparencyAttributes(transparencyAttributes);
// Create the geometry for the rectangle
QuadArray overlayGeometry = new QuadArray(4, QuadArray.COORDINATES);
overlayGeometry.setCoordinate(0, new Point3f(-1f, 0.47f, 0f));
overlayGeometry.setCoordinate(1, new Point3f(1f, 0.47f, 0f));
overlayGeometry.setCoordinate(2, new Point3f(1f, 2f, 0f));
overlayGeometry.setCoordinate(3, new Point3f(-1f, 2f, 0f));
// Create the shape with the appearance
Shape3D overlay = new Shape3D(overlayGeometry, overlayAppearance);
// Create a transform group for positioning the rectangle
TransformGroup overlayTransformGroup = new TransformGroup();
Transform3D transform = new Transform3D();
transform.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f)); // Position the rectangle
overlayTransformGroup.setTransform(transform);
// Add the rectangle to the transform group and the root
overlayTransformGroup.addChild(overlay);
root.addChild(overlayTransformGroup);
}
public static void addOverlayRectFar(BranchGroup root) {
// Create an appearance with semi-transparent grey color
Appearance overlayAppearance = new Appearance();
ColoringAttributes coloringAttributes = new ColoringAttributes();
coloringAttributes.setColor(new Color3f(0.5f, 0.5f, 0.5f));
overlayAppearance.setColoringAttributes(coloringAttributes);
TransparencyAttributes transparencyAttributes = new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.5f);
overlayAppearance.setTransparencyAttributes(transparencyAttributes);
// Create the geometry for the rectangle
QuadArray overlayGeometry = new QuadArray(4, QuadArray.COORDINATES);
overlayGeometry.setCoordinate(0, new Point3f(-10.0f, -10f, -2.0f)); // Bottom-left
overlayGeometry.setCoordinate(1, new Point3f(10.0f, -10f, -2.0f)); // Bottom-right
overlayGeometry.setCoordinate(2, new Point3f(10.0f, 10f, -2.0f)); // Top-right
overlayGeometry.setCoordinate(3, new Point3f(-10.0f, 10f, -2.0f)); // Top-left
// Create the shape with the appearance
Shape3D overlay = new Shape3D(overlayGeometry, overlayAppearance);
// Create a transform group for positioning the rectangle
TransformGroup overlayTransformGroup = new TransformGroup();
Transform3D transform = new Transform3D();
transform.setTranslation(new Vector3f(0.0f, 0.0f, 0f)); // Position the rectangle
overlayTransformGroup.setTransform(transform);
// Add the rectangle to the transform group and the root
overlayTransformGroup.addChild(overlay);
root.addChild(overlayTransformGroup);
}
private static BufferedImage loadImage(String filename) { //optimised buffered image loader
if (imageCache.containsKey(filename)) {
return imageCache.get(filename);
} else {
try {
BufferedImage image = ImageIO.read(new File(filename));
imageCache.put(filename, image);
return image;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}