Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mygame;
- import com.jme3.app.state.AbstractAppState;
- import com.jme3.asset.AssetManager;
- import com.jme3.input.ChaseCamera;
- import com.jme3.material.Material;
- import com.jme3.material.RenderState.BlendMode;
- import com.jme3.material.RenderState.FaceCullMode;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.FastMath;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.queue.RenderQueue.Bucket;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Mesh;
- import com.jme3.scene.Node;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.VertexBuffer;
- import com.jme3.scene.shape.Quad;
- import com.jme3.util.BufferUtils;
- public class Canvas extends AbstractAppState
- {
- private static AssetManager assetManager;
- private static Node sceneNode, camNode;
- public static Node n;
- private static ChaseCamera view;
- private static Quad xPosFace, yPosFace, zPosFace,xNegFace, yNegFace, zNegFace;
- //dark colours used for positive axis
- private static ColorRGBA redD = new ColorRGBA(0.69f, 0.05f, 0.05f, 0);
- private static ColorRGBA greenD = new ColorRGBA(0.05f, 0.69f, 0.05f, 0);
- private static ColorRGBA blueD = new ColorRGBA(0.05f, 0.05f, 0.69f, 0);
- //light colours used for negative axis
- private static ColorRGBA redL = new ColorRGBA(0.98f, 0.55f, 0.55f, 0);
- private static ColorRGBA greenL = new ColorRGBA(0.55f, 0.98f, 0.55f, 0);
- private static ColorRGBA blueL = new ColorRGBA(0.55f, 0.55f, 0.98f, 0);
- //dark alpha colours used for positive plaes
- private static ColorRGBA redDA = new ColorRGBA(0.69f, 0.05f, 0.05f, 0.5f);
- private static ColorRGBA greenDA = new ColorRGBA(0.05f, 0.69f, 0.05f, 0.5f);
- private static ColorRGBA blueDA = new ColorRGBA(0.05f, 0.05f, 0.69f, 0.5f);
- //light alpha colours used for negative planes
- private static ColorRGBA redLA = new ColorRGBA(0.98f, 0.55f, 0.55f, 0.8f);
- private static ColorRGBA greenLA = new ColorRGBA(0.55f, 0.98f, 0.55f, 0.8f);
- private static ColorRGBA blueLA = new ColorRGBA(0.55f, 0.55f, 0.98f, 0.8f);
- private static int planeID = 0; //used to name created sketchs planes with identifying number
- /**
- * sets default Node and assetmanager
- * @param a
- * @param b
- */
- public static void setRootAndManager(AssetManager a, Node b)
- {
- assetManager = a;
- sceneNode = b;
- }
- /**
- * creates a line with a given colour and name in the given direction
- *
- * @param name
- * @param lineVerticies
- * @param lineColor
- */
- private static void plotLine(String name, Vector3f[] lineVerticies, ColorRGBA lineColor)
- {
- Mesh m = new Mesh();
- m.setMode(Mesh.Mode.Lines);
- m.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(lineVerticies));
- short[] indexes = new short[2 * lineVerticies.length]; //Indexes are in pairs, from a vertex and to a vertex
- for (short i = 0; i < lineVerticies.length - 1; i++)
- {
- indexes[2 * i] = i;
- indexes[2 * i + 1] = (short) (i + 1);
- }
- m.setBuffer(VertexBuffer.Type.Index, 2, indexes);
- m.updateBound();
- m.updateCounts();
- Geometry geo = new Geometry(name, m);
- Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- mat.setColor("Color", lineColor);
- geo.setMaterial(mat);
- sceneNode.attachChild(geo);
- }
- public static void createPlane(Node modelNode)
- {
- planeID++;
- Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- //allow double sided material on a plane
- mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
- mat.setColor("Color",blueLA);
- mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
- mat.getAdditionalRenderState().setAlphaTest(true);
- float w = 5f, h = 5f;
- Quad face = new Quad(w, h);
- Geometry plane = new Geometry("plane" + planeID, face);
- plane.setQueueBucket(Bucket.Transparent);
- plane.setMaterial(mat);
- plane.setLocalTranslation(OrbitCamera.getCameraLocation());
- plane.rotate(OrbitCamera.cam.getRotation()); //rotate to place on x axis
- modelNode.attachChild(plane);
- }
- public static void createXYZPlanes()
- {
- Material xmat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- //allow double sided material on a plane
- xmat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
- xmat.setColor("Color",redDA);
- xmat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
- xmat.getAdditionalRenderState().setAlphaTest(true);
- Material ymat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- //allow double sided material on a plane
- ymat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
- ymat.setColor("Color",greenDA);
- ymat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
- ymat.getAdditionalRenderState().setAlphaTest(true);
- Material zmat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- //allow double sided material on a plane
- zmat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
- zmat.setColor("Color",blueDA);
- zmat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
- zmat.getAdditionalRenderState().setAlphaTest(true);
- float w = 5f, h = 5f;
- xPosFace = new Quad(w, h);
- Geometry xPosFaceGeom = new Geometry("xPosface", xPosFace);
- xPosFaceGeom.setQueueBucket(Bucket.Transparent);
- xPosFaceGeom.setMaterial(xmat);
- xPosFaceGeom.rotate(FastMath.PI / 2, 0, 0); //rotate to place on x axis
- yPosFace = new Quad(w, h);
- Geometry yPosFaceGeom = new Geometry("yPosface", yPosFace);
- yPosFaceGeom.setQueueBucket(Bucket.Transparent);
- yPosFaceGeom.setMaterial(ymat);
- zPosFace = new Quad(w, h);
- Geometry zPosFaceGeom = new Geometry("zPosface", xPosFace);
- zPosFaceGeom.setQueueBucket(Bucket.Transparent);
- zPosFaceGeom.setMaterial(zmat);
- zPosFaceGeom.rotate(0,-(FastMath.PI / 2),0); //rotate to place on z axis
- sceneNode.attachChild(xPosFaceGeom);
- sceneNode.attachChild(yPosFaceGeom);
- sceneNode.attachChild(zPosFaceGeom);
- }
- public static void createXYZAxis()
- {
- //x axis lines
- Vector3f[] xPos =
- {
- new Vector3f(0, 0, 0), new Vector3f(1000, 0, 0)
- };
- //x axis lines
- Vector3f[] xNeg =
- {
- new Vector3f(0, 0, 0), new Vector3f(-1000, 0, 0)
- };
- // y axis lines
- Vector3f[] yPos =
- {
- new Vector3f(0, 0, 0), new Vector3f(0, 1000, 0)
- };
- // y axis lines
- Vector3f[] yNeg =
- {
- new Vector3f(0, 0, 0), new Vector3f(0, -1000, 0)
- };
- //z axis lines
- Vector3f[] zPos =
- {
- new Vector3f(0, 0, 0), new Vector3f(0, 0, 1000)
- };
- //z axis lines
- Vector3f[] zNeg =
- {
- new Vector3f(0, 0, 0), new Vector3f(0, 0, -1000)
- };
- //create pos x axis as red
- plotLine("xPos", xPos, redD);
- //create pos y axis as green
- plotLine("yPos", yPos, greenD);
- //create pos z axis as blue
- plotLine("zPos", zPos, blueD);
- //create neg x axis as red
- plotLine("xNeg", xNeg, redL);
- //create neg y axis as green
- plotLine("yNeg", yNeg, greenL);
- //create neg z axis as blue
- plotLine("zNeg", zNeg, blueL);
- }
- public static void createDragArrows(Node node)
- {
- float scale = 0.5f;
- Spatial xarrowP = assetManager.loadModel("Models/arrow.j3o");
- xarrowP.setName("moveX+");
- xarrowP.setLocalScale(scale);
- xarrowP.setLocalTranslation(0.5f, 0, 0);
- xarrowP.rotate(0, -(FastMath.PI / 2), 0);
- Spatial yarrowP = assetManager.loadModel("Models/arrow.j3o");
- yarrowP.rotate(FastMath.PI / 2,0, 0);
- yarrowP.setName("moveY+");
- yarrowP.setLocalScale(scale);
- yarrowP.setLocalTranslation(0,0.5f, 0);
- Spatial zarrowP = assetManager.loadModel("Models/arrow.j3o");
- zarrowP.rotate(0, -(FastMath.PI / 2)*2, 0);
- zarrowP.setName("moveZ+");
- zarrowP.setLocalScale(scale);
- zarrowP.setLocalTranslation( 0, 0,0.5f);
- Spatial xarrowN = assetManager.loadModel("Models/arrow.j3o");
- xarrowN.setName("moveX-");
- xarrowN.setLocalScale(scale);
- xarrowN.setLocalTranslation(-0.5f, 0, 0);
- xarrowN.rotate(0, (FastMath.PI / 2), 0);
- Spatial yarrowN = assetManager.loadModel("Models/arrow.j3o");
- yarrowN.rotate(-(FastMath.PI / 2),0, 0);
- yarrowN.setName("moveY-");
- yarrowN.setLocalScale(scale);
- yarrowN.setLocalTranslation(0,-0.5f, 0);
- Spatial zarrowN = assetManager.loadModel("Models/arrow.j3o");
- zarrowN.rotate(0, 0, 0);
- zarrowN.setName("moveZ-");
- zarrowN.setLocalScale(scale);
- zarrowN.setLocalTranslation( 0, 0,-0.5f);
- Material xmatP = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- //allow double sided material on a plane
- xmatP.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
- xmatP.setColor("Color",redDA);
- xmatP.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
- xmatP.getAdditionalRenderState().setAlphaTest(true);
- Material ymatP = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- //allow double sided material on a plane
- ymatP.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
- ymatP.setColor("Color",greenDA);
- ymatP.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
- ymatP.getAdditionalRenderState().setAlphaTest(true);
- Material zmatP = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- //allow double sided material on a plane
- zmatP.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
- zmatP.setColor("Color",blueDA);
- zmatP.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
- zmatP.getAdditionalRenderState().setAlphaTest(true);
- Material xmatN = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- //allow double sided material on a plane
- xmatN.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
- xmatN.setColor("Color",redLA);
- xmatN.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
- xmatN.getAdditionalRenderState().setAlphaTest(true);
- Material ymatN = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- //allow double sided material on a plane
- ymatN.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
- ymatN.setColor("Color",greenLA);
- ymatN.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
- ymatN.getAdditionalRenderState().setAlphaTest(true);
- Material zmatN = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- //allow double sided material on a plane
- zmatN.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
- zmatN.setColor("Color",blueLA);
- zmatN.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
- zmatN.getAdditionalRenderState().setAlphaTest(true);
- xarrowP.setMaterial(xmatP);
- yarrowP.setMaterial(ymatP);
- zarrowP.setMaterial(zmatP);
- xarrowN.setMaterial(xmatN);
- yarrowN.setMaterial(ymatN);
- zarrowN.setMaterial(zmatN);
- n = new Node();
- n.setName("arrows node");
- n.setLocalTranslation(-0.35f, -0.35f, 1.5f);
- // n.setCullHint(Spatial.CullHint.Never);
- n.setLocalScale(0.02f);
- n.attachChild(xarrowP);
- n.attachChild(yarrowP);
- n.attachChild(zarrowP);
- n.attachChild(xarrowN);
- n.attachChild(yarrowN);
- n.attachChild(zarrowN);
- node.attachChild(n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment