2
0

hellovector.adoc 7.3 KB

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