hellovector.adoc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. = hellovector
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :relfileprefix: ../../
  6. :imagesdir: ../..
  7. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  8. image:jme3/beginner/hellovectorsumm2.png[hellovectorsumm2.png,width="",height=""]
  9. HelloVectorSumm.java
  10. [source,java]
  11. ----
  12. package org.jmonkey.chapter2.hellovector;
  13. import com.jme3.app.SimpleApplication;
  14. import com.jme3.material.Material;
  15. import com.jme3.math.ColorRGBA;
  16. import com.jme3.math.Vector3f;
  17. import com.jme3.renderer.RenderManager;
  18. import com.jme3.scene.Geometry;
  19. import com.jme3.scene.Node;
  20. import com.jme3.scene.shape.Box;
  21. import org.jmonkey.utils.Debug;
  22. import org.jmonkey.utils.MaterialUtils;
  23. import org.jmonkey.utils.SpatialUtils;
  24. /**
  25. Example Vector Summ
  26. @author Alex Cham aka Jcrypto
  27. */
  28. public class HelloVectorSumm extends SimpleApplication
  29. {
  30. private Node vctrNode = SpatialUtils.makeNode("vectorNode");
  31. //
  32. private Vector3f vctrNodeLoc = new Vector3f(64.0f, 64.0f, 64.0f);
  33. private Vector3f camLocVctr = new Vector3f(512.0f, 64.0f, 0.0f);
  34. //
  35. private Vector3f vctrNodeSpatLoc = new Vector3f(64.0f, 128.0f, -32.0f);
  36. //
  37. private Vector3f vctrSumm = null;
  38. private Vector3f scale = new Vector3f(8, 8, 8);
  39. public static void main(String[] args)
  40. {
  41. HelloVectorSumm app = new HelloVectorSumm();
  42. //app.setShowSettings(false);
  43. app.start();
  44. }
  45. @Override
  46. public void simpleInitApp()
  47. {
  48. cam.setLocation(camLocVctr);
  49. cam.lookAt(Vector3f.ZERO, cam.getUp());
  50. flyCam.setMoveSpeed(100.0f);
  51. //
  52. Debug.showNodeAxes(assetManager, rootNode, 128);
  53. Debug.attachWireFrameDebugGrid(assetManager, rootNode, Vector3f.ZERO, 256, ColorRGBA.DarkGray);
  54. //
  55. Box box = new Box(1, 1, 1);
  56. //
  57. Material mat = MaterialUtils.makeMaterial(assetManager, "Common/MatDefs/Misc/Unshaded.j3md", ColorRGBA.Blue);
  58. Geometry geom = SpatialUtils.makeGeometry(vctrNodeSpatLoc, scale, box, mat, "box");
  59. vctrNode.attachChild(geom);
  60. vctrNode.setLocalTranslation(vctrNodeLoc);
  61. vctrSumm = vctrNodeLoc.add(vctrNodeSpatLoc);
  62. //
  63. Debug.showNodeAxes(assetManager, vctrNode, 4.0f);
  64. Debug.showVector3fArrow(assetManager, rootNode, vctrNodeLoc, ColorRGBA.Red, "vctrNodeLoc");
  65. Debug.showVector3fArrow(assetManager, vctrNode, vctrNodeSpatLoc, ColorRGBA.Green, "vctrNodeSpatLoc");
  66. Debug.showVector3fArrow(assetManager, rootNode, vctrSumm, ColorRGBA.Blue, "vctrSumm");
  67. //
  68. rootNode.attachChild(vctrNode);
  69. }
  70. @Override
  71. public void simpleUpdate(float tpf)
  72. {
  73. //n.move(tpf * 10, 0, 0);
  74. }
  75. @Override
  76. public void simpleRender(RenderManager rm)
  77. {
  78. //TODO: add render code
  79. }
  80. }
  81. ----
  82. [source,java]
  83. ----
  84. package org.jmonkey.utils;
  85. import com.jme3.asset.AssetManager;
  86. import com.jme3.material.Material;
  87. import com.jme3.math.ColorRGBA;
  88. /**
  89. * Example Vector Summ
  90. * @author Alex Cham aka Jcrypto
  91. */
  92. public class MaterialUtils
  93. {
  94. public MaterialUtils()
  95. {
  96. }
  97. //"Common/MatDefs/Misc/Unshaded.j3md"
  98. public static Material makeMaterial(AssetManager am, String name, ColorRGBA color)
  99. {
  100. Material mat = new Material(am, name);
  101. mat.setColor("Color", color);
  102. return mat;
  103. }
  104. }
  105. ----
  106. [source,java]
  107. ----
  108. package org.jmonkey.utils;
  109. import com.jme3.material.Material;
  110. import com.jme3.math.Vector3f;
  111. import com.jme3.scene.Geometry;
  112. import com.jme3.scene.Mesh;
  113. import com.jme3.scene.Node;
  114. /**
  115. * Example Vector Summ
  116. * @author Alex Cham aka Jcrypto
  117. */
  118. public class SpatialUtils
  119. {
  120. //
  121. public static Node makeNode(String name)
  122. {
  123. Node n = new Node(name);
  124. return n;
  125. }
  126. //
  127. public static Geometry makeGeometry(Mesh mesh, Material mat, String name)
  128. {
  129. Geometry geom = new Geometry(name, mesh);
  130. geom.setMaterial(mat);
  131. return geom;
  132. }
  133. //
  134. public static Geometry makeGeometry(Vector3f loc, Vector3f scl, Mesh mesh, Material mat, String name)
  135. {
  136. Geometry geom = new Geometry(name, mesh);
  137. geom.setMaterial(mat);
  138. geom.setLocalTranslation(loc);
  139. geom.setLocalScale(scl);
  140. return geom;
  141. }
  142. }
  143. ----
  144. Debug.java
  145. [source,java]
  146. ----
  147. package org.jmonkey.utils;
  148. import com.jme3.animation.AnimControl;
  149. import com.jme3.asset.AssetManager;
  150. import com.jme3.material.Material;
  151. import com.jme3.math.ColorRGBA;
  152. import com.jme3.math.Vector3f;
  153. import com.jme3.scene.Geometry;
  154. import com.jme3.scene.Node;
  155. import com.jme3.scene.debug.Arrow;
  156. import com.jme3.scene.debug.Grid;
  157. import com.jme3.scene.debug.SkeletonDebugger;
  158. import com.jme3.scene.shape.Line;
  159. import static org.jmonkey.utils.SpatialUtils.makeGeometry;
  160. /**
  161. Example Vector Summ
  162. @author Alex Cham aka Jcrypto
  163. */
  164. public class Debug
  165. {
  166. public static void showNodeAxes(AssetManager am, Node n, float axisLen)
  167. {
  168. Vector3f v = new Vector3f(axisLen, 0, 0);
  169. Arrow a = new Arrow(v);
  170. Material mat = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
  171. mat.setColor("Color", ColorRGBA.Red);
  172. Geometry geom = new Geometry(n.getName() + "XAxis", a);
  173. geom.setMaterial(mat);
  174. n.attachChild(geom);
  175. //
  176. v = new Vector3f(0, axisLen, 0);
  177. a = new Arrow(v);
  178. mat = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
  179. mat.setColor("Color", ColorRGBA.Green);
  180. geom = new Geometry(n.getName() + "YAxis", a);
  181. geom.setMaterial(mat);
  182. n.attachChild(geom);
  183. //
  184. v = new Vector3f(0, 0, axisLen);
  185. a = new Arrow(v);
  186. mat = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
  187. mat.setColor("Color", ColorRGBA.Blue);
  188. geom = new Geometry(n.getName() + "ZAxis", a);
  189. geom.setMaterial(mat);
  190. n.attachChild(geom);
  191. }
  192. //
  193. public static void showVector3fArrow(AssetManager am, Node n, Vector3f v, ColorRGBA color, String name)
  194. {
  195. Arrow a = new Arrow(v);
  196. Material mat = MaterialUtils.makeMaterial(am, "Common/MatDefs/Misc/Unshaded.j3md", color);
  197. Geometry geom = makeGeometry(a, mat, name);
  198. n.attachChild(geom);
  199. }
  200. public static void showVector3fLine(AssetManager am, Node n, Vector3f v, ColorRGBA color, String name)
  201. {
  202. Line l = new Line(v.subtract(v), v);
  203. Material mat = MaterialUtils.makeMaterial(am, "Common/MatDefs/Misc/Unshaded.j3md", color);
  204. Geometry geom = makeGeometry(l, mat, name);
  205. n.attachChild(geom);
  206. }
  207. //Skeleton Debugger
  208. public static void attachSkeleton(AssetManager am, Node player, AnimControl control)
  209. {
  210. SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton", control.getSkeleton());
  211. Material mat2 = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
  212. mat2.setColor("Color", ColorRGBA.Yellow);
  213. mat2.getAdditionalRenderState().setDepthTest(false);
  214. skeletonDebug.setMaterial(mat2);
  215. player.attachChild(skeletonDebug);
  216. }
  217. ///
  218. public static void attachWireFrameDebugGrid(AssetManager assetManager, Node n, Vector3f pos, Integer size, ColorRGBA color)
  219. {
  220. Geometry g = new Geometry("wireFrameDebugGrid", new Grid(size, size, 1.0f));//1WU
  221. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  222. mat.getAdditionalRenderState().setWireframe(true);
  223. mat.setColor("Color", color);
  224. g.setMaterial(mat);
  225. g.center().move(pos);
  226. n.attachChild(g);
  227. }
  228. }
  229. ----