{"id":1549,"date":"2016-10-17T10:27:54","date_gmt":"2016-10-17T10:27:54","guid":{"rendered":"http:\/\/www.migenius.com\/?p=1549"},"modified":"2016-10-25T03:46:58","modified_gmt":"2016-10-25T03:46:58","slug":"3d-transformations-part1-matrices","status":"publish","type":"post","link":"https:\/\/www.migenius.com\/articles\/3d-transformations-part1-matrices","title":{"rendered":"3D Transformations – Part 1 Matrices"},"content":{"rendered":"

Transformations are fundamental to working with 3D scenes and something that can be frequently confusing to those that haven’t worked in 3D before. In this, the first of two articles I will show you how to encode 3D transformations as a single 4×4 matrix which you can then pass into the appropriate RealityServer command to position, orient and scale objects in your scene. In a second part I will dive into a newer method of specifying transformations in RealityServer called SRT transformations which also allows for the easy animation of objects.<\/p>\n

<\/p>\n

Math and Notes<\/h3>\n

There is a bit of math in this article since we are dealing with matrices<\/a>. A basic knowledge of vectors and matrices is assumed, however you don’t need to have done a full blown vector calculus course to understand how to make practical use of the information here. If you are so inclined however there is a lot of great information online, Google is your friend.<\/p>\n

Also just a quick note, for angles expressed in this article we will be using radians<\/a> rather than degrees. While it is common for user interfaces to present angles in degrees, the built in trigonometric functions of most programming languages expect angles in radians. You can easily code the conversion if needed using the following formula.<\/p>\n\n\"\n\n

Another important thing to remember is that RealityServer uses a right handed coordinate system<\/a>. This dictates which way rotations will proceed around an axis.\u00a0In our examples we will be assuming Z<\/em> is our up axis. However this is a completely arbitrary choice and you can easily use Y<\/em> as your up axis if you like (or any other axis for that matter). This only affects how you setup your camera. Up-ness<\/em> is a topic for another article however.<\/p>\n

In RealityServer, all transformations are encoded as a world to object space affine transformation<\/em> encoded as a 4×4 matrix in row major order. Since the transform is from world to object space it may be the inverse of what you commonly see in other 3D applications. By default, when instances of objects are created they are assigned the default transformation matrix which is the identity matrix. This has no effect on the object at all.<\/p>\n\n\"\n\n

We will be setting the values in this matrix in order to translate, rotate and scale our object. Note that object transformations can be nested along the scene graph. So if you had an instance which translated an object 3 units in the x direction which is contained within a group that is translated another 2 units in the x direction then the object will be translated by 5 units in the end.<\/p>\n

If are already working with an application that uses transformation matrices but they are in object to world space form, you can usually simply invert the matrix before use in RealityServer. It if is also in column major rather than row major form then you will also need to transpose<\/a> the matrix before inverting it. If building your matrices from scratch then if you follow this article you should be fine.<\/p>\n

The examples in this article are the raw JSON-RPC command sequences that get sent to RealityServer. If you have not done so already please read the article on Exploring the RealityServer JSON-RPC API<\/a>.<\/p>\n

Starting Point<\/h3>\n

Throughout this article we will be applying transformations to a simple object in a test scene I\u00a0have created. I’ll use a box which is 4 units long, 2 units high and 1 unit thick. The large grid areas on the floor are 1 unit in size and the small grid areas are 0.2 units. You can see our initial scene with our test object in it at its initial position to the right (it has the identity transformation applied to it).<\/p>\n

First I will cover the three fundamental elements of transformations – translation, rotation and scaling. I’ll show you how to create matrices to represent each of these and then how to combine all three operations into a single matrix using matrix multiplication.<\/p>\n

Even if you do not understand the mathematics involved you should still be able to follow along well enough to code transformations into your application by following the examples.<\/p>\n<\/div><\/div>

\"Test<\/p>\n

Test Object in Initial Position<\/p>\n<\/div>\n<\/div><\/div><\/div>\n

Only the code for actually setting the transformations will be shown in the rest of this article, however if you wish to follow along with the same scene, here is the full code for creating the image above. The box object is called exBoxInstance<\/em> if you want to perform operations on it.<\/p>\n

Scene Creation Commands<\/i><\/i><\/h3>

\n

\r\n[\r\n\t{"jsonrpc": "2.0", "method": "create_scope", "params": {\r\n\t\t"scope_name" : "exScope"\r\n\t}, "id": 1001},\r\n\t{"jsonrpc": "2.0", "method": "use_scope", "params": {\r\n\t\t"scope_name" : "exScope"\r\n\t}, "id": 1002},\r\n\t{"jsonrpc": "2.0", "method": "create_scene", "params": {\r\n\t\t"scene_name" : "exScene"\r\n\t}, "id": 1003},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exOptions",\r\n\t\t"element_type" : "Options"\r\n\t}, "id": 1004},\r\n\t{"jsonrpc": "2.0", "method": "scene_set_options", "params": {\r\n\t\t"scene_name" : "exScene",\r\n\t\t"options" : "exOptions"\r\n\t}, "id": 1005},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exRootGroup",\r\n\t\t"element_type" : "Group"\r\n\t}, "id": 1006},\r\n\t{"jsonrpc": "2.0", "method": "scene_set_rootgroup", "params": {\r\n\t\t"scene_name" : "exScene",\r\n\t\t"group" : "exRootGroup"\r\n\t}, "id": 1007},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exCamera",\r\n\t\t"element_type" : "Camera"\r\n\t}, "id": 1008},\r\n\t{"jsonrpc": "2.0", "method": "camera_set_resolution", "params": {\r\n\t\t"camera_name" : "exCamera",\r\n\t\t"resolution" : { "x" : 380, "y" : 380 }\r\n\t}, "id": 1009},\r\n\t{"jsonrpc": "2.0", "method": "camera_set_aspect", "params": {\r\n\t\t"camera_name" : "exCamera",\r\n\t\t"aspect" : 1.0\r\n\t}, "id": 1010},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exCameraInstance",\r\n\t\t"element_type" : "Instance"\r\n\t}, "id": 1011},\r\n\t{"jsonrpc": "2.0", "method": "instance_set_world_to_obj", "params": {\r\n\t\t"instance_name" : "exCameraInstance",\r\n\t\t"transform" : {\r\n\t\t\t"xx": 1,\r\n\t\t\t"xy": 0,\r\n\t\t\t"xz": 0,\r\n\t\t\t"xw": 0,\r\n\t\t\t"yx": 0,\r\n\t\t\t"yy": 0.5070201265633939,\r\n\t\t\t"yz": -0.86193421515776947,\r\n\t\t\t"yw": 0,\r\n\t\t\t"zx": 0,\r\n\t\t\t"zy": 0.86193421515776969,\r\n\t\t\t"zz": 0.50702012656339379,\r\n\t\t\t"zw": 0,\r\n\t\t\t"wx": -2,\r\n\t\t\t"wy": -0.25351006328169756,\r\n\t\t\t"wz": -9.4305743540791251,\r\n\t\t\t"ww": 1\r\n\t\t}\r\n\t}, "id": 1012},\r\n\t{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t\t"instance_name" : "exCameraInstance",\r\n\t\t"item_name" : "exCamera"\r\n\t}, "id": 1013},\r\n\t{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t\t"group_name" : "exRootGroup",\r\n\t\t"item_name" : "exCameraInstance"\r\n\t}, "id": 1014},\r\n\t{"jsonrpc": "2.0", "method": "scene_set_camera_instance", "params": {\r\n\t\t"scene_name" : "exScene",\r\n\t\t"camera_instance" : "exCameraInstance"\r\n\t}, "id": 1015},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attributes", "params": {\r\n\t\t"create" : true,\r\n\t\t"element_name" : "exCamera",\r\n\t\t"attributes" : {\r\n\t\t\t"tm_tonemapper" : {\r\n\t\t\t\t"type" : "String",\r\n\t\t\t\t"value" : "mia_exposure_photographic"\r\n\t\t\t},\r\n\t\t\t"mip_cm2_factor" : {\r\n\t\t\t\t"type" : "Float32",\r\n\t\t\t\t"value" : 1.0\r\n\t\t\t},\r\n\t\t\t"mip_film_iso" : {\r\n\t\t\t\t"type" : "Float32",\r\n\t\t\t\t"value" : 300.0\r\n\t\t\t},\r\n\t\t\t"mip_camera_shutter" : {\r\n\t\t\t\t"type" : "Float32",\r\n\t\t\t\t"value" : 0.05\r\n\t\t\t},\r\n\t\t\t"mip_f_number" : {\r\n\t\t\t\t"type" : "Float32",\r\n\t\t\t\t"value" : 1.75\r\n\t\t\t},\r\n\t\t\t"mip_gamma" : {\r\n\t\t\t\t"type" : "Float32",\r\n\t\t\t\t"value" : 2.2\r\n\t\t\t}\r\n\t\t}\r\n\t}, "id": 1016},\r\n\t{"jsonrpc": "2.0", "method": "generate_box", "params": {\r\n\t\t"name" : "exBox",\r\n\t\t"length" : 4.0,\r\n\t\t"width" : 1.0,\r\n\t\t"height" : 2.0\r\n\t}, "id": 1017},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t\t"element_name" : "exBox",\r\n\t\t"attribute_type" : "Boolean",\r\n\t\t"attribute_name" : "visible",\r\n\t\t"attribute_value" : true,\r\n\t\t"create" : true\r\n\t}, "id": 1018},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exBoxInstance",\r\n\t\t"element_type" : "Instance"\r\n\t}, "id": 1019},\r\n\t{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t\t"instance_name" : "exBoxInstance",\r\n\t\t"item_name" : "exBox"\r\n\t}, "id": 1020},\r\n\t{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t\t"group_name" : "exRootGroup",\r\n\t\t"item_name" : "exBoxInstance"\r\n\t}, "id": 1021},\r\n\t{"jsonrpc": "2.0", "method": "import_scene_elements", "params": {\r\n\t\t"filename": "${shader}\/material_examples\/architectural.mdl"\r\n\t}, "id": 1022},\r\n\t{"jsonrpc": "2.0", "method": "create_material_instance_from_definition", "params": {\r\n\t\t"arguments": {\r\n\t\t\t"diffuse" : { "r" : 0.5, "g" : 0.5, "b" : 0.7 },\r\n\t\t\t"reflectivity" : 0.0\r\n\t\t},\r\n\t\t"material_definition_name": "mdl::material_examples::architectural::architectural",\r\n\t\t"material_name": "exBoxMaterial"\r\n\t}, "id": 1023},\r\n\t{"jsonrpc": "2.0", "method": "instance_set_material", "params": {\r\n\t\t"instance_name" : "exBoxInstance",\r\n\t\t"material_name" : "exBoxMaterial"\r\n\t}, "id": 1024},\r\n\t{"jsonrpc": "2.0", "method": "generate_mesh", "params": {\r\n\t\t"name" : "exPlane",\r\n\t\t"mesh" : {\r\n\t\t\t"vectors" : {\r\n\t\t\t\t"points" : [\r\n\t\t\t\t\t{"x" : -50.0, "y" : -50.0, "z": 0.0},\r\n\t\t\t\t\t{"x" : -50.0, "y" : 50.00, "z": 0.0},\r\n\t\t\t\t\t{"x" : 50.00, "y" : 50.00, "z": 0.0},\r\n\t\t\t\t\t{"x" : 50.00, "y" : -50.0, "z": 0.0}\r\n\t\t\t\t],\r\n\t\t\t\t"normals" : [\r\n\t\t\t\t\t{"x" : 0.0, "y" : 0.0, "z": 1.0}\r\n\t\t\t\t],\r\n\t\t\t\t"uvs" : [\r\n\t\t\t\t\t{"x" : 0.00, "y" : 0.00},\r\n\t\t\t\t\t{"x" : 0.00, "y" : 10.0},\r\n\t\t\t\t\t{"x" : 10.0, "y" : 10.0},\r\n\t\t\t\t\t{"x" : 10.0, "y" : 0.00}\r\n\t\t\t\t]\r\n\t\t\t},\r\n\t\t\t"vertices" : [\r\n\t\t\t\t{"v" : 0, "n": 0, "t": 0},\r\n\t\t\t\t{"v" : 1, "n": 0, "t": 1},\r\n\t\t\t\t{"v" : 2, "n": 0, "t": 2},\r\n\t\t\t\t{"v" : 3, "n": 0, "t": 3}\r\n\t\t\t],\r\n\t\t\t"polygons" : [\r\n\t\t\t\t[0, 1, 2, 3]\r\n\t\t\t]\r\n\t\t}\r\n\t}, "id": 1025},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t\t"element_name" : "exPlane",\r\n\t\t"attribute_type" : "Boolean",\r\n\t\t"attribute_name" : "visible",\r\n\t\t"attribute_value" : true,\r\n\t\t"create" : true\r\n\t}, "id": 1026},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exPlaneInstance",\r\n\t\t"element_type" : "Instance"\r\n\t}, "id": 1027},\r\n\t{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t\t"instance_name" : "exPlaneInstance",\r\n\t\t"item_name" : "exPlane"\r\n\t}, "id": 1028},\r\n\t{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t\t"group_name" : "exRootGroup",\r\n\t\t"item_name" : "exPlaneInstance"\r\n\t}, "id": 1029},\r\n\t{"jsonrpc": "2.0", "method": "create_material_instance_from_definition", "params": {\r\n\t\t"arguments": {\r\n\t\t\t"diffuse" : { "r" : 0.7, "g" : 0.7, "b" : 0.7 },\r\n\t\t\t"reflectivity" : 0.0\r\n\t\t},\r\n\t\t"material_definition_name": "mdl::material_examples::architectural::architectural",\r\n\t\t"material_name": "exPlaneMaterial"\r\n\t}, "id": 1030},\r\n\t{"jsonrpc": "2.0", "method": "material_attach_texture_to_argument", "params": {\r\n\t\t"material_name" : "exPlaneMaterial",\r\n\t\t"argument_name" : "diffuse",\r\n\t\t"texture_name" : "scenes\/meyemII\/images\/metaslgrid09.png",\r\n\t\t"texture_options" : {},\r\n\t\t"create_from_file" : true\r\n\t}, "id": 1031},\t\r\n\t{"jsonrpc": "2.0", "method": "instance_set_material", "params": {\r\n\t\t"instance_name" : "exPlaneInstance",\r\n\t\t"material_name" : "exPlaneMaterial"\r\n\t}, "id": 1032},\r\n\t{"jsonrpc": "2.0", "method": "import_scene_elements", "params": {\r\n\t\t"filename": "${shader}\/material_examples\/lights_photometric.mdl"\r\n\t}, "id": 1033},\r\n\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exRectLight",\r\n\t\t"element_type" : "Light"\r\n\t}, "id": 1034},\r\n\t{"jsonrpc": "2.0", "method": "light_set_area_shape", "params": {\r\n\t\t"light_name" : "exRectLight",\r\n\t\t"area_shape" : "rectangle"\r\n\t}, "id": 1035},\r\n\t{"jsonrpc": "2.0", "method": "light_set_area_size", "params": {\r\n\t\t"light_name" : "exRectLight",\r\n\t\t"area_size" : {"x": 8.0, "y" : 8.0}\r\n\t}, "id": 1036},\r\n\t{"jsonrpc": "2.0", "method": "create_material_instance_from_definition", "params": {\r\n\t\t"material_definition_name" : "mdl::material_examples::lights_photometric::diffuse_area_light",\r\n\t\t"material_name" : "exRectLightShader",\r\n\t\t"arguments" : {\r\n\t\t\t"flux" : 156.25\r\n\t\t}\r\n\t}, "id": 1037},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t\t"element_name" : "exRectLight",\r\n\t\t"attribute_type" : "Ref",\r\n\t\t"attribute_name" : "material",\r\n\t\t"attribute_value" : "exRectLightShader",\r\n\t\t"create" : true\r\n\t}, "id": 1038},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exRectLightInstance",\r\n\t\t"element_type" : "Instance"\r\n\t}, "id": 1039},\r\n\t{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t\t"instance_name" : "exRectLightInstance",\r\n\t\t"item_name" : "exRectLight"\r\n\t}, "id": 1040},\r\n\t{"jsonrpc": "2.0", "method": "instance_set_world_to_obj", "params": {\r\n\t\t"instance_name" : "exRectLightInstance",\r\n\t\t"transform" : {\r\n\t\t\t"xx": 0.70710678118654735,\r\n\t\t\t"xy": 0,\r\n\t\t\t"xz": 0.70710678118654746,\r\n\t\t\t"xw": 0,\r\n\t\t\t"yx": 0.0,\r\n\t\t\t"yy": 1.0,\r\n\t\t\t"yz": 0.0,\r\n\t\t\t"yw": 0.0,\r\n\t\t\t"zx": -0.70710678118654746,\r\n\t\t\t"zy": 0,\r\n\t\t\t"zz": 0.70710678118654768,\r\n\t\t\t"zw": 0,\r\n\t\t\t"wx": -4.2426406871192839,\r\n\t\t\t"wy": -1,\r\n\t\t\t"wz": -9.8994949366116654,\r\n\t\t\t"ww": 1\r\n\t\t}\r\n\t}, "id": 1041},\r\n\t{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t\t"group_name" : "exRootGroup",\r\n\t\t"item_name" : "exRectLightInstance"\r\n\t}, "id": 1042},\r\n\r\n\t{"jsonrpc": "2.0", "method": "export_scene", "params": {\r\n\t\t"scene_name" : "exScene",\r\n\t\t"filename" : "scenes\/exScene.mi"\r\n\t}, "id": 8001},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attributes", "params": {\r\n\t\t"element_name" : "exOptions",\r\n\t\t"create" : true,\r\n\t\t"attributes" : {\r\n\t\t\t"progressive_rendering_max_samples" : {\r\n\t\t\t\t"type" : "Sint32",\r\n\t\t\t\t"value" : 2500\r\n\t\t\t},\r\n\t\t\t"progressive_rendering_max_time" : {\r\n\t\t\t\t"type" : "Sint32",\r\n\t\t\t\t"value" : 1000\r\n\t\t\t},\r\n\t\t\t"progressive_rendering_quality_enabled" : {\r\n\t\t\t\t"type" : "Boolean",\r\n\t\t\t\t"value" : false\r\n\t\t\t}\r\n\t\t}\r\n\t}, "id": 9001},\r\n\t{"jsonrpc": "2.0", "method": "render", "params": {\r\n\t\t"scene_name" : "exScene",\r\n\t\t"renderer" : "iray",\r\n\t\t"render_context_options" : {\r\n\t\t\t"scheduler_mode" : {\r\n\t\t\t\t"type" : "String",\r\n\t\t\t\t"value" : "batch"\r\n\t\t\t}\r\n\t\t}\r\n\t}, "id": 9002},\r\n\t{"jsonrpc": "2.0", "method": "delete_scope", "params": {\r\n\t\t"scope_name" : "exScope"\r\n\t}, "id": 9003}\r\n]\r\n<\/pre>\n<\/p><\/div><\/div>\n

