JSGraphics.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "JSMath.h"
  23. #include "JSGraphics.h"
  24. #include <Atomic/Graphics/Material.h>
  25. #include <Atomic/Graphics/Light.h>
  26. #include <Atomic/Graphics/Octree.h>
  27. #include <Atomic/Graphics/Camera.h>
  28. #include <Atomic/Scene/Node.h>
  29. #include <Atomic/Script/ScriptRenderPathCommand.h>
  30. namespace Atomic
  31. {
  32. static int Light_SetShadowCascade(duk_context* ctx)
  33. {
  34. //CascadeParameters(float split1, float split2, float split3, float split4, float fadeStart, float biasAutoAdjust = 1.0f) :
  35. int numargs = duk_get_top(ctx);
  36. float split1;
  37. float split2;
  38. float split3;
  39. float split4;
  40. float fadeStart;
  41. float biasAutoAdjust = 1.0f;
  42. split1 = (float) duk_to_number(ctx, 0);
  43. split2 = (float) duk_to_number(ctx, 1);
  44. split3 = (float) duk_to_number(ctx, 2);
  45. split4 = (float) duk_to_number(ctx, 3);
  46. fadeStart = (float) duk_to_number(ctx, 4);
  47. if (numargs == 6)
  48. biasAutoAdjust = (float) duk_to_number(ctx, 5);
  49. CascadeParameters cparms(split1, split2, split3, split4, fadeStart, biasAutoAdjust);
  50. duk_push_this(ctx);
  51. Light* light = js_to_class_instance<Light>(ctx, -1, 0);
  52. light->SetShadowCascade(cparms);
  53. return 0;
  54. }
  55. static int Light_SetShadowCascadeParameter(duk_context* ctx)
  56. {
  57. int index = (int) duk_require_number(ctx, 0);
  58. float value = (float) duk_require_number(ctx, 1);
  59. duk_push_this(ctx);
  60. Light* light = js_to_class_instance<Light>(ctx, -1, 0);
  61. CascadeParameters parms = light->GetShadowCascade();
  62. switch (index)
  63. {
  64. case 0:
  65. parms.splits_[0] = value;
  66. break;
  67. case 1:
  68. parms.splits_[1] = value;
  69. break;
  70. case 2:
  71. parms.splits_[2] = value;
  72. break;
  73. case 3:
  74. parms.splits_[3] = value;
  75. break;
  76. case 4:
  77. parms.fadeStart_ = value;
  78. break;
  79. case 5:
  80. parms.biasAutoAdjust_ = value;
  81. break;
  82. }
  83. light->SetShadowCascade(parms);
  84. return 0;
  85. }
  86. static int Light_GetShadowCascade(duk_context* ctx)
  87. {
  88. duk_push_this(ctx);
  89. Light* light = js_to_class_instance<Light>(ctx, -1, 0);
  90. const CascadeParameters& parms = light->GetShadowCascade();
  91. duk_push_array(ctx);
  92. duk_push_number(ctx, parms.splits_[0]);
  93. duk_put_prop_index(ctx, -2, 0);
  94. duk_push_number(ctx, parms.splits_[1]);
  95. duk_put_prop_index(ctx, -2, 1);
  96. duk_push_number(ctx, parms.splits_[2]);
  97. duk_put_prop_index(ctx, -2, 2);
  98. duk_push_number(ctx, parms.splits_[3]);
  99. duk_put_prop_index(ctx, -2, 3);
  100. duk_push_number(ctx, parms.fadeStart_);
  101. duk_put_prop_index(ctx, -2, 4);
  102. duk_push_number(ctx, parms.biasAutoAdjust_);
  103. duk_put_prop_index(ctx, -2, 5);
  104. return 1;
  105. }
  106. static int Light_SetShadowBias(duk_context* ctx)
  107. {
  108. float constantBias = (float) duk_to_number(ctx, 0);
  109. float slopeScaledBias = (float) duk_to_number(ctx, 1);
  110. BiasParameters bparms(constantBias, slopeScaledBias);
  111. duk_push_this(ctx);
  112. Light* light = js_to_class_instance<Light>(ctx, -1, 0);
  113. light->SetShadowBias(bparms);
  114. return 0;
  115. }
  116. // Material
  117. static int Material_SetShaderParameter(duk_context* ctx)
  118. {
  119. duk_push_this(ctx);
  120. Material* material = js_to_class_instance<Material>(ctx, -1, 0);
  121. const char* name = duk_require_string(ctx, 0);
  122. String value = duk_require_string(ctx, 1);
  123. const Variant& v = material->GetShaderParameter(name);
  124. if (v == Variant::EMPTY)
  125. return 0;
  126. Variant vset;
  127. vset.FromString(v.GetType(), value);
  128. material->SetShaderParameter(name, vset);
  129. return 0;
  130. }
  131. static int Material_GetShaderParameters(duk_context* ctx)
  132. {
  133. duk_push_this(ctx);
  134. Material* material = js_to_class_instance<Material>(ctx, -1, 0);
  135. const HashMap<StringHash, MaterialShaderParameter>& params = material->GetShaderParameters();
  136. duk_push_array(ctx);
  137. unsigned j = 0;
  138. for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = params.Begin(); i != params.End(); ++i)
  139. {
  140. duk_push_object(ctx);
  141. duk_push_string(ctx, i->second_.name_.CString());
  142. duk_put_prop_string(ctx, -2, "name");
  143. js_push_variant(ctx, i->second_.value_);
  144. duk_put_prop_string(ctx, -2, "value");
  145. duk_push_string(ctx, i->second_.value_.ToString().CString());
  146. duk_put_prop_string(ctx, -2, "valueString");
  147. duk_push_string(ctx, i->second_.value_.GetTypeName().CString());
  148. duk_put_prop_string(ctx, -2, "typeName");
  149. duk_push_number(ctx, (double) i->second_.value_.GetType());
  150. duk_put_prop_string(ctx, -2, "type");
  151. duk_put_prop_index(ctx, -2, j++);
  152. }
  153. return 1;
  154. }
  155. static int Material_GetTextureUnitName(duk_context* ctx)
  156. {
  157. duk_push_string(ctx, Material::GetTextureUnitName((TextureUnit) ((int) duk_get_number(ctx, 0))).CString());
  158. return 1;
  159. }
  160. static int Camera_GetScreenRay(duk_context* ctx)
  161. {
  162. float x = (float) duk_to_number(ctx, 0);
  163. float y = (float) duk_to_number(ctx, 1);
  164. duk_push_this(ctx);
  165. Camera* camera = js_to_class_instance<Camera>(ctx, -1, 0);
  166. Ray ray = camera->GetScreenRay(x, y);
  167. duk_push_new_ray(ctx, ray);
  168. return 1;
  169. }
  170. // Octree Queries
  171. static duk_int_t DUK_MAGIC_RAYCAST = 1;
  172. static duk_int_t DUK_MAGIC_RAYCAST_SINGLE = 2;
  173. static void duk_push_rayqueryresult(duk_context* ctx, const RayQueryResult& result)
  174. {
  175. duk_push_object(ctx);
  176. duk_push_new_vector3(ctx, result.position_);
  177. duk_put_prop_string(ctx, -2, "position");
  178. duk_push_new_vector3(ctx, result.normal_);
  179. duk_put_prop_string(ctx, -2, "normal");
  180. duk_push_new_vector2(ctx, result.textureUV_);
  181. duk_put_prop_string(ctx, -2, "textureUV");
  182. duk_push_number(ctx, result.distance_);
  183. duk_put_prop_string(ctx, -2, "distance");
  184. js_push_class_object_instance(ctx, result.drawable_, "Drawable");
  185. duk_put_prop_string(ctx, -2, "drawable");
  186. js_push_class_object_instance(ctx, result.node_, "Node");
  187. duk_put_prop_string(ctx, -2, "node");
  188. duk_push_number(ctx, (duk_double_t) result.subObject_);
  189. duk_put_prop_string(ctx, -2, "subObject");
  190. }
  191. static int Octree_Raycast(duk_context* ctx)
  192. {
  193. bool single = duk_get_current_magic(ctx) == DUK_MAGIC_RAYCAST_SINGLE;
  194. duk_idx_t nargs = duk_get_top(ctx);
  195. // require at least the ray
  196. if (nargs < 1)
  197. {
  198. duk_push_undefined(ctx);
  199. return 1;
  200. }
  201. Ray ray;
  202. if (!duk_get_ray(ctx, 0, ray))
  203. {
  204. duk_push_undefined(ctx);
  205. return 1;
  206. }
  207. RayQueryLevel level = RAY_TRIANGLE;
  208. if (nargs > 1)
  209. {
  210. unsigned _level = (unsigned) duk_to_number(ctx, 1);
  211. if (_level > (unsigned) RAY_TRIANGLE_UV)
  212. {
  213. duk_push_undefined(ctx);
  214. return 1;
  215. }
  216. level = (RayQueryLevel) _level;
  217. }
  218. float maxDistance = M_INFINITY;
  219. if (nargs > 2)
  220. {
  221. maxDistance = (float) duk_to_number(ctx, 2);
  222. }
  223. unsigned char drawableFlags = DRAWABLE_ANY;
  224. if (nargs > 3)
  225. {
  226. drawableFlags = (unsigned char) duk_to_number(ctx, 3);
  227. }
  228. unsigned viewMask = DEFAULT_VIEWMASK;
  229. if (nargs > 4)
  230. {
  231. viewMask = (unsigned) duk_to_number(ctx, 4);
  232. }
  233. duk_push_this(ctx);
  234. Octree* octree = js_to_class_instance<Octree>(ctx, -1, 0);
  235. PODVector<RayQueryResult> result;
  236. RayOctreeQuery query(result, ray, level, maxDistance, drawableFlags, viewMask);
  237. single ? octree->RaycastSingle(query) : octree->Raycast(query);
  238. // handle case of nothing hit
  239. if (!result.Size())
  240. {
  241. if (single)
  242. duk_push_null(ctx);
  243. else
  244. duk_push_array(ctx);
  245. return 1;
  246. }
  247. else
  248. {
  249. if (single)
  250. {
  251. duk_push_rayqueryresult(ctx, result[0]);
  252. }
  253. else
  254. {
  255. duk_push_array(ctx);
  256. for (unsigned i = 0; i < result.Size(); i++)
  257. {
  258. duk_push_rayqueryresult(ctx, result[i]);
  259. duk_put_prop_index(ctx, -2, i);
  260. }
  261. }
  262. }
  263. return 1;
  264. }
  265. static int RenderPath_SetShaderParameter(duk_context* ctx)
  266. {
  267. const char* name = duk_require_string(ctx, 0);
  268. Variant value;
  269. js_to_variant(ctx, 1, value);
  270. duk_push_this(ctx);
  271. RenderPath* renderPath = js_to_class_instance<RenderPath>(ctx, -1, 0);
  272. renderPath->SetShaderParameter(name, value);
  273. return 0;
  274. }
  275. static int RenderPathCommand_SetShaderParameter(duk_context* ctx)
  276. {
  277. const char* name = duk_require_string(ctx, 0);
  278. Variant value;
  279. js_to_variant(ctx, 1, value);
  280. duk_push_this(ctx);
  281. ScriptRenderPathCommand* scriptRenderPathCommand = js_to_class_instance<ScriptRenderPathCommand>(ctx, -1, 0);
  282. scriptRenderPathCommand->renderPathCommand_.SetShaderParameter(name, value);
  283. return 0;
  284. }
  285. void jsapi_init_graphics(JSVM* vm)
  286. {
  287. duk_context* ctx = vm->GetJSContext();
  288. js_class_get_prototype(ctx, "Atomic", "Light");
  289. duk_push_c_function(ctx, Light_SetShadowCascade, DUK_VARARGS);
  290. duk_put_prop_string(ctx, -2, "setShadowCascade");
  291. duk_push_c_function(ctx, Light_SetShadowCascadeParameter, 2);
  292. duk_put_prop_string(ctx, -2, "setShadowCascadeParameter");
  293. duk_push_c_function(ctx, Light_GetShadowCascade, 0);
  294. duk_put_prop_string(ctx, -2, "getShadowCascade");
  295. duk_push_c_function(ctx, Light_SetShadowBias, 2);
  296. duk_put_prop_string(ctx, -2, "setShadowBias");
  297. duk_pop(ctx);
  298. js_class_get_prototype(ctx, "Atomic", "Material");
  299. duk_push_c_function(ctx, Material_GetShaderParameters, 0);
  300. duk_put_prop_string(ctx, -2, "getShaderParameters");
  301. duk_push_c_function(ctx, Material_SetShaderParameter, 2);
  302. duk_put_prop_string(ctx, -2, "setShaderParameter");
  303. duk_pop(ctx);
  304. js_class_get_prototype(ctx, "Atomic", "Octree");
  305. duk_push_c_function(ctx, Octree_Raycast, DUK_VARARGS);
  306. duk_set_magic(ctx, -1, (unsigned) DUK_MAGIC_RAYCAST);
  307. duk_put_prop_string(ctx, -2, "rayCast");
  308. duk_push_c_function(ctx, Octree_Raycast, DUK_VARARGS);
  309. duk_set_magic(ctx, -1, (unsigned) DUK_MAGIC_RAYCAST_SINGLE);
  310. duk_put_prop_string(ctx, -2, "rayCastSingle");
  311. duk_pop(ctx);
  312. js_class_get_prototype(ctx, "Atomic", "Camera");
  313. duk_push_c_function(ctx, Camera_GetScreenRay, 2);
  314. duk_put_prop_string(ctx, -2, "getScreenRay");
  315. duk_pop(ctx);
  316. js_class_get_prototype(ctx, "Atomic", "RenderPath");
  317. duk_push_c_function(ctx, RenderPath_SetShaderParameter, 2);
  318. duk_put_prop_string(ctx, -2, "setShaderParameter");
  319. duk_pop(ctx);
  320. js_class_get_prototype(ctx, "Atomic", "RenderPathCommand");
  321. duk_push_c_function(ctx, RenderPathCommand_SetShaderParameter, 2);
  322. duk_put_prop_string(ctx, -2, "setShaderParameter");
  323. duk_pop(ctx);
  324. // static methods
  325. js_class_get_constructor(ctx, "Atomic", "Material");
  326. duk_push_c_function(ctx, Material_GetTextureUnitName, 1);
  327. duk_put_prop_string(ctx, -2, "getTextureUnitName");
  328. duk_pop(ctx);
  329. }
  330. }