Added 3D window to GameView3D to begin adding assets to. Used it to refine all 3D assets implemented by ModelFactory and manually test that they work.

#implement #test
This commit is contained in:
cir27
2017-09-08 01:50:56 +12:00
parent eed5f56690
commit 0bf6dd9e6b
26 changed files with 690 additions and 886 deletions
@@ -0,0 +1,22 @@
package seng302.visualiser.fxObjects.assets_3D;
/**
* Enum for boat meshes. Enum values should be of the form :
* ENUM_VALUE (hull file, mast file, X offset of mast CoR from origin, sail file, X offset of sail CoR from origin)
* Files must be valid .stl files.
*/
public enum BoatMeshType {
DINGHY ("dinghy_hull.stl", "dinghy_mast.stl", 0, "dinghy_sail.stl", -1.36653);
final String hullFile, mastFile, sailFile;
final double mastOffset, sailOffset;
BoatMeshType(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;
}
}
@@ -0,0 +1,72 @@
package seng302.visualiser.fxObjects.assets_3D;
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;
/**
* Container class for a group of 3d objects representing a boat and it's animation.
*/
public class BoatModel extends Model {
private static final int HULL_INDEX = 0;
private static final int MAST_INDEX = 1;
private static final int SAIL_INDEX = 2;
private BoatMeshType 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.
*/
BoatModel(Group boatAssets, AnimationTimer animation, BoatMeshType meshType) {
super(boatAssets, animation);
this.meshType = meshType;
}
/**
* 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 = getMeshViewChild(MAST_INDEX);
MeshView sail = getMeshViewChild(SAIL_INDEX);
mast.getTransforms().setAll(
new Rotate(degrees, meshType.mastOffset, 0,0, new Point3D(0, 0, 1))
);
sail.getTransforms().setAll(
new Rotate(degrees, meshType.sailOffset, 0,0, new Point3D(0, 0, 1))
);
}
public void hideSail() {
getMeshViewChild(SAIL_INDEX).setVisible(false);
}
public void showSail() {
getMeshViewChild(SAIL_INDEX).setVisible(true);
}
/**
* Changes the colour of the model in this class.
* @param newColour the new colour for the boat.
*/
public void changeColour(Color newColour) {
changeColourChild(HULL_INDEX, newColour);
changeColourChild(SAIL_INDEX, 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);
}
}
@@ -0,0 +1,42 @@
package seng302.visualiser.fxObjects.assets_3D;
import javafx.animation.AnimationTimer;
import javafx.scene.Group;
/**
* Created by CJIRWIN on 7/09/2017.
*/
public class Model {
AnimationTimer animationTimer;
Group assets;
Model (Group assets, AnimationTimer animation) {
this.assets = assets;
this.animationTimer = animation;
if (animation != null) {
animation.start();
}
}
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 Group getAssets() {
return this.assets;
}
}
@@ -0,0 +1,114 @@
package seng302.visualiser.fxObjects.assets_3D;
import com.interactivemesh.jfx.importer.col.ColModelImporter;
import com.interactivemesh.jfx.importer.stl.StlMeshImporter;
import javafx.animation.AnimationTimer;
import javafx.geometry.Point3D;
import javafx.scene.AmbientLight;
import javafx.scene.Group;
import javafx.scene.Node;
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;
/**
* Factory class for creating 3D models of boats.
*/
public class ModelFactory {
public static BoatModel boatIconView(BoatMeshType boatType, Color primaryColour) {
Group boatAssets = getUnmodifiedBoatModel(boatType, primaryColour);
boatAssets.getTransforms().addAll(
new Scale(20, 20, 20),
new Rotate(90, new Point3D(0,0,1)),
new Rotate(90, new Point3D(0, 1, 0))
);
boatAssets.getChildren().add(new AmbientLight(new Color(1, 1, 1, 0.01)));
return new BoatModel(boatAssets, null, boatType);
}
public static BoatModel boatRotatingView(BoatMeshType boatType, Color primaryColour) {
Group boatAssets = getUnmodifiedBoatModel(boatType, primaryColour);
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 static BoatModel boatGameView(BoatMeshType boatType, Color primaryColour) {
Group boatAssets = getUnmodifiedBoatModel(boatType, primaryColour);
boatAssets.getTransforms().setAll(
new Scale(0.5, 0.5, 0.5)
);
return new BoatModel(boatAssets, null, boatType);
}
private static Group getUnmodifiedBoatModel(BoatMeshType 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.sailFile);
sail.setMaterial(new PhongMaterial(Color.WHITE));
boatAssets.getChildren().addAll(hull, mast, sail);
return boatAssets;
}
private static MeshView importFile(String fileName) {
StlMeshImporter importer = new StlMeshImporter();
importer.read(ModelFactory.class.getResource("/meshes/" + fileName));
return new MeshView(importer.getImport());
}
public static Model importModel(ModelType tokenType) {
ColModelImporter importer = new ColModelImporter();
importer.read(ModelFactory.class.getResource("/meshes/" + tokenType.filename));
Group model = new Group(importer.getImport());
AnimationTimer animationTimer;
switch (tokenType) {
case VELOCITY_COIN:
model.setRotationAxis(new Point3D(1,0,0));
model.setRotate(90);
model.setTranslateX(0.2);
model.setTranslateY(1);
model.getTransforms().add(new Rotate(0 ,new Point3D(0,1,0)));
animationTimer = new AnimationTimer() {
private double rotation = 0;
private Group group = model;
@Override
public void handle(long now) {
rotation += 0.5;
((Rotate) group.getTransforms().get(0)).setAngle(rotation);
}
};
break;
case FINISH_MARKER:
case PLAIN_MARKER:
case START_MARKER:
model.getTransforms().add(new Rotate(90, new Point3D(1, 0, 0)));
default:
animationTimer = null;
break;
}
return new Model(model, animationTimer);
}
}
@@ -0,0 +1,20 @@
package seng302.visualiser.fxObjects.assets_3D;
/**
* Enum for models. Values should be the name of the file and files should be .dae files with texture
* information included.
*/
public enum ModelType {
VELOCITY_COIN ("velocity_pickup.dae"),
FINISH_MARKER ("finish_marker.dae"),
START_MARKER ("start_marker.dae"),
PLAIN_MARKER ("plain_marker.dae");
final String filename;
ModelType(String filename) {
this.filename = filename;
}
}