Translation<\/h3>\n

Let’s start with the simplest type of transformation, translations. This is what moves your objects around your scene. When used alone these are very simple and only affect three values in our transformation matrix.<\/p>\n<\/div><\/div>

\n\"\n\n<\/div><\/div><\/div>\n

So for example, if you want to move your object 1 unit in the x<\/span><\/em> direction, -2 units in the y<\/span><\/em> direction and 0.5 units in the z<\/span><\/em> direction we would use the following matrix shown to the right. We can set this using the code below.<\/p>\n<\/div><\/div>

\n\"\n\n<\/div><\/div><\/div>\n
\r\n{\r\n  "jsonrpc": "2.0",\r\n  "method": "instance_set_world_to_obj",\r\n  "params": {\r\n    "instance_name" : "exBoxInstance",\r\n    "transform" : {\r\n      "xx": 1, "xy": 0, "xz": 0, "xw": 0,\r\n      "yx": 0, "yy": 1, "yz": 0, "yw": 0,\r\n      "zx": 0, "zy": 0, "zz": 1, "zw": 0,\r\n      "wx": -1, "wy": 2, "wz": -0.5, "ww": 1\r\n    }\r\n  },\r\n  "id": 1000\r\n}\r\n<\/pre>\n<\/div><\/div>
\n
\n
Transformed<\/div>\n

