diff --git a/src/main/java/seng302/visualiser/fxObjects/ModelFactory.java b/src/main/java/seng302/visualiser/fxObjects/ModelFactory.java new file mode 100644 index 00000000..ce6e093a --- /dev/null +++ b/src/main/java/seng302/visualiser/fxObjects/ModelFactory.java @@ -0,0 +1,177 @@ +package seng302.visualiser.fxObjects; + +import com.interactivemesh.jfx.importer.stl.StlMeshImporter; +import javafx.animation.AnimationTimer; +import javafx.geometry.Point3D; +import javafx.scene.Group; +import javafx.scene.paint.Color; +import javafx.scene.paint.PhongMaterial; +import javafx.scene.shape.MeshView; +import javafx.scene.transform.Rotate; +import javafx.scene.transform.Scale; +import seng302.visualiser.test3d; + +/** + * Factory class for creating 3D models of boats. + */ +public class ModelFactory { + + /** + * Enum for boat meshes. Enum values should be of the form : + * ENUM_VALUE (hull file, mast file, y offset of mast CoR from origin, sail file, y offset of sail CoR from origin) + */ + public enum BoatMesh { + + DINGHY ("dinghy_hull.stl", "dinghy_mast.stl", 0, "dinghy_sail.stl", -1.36653); + + private final String hullFile, mastFile, sailFile; + private final double mastOffset, sailOffset; + + BoatMesh (String hullFile, String mastFile, double mastOffset, String sailFile, double sailOffset) { + this.hullFile = hullFile; + this.mastFile = mastFile; + this.mastOffset = mastOffset; + this.sailFile = sailFile; + this.sailOffset = sailOffset; + } + } + + /** + * Container class for a group of 3d objects representing a boat and it's animation. + */ + public class BoatModel { + + private AnimationTimer animationTimer; + private Group assets; + private BoatMesh meshType; + + /** + * Stores a model and it's optional animation. + * @param boatAssets The group with 3d assets for the boat. + * @param animation Animation, can be null. + */ + private BoatModel(Group boatAssets, AnimationTimer animation, BoatMesh meshType) { + this.assets = boatAssets; + this.animationTimer = animation; + this.meshType = meshType; + if (animation != null) { + animation.start(); + } + } + + /** + * Rotates the sail of this model by the given amount. + * @param degrees The rotation of the sail in degrees + */ + public void RotateSail(double degrees) { + MeshView mast = (MeshView) assets.getChildren().get(1); + MeshView sail = (MeshView) assets.getChildren().get(2); + mast.getTransforms().setAll( + new Rotate(degrees, 0, meshType.mastOffset, 0, new Point3D(0, 0, 1)) + ); + sail.getTransforms().setAll( + new Rotate(degrees, 0, meshType.sailOffset, 0, new Point3D(0, 0, 1)) + ); + } + + public Group getAssets() { + return this.assets; + } + + /** + * Changes the colour of the model in this class. + * @param newColour the new colour for the boat. + */ + public void changeColour(Color newColour) { + changeColourChild(0, newColour); + changeColourChild(1, newColour); + } + + private void changeColourChild(int index, Color newColour) { + MeshView meshView = getMeshViewChild(index); + meshView.setMaterial(new PhongMaterial(newColour)); + } + + private MeshView getMeshViewChild(int index) { + return (MeshView) assets.getChildren().get(index); + } + + private void setAnimation(AnimationTimer animation) { + animationTimer = animation; + if (animation != null) { + animation.start(); + } + } + + /** + * Stops the animation of this model. + */ + public void stopAnimation() { + if (animationTimer != null) { + animationTimer.stop(); + animationTimer = null; + } + } + } + + public BoatModel getIconView(BoatMesh boatType, Color primaryColour) { + Group boatAssets = new Group(); + MeshView hull = importFile(boatType.hullFile); + hull.setMaterial(new PhongMaterial(primaryColour)); + MeshView mast = importFile(boatType.mastFile); + mast.setMaterial(new PhongMaterial(primaryColour)); + MeshView sail = importFile(boatType.hullFile); + sail.setMaterial(new PhongMaterial(Color.WHITE)); + boatAssets.getChildren().addAll(hull, mast, sail); + boatAssets.getTransforms().addAll( + new Scale(20, 20, 20), + new Rotate(90, new Point3D(0,0,1)), + new Rotate(90, new Point3D(0, 1, 0)) + ); + return new BoatModel(boatAssets, null, boatType); + } + + public BoatModel getRotatingView(BoatMesh boatType, Color primaryColour) { + Group boatAssets = new Group(); + MeshView hull = importFile(boatType.hullFile); + hull.setMaterial(new PhongMaterial(primaryColour)); + MeshView mast = importFile(boatType.mastFile); + mast.setMaterial(new PhongMaterial(primaryColour)); + MeshView sail = importFile(boatType.hullFile); + sail.setMaterial(new PhongMaterial(Color.WHITE)); + boatAssets.getChildren().addAll(hull, mast, sail); + boatAssets.getTransforms().addAll( + new Scale(40, 40, 40), + new Rotate(90, new Point3D(0,0,1)), + new Rotate(90, new Point3D(0, 1, 0)), + new Rotate(0, new Point3D(1,1,1)) + ); + // TODO: 7/09/17 This seems like it will never be garbage claimed. Might have to call BoatModel.stopAnimation(); + return new BoatModel(boatAssets, new AnimationTimer() { + + private double rotation = 0; + private final Group group = boatAssets; + + @Override + public void handle(long now) { + rotation += 0.5; + ((Rotate) group.getTransforms().get(3)).setAngle(rotation); + } + }, boatType); + } + + public BoatModel getGameView(BoatMesh boatType, Color primaryColour) { + BoatModel model = getIconView(boatType, primaryColour); + model.getAssets().getTransforms().setAll( + new Scale(20, 20, 20), + new Rotate(90, new Point3D(0,0,1)) + ); + return model; + } + + private MeshView importFile(String fileName) { + StlMeshImporter importer = new StlMeshImporter(); + importer.read(test3d.class.getResource("/meshes/" + fileName).toString()); + return new MeshView(importer.getImport()); + } +} diff --git a/src/main/java/seng302/visualiser/test3d.java b/src/main/java/seng302/visualiser/test3d.java index 8c0907af..937c9e1c 100644 --- a/src/main/java/seng302/visualiser/test3d.java +++ b/src/main/java/seng302/visualiser/test3d.java @@ -40,17 +40,17 @@ public class test3d extends Application { primaryStage.show(); StlMeshImporter importer = new StlMeshImporter(); - importer.read(test3d.class.getResource("/meshes/high_poly_boat.stl").toString()); + importer.read(test3d.class.getResource("/meshes/dinghy_hull.stl").toString()); MeshView boat = new MeshView(importer.getImport()); boat.setMaterial(new PhongMaterial(Color.GREENYELLOW)); importer = new StlMeshImporter(); - importer.read(getClass().getResource("/meshes/boat-mast.stl").toString()); + importer.read(getClass().getResource("/meshes/dinghy_mast.stl").toString()); MeshView mast = new MeshView(importer.getImport()); mast.setMaterial(new PhongMaterial(Color.GREENYELLOW)); importer = new StlMeshImporter(); - importer.read(getClass().getResource("/meshes/sail_centered.stl").toString()); + importer.read(getClass().getResource("/meshes/dinghy_sail.stl").toString()); MeshView sail = new MeshView(importer.getImport()); sail.setMaterial(new PhongMaterial(Color.LIGHTGREY)); @@ -97,6 +97,6 @@ public class test3d extends Application { } }; - animationTimer.start(); +// animationTimer.start(); } } diff --git a/src/main/resources/meshes/boat-hull.stl b/src/main/resources/meshes/dinghy_hull.stl similarity index 100% rename from src/main/resources/meshes/boat-hull.stl rename to src/main/resources/meshes/dinghy_hull.stl diff --git a/src/main/resources/meshes/boat-mast.stl b/src/main/resources/meshes/dinghy_mast.stl similarity index 100% rename from src/main/resources/meshes/boat-mast.stl rename to src/main/resources/meshes/dinghy_mast.stl diff --git a/src/main/resources/meshes/boat-sail.stl b/src/main/resources/meshes/dinghy_sail.stl similarity index 100% rename from src/main/resources/meshes/boat-sail.stl rename to src/main/resources/meshes/dinghy_sail.stl diff --git a/src/main/resources/meshes/high_poly_boat.stl b/src/main/resources/meshes/high_poly_boat.stl deleted file mode 100644 index 0659488a..00000000 Binary files a/src/main/resources/meshes/high_poly_boat.stl and /dev/null differ diff --git a/src/main/resources/meshes/hollow_simple_boat.dae b/src/main/resources/meshes/hollow_simple_boat.dae deleted file mode 100644 index a7e0a694..00000000 --- a/src/main/resources/meshes/hollow_simple_boat.dae +++ /dev/null @@ -1,255 +0,0 @@ - - - - - Blender User - Blender 2.78.0 commit date:2016-09-26, commit time:12:42, hash:4bb1e22 - - 2017-09-05T21:06:16 - 2017-09-05T21:06:16 - - Z_UP - - - - - - - 49.13434 - 1.777778 - 0.1 - 100 - - - - - - 0 - 0 - 0 - - - - - - - - 49.13434 - 1.777778 - 0.1 - 100 - - - - - - 0 - 0 - 0 - - - - - - - - - 1 1 1 - 1 - 0 - 0.00111109 - - - - - 0.000999987 - 1 - 0.1 - 0.1 - 1 - 1 - 1 - 2 - 0 - 1 - 1 - 1 - 1 - 1 - 0 - 2880 - 2 - 30.002 - 1.000799 - 0.04999995 - 29.99998 - 1 - 2 - 0 - 0 - 1 - 1 - 1 - 1 - 8192 - 1 - 1 - 0 - 1 - 1 - 1 - 3 - 0 - 0 - 0 - 0 - 0 - 1 - 1 - 1 - 3 - 0.15 - 75 - 1 - 1 - 0 - 1 - 1 - 0 - - - - - - - 1 1 1 - 1 - 0 - 0.00111109 - - - - - 9.99987e-4 - 1 - 0.1 - 0.1 - 1 - 1 - 1 - 2 - 0 - 1 - 1 - 1 - 1 - 1 - 0 - 2880 - 2 - 30.002 - 1.000799 - 0.04999995 - 29.99998 - 1 - 2 - 0 - 0 - 1 - 1 - 1 - 1 - 8192 - 1 - 1 - 0 - 1 - 1 - 1 - 3 - 0 - 0 - 0 - 0 - 0 - 1 - 1 - 1 - 3 - 0.15 - 75 - 1 - 1 - 0 - 1 - 1 - 0 - - - - - - - - - - -1 -1 0 1 -1 0 -1 1 0 1 1 0 0 -2.5 0 0 2.5 0 -1 1 0.2928113 -1 -1 0.2928113 0 -2.8 0.5 1 -1 0.2928113 1 1 0.2928113 0 2.8 0.5 1 0 0.2928113 -1 0 0.2928113 -0.5 0 0.2928113 0.5 0 0.2928113 0.5 0.5 0.29281 -0.5 0.5 0.29281 0.2 0 0.5528544 0.2 0.2 0.5528531 -0.2 0.2 0.5528531 -0.2 0 0.5528544 0.5 -0.5 0.2928113 -0.5 -0.5 0.2928113 0.2 -0.2 0.5528544 -0.2 -0.2 0.5528544 0.1999999 1.94579e-6 1.120592 0.1999999 0.2000018 1.12059 -0.2 0.2000018 1.12059 -0.2 1.94579e-6 1.120592 0.1999999 -0.199998 1.120592 -0.2 -0.199998 1.120592 0.1999999 5.95355e-6 2.241243 0.1999999 0.200006 2.241241 -0.2 0.200006 2.241241 -0.2 5.95355e-6 2.241243 0.1999999 -0.199994 2.241243 -0.2 -0.199994 2.241243 -0.04999995 0.2000018 1.12059 0.04999995 0.2000018 1.12059 0.04999995 0.200006 2.241241 -0.04999995 0.200006 2.241241 0.04999995 1.611272 1.120585 -0.04999995 1.611272 1.120585 - - - - - - - - - - 0 0 -1 0 0 1 0.8207915 -0.489824 -0.2938944 -0.8320503 -0.5547002 0 1 0 0 -1 0 0 0.8320503 0.5547002 0 -0.8207915 0.489824 -0.2938944 1 0 1.83726e-7 -0.6549915 2.02677e-6 0.7556363 0.6549915 2.02677e-6 0.7556364 0.6549926 0 0.7556353 0 0.6549926 0.7556354 0 -0.6549926 0.7556354 -0.6549926 0 0.7556353 -1 -1.31233e-7 0 1 0 1.83726e-7 0 -1 3.47767e-6 0 1 -3.33332e-6 0 9.53674e-6 1 0 -1 3.59016e-6 0 1 -3.63448e-6 1 0 0 0 3.54772e-6 1 0 -3.54772e-6 -1 1 0 0 0 0.6218634 0.7831258 0 -2.6226e-6 1 0 0.1143498 0.9934406 2.6226e-6 0 1 0 -0.1143498 0.9934406 0.8320503 -0.5547002 0 -0.8207915 -0.489824 -0.2938944 0.8207915 0.489824 -0.2938944 -0.8320503 0.5547002 0 1 0 1.83726e-7 -0.6549926 4.84173e-6 0.7556353 0.6549926 4.87927e-6 0.7556353 0.6549925 0 0.7556354 0 0.6549926 0.7556353 0 -0.6549926 0.7556353 -0.6549926 0 0.7556354 -1 0 0 -1 0 0 1 0 1.83726e-7 0 -1 3.54329e-6 0 1 -3.29723e-6 0 1 -3.28083e-6 0 1 -3.32457e-6 0 9.53674e-6 1 0 9.53674e-6 1 0 -1 3.55692e-6 0 1 -3.6788e-6 0 1 -3.65665e-6 0 1 -3.63448e-6 0 1 -3.6788e-6 0 2.6226e-6 1 0 3.54772e-6 1 0 -3.54772e-6 -1 -2.6226e-6 0 1 - - - - - - - - - - - - - - - 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 -

2 0 1 0 0 0 23 1 13 1 7 1 4 2 9 2 8 2 4 3 7 3 0 3 3 4 12 4 1 4 0 5 13 5 2 5 5 6 10 6 3 6 5 7 6 7 11 7 18 8 30 8 24 8 14 9 20 9 17 9 19 10 15 10 16 10 24 11 15 11 18 11 20 12 16 12 17 12 24 13 23 13 22 13 14 14 25 14 21 14 29 5 34 5 28 5 21 15 28 15 20 15 27 16 18 16 19 16 30 17 25 17 24 17 31 5 21 5 25 5 39 18 27 18 19 18 40 19 35 19 32 19 37 5 29 5 31 5 36 20 31 20 30 20 40 21 33 21 39 21 33 22 26 22 27 22 26 4 36 4 30 4 39 23 43 23 38 23 38 24 42 24 39 24 38 5 41 5 43 5 40 25 39 25 42 25 41 26 42 26 43 26 4 0 0 0 1 0 2 0 5 0 3 0 3 0 1 0 2 0 10 27 17 27 16 27 8 28 9 28 7 28 13 29 17 29 6 29 11 30 6 30 10 30 7 1 22 1 23 1 4 31 1 31 9 31 4 32 8 32 7 32 9 4 1 4 12 4 3 4 10 4 12 4 6 5 2 5 13 5 0 5 7 5 13 5 5 33 11 33 10 33 5 34 2 34 6 34 18 35 26 35 30 35 14 36 21 36 20 36 19 37 18 37 15 37 24 38 22 38 15 38 20 39 19 39 16 39 24 40 25 40 23 40 14 41 23 41 25 41 29 42 35 42 34 42 12 1 22 1 9 1 21 43 29 43 28 43 27 44 26 44 18 44 30 45 31 45 25 45 31 5 29 5 21 5 19 46 20 46 38 46 38 47 39 47 19 47 20 48 28 48 38 48 36 1 32 1 35 1 40 49 41 49 35 49 32 50 33 50 40 50 35 1 37 1 36 1 41 50 34 50 35 50 37 5 35 5 29 5 36 51 37 51 31 51 38 52 28 52 34 52 41 53 40 53 39 53 38 54 34 54 41 54 33 55 27 55 39 55 12 56 16 56 15 56 33 4 32 4 26 4 26 4 32 4 36 4 39 57 42 57 43 57 38 58 43 58 42 58 41 26 40 26 42 26 23 1 14 1 13 1 10 27 6 27 17 27 13 56 14 56 17 56 7 1 9 1 22 1 12 1 15 1 22 1 12 59 10 59 16 59

-
-
-
-
- - - - - 0.6859207 -0.3240135 0.6515582 7.481132 0.7276763 0.3054208 -0.6141704 -6.50764 0 0.8953956 0.4452714 5.343665 0 0 0 1 - - - - -0.2908646 -0.7711008 0.5663932 4.076245 0.9551712 -0.1998834 0.2183912 1.005454 -0.05518906 0.6045247 0.7946723 5.903862 0 0 0 1 - - - - 0.6859207 -0.3240134 0.6515582 7.481132 0.7276763 0.3054208 -0.6141704 -6.50764 -4.01133e-9 0.8953956 0.4452714 5.343665 0 0 0 1 - - - - -0.2908646 -0.7711008 0.5663933 4.076245 0.9551712 -0.1998833 0.2183912 1.005454 -0.05518906 0.6045247 0.7946723 5.903862 0 0 0 1 - - - - 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 - - - - - - - -
\ No newline at end of file diff --git a/src/main/resources/meshes/hollow_simple_boat.stl b/src/main/resources/meshes/hollow_simple_boat.stl deleted file mode 100644 index b8d0b608..00000000 Binary files a/src/main/resources/meshes/hollow_simple_boat.stl and /dev/null differ diff --git a/src/main/resources/meshes/sail_centered.stl b/src/main/resources/meshes/sail_centered.stl deleted file mode 100644 index 948ad9e7..00000000 Binary files a/src/main/resources/meshes/sail_centered.stl and /dev/null differ