-1

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#.

8
  • 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. Commented 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. Commented Aug 25, 2012 at 0:16
  • 1
    Sorry, 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. Commented 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. Commented 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. Commented Aug 25, 2012 at 1:41

3 Answers 3

6

This shows the two-way interaction between Javascript and c#.

  1. Javascript calls a c# method
  2. 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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

I am sorry but how will the link help
@Belos It lists many standalone implementations right at the the top of the page. Rhino, V8, etc.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.