\"Translation\"<\/p>\n<\/div>\n

\n
Original<\/div>\n

\"Original\"<\/p>\n<\/div>\n<\/div>\n<\/div><\/div><\/div>\n

Rotations<\/h3>\n

Rotations are quite a bit more complex. For this we need to compute a standard 3×3 rotation matrix which will become part of our 4×4 matrix. Again though, when used alone, it is relatively straight forward to setup. Let’s first show how to compute rotation matrices for each of the standard orthogonal axis.<\/p>\n

X Axis<\/h5>\n

We’ll start with the x<\/span><\/em>\u00a0axis (1,0,0)<\/span><\/em> and rotating around that. To the right you can see the general form of the matrix required for this. Notice the rotation part takes up the top left 3 x 3 positions.<\/p>\n<\/div><\/div>

\n\"\n\n<\/div><\/div><\/div>\n

Let’s do a quick example. We’ll rotate 45\u00b0 in the x<\/span><\/em> axis. So that will be 0.7853981634 radians. Remember we must negate our angles before passing them to our trigonometric functions. Some quick math and we get the matrix to the right. The command for applying this is below.<\/p>\n<\/div><\/div>

\n\"\n\n<\/div><\/div><\/div>\n
\r\n{\r\n  "jsonrpc": "2.0",\r\n  "method": "instance_set_world_to_obj",\r\n  "params": {\r\n    "instance_name" : "exBoxInstance",\r\n    "transform" : {\r\n      "xx": 1,\r\n      "xy": 0,\r\n      "xz": 0,\r\n      "xw": 0,\r\n      "yx": 0,\r\n      "yy": 0.7071067811865476,\r\n      "yz": -0.7071067811865475,\r\n      "yw": 0,\r\n      "zx": 0,\r\n      "zy": 0.7071067811865475,\r\n      "zz": 0.7071067811865476,\r\n      "zw": 0,\r\n      "wx": 0,\r\n      "wy": 0,\r\n      "wz": 0,\r\n      "ww": 1\r\n    }\r\n  },\r\n  "id": 1000\r\n}\r\n<\/pre>\n<\/div><\/div>
\n
\n
Transformed<\/div>\n

