"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var stardust_core_1 = require("stardust-core"); var stardust_core_2 = require("stardust-core"); var intrinsicImplementations = new stardust_core_2.Dictionary(); var intrinsicsCodeBase = new stardust_core_2.Dictionary(); function ImplementFunction(name, argTypes, returnType, code) { var internalName = stardust_core_1.Intrinsics.getInternalName({ name: name, argTypes: argTypes, returnType: returnType }); intrinsicImplementations.set(internalName, code); } function ImplementSimpleFunction(name, argTypes, returnType, funcName, funcCode) { ImplementFunction(name, argTypes, returnType, function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return funcName + "(" + args.join(", ") + ")"; }); if (funcCode) { var internalName = stardust_core_1.Intrinsics.getInternalName({ name: name, argTypes: argTypes, returnType: returnType }); intrinsicsCodeBase.set(internalName, funcCode); } } function ImplementOperator(name, argTypes, returnType, code) { ImplementFunction("@" + name, argTypes, returnType, code); } function ImplementTypeConversion(srcType, destType, code) { ImplementFunction("cast:" + srcType + ":" + destType, [srcType], destType, code); } for (var _i = 0, _a = ["float", "int", "Vector2", "Vector3", "Vector4", "Color"]; _i < _a.length; _i++) { var type = _a[_i]; ImplementOperator("+", [type, type], type, function (a, b) { return "(" + a + ") + (" + b + ")"; }); ImplementOperator("-", [type, type], type, function (a, b) { return "(" + a + ") - (" + b + ")"; }); ImplementOperator("*", [type, type], type, function (a, b) { return "(" + a + ") * (" + b + ")"; }); ImplementOperator("/", [type, type], type, function (a, b) { return "(" + a + ") / (" + b + ")"; }); if (type != "Color") { ImplementOperator("+", [type], type, function (a, b) { return "" + a; }); ImplementOperator("-", [type], type, function (a, b) { return "-(" + a + ")"; }); } } ImplementOperator("*", ["float", "Vector2"], "Vector2", function (a, b) { return "(" + a + ") * (" + b + ")"; }); ImplementOperator("*", ["float", "Vector3"], "Vector3", function (a, b) { return "(" + a + ") * (" + b + ")"; }); ImplementOperator("*", ["float", "Vector4"], "Vector4", function (a, b) { return "(" + a + ") * (" + b + ")"; }); ImplementOperator("*", ["Vector2", "float"], "Vector2", function (a, b) { return "(" + a + ") * (" + b + ")"; }); ImplementOperator("*", ["Vector3", "float"], "Vector3", function (a, b) { return "(" + a + ") * (" + b + ")"; }); ImplementOperator("*", ["Vector4", "float"], "Vector4", function (a, b) { return "(" + a + ") * (" + b + ")"; }); ImplementOperator("/", ["Vector2", "float"], "Vector2", function (a, b) { return "(" + a + ") / (" + b + ")"; }); ImplementOperator("/", ["Vector3", "float"], "Vector3", function (a, b) { return "(" + a + ") / (" + b + ")"; }); ImplementOperator("/", ["Vector4", "float"], "Vector4", function (a, b) { return "(" + a + ") / (" + b + ")"; }); // ImplementOperator("%", [ "int", "int" ], "int", (a, b) => `(${a}) % (${b})`); ImplementOperator("%", ["float", "float"], "float", function (a, b) { return "mod(" + a + ", " + b + ")"; }); for (var _b = 0, _c = ["float", "int", "bool"]; _b < _c.length; _b++) { var type = _c[_b]; ImplementOperator("==", [type, type], "bool", function (a, b) { return "(" + a + ") == (" + b + ")"; }); } for (var _d = 0, _e = ["float", "int"]; _d < _e.length; _d++) { var type = _e[_d]; ImplementOperator(">", [type, type], "bool", function (a, b) { return "(" + a + ") > (" + b + ")"; }); ImplementOperator("<", [type, type], "bool", function (a, b) { return "(" + a + ") < (" + b + ")"; }); ImplementOperator(">=", [type, type], "bool", function (a, b) { return "(" + a + ") >= (" + b + ")"; }); ImplementOperator("<=", [type, type], "bool", function (a, b) { return "(" + a + ") <= (" + b + ")"; }); } ImplementOperator("!", ["bool"], "bool", function (a) { return "!(" + a + ")"; }); ImplementOperator("&&", ["bool", "bool"], "bool", function (a, b) { return "(" + a + ") && (" + b + ")"; }); ImplementOperator("||", ["bool", "bool"], "bool", function (a, b) { return "(" + a + ") || (" + b + ")"; }); ImplementSimpleFunction("Vector2", ["float", "float"], "Vector2", "vec2"); ImplementSimpleFunction("Vector3", ["float", "float", "float"], "Vector3", "vec3"); ImplementSimpleFunction("Vector4", ["float", "float", "float", "float"], "Vector4", "vec4"); ImplementSimpleFunction("Color", ["float", "float", "float", "float"], "Color", "vec4"); ImplementSimpleFunction("Quaternion", ["float", "float", "float", "float"], "Quaternion", "vec4"); ImplementSimpleFunction("normalize", ["Vector2"], "Vector2", "normalize"); ImplementSimpleFunction("normalize", ["Vector3"], "Vector3", "normalize"); ImplementSimpleFunction("normalize", ["Vector4"], "Vector4", "normalize"); ImplementSimpleFunction("normalize", ["Quaternion"], "Vector4", "normalize"); ImplementSimpleFunction("dot", ["Vector2", "Vector2"], "float", "dot"); ImplementSimpleFunction("dot", ["Vector3", "Vector3"], "float", "dot"); ImplementSimpleFunction("dot", ["Vector4", "Vector4"], "float", "dot"); ImplementSimpleFunction("length", ["Vector2"], "float", "length"); ImplementSimpleFunction("length", ["Vector3"], "float", "length"); ImplementSimpleFunction("length", ["Vector4"], "float", "length"); ImplementSimpleFunction("length", ["Quaternion"], "float", "length"); ImplementSimpleFunction("cross", ["Vector3", "Vector3"], "Vector3", "cross"); ImplementSimpleFunction("quat_mul", ["Quaternion", "Quaternion"], "Quaternion", "s3_quat_mul", "\n vec4 s3_quat_mul(vec4 q1, vec4 q2) {\n return vec4(\n q1.w * q2.xyz + q2.w * q1.xyz + cross(q1.xyz, q2.xyz),\n q1.w * q2.w - dot(q1.xyz, q2.xyz)\n );\n }\n"); ImplementSimpleFunction("quat_rotate", ["Quaternion", "Vector3"], "Vector3", "s3_quat_rotate", "\n vec3 s3_quat_rotate(vec4 q, vec3 v) {\n float d = dot(q.xyz, v);\n vec3 c = cross(q.xyz, v);\n return q.w * q.w * v + (q.w + q.w) * c + d * q.xyz - cross(c, q.xyz);\n }\n"); var colorCode = "\n float s3_lab2rgb_curve(float v) {\n float p = pow(v, 3.0);\n if(p > 0.008856) {\n return p;\n } else {\n return (v - 16.0 / 116.0) / 7.787;\n }\n }\n float s3_lab2rgb_curve2(float v) {\n if(v > 0.0031308) {\n return 1.055 * pow(v , (1.0 / 2.4)) - 0.055;\n } else {\n return 12.92 * v;\n }\n }\n vec4 s3_lab2rgb(vec4 lab) {\n float var_Y = (lab.x + 0.160) / 1.160;\n float var_X = lab.y / 5.0 + var_Y;\n float var_Z = var_Y - lab.z / 2.0;\n\n var_X = s3_lab2rgb_curve(var_X) * 0.95047;\n var_Y = s3_lab2rgb_curve(var_Y);\n var_Z = s3_lab2rgb_curve(var_Z) * 1.08883;\n\n float var_R = var_X * 3.2406 + var_Y * -1.5372 + var_Z * -0.4986;\n float var_G = var_X * -0.9689 + var_Y * 1.8758 + var_Z * 0.0415;\n float var_B = var_X * 0.0557 + var_Y * -0.2040 + var_Z * 1.0570;\n\n var_R = s3_lab2rgb_curve2(var_R);\n var_G = s3_lab2rgb_curve2(var_G);\n var_B = s3_lab2rgb_curve2(var_B);\n\n return vec4(var_R, var_G, var_B, lab.a);\n }\n vec4 s3_hcl2rgb(vec4 hcl) {\n vec4 lab = vec4(hcl.z, hcl.y * cos(hcl.x), hcl.y * sin(hcl.x), hcl.a);\n return s3_lab2rgb(lab);\n }\n"; ImplementSimpleFunction("lab2rgb", ["Color"], "Color", "s3_lab2rgb", colorCode); ImplementSimpleFunction("hcl2rgb", ["Color"], "Color", "s3_hcl2rgb", colorCode); ImplementSimpleFunction("abs", ["float"], "float", "abs"); ImplementSimpleFunction("sqrt", ["float"], "float", "sqrt"); ImplementSimpleFunction("exp", ["float"], "float", "exp"); ImplementSimpleFunction("log", ["float"], "float", "log"); ImplementSimpleFunction("sin", ["float"], "float", "sin"); ImplementSimpleFunction("cos", ["float"], "float", "cos"); ImplementSimpleFunction("tan", ["float"], "float", "tan"); ImplementSimpleFunction("asin", ["float"], "float", "asin"); ImplementSimpleFunction("acos", ["float"], "float", "acos"); ImplementSimpleFunction("atan", ["float"], "float", "atan"); ImplementSimpleFunction("atan2", ["float", "float"], "float", "atan2"); ImplementSimpleFunction("abs", ["int"], "int", "abs"); ImplementSimpleFunction("min", ["float", "float"], "float", "min"); ImplementSimpleFunction("max", ["float", "float"], "float", "max"); ImplementSimpleFunction("ceil", ["float"], "float", "ceil"); ImplementSimpleFunction("floor", ["float"], "float", "floor"); ImplementSimpleFunction("mix", ["float", "float", "float"], "float", "mix"); ImplementSimpleFunction("mix", ["Vector2", "Vector2", "float"], "Vector2", "mix"); ImplementSimpleFunction("mix", ["Vector3", "Vector3", "float"], "Vector3", "mix"); ImplementSimpleFunction("mix", ["Vector4", "Vector4", "float"], "Vector4", "mix"); ImplementSimpleFunction("mix", ["Color", "Color", "float"], "Color", "mix"); ImplementFunction("clamp", ["float"], "float", function (a) { return "clamp(" + a + ", 0, 1)"; }); ImplementTypeConversion("float", "int", function (a) { return "int(" + a + ")"; }); ImplementTypeConversion("int", "float", function (a) { return "float(" + a + ")"; }); ImplementFunction("array", ["FloatArray", "float"], "float", function (a, b) { return "texture2D(" + a + ", vec2((" + b + " + 0.5) / float(" + a + "_length), 0.5)).x"; }); ImplementFunction("array", ["Vector2Array", "float"], "Vector2", function (a, b) { return "texture2D(" + a + ", vec2((" + b + " + 0.5) / float(" + a + "_length), 0.5)).xy"; }); ImplementFunction("array", ["Vector3Array", "float"], "Vector3", function (a, b) { return "texture2D(" + a + ", vec2((" + b + " + 0.5) / float(" + a + "_length), 0.5)).xyz"; }); ImplementFunction("array", ["Vector4Array", "float"], "Vector4", function (a, b) { return "texture2D(" + a + ", vec2((" + b + " + 0.5) / float(" + a + "_length), 0.5)).xyzw"; }); ImplementFunction("array", ["ColorArray", "float"], "Color", function (a, b) { return "texture2D(" + a + ", vec2((" + b + " + 0.5) / float(" + a + "_length), 0.5)).rgba"; }); ImplementFunction("image", ["Image", "Vector2"], "Color", function (a, b) { return "texture2D(" + a + ", (" + b + " + 0.5) / vec2(" + a + "_width, " + a + "_height))"; }); ImplementFunction("image", ["FloatImage", "Vector2"], "float", function (a, b) { return "texture2D(" + a + ", (" + b + " + 0.5) / vec2(" + a + "_width, " + a + "_height)).x"; }); ImplementFunction("image", ["Vector2Image", "Vector2"], "Vector2", function (a, b) { return "texture2D(" + a + ", (" + b + " + 0.5) / vec2(" + a + "_width, " + a + "_height)).xy"; }); ImplementFunction("image", ["Vector3Image", "Vector2"], "Vector3", function (a, b) { return "texture2D(" + a + ", (" + b + " + 0.5) / vec2(" + a + "_width, " + a + "_height)).xyz"; }); ImplementFunction("image", ["Vector4Image", "Vector2"], "Vector4", function (a, b) { return "texture2D(" + a + ", (" + b + " + 0.5) / vec2(" + a + "_width, " + a + "_height)).xyzw"; }); ImplementFunction("image", ["ColorImage", "Vector2"], "Color", function (a, b) { return "texture2D(" + a + ", (" + b + " + 0.5) / vec2(" + a + "_width, " + a + "_height)).rgba"; }); ImplementFunction("get_camera_position", [], "Vector3", function () { return "s3_get_camera_position()"; }); ImplementFunction("get_camera_direction", ["Vector3"], "Vector3", function (a) { return "s3_get_camera_direction(" + a + ")"; }); function generateIntrinsicFunction(name, args) { if (intrinsicImplementations.has(name)) { if (intrinsicsCodeBase.has(name)) { return { code: intrinsicImplementations.get(name).apply(void 0, args), additionalCode: intrinsicsCodeBase.get(name) }; } else { return { code: intrinsicImplementations.get(name).apply(void 0, args), additionalCode: null }; } } else { throw new Error("intrinsic function " + name + " is not defined."); } } exports.generateIntrinsicFunction = generateIntrinsicFunction; //# sourceMappingURL=intrinsics.js.map