I am writing a raytracer as a part of my complete 3d engine. I am planning on using javascript for the scripting language instead of writing my own. The question is how can I use it? Btw the raytracer and the UI are written in C#.
-
Does your ray tracer actually require scripting? Can you describe the nature of the scripts? It's possible there's a better technology to use for this scripting.John Saunders– John Saunders2012-08-25 00:03:38 +00:00Commented Aug 25, 2012 at 0:03
-
It will be much easier to create animations. Currently I am using xml and it is very lengthy and harder for animations. Javascript will be less verbose.Belos– Belos2012-08-25 00:16:10 +00:00Commented Aug 25, 2012 at 0:16
-
1Sorry, I guess I have an outdated concept of what a "ray tracer" is. I knew them as programs which helped render a 3D graphic by tracing the path taken by rays of light from the lighting sources. Such a thing would have had no use for scripting. In any case, I question whether it's necessary to jump all the way from XML to JavaScript. Perhaps a domain-specific language would be a good intermediate step. Such a thing could produce code that could be compiled to provide higher performance.John Saunders– John Saunders2012-08-25 00:37:17 +00:00Commented Aug 25, 2012 at 0:37
-
I want to use javascript as part of the UI not the raytracing library. Javascript will be an easy and fast way to write code.Belos– Belos2012-08-25 00:50:28 +00:00Commented Aug 25, 2012 at 0:50
-
@JohnSaunders but with an answer to this question, it would be richer and less work than a domain specific language from scratch. I'm not answering since I haven't done so since before .NET, but adding full support for a well-known scripting language in half a day through COM sure beats putting a few days into just getting the initial spec down on a custom language.Jon Hanna– Jon Hanna2012-08-25 01:41:58 +00:00Commented Aug 25, 2012 at 1:41
3 Answers
This shows the two-way interaction between Javascript and c#.
- Javascript calls a c# method
- C# gets the result of an expression in Javascript
-
Type scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));
dynamic obj = Activator.CreateInstance(scriptType, false);
obj.Language = "javascript";
obj.AddObject("MyClass",new JSAccessibleClass());
obj.Eval("MyClass.MsgBox('Hello World')"); //<--1
var result = obj.Eval("3+5"); //<--2
[ComVisible(true)]
public class JSAccessibleClass
{
public void MsgBox(string s)
{
MessageBox.Show(s);
}
}
Comments
There are many standalone implementations of JavaScript that you may be able to leverage for this purpose:
Here is a link to the stackoverflow info page on JavaScript which lists at the top many of these implementations: https://stackoverflow.com/tags/javascript/info
While it is possible to use JavaScript as scripting for .Net application it is not most popular choice. If you can pick other language - IronPython or LUA.Net may be choices you'll get better support. I listed several links in similar question recently: Write npc scripts to be executed by c# using class with npc methods.
Otherwise JavaScript.Net (part of .Net framework) or other implementations either explicitly .Net or with .Net bindings will work as scripting language.