\"Rotation<\/p>\n<\/div>\n

\n
Original<\/div>\n

\"Original\"<\/p>\n<\/div>\n<\/div>\n<\/div><\/div><\/div>\n

Y Axis<\/h5>\n

Let’s move onto the y<\/span><\/em>\u00a0axis (0,1,0)<\/span><\/em> and rotating around that. To the right you can see the general form of the matrix required for this. Notice the rotation part takes up the top left 3 x 3 positions as before.<\/p>\n<\/div><\/div>

\n\"\n\n<\/div><\/div><\/div>\n

Let’s do another quick example. We’ll rotate -90° in the y<\/span><\/em> axis. So that will be -1.5707963268 radians. A bit more math and we get the following matrix. The command for applying this is below.<\/p>\n<\/div><\/div>

\n\"\n\n<\/div><\/div><\/div>\n
\r\n{\r\n  "jsonrpc": "2.0",\r\n  "method": "instance_set_world_to_obj",\r\n  "params": {\r\n    "instance_name" : "exBoxInstance",\r\n    "transform" : {\r\n        "xx": 0, "xy": 0, "xz": -1, "xw": 0,\r\n        "yx": 0, "yy": 1, "yz": 0, "yw": 0,\r\n        "zx": 1, "zy": 0, "zz": 0, "zw": 0,\r\n        "wx": 0, "wy": 0, "wz": 0, "ww": 1\r\n    }\r\n  },\r\n  "id": 1000\r\n}\r\n<\/pre>\n<\/div><\/div>
\n
\n
Transformed<\/div>\n

