Guest User

Untitled

a guest
Mar 29th, 2015
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.54 KB | None | 0 0
  1.  
  2. package mygame;
  3.  
  4. import com.jme3.app.state.AbstractAppState;
  5. import com.jme3.asset.AssetManager;
  6. import com.jme3.input.ChaseCamera;
  7. import com.jme3.material.Material;
  8. import com.jme3.material.RenderState.BlendMode;
  9. import com.jme3.material.RenderState.FaceCullMode;
  10. import com.jme3.math.ColorRGBA;
  11. import com.jme3.math.FastMath;
  12. import com.jme3.math.Vector3f;
  13. import com.jme3.renderer.queue.RenderQueue.Bucket;
  14. import com.jme3.scene.Geometry;
  15. import com.jme3.scene.Mesh;
  16. import com.jme3.scene.Node;
  17. import com.jme3.scene.Spatial;
  18. import com.jme3.scene.VertexBuffer;
  19. import com.jme3.scene.shape.Quad;
  20. import com.jme3.util.BufferUtils;
  21.  
  22.  
  23. public class Canvas extends AbstractAppState
  24. {
  25.  
  26.     private static AssetManager assetManager;
  27.     private static Node sceneNode, camNode;
  28.     public static Node n;
  29.     private static ChaseCamera view;
  30.     private static Quad xPosFace, yPosFace, zPosFace,xNegFace, yNegFace, zNegFace;
  31.    
  32.     //dark colours used for positive axis
  33.     private static ColorRGBA redD = new ColorRGBA(0.69f, 0.05f, 0.05f, 0);
  34.     private static ColorRGBA greenD = new ColorRGBA(0.05f, 0.69f, 0.05f, 0);
  35.     private static ColorRGBA blueD = new ColorRGBA(0.05f, 0.05f, 0.69f, 0);
  36.     //light colours used for negative axis
  37.     private static ColorRGBA redL = new ColorRGBA(0.98f, 0.55f, 0.55f, 0);
  38.     private static ColorRGBA greenL = new ColorRGBA(0.55f, 0.98f, 0.55f, 0);
  39.     private static ColorRGBA blueL = new ColorRGBA(0.55f, 0.55f, 0.98f, 0);
  40.     //dark alpha colours used for positive plaes
  41.     private static ColorRGBA redDA = new ColorRGBA(0.69f, 0.05f, 0.05f, 0.5f);
  42.     private static ColorRGBA greenDA = new ColorRGBA(0.05f, 0.69f, 0.05f, 0.5f);
  43.     private static ColorRGBA blueDA = new ColorRGBA(0.05f, 0.05f, 0.69f, 0.5f);
  44.     //light alpha colours used for negative planes
  45.     private static ColorRGBA redLA = new ColorRGBA(0.98f, 0.55f, 0.55f, 0.8f);
  46.     private static ColorRGBA greenLA = new ColorRGBA(0.55f, 0.98f, 0.55f, 0.8f);
  47.     private static ColorRGBA blueLA = new ColorRGBA(0.55f, 0.55f, 0.98f, 0.8f);
  48.    
  49.     private static int planeID = 0; //used to name created sketchs planes with identifying number
  50.    
  51.     /**
  52.      * sets default Node and assetmanager
  53.      * @param a
  54.      * @param b
  55.      */
  56.     public static void setRootAndManager(AssetManager a, Node b)
  57.     {
  58.         assetManager = a;
  59.         sceneNode = b;
  60.     }
  61.  
  62.     /**
  63.      * creates a line with a given colour and name in the given direction
  64.      *
  65.      * @param name
  66.      * @param lineVerticies
  67.      * @param lineColor
  68.      */
  69.     private static void plotLine(String name, Vector3f[] lineVerticies, ColorRGBA lineColor)
  70.     {
  71.         Mesh m = new Mesh();
  72.         m.setMode(Mesh.Mode.Lines);
  73.  
  74.  
  75.         m.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(lineVerticies));
  76.  
  77.         short[] indexes = new short[2 * lineVerticies.length]; //Indexes are in pairs, from a vertex and to a vertex
  78.  
  79.         for (short i = 0; i < lineVerticies.length - 1; i++)
  80.         {
  81.             indexes[2 * i] = i;
  82.             indexes[2 * i + 1] = (short) (i + 1);
  83.         }
  84.  
  85.         m.setBuffer(VertexBuffer.Type.Index, 2, indexes);
  86.  
  87.         m.updateBound();
  88.         m.updateCounts();
  89.  
  90.         Geometry geo = new Geometry(name, m);
  91.         Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  92.         mat.setColor("Color", lineColor);
  93.         geo.setMaterial(mat);
  94.  
  95.         sceneNode.attachChild(geo);
  96.     }
  97.    
  98.     public static void createPlane(Node modelNode)
  99.     {
  100.         planeID++;
  101.         Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  102.         //allow double sided material on a plane
  103.         mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  104.         mat.setColor("Color",blueLA);
  105.         mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  106.         mat.getAdditionalRenderState().setAlphaTest(true);
  107.        
  108.         float w = 5f, h = 5f;
  109.         Quad face = new Quad(w, h);
  110.         Geometry plane = new Geometry("plane" + planeID, face);
  111.         plane.setQueueBucket(Bucket.Transparent);
  112.         plane.setMaterial(mat);
  113.         plane.setLocalTranslation(OrbitCamera.getCameraLocation());
  114.         plane.rotate(OrbitCamera.cam.getRotation()); //rotate to place on x axis
  115.        
  116.         modelNode.attachChild(plane);
  117.        
  118.     }
  119.     public static void createXYZPlanes()
  120.     {
  121.  
  122.         Material xmat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  123.         //allow double sided material on a plane
  124.         xmat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  125.         xmat.setColor("Color",redDA);
  126.         xmat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  127.         xmat.getAdditionalRenderState().setAlphaTest(true);
  128.        
  129.         Material ymat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  130.         //allow double sided material on a plane
  131.         ymat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  132.         ymat.setColor("Color",greenDA);
  133.         ymat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  134.         ymat.getAdditionalRenderState().setAlphaTest(true);
  135.        
  136.         Material zmat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  137.         //allow double sided material on a plane
  138.         zmat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  139.         zmat.setColor("Color",blueDA);
  140.         zmat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  141.         zmat.getAdditionalRenderState().setAlphaTest(true);
  142.  
  143.         float w = 5f, h = 5f;
  144.         xPosFace = new Quad(w, h);
  145.         Geometry xPosFaceGeom = new Geometry("xPosface", xPosFace);
  146.         xPosFaceGeom.setQueueBucket(Bucket.Transparent);
  147.         xPosFaceGeom.setMaterial(xmat);
  148.         xPosFaceGeom.rotate(FastMath.PI / 2, 0, 0); //rotate to place on x axis
  149.        
  150.         yPosFace = new Quad(w, h);
  151.         Geometry yPosFaceGeom = new Geometry("yPosface", yPosFace);
  152.         yPosFaceGeom.setQueueBucket(Bucket.Transparent);
  153.         yPosFaceGeom.setMaterial(ymat);
  154.        
  155.         zPosFace = new Quad(w, h);
  156.         Geometry zPosFaceGeom = new Geometry("zPosface", xPosFace);
  157.         zPosFaceGeom.setQueueBucket(Bucket.Transparent);
  158.         zPosFaceGeom.setMaterial(zmat);
  159.         zPosFaceGeom.rotate(0,-(FastMath.PI / 2),0); //rotate to place on z axis
  160.  
  161.         sceneNode.attachChild(xPosFaceGeom);
  162.         sceneNode.attachChild(yPosFaceGeom);
  163.         sceneNode.attachChild(zPosFaceGeom);
  164.     }
  165.  
  166.     public static void createXYZAxis()
  167.     {
  168.         //x axis lines  
  169.         Vector3f[] xPos =
  170.         {
  171.             new Vector3f(0, 0, 0), new Vector3f(1000, 0, 0)
  172.         };
  173.  
  174.         //x axis lines  
  175.         Vector3f[] xNeg =
  176.         {
  177.             new Vector3f(0, 0, 0), new Vector3f(-1000, 0, 0)
  178.         };
  179.  
  180.         // y axis lines
  181.         Vector3f[] yPos =
  182.         {
  183.             new Vector3f(0, 0, 0), new Vector3f(0, 1000, 0)
  184.         };
  185.  
  186.         // y axis lines
  187.         Vector3f[] yNeg =
  188.         {
  189.             new Vector3f(0, 0, 0), new Vector3f(0, -1000, 0)
  190.         };
  191.  
  192.         //z axis lines
  193.         Vector3f[] zPos =
  194.         {
  195.             new Vector3f(0, 0, 0), new Vector3f(0, 0, 1000)
  196.         };
  197.  
  198.         //z axis lines
  199.         Vector3f[] zNeg =
  200.         {
  201.             new Vector3f(0, 0, 0), new Vector3f(0, 0, -1000)
  202.         };
  203.  
  204.         //create  pos x axis as red
  205.         plotLine("xPos", xPos, redD);
  206.         //create pos y axis as green
  207.         plotLine("yPos", yPos, greenD);
  208.         //create pos z axis as blue
  209.         plotLine("zPos", zPos, blueD);
  210.  
  211.         //create neg x axis as red
  212.         plotLine("xNeg", xNeg, redL);
  213.         //create neg y axis as green
  214.         plotLine("yNeg", yNeg, greenL);
  215.         //create neg z axis as blue
  216.         plotLine("zNeg", zNeg, blueL);
  217.     }
  218.  
  219.     public static void createDragArrows(Node node)
  220.     {
  221.         float scale = 0.5f;
  222.          Spatial xarrowP = assetManager.loadModel("Models/arrow.j3o");
  223.          xarrowP.setName("moveX+");
  224.          xarrowP.setLocalScale(scale);
  225.          xarrowP.setLocalTranslation(0.5f, 0, 0);
  226.          xarrowP.rotate(0, -(FastMath.PI / 2), 0);
  227.          
  228.          Spatial yarrowP = assetManager.loadModel("Models/arrow.j3o");
  229.          yarrowP.rotate(FastMath.PI / 2,0, 0);
  230.          yarrowP.setName("moveY+");
  231.          yarrowP.setLocalScale(scale);
  232.          yarrowP.setLocalTranslation(0,0.5f, 0);
  233.          
  234.          Spatial zarrowP = assetManager.loadModel("Models/arrow.j3o");
  235.          zarrowP.rotate(0, -(FastMath.PI / 2)*2, 0);
  236.          zarrowP.setName("moveZ+");
  237.          zarrowP.setLocalScale(scale);
  238.          zarrowP.setLocalTranslation( 0, 0,0.5f);
  239.          
  240.          Spatial xarrowN = assetManager.loadModel("Models/arrow.j3o");
  241.          xarrowN.setName("moveX-");
  242.          xarrowN.setLocalScale(scale);
  243.          xarrowN.setLocalTranslation(-0.5f, 0, 0);
  244.          xarrowN.rotate(0, (FastMath.PI / 2), 0);
  245.          
  246.          Spatial yarrowN = assetManager.loadModel("Models/arrow.j3o");
  247.          yarrowN.rotate(-(FastMath.PI / 2),0, 0);
  248.          yarrowN.setName("moveY-");
  249.          yarrowN.setLocalScale(scale);
  250.          yarrowN.setLocalTranslation(0,-0.5f, 0);
  251.          
  252.          Spatial zarrowN = assetManager.loadModel("Models/arrow.j3o");
  253.          zarrowN.rotate(0, 0, 0);
  254.          zarrowN.setName("moveZ-");
  255.          zarrowN.setLocalScale(scale);
  256.          zarrowN.setLocalTranslation( 0, 0,-0.5f);
  257.          
  258.          Material xmatP = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  259.         //allow double sided material on a plane
  260.         xmatP.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  261.         xmatP.setColor("Color",redDA);
  262.         xmatP.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  263.         xmatP.getAdditionalRenderState().setAlphaTest(true);
  264.        
  265.          Material ymatP = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  266.         //allow double sided material on a plane
  267.         ymatP.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  268.         ymatP.setColor("Color",greenDA);
  269.         ymatP.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  270.         ymatP.getAdditionalRenderState().setAlphaTest(true);
  271.        
  272.         Material zmatP = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  273.         //allow double sided material on a plane
  274.         zmatP.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  275.         zmatP.setColor("Color",blueDA);
  276.         zmatP.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  277.         zmatP.getAdditionalRenderState().setAlphaTest(true);
  278.        
  279.         Material xmatN = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  280.         //allow double sided material on a plane
  281.         xmatN.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  282.         xmatN.setColor("Color",redLA);
  283.         xmatN.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  284.         xmatN.getAdditionalRenderState().setAlphaTest(true);
  285.        
  286.          Material ymatN = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  287.         //allow double sided material on a plane
  288.         ymatN.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  289.         ymatN.setColor("Color",greenLA);
  290.         ymatN.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  291.         ymatN.getAdditionalRenderState().setAlphaTest(true);
  292.        
  293.         Material zmatN = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  294.         //allow double sided material on a plane
  295.         zmatN.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  296.         zmatN.setColor("Color",blueLA);
  297.         zmatN.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  298.         zmatN.getAdditionalRenderState().setAlphaTest(true);
  299.        
  300.         xarrowP.setMaterial(xmatP);
  301.        
  302.          yarrowP.setMaterial(ymatP);
  303.          
  304.           zarrowP.setMaterial(zmatP);
  305.          
  306.           xarrowN.setMaterial(xmatN);
  307.        
  308.          yarrowN.setMaterial(ymatN);
  309.          
  310.           zarrowN.setMaterial(zmatN);
  311.        
  312.         n = new Node();
  313.         n.setName("arrows node");
  314.         n.setLocalTranslation(-0.35f, -0.35f, 1.5f);
  315.    //     n.setCullHint(Spatial.CullHint.Never);
  316.        
  317.        
  318.         n.setLocalScale(0.02f);
  319.         n.attachChild(xarrowP);
  320.         n.attachChild(yarrowP);
  321.         n.attachChild(zarrowP);
  322.          n.attachChild(xarrowN);
  323.         n.attachChild(yarrowN);
  324.         n.attachChild(zarrowN);
  325.         node.attachChild(n);
  326.     }
  327.  
  328. }
Advertisement
Add Comment
Please, Sign In to add comment