MaterialScript.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package hide.tools;
  2. /*
  3. class RendererScript extends h3d.scene.Renderer {
  4. var callb : Dynamic;
  5. var hasError = false;
  6. public function new(callb:Dynamic) {
  7. super();
  8. this.callb = callb;
  9. }
  10. override function render() {
  11. if( hasError ) {
  12. callb();
  13. return;
  14. }
  15. try {
  16. callb();
  17. } catch( e : hscript.Expr.Error ) {
  18. hasError = true;
  19. hide.Ide.inst.error(hscript.Printer.errorToString(e));
  20. }
  21. }
  22. }
  23. class Properties extends hxd.impl.Properties {
  24. public var obj : Dynamic;
  25. var interp : Interp;
  26. public function new(interp) {
  27. this.interp = interp;
  28. }
  29. public function getFields( o : Dynamic ) {
  30. if( o == obj )
  31. return Reflect.fields(obj).concat(Lambda.array({ iterator : interp.variables.keys }));
  32. return Reflect.fields(o);
  33. }
  34. override function getField( o : Dynamic, f : String ) {
  35. if( o == obj && interp.variables.exists(f) )
  36. return interp.variables.get(f);
  37. return @:privateAccess interp.get(o, f);
  38. }
  39. override function setField( o : Dynamic, f : String, v : Dynamic ) {
  40. if( o == obj )
  41. throw "TODO";
  42. @:privateAccess interp.set(o, f, v);
  43. }
  44. }
  45. class MaterialScript extends h3d.mat.MaterialScript {
  46. var ide : hide.Ide;
  47. public var renderer : Properties;
  48. public function new() {
  49. super(); // name will be set by script itself
  50. ide = hide.Ide.inst;
  51. }
  52. function loadModule( path : String ) : Dynamic {
  53. var fullPath = ide.getPath(path);
  54. var script = try sys.io.File.getContent(fullPath) catch( e : Dynamic ) throw "File not found " + path;
  55. var parser = new hscript.Parser();
  56. parser.preprocesorValues.set("script", true);
  57. var decls = try parser.parseModule(script, path) catch( e : hscript.Expr.Error ) { onError(Std.string(e) + " line " + parser.line); return null; }
  58. var objs : Dynamic = {};
  59. for( d in decls )
  60. switch( d ) {
  61. case DClass(c):
  62. Reflect.setField(objs, c.name, makeClass.bind(c));
  63. default:
  64. }
  65. return objs;
  66. }
  67. function lookupShader( shader : hxsl.Shader, ?passName : String ) {
  68. var s = @:privateAccess shader.shader;
  69. var scene = hide.comp.Scene.getCurrent();
  70. for( m in scene.s3d.getMaterials() )
  71. for( p in m.getPasses() ) {
  72. if( passName != null && p.name != passName ) continue;
  73. for( ss in p.getShaders() )
  74. if( @:privateAccess ss.shader == s )
  75. return ss;
  76. }
  77. return shader;
  78. }
  79. function makeClass( c : hscript.Expr.ClassDecl, ?args : Array<Dynamic> ) {
  80. var interp = new Interp();
  81. var makeObj = null;
  82. if( c.extend != null )
  83. switch( c.extend ) {
  84. case CTPath(["h3d", "scene", "Renderer"], _):
  85. makeObj = function() return new RendererScript(interp.variables.get("render"));
  86. interp.shareEnum(hxsl.Output);
  87. interp.shareEnum(h3d.impl.Driver.Feature);
  88. interp.shareEnum(h3d.mat.Data.Wrap);
  89. interp.shareEnum(h3d.mat.BlendMode);
  90. default:
  91. }
  92. if( makeObj == null )
  93. throw "Don't know what to do with " + c.name;
  94. interp.variables.set("loadShader", function(name) return ide.shaderLoader.load(name));
  95. interp.variables.set("lookupShader", lookupShader);
  96. interp.variables.set("getS3D", function() return hide.comp.Scene.getCurrent().s3d);
  97. var props = new Properties(interp);
  98. interp.variables.set("applyProperties", props.apply);
  99. for( f in c.fields )
  100. switch( f.kind ) {
  101. case KVar(v):
  102. interp.variables.set(f.name, v.expr == null ? null : @:privateAccess interp.exprReturn(v.expr));
  103. case KFunction(fd):
  104. var ed : hscript.Expr.ExprDef = EFunction(fd.args, fd.expr, f.name, fd.ret);
  105. var e = #if hscriptPos { pmin : 0, pmax : 0, origin : null, line : 0, e : ed } #else ed #end;
  106. interp.variables.set(f.name, @:privateAccess interp.exprReturn(e));
  107. default:
  108. }
  109. // share functions
  110. var obj = makeObj();
  111. props.obj = obj;
  112. interp.shareObject(obj);
  113. interp.variables.set("super", obj);
  114. interp.variables.set("this", obj);
  115. var fnew = interp.variables.get("new");
  116. if( fnew != null ) Reflect.callMethod(null, fnew, args == null ? [] : args);
  117. renderer = props;
  118. return obj;
  119. }
  120. }
  121. */