\"Rotation<\/p>\n<\/div>\n

\n
Original<\/div>\n

\"Original\"<\/p>\n<\/div>\n<\/div>\n<\/div><\/div><\/div>\n

Z Axis<\/h5>\n

Finally we’ll do the z<\/span><\/em>\u00a0axis (0,0,1)<\/span><\/em> rotation. To the right you can see the general form of the matrix required for this. Notice the rotation part takes up the top left 3 x 3 positions as before.<\/p>\n<\/div><\/div>

\n\"\n\n<\/div><\/div><\/div>\n

One more angle, we’ll rotate 90° in the z<\/span><\/em> axis this time. So that will be 1.5707963268 radians. The command for applying this is below.<\/p>\n<\/div><\/div>

\n\"\n\n<\/div><\/div><\/div>\n
\r\n{\r\n  "jsonrpc": "2.0",\r\n  "method": "instance_set_world_to_obj",\r\n  "params": {\r\n    "instance_name" : "exBoxInstance",\r\n    "transform" : {\r\n        "xx": 0, "xy": -1, "xz": 0, "xw": 0,\r\n        "yx": 1, "yy": 0, "yz": 0, "yw": 0,\r\n        "zx": 0, "zy": 0, "zz": 1, "zw": 0,\r\n        "wx": 0, "wy": 0, "wz": 0, "ww": 1\r\n    }\r\n  },\r\n  "id": 1000\r\n}\r\n<\/pre>\n<\/div><\/div>
\n
\n
Transformed<\/div>\n

\"trans_05_rotation_z\"<\/p>\n<\/div>\n

\n
Original<\/div>\n

\"Original\"<\/p>\n<\/div>\n<\/div>\n<\/div><\/div><\/div>\n

Arbitary Axis<\/h5>\n

Rotating around the standard orthogonal axis is probably the most common rotation operation. The individual rotations above can be combined into a single operation using matrix multiplication (more on that later). However what about rotation around an arbitary axis? This is also a very common operation (for example if you want to roll a camera around its axis). For this we need a more complex general matrix shown below.<\/p>\n\n\"\n\n

To use this type of rotation we need to specify both and angle and an axis about which to rotate. Let’s try rotating -90° around the (0.577350269, 0.577350269, 0.577350269)<\/span><\/em> axis. So that’s -1.5707963268 radians giving us the following matrix.<\/p>\n\n\"\n\n

If you want to see code for how to do this, which actually looks much simpler than the maths here you can take a look at the example JavaScript matrix class near the end of this article. You can easily port this to any language.<\/p>\n

\r\n{\r\n  "jsonrpc": "2.0",\r\n  "method": "instance_set_world_to_obj",\r\n  "params": {\r\n    "instance_name" : "exBoxInstance",\r\n    "transform" : {\r\n        "xx": 0.3333333331143723,\r\n        "xy": 0.9106836021143723,\r\n        "xz": -0.24401693588562773,\r\n        "xw": 0,\r\n        "yx": -0.24401693588562773,\r\n        "yy": 0.3333333331143723,\r\n        "yz": 0.9106836021143723,\r\n        "yw": 0,\r\n        "zx": 0.9106836021143723,\r\n        "zy": -0.24401693588562773,\r\n        "zz": 0.3333333331143723,\r\n        "zw": 0,\r\n        "wx": 0,\r\n        "wy": 0,\r\n        "wz": 0,\r\n        "ww": 1\r\n    }\r\n  },\r\n  "id": 1000\r\n}\r\n<\/pre>\n<\/div><\/div>
\n
\n
Transformed<\/div>\n

\"Rotation<\/p>\n<\/div>\n

\n
Original<\/div>\n

\"Original\"<\/p>\n<\/div>\n<\/div>\n<\/div><\/div><\/div>\n

Scaling<\/h3>\n

The last fundament transformation type is scaling, making our object larger or smaller in a given axis. Fortunately this one is very simple. To the right is the general form of the scaling matrix we need.<\/p>\n<\/div><\/div>

\n\"\n\n<\/div><\/div><\/div>\n

For a quick example let’s scale by 1.25 in the x<\/span><\/em> axis, 4 in the y<\/span><\/em> axis and 0.5 in the z<\/span><\/em> axis. This is obviously a non-uniform scaling. Note that negative scales will mirror your object. Here is our matrix on the right.<\/p>\n<\/div><\/div>

\n\"\n\n<\/div><\/div><\/div>\n
\r\n{\r\n  "jsonrpc": "2.0",\r\n  "method": "instance_set_world_to_obj",\r\n  "params": {\r\n    "instance_name" : "exBoxInstance",\r\n    "transform" : {\r\n        "xx": 0.8, "xy": 0, "xz": 0, "xw": 0,\r\n        "yx": 0, "yy": 0.25, "yz": 0, "yw": 0,\r\n        "zx": 0, "zy": 0, "zz": 2, "zw": 0,\r\n        "wx": 0, "wy": 0, "wz": 0, "ww": 1\r\n    }\r\n  },\r\n  "id": 1000\r\n}\r\n<\/pre>\n<\/div><\/div>
\n
\n
Transformed<\/div>\n

\"Scaling\"<\/p>\n<\/div>\n

\n
Original<\/div>\n

\"Original\"<\/p>\n<\/div>\n<\/div>\n<\/div><\/div><\/div>\n

Combining Transformations<\/h3>\n

So, we can do all of the fundamental transformations now, however in the examples above we are only doing a single operation. What if we want to move, rotate and scale our object? For this we need to combine our individual transformation matrices into a single 4×4 matrix. We do this with matrix multiplication<\/a>. For matrix multiplication, let’s identify the individual elements of our matrix as follows.<\/p>\n\n\"\n\n

Using these identifiers, we can multiply matrix A by matrix B as follows. Note that order is important<\/strong> when performing matrix multiplication, so ABC ≠ CBA<\/em>, unlike multiplying numbers. Here is how we do the multiplication.<\/p>\n\n\"\n\n

Ok, that’s pretty crazy but of course you will have this in a nice matrix multiplication function in your code, so you can easily multiply any number of matrices together. Now, when combining transformation matrices you must multiply them in the reverse order to that in which you want them applied<\/strong>. This is critical, you will get completely the wrong answer otherwise. So, if we want to scale our object, then rotate it and then translate it. We must multiply the translation matrix by the rotation matrix by the scaling matrix, in that order.<\/p>\n

You can multiply together as many transformation matrices as you like, including multiple matrices of the same type of transformation. So it’s perfectly valid to perform three rotations, followed by two translations, followed by another rotation followed by a scaling operation. Or any other combination you can think of, however pay close attention to how the order of your transformations affects the results.<\/p>\n

Transformation Library<\/h3>\n

Using all of the principles explained in this article, here then is a simple, self contained JavaScript matrix library, based in part on the code we ship with RealityServer in our client library. However the code below can be used completely independently from RealityServer if you wish. With it you can build tools to create suitable input for the instance_set_world_to_obj <\/em>command.<\/p>\n

JavaScript Matrix Library<\/i><\/i><\/h3>

\n

\r\n\/**\r\n * This file defines the Matrix4x4 class.\r\n * @file Matrix4x4.js\r\n *\/\r\n\r\n\/**\r\n * Generic class for representing 4x4 matrices.\r\n * @constructor\r\n * @param {Object} matrix - An object with the initial values for the Matrix4x4. \r\n * Can be either an Object or Matrix4x4.\r\n *\/\r\nvar Matrix4x4 = function(matrix) {    \r\n    if (matrix) {\r\n        this.set_from_object(matrix);\r\n    } else {\r\n        this.set_identity();\r\n    }\r\n}\r\n\r\n\/**\r\n * xx component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.xx;\r\n\r\n\/**\r\n * xy component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.xy;\r\n\r\n\/**\r\n * xz component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.xz;\r\n\r\n\/**\r\n * xw component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.xw;\r\n\r\n\/**\r\n * yx component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.yx;\r\n\r\n\/**\r\n * yy component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.yy;\r\n\r\n\/**\r\n * yz component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.yz;\r\n\r\n\/**\r\n * yw component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.yw;\r\n\r\n\/**\r\n * zx component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.zx;\r\n\r\n\/**\r\n * zy component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.zy;\r\n\r\n\/**\r\n * zz component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.zz;\r\n\r\n\/**\r\n * zw component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.zw;\r\n\r\n\/**\r\n * wx component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.wx;\r\n\r\n\/**\r\n * wy component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.wy;\r\n\r\n\/**\r\n * wz component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.wz;\r\n\r\n\/**\r\n * ww component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.ww;\r\n\r\n\/**\r\n * xx component of the matrix.\r\n * @type {Number}\r\n * @public\r\n *\/\r\n\r\n\/**\r\n * Set matrix components from object\r\n * @param {Object} obj - Object with each component of the matrix, all components must be present.\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.set_from_object = function(obj) {\r\n\r\n    this.xx = obj.xx; this.xy = obj.xy; this.xz = obj.xz; this.xw = obj.xw;\r\n    this.yx = obj.yx; this.yy = obj.yy; this.yz = obj.yz; this.yw = obj.yw;\r\n    this.zx = obj.zx; this.zy = obj.zy; this.zz = obj.zz; this.zw = obj.zw;\r\n    this.wx = obj.wx; this.wy = obj.wy; this.wz = obj.wz; this.ww = obj.ww;\r\n\r\n}\r\n\r\n\/**\r\n * Set all matrix components to 0.\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.clear = function() {\r\n    this.xx = this.xy = this.xz = this.xw =\r\n    this.yx = this.yy = this.yz = this.yw =\r\n    this.zx = this.zy = this.zz = this.zw =\r\n    this.wx = this.wy = this.wz = this.ww = 0;\r\n}\r\n\r\n\/**\r\n * Set matrix to the identity matrix.\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.set_identity = function() {\r\n    this.clear();\r\n    this.xx = this.yy = this.zz = this.ww = 1;\r\n}\r\n\r\n\/**\r\n * Sets this matrix to a rotation matrix.\r\n * @param {Number} axis - The vector to rotate around.\r\n * @param {Number} angle - The angle to rotate in radians.\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.set_rotation = function(axis, angle) {\r\n    this.set_identity();\r\n\r\n    var c = Math.cos(angle);\r\n    var s = Math.sin(angle);\r\n    var t = 1-c;\r\n    var X = axis.x;\r\n    var Y = axis.y;\r\n    var Z = axis.z;\r\n\r\n    this.xx = t * X * X + c;\r\n    this.xy = t * X * Y + s * Z;\r\n    this.xz = t * X * Z - s * Y;\r\n\r\n    this.yx = t * X * Y - s * Z;\r\n    this.yy = t * Y * Y + c;\r\n    this.yz = t * Y * Z + s * X;\r\n\r\n    this.zx = t * X * Z + s * Y;\r\n    this.zy = t * Y * Z - s * X;\r\n    this.zz = t * Z * Z + c;\r\n}\r\n\r\n\/**\r\n * Sets this matrix to a rotation matrix.\r\n * @param {Number} x - Scaling factor in the x axis.\r\n * @param {Number} y - Scaling factor in the y axis.\r\n * @param {Number} z - Scaling factor in the z axis.\r\n * @public\r\n *\/\r\nMatrix4x4.prototype.set_scaling = function(x, y, z) {\r\n    this.set_identity();\r\n\r\n    this.xx = x;\r\n    this.yy = y;\r\n    this.zz = z;\r\n}\r\n\r\n\/**\r\n * Sets the translation elements of this matrix while leaving the \r\n * rest of the matrix untouched. \r\n * @param {Number} x - Translation amount in the x axis.\r\n * @param {Number} y - Translation amount in the y axis.\r\n * @param {Number} z - Translation amount in the z axis.\r\n *\/ \r\nMatrix4x4.prototype.set_translation_elements = function(x, y, z) {\r\n    this.wx = x;\r\n    this.wy = y;\r\n    this.wz = z;\r\n}\r\n\r\n\/**\r\n * Sets this matrix to the dot product between this matrix and the \r\n * matrix specified by rhs.\r\n * @param {Matrix4x4} matrix - The matrix on the right hand side of the dot product.\r\n *\/ \r\nMatrix4x4.prototype.multiply = function(matrix) {\r\n\r\n    var _mat = new Matrix4x4(this);\r\n\r\n    this.xx = _mat.xx * matrix.xx + _mat.xy * matrix.yx + _mat.xz * matrix.zx + _mat.xw * matrix.wx;\r\n    this.xy = _mat.xx * matrix.xy + _mat.xy * matrix.yy + _mat.xz * matrix.zy + _mat.xw * matrix.wy;\r\n    this.xz = _mat.xx * matrix.xz + _mat.xy * matrix.yz + _mat.xz * matrix.zz + _mat.xw * matrix.wz;\r\n    this.xw = _mat.xx * matrix.xw + _mat.xy * matrix.yw + _mat.xz * matrix.zw + _mat.xw * matrix.ww;\r\n    this.yx = _mat.yx * matrix.xx + _mat.yy * matrix.yx + _mat.yz * matrix.zx + _mat.yw * matrix.wx;\r\n    this.yy = _mat.yx * matrix.xy + _mat.yy * matrix.yy + _mat.yz * matrix.zy + _mat.yw * matrix.wy;\r\n    this.yz = _mat.yx * matrix.xz + _mat.yy * matrix.yz + _mat.yz * matrix.zz + _mat.yw * matrix.wz;\r\n    this.yw = _mat.yx * matrix.xw + _mat.yy * matrix.yw + _mat.yz * matrix.zw + _mat.yw * matrix.ww;\r\n    this.zx = _mat.zx * matrix.xx + _mat.zy * matrix.yx + _mat.zz * matrix.zx + _mat.zw * matrix.wx;\r\n    this.zy = _mat.zx * matrix.xy + _mat.zy * matrix.yy + _mat.zz * matrix.zy + _mat.zw * matrix.wy;\r\n    this.zz = _mat.zx * matrix.xz + _mat.zy * matrix.yz + _mat.zz * matrix.zz + _mat.zw * matrix.wz;\r\n    this.zw = _mat.zx * matrix.xw + _mat.zy * matrix.yw + _mat.zz * matrix.zw + _mat.zw * matrix.ww;\r\n    this.wx = _mat.wx * matrix.xx + _mat.wy * matrix.yx + _mat.wz * matrix.zx + _mat.ww * matrix.wx;\r\n    this.wy = _mat.wx * matrix.xy + _mat.wy * matrix.yy + _mat.wz * matrix.zy + _mat.ww * matrix.wy;\r\n    this.wz = _mat.wx * matrix.xz + _mat.wy * matrix.yz + _mat.wz * matrix.zz + _mat.ww * matrix.wz;\r\n    this.ww = _mat.wx * matrix.xw + _mat.wy * matrix.yw + _mat.wz * matrix.zw + _mat.ww * matrix.ww;\r\n}\r\n\r\n\/**\r\n * Returns a deep copy of this matrix.\r\n * @return {Matrix4x4} A deep copy of this matrix.\r\n *\/ \r\nMatrix4x4.prototype.clone = function() {\r\n    return new Matrix4x4(this);\r\n}\r\n\r\n\/**\r\n * Returns a pretty print string representation of the matrix.\r\n * @return {String} Pretty printed string of the matrix.\r\n *\/ \r\nMatrix4x4.prototype.to_string = function() {\r\n    return '{\\n'\r\n        + '\\t"xx": ' + this.xx + ', "xy": ' + this.xy + ', "xz": ' + this.xz + ', "xw": ' + this.xw + ',\\n'\r\n        + '\\t"yx": ' + this.yx + ', "yy": ' + this.yy + ', "yz": ' + this.yz + ', "yw": ' + this.yw + ',\\n'\r\n        + '\\t"zx": ' + this.zx + ', "zy": ' + this.zy + ', "zz": ' + this.zz + ', "zw": ' + this.zw + ',\\n'\r\n        + '\\t"wx": ' + this.wx + ', "wy": ' + this.wy + ', "wz": ' + this.wz + ', "ww": ' + this.ww + '\\n'\r\n        +'}';\r\n}\r\n\r\nmodule.exports = Matrix4x4;\r\n<\/pre>\n<\/p><\/div><\/div>\n

Note that this matrix class is generic and just works directly on the values provided. To be used with RealityServer you need to negate values used for translations, negate angles used for rotations and invert values used for scaling. As an example see this simple test code.<\/p>\n

JavaScript Matrix Library Tests<\/i><\/i><\/h3>

\n

\r\nvar Matrix4x4 = require('.\/Matrix4x4.js');\r\n\r\nvar test_translation_matrix = new Matrix4x4();\r\nvar test_translation_vector = {x: 1.5, y: -2.5, z: -0.2};\r\ntest_translation_matrix.set_translation_elements(\r\n\t-test_translation_vector.x,\r\n\t-test_translation_vector.y,\r\n\t-test_translation_vector.z\r\n);\r\nconsole.info("Translation elements set to 1.5, -2.5, 0.2:");\r\nconsole.info(test_translation_matrix.to_string());\r\n\r\nvar test_rotation_matrix = new Matrix4x4();\r\nvar test_rotation_angle = 45;\r\nvar test_rotation_axis = {x: 0, y: 0, z: 1};\r\ntest_rotation_matrix.set_rotation(test_rotation_axis, test_rotation_angle * (Math.PI \/ 180.0));\r\nconsole.info("Rotation matrix for 45 degree rotation around 0, 0, 1 axis:");\r\nconsole.info(test_rotation_matrix.to_string());\r\n\r\nvar test_scaling_matrix = new Matrix4x4();\r\ntest_scaling_vector = {x: 2.0, y: 0.4, z: 10};\r\ntest_scaling_matrix.set_scaling(\r\n\t1 \/ test_scaling_vector.x,\r\n\t1 \/ test_scaling_vector.y,\r\n\t1 \/ test_scaling_vector.z\r\n);\r\nconsole.info("Scaling matrix by 2.0, 0.4, 10:");\r\nconsole.info(test_scaling_matrix.to_string());\r\n\r\nvar test_rotate_then_translate = test_translation_matrix.clone();\r\ntest_rotate_then_translate.multiply(test_rotation_matrix);\r\nconsole.info("Rotate then translate (translate matrix x rotation matrix):");\r\nconsole.info(test_rotate_then_translate.to_string());\r\n\r\nvar test_rotate_then_translate_then_scale = test_rotate_then_translate.clone();\r\ntest_rotate_then_translate_then_scale.multiply(test_scaling_matrix);\r\nconsole.info("Scale then rotate then translate (translate matrix x rotation matrix x scale matrix):");\r\nconsole.info(test_rotate_then_translate_then_scale.to_string());\r\n<\/pre>\n<\/p><\/div><\/div>\n

The above code uses Node.js module style but is easily adapted for use in browsers or other places. It can also obviously be easily ported to any other language.<\/p>\n

Going Further<\/h3>\n

That covers the essentials of setting up transformation matrices in RealityServer (and likely also a wide range of other 3D tools that use affine transformation matrices). While there is some complexity involved, once you have a nice library for working with your transformations it is all quite simple. Often our new users know about transformation matrices but are not sure about the format used for those in RealityServer, the above hopefully clarifies this. If not or if you have any other questions, don’t hesitate to contact us<\/a> for more information.<\/p>\n

Also, keep an eye out for part 2 of this article where I will be diving into the newer SRT transformation type in RealityServer and how it can help with easily animating your objects.<\/p>\n","protected":false},"excerpt":{"rendered":"

Transformations are fundamental to working with 3D scenes and something that can be frequently confusing to those that haven’t worked in 3D before. In this, the first of two articles I will show you how to encode 3D transformations as a single 4×4 matrix which you can then pass into the appropriate RealityServer command to […]<\/p>\n","protected":false},"author":2,"featured_media":1550,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[15,30],"tags":[26,14,27],"jetpack_featured_media_url":"https:\/\/www.migenius.com\/migenius\/wp-content\/uploads\/2016\/10\/transformations_feature.jpg","_links":{"self":[{"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/posts\/1549"}],"collection":[{"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/comments?post=1549"}],"version-history":[{"count":45,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/posts\/1549\/revisions"}],"predecessor-version":[{"id":1605,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/posts\/1549\/revisions\/1605"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/media\/1550"}],"wp:attachment":[{"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/media?parent=1549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/categories?post=1549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/tags?post=1549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}