JSGraphics.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "JSGraphics.h"
  23. #include "JSVM.h"
  24. #include <Atomic/Graphics/Material.h>
  25. #include <Atomic/Graphics/Light.h>
  26. namespace Atomic
  27. {
  28. static int Light_SetShadowCascade(duk_context* ctx)
  29. {
  30. //CascadeParameters(float split1, float split2, float split3, float split4, float fadeStart, float biasAutoAdjust = 1.0f) :
  31. int numargs = duk_get_top(ctx);
  32. float split1;
  33. float split2;
  34. float split3;
  35. float split4;
  36. float fadeStart;
  37. float biasAutoAdjust = 1.0f;
  38. split1 = (float) duk_to_number(ctx, 0);
  39. split2 = (float) duk_to_number(ctx, 1);
  40. split3 = (float) duk_to_number(ctx, 2);
  41. split4 = (float) duk_to_number(ctx, 3);
  42. fadeStart = (float) duk_to_number(ctx, 4);
  43. if (numargs == 6)
  44. biasAutoAdjust = (float) duk_to_number(ctx, 5);
  45. CascadeParameters cparms(split1, split2, split3, split4, fadeStart, biasAutoAdjust);
  46. duk_push_this(ctx);
  47. Light* light = js_to_class_instance<Light>(ctx, -1, 0);
  48. light->SetShadowCascade(cparms);
  49. return 0;
  50. }
  51. static int Light_SetShadowCascadeParameter(duk_context* ctx)
  52. {
  53. int index = (int) duk_require_number(ctx, 0);
  54. float value = (float) duk_require_number(ctx, 1);
  55. duk_push_this(ctx);
  56. Light* light = js_to_class_instance<Light>(ctx, -1, 0);
  57. CascadeParameters parms = light->GetShadowCascade();
  58. switch (index)
  59. {
  60. case 0:
  61. parms.splits_[0] = value;
  62. break;
  63. case 1:
  64. parms.splits_[1] = value;
  65. break;
  66. case 2:
  67. parms.splits_[2] = value;
  68. break;
  69. case 3:
  70. parms.splits_[3] = value;
  71. break;
  72. case 4:
  73. parms.fadeStart_ = value;
  74. break;
  75. case 5:
  76. parms.biasAutoAdjust_ = value;
  77. break;
  78. }
  79. light->SetShadowCascade(parms);
  80. return 0;
  81. }
  82. static int Light_GetShadowCascade(duk_context* ctx)
  83. {
  84. duk_push_this(ctx);
  85. Light* light = js_to_class_instance<Light>(ctx, -1, 0);
  86. const CascadeParameters& parms = light->GetShadowCascade();
  87. duk_push_array(ctx);
  88. duk_push_number(ctx, parms.splits_[0]);
  89. duk_put_prop_index(ctx, -2, 0);
  90. duk_push_number(ctx, parms.splits_[1]);
  91. duk_put_prop_index(ctx, -2, 1);
  92. duk_push_number(ctx, parms.splits_[2]);
  93. duk_put_prop_index(ctx, -2, 2);
  94. duk_push_number(ctx, parms.splits_[3]);
  95. duk_put_prop_index(ctx, -2, 3);
  96. duk_push_number(ctx, parms.fadeStart_);
  97. duk_put_prop_index(ctx, -2, 4);
  98. duk_push_number(ctx, parms.biasAutoAdjust_);
  99. duk_put_prop_index(ctx, -2, 5);
  100. return 1;
  101. }
  102. static int Light_SetShadowBias(duk_context* ctx)
  103. {
  104. float constantBias = (float) duk_to_number(ctx, 0);
  105. float slopeScaledBias = (float) duk_to_number(ctx, 1);
  106. BiasParameters bparms(constantBias, slopeScaledBias);
  107. duk_push_this(ctx);
  108. Light* light = js_to_class_instance<Light>(ctx, -1, 0);
  109. light->SetShadowBias(bparms);
  110. return 0;
  111. }
  112. // Material
  113. static int Material_SetShaderParameter(duk_context* ctx)
  114. {
  115. duk_push_this(ctx);
  116. Material* material = js_to_class_instance<Material>(ctx, -1, 0);
  117. const char* name = duk_require_string(ctx, 0);
  118. String value = duk_require_string(ctx, 1);
  119. const Variant& v = material->GetShaderParameter(name);
  120. if (v == Variant::EMPTY)
  121. return 0;
  122. Variant vset;
  123. vset.FromString(v.GetType(), value);
  124. material->SetShaderParameter(name, vset);
  125. return 0;
  126. }
  127. static int Material_GetShaderParameters(duk_context* ctx)
  128. {
  129. duk_push_this(ctx);
  130. Material* material = js_to_class_instance<Material>(ctx, -1, 0);
  131. const HashMap<StringHash, MaterialShaderParameter>& params = material->GetShaderParameters();
  132. duk_push_array(ctx);
  133. unsigned j = 0;
  134. for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = params.Begin(); i != params.End(); ++i)
  135. {
  136. duk_push_object(ctx);
  137. duk_push_string(ctx, i->second_.name_.CString());
  138. duk_put_prop_string(ctx, -2, "name");
  139. js_push_variant(ctx, i->second_.value_);
  140. duk_put_prop_string(ctx, -2, "value");
  141. duk_push_string(ctx, i->second_.value_.ToString().CString());
  142. duk_put_prop_string(ctx, -2, "valueString");
  143. duk_push_string(ctx, i->second_.value_.GetTypeName().CString());
  144. duk_put_prop_string(ctx, -2, "typeName");
  145. duk_push_number(ctx, (double) i->second_.value_.GetType());
  146. duk_put_prop_string(ctx, -2, "type");
  147. duk_put_prop_index(ctx, -2, j++);
  148. }
  149. return 1;
  150. }
  151. static int Material_GetTextureUnitName(duk_context* ctx)
  152. {
  153. duk_push_string(ctx, Material::GetTextureUnitName((TextureUnit) ((int) duk_get_number(ctx, 0))).CString());
  154. return 1;
  155. }
  156. void jsapi_init_graphics(JSVM* vm)
  157. {
  158. duk_context* ctx = vm->GetJSContext();
  159. js_class_get_prototype(ctx, "Atomic", "Light");
  160. duk_push_c_function(ctx, Light_SetShadowCascade, DUK_VARARGS);
  161. duk_put_prop_string(ctx, -2, "setShadowCascade");
  162. duk_push_c_function(ctx, Light_SetShadowCascadeParameter, 2);
  163. duk_put_prop_string(ctx, -2, "setShadowCascadeParameter");
  164. duk_push_c_function(ctx, Light_GetShadowCascade, 0);
  165. duk_put_prop_string(ctx, -2, "getShadowCascade");
  166. duk_push_c_function(ctx, Light_SetShadowBias, 2);
  167. duk_put_prop_string(ctx, -2, "setShadowBias");
  168. duk_pop(ctx);
  169. js_class_get_prototype(ctx, "Atomic", "Material");
  170. duk_push_c_function(ctx, Material_GetShaderParameters, 0);
  171. duk_put_prop_string(ctx, -2, "getShaderParameters");
  172. duk_push_c_function(ctx, Material_SetShaderParameter, 2);
  173. duk_put_prop_string(ctx, -2, "setShaderParameter");
  174. duk_pop(ctx);
  175. // static methods
  176. js_class_get_constructor(ctx, "Atomic", "Material");
  177. duk_push_c_function(ctx, Material_GetTextureUnitName, 1);
  178. duk_put_prop_string(ctx, -2, "getTextureUnitName");
  179. duk_pop(ctx);
  180. }
  181. }