From 455c06ecd466424cdf1b444a7c289b322390e795 Mon Sep 17 00:00:00 2001 From: reduz Date: Wed, 20 Jul 2022 01:11:13 +0200 Subject: Implement Vector4, Vector4i, Projection Implement built-in classes Vector4, Vector4i and Projection. * Two versions of Vector4 (float and integer). * A Projection class, which is a 4x4 matrix specialized in projection types. These types have been requested for a long time, but given they were very corner case they were not added before. Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity. **Q**: Why Projection and not Matrix4? **A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming. --- core/variant/variant_parser.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'core/variant/variant_parser.cpp') diff --git a/core/variant/variant_parser.cpp b/core/variant/variant_parser.cpp index 259ca8a60d..1df5fa969e 100644 --- a/core/variant/variant_parser.cpp +++ b/core/variant/variant_parser.cpp @@ -649,6 +649,32 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, } value = Vector3i(args[0], args[1], args[2]); + } else if (id == "Vector4") { + Vector args; + Error err = _parse_construct(p_stream, args, line, r_err_str); + if (err) { + return err; + } + + if (args.size() != 4) { + r_err_str = "Expected 4 arguments for constructor"; + return ERR_PARSE_ERROR; + } + + value = Vector4(args[0], args[1], args[2], args[3]); + } else if (id == "Vector4i") { + Vector args; + Error err = _parse_construct(p_stream, args, line, r_err_str); + if (err) { + return err; + } + + if (args.size() != 4) { + r_err_str = "Expected 4 arguments for constructor"; + return ERR_PARSE_ERROR; + } + + value = Vector4i(args[0], args[1], args[2], args[3]); } else if (id == "Transform2D" || id == "Matrix32") { //compatibility Vector args; Error err = _parse_construct(p_stream, args, line, r_err_str); @@ -731,6 +757,19 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, } value = Transform3D(Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]), Vector3(args[9], args[10], args[11])); + } else if (id == "Projection") { // "Transform" kept for compatibility with Godot <4. + Vector args; + Error err = _parse_construct(p_stream, args, line, r_err_str); + if (err) { + return err; + } + + if (args.size() != 16) { + r_err_str = "Expected 16 arguments for constructor"; + return ERR_PARSE_ERROR; + } + + value = Projection(Vector4(args[0], args[1], args[2], args[3]), Vector4(args[4], args[5], args[6], args[7]), Vector4(args[8], args[9], args[10], args[11]), Vector4(args[12], args[13], args[14], args[15])); } else if (id == "Color") { Vector args; Error err = _parse_construct(p_stream, args, line, r_err_str); @@ -1534,6 +1573,14 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str Vector3i v = p_variant; p_store_string_func(p_store_string_ud, "Vector3i(" + itos(v.x) + ", " + itos(v.y) + ", " + itos(v.z) + ")"); } break; + case Variant::VECTOR4: { + Vector4 v = p_variant; + p_store_string_func(p_store_string_ud, "Vector4(" + rtos_fix(v.x) + ", " + rtos_fix(v.y) + ", " + rtos_fix(v.z) + ")"); + } break; + case Variant::VECTOR4I: { + Vector4i v = p_variant; + p_store_string_func(p_store_string_ud, "Vector4i(" + itos(v.x) + ", " + itos(v.y) + ", " + itos(v.z) + ")"); + } break; case Variant::PLANE: { Plane p = p_variant; p_store_string_func(p_store_string_ud, "Plane(" + rtos_fix(p.normal.x) + ", " + rtos_fix(p.normal.y) + ", " + rtos_fix(p.normal.z) + ", " + rtos_fix(p.d) + ")"); @@ -1596,6 +1643,20 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str p_store_string_func(p_store_string_ud, s + ")"); } break; + case Variant::PROJECTION: { + String s = "Projection("; + Projection t = p_variant; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + if (i != 0 || j != 0) { + s += ", "; + } + s += rtos_fix(t.matrix[i][j]); + } + } + + p_store_string_func(p_store_string_ud, s + ")"); + } break; // misc types case Variant::COLOR: { -- cgit v1.2.3