{"id":1518,"date":"2016-10-06T08:16:19","date_gmt":"2016-10-06T08:16:19","guid":{"rendered":"http:\/\/www.migenius.com\/?p=1518"},"modified":"2016-10-25T03:47:54","modified_gmt":"2016-10-25T03:47:54","slug":"creating-lighting-programmatically","status":"publish","type":"post","link":"https:\/\/www.migenius.com\/articles\/creating-lighting-programmatically","title":{"rendered":"Creating Lighting Programmatically"},"content":{"rendered":"

In this article I am going to show you how add light sources to your RealityServer scene using the Web-services API. You will learn how to add several different types of lights, including a photometric light using an IES data file, an area light, a spot light and daylight. This will be a very simple example but will give you all of the pieces you need to programmatically add lighting to your scene. You can expand on the concepts shown here to make different types of lighting very easily.<\/p>\n

<\/p>\n

Before You Begin<\/h3>\n

If you haven\u2019t already done so, I highly recommend reading the articles on\u00a0Exploring the RealityServer JSON-RPC API<\/a>\u00a0and\u00a0Creating a Simple Scene Programmatically<\/a>. This article will be assuming you are familiar with the information there, particularly how RealityServer commands work. The scene you will build here will be created entirely by giving RealityServer commands to execute. To follow along you will need an environment from which to send these commands to RealityServer as well as a running instance of RealityServer itself. I\u00a0recommend\u00a0Postman<\/a>\u00a0for sending commands, however any tool that can send HTTP POST requests will do.<\/p>\n

This article also assumes you are running RealityServer 4.4 build 1527.40 or later and uses some features introduced in that version. Even so the information can be easily adapted to earlier versions if needed. Of course we do recommend you use the latest version if at all possible.<\/p>\n

Running in a Scope<\/h3>\n

He is a quick tip for when you are testing your command sequences which can be very useful if running the same sequences over and over again on your server. If you use this pattern you can keep running your command sequences without restarting your server or deleting things from the database. Here’s the code:<\/p>\n

\r\n[\r\n\t{"jsonrpc": "2.0", "method": "create_scope", "params": {\r\n\t\t"scope_name" : "exScope"\r\n\t}, "id": 1},\r\n\t{"jsonrpc": "2.0", "method": "use_scope", "params": {\r\n\t\t"scope_name" : "exScope"\r\n\t}, "id": 2},\r\n\r\n\t... good stuff goes here ...\r\n\r\n\t{"jsonrpc": "2.0", "method": "delete_scope", "params": {\r\n\t\t"scope_name" : "exScope"\r\n\t}, "id": 1000}\r\n]\r\n<\/pre>\n

If you have multiple people using your RealityServer or you plan to make multiple simultaneous requests you might want to use a more random scope name, however if you are just testing on your own server a fixed name is fine. Put all of your commands in the middle and when you run the sequence everything you make will go into the scope, when you delete the scope it will all be removed. This way you can run it over and over again without name clashes. Great for testing and experimentation.<\/p>\n

Setting up the Scene<\/h3>\n

We won’t focus on building the geometry of the scene in this article. All of the code is here for you to examine but we won’t expand too much on the detail as we want to focus on adding lights to the scene.<\/p>\n

Before adding lighting though, we of course need something to illuminate. Using some of the concepts from earlier articles we are going to make our scene entirely using RealityServer commands rather than importing a pre-existing scene. For our lighting example we are going to build a small room with some holes in the ceiling to serve as skylights in the final steps when we add daylighting to the scene. Most of the complexity comes in the generate_mesh<\/em> command and working out the input for it. I’ll leave working that out as an exercise for the reader. On the right is the geometry of the scene we are making.<\/p>\n<\/div><\/div>

\"Room\"<\/a><\/p>\n

The Geometry of our Test Room<\/p>\n<\/div>\n<\/div><\/div><\/div>\n

Code and Instructions for Building Scene<\/i><\/i><\/h3>

\n

Use create_scene<\/em> to setup the container in our database to hold the scene information:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "create_scene", "params": {\r\n\t"scene_name" : "exScene"\r\n}, "id": 1}\r\n<\/pre>\n

Call create_element<\/em> with the type Options<\/em> to make our rendering options:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exOptions",\r\n\t"element_type" : "Options"\r\n}, "id": 2}\r\n<\/pre>\n

Attach the new options to the scene with the scene_set_options<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "scene_set_options", "params": {\r\n\t"scene_name" : "exScene",\r\n\t"options" : "exOptions"\r\n}, "id": 3}\r\n<\/pre>\n

Create a new group with the create_element<\/em> command using the type Group<\/em>:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exRootGroup",\r\n\t"element_type" : "Group"\r\n}, "id": 4}\r\n<\/pre>\n

Attach the root group to the scene with the scene_set_rootgroup<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "scene_set_rootgroup", "params": {\r\n\t"scene_name" : "exScene",\r\n\t"group" : "exRootGroup"\r\n}, "id": 5}\r\n<\/pre>\n

Create the camera using the create_element<\/em> command with the type Camera<\/em>:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exCamera",\r\n\t"element_type" : "Camera"\r\n}, "id": 6}\r\n<\/pre>\n

Set the resolution of the camera with the camera_set_resolution<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "camera_set_resolution", "params": {\r\n\t"camera_name" : "exCamera",\r\n\t"resolution" : { "x" : 640, "y" : 200 }\r\n}, "id": 7}\r\n<\/pre>\n

Set the aspect ratio of the camera with the camera_set_aspect<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "camera_set_aspect", "params": {\r\n\t"camera_name" : "exCamera",\r\n\t"aspect" : 3.2\r\n}, "id": 8}\r\n<\/pre>\n

Create a new instance for the camera with the create_element<\/em> command and the type Instance<\/em>:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exCameraInstance",\r\n\t"element_type" : "Instance"\r\n}, "id": 9}\r\n<\/pre>\n

Move and orient the camera instance into position with the instance_set_world_to_obj<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "instance_set_world_to_obj", "params": {\r\n\t"instance_name" : "exCameraInstance",\r\n\t"transform" : {\r\n\t\t"xx": 1.0,  "xy": 0.0,  "xz": 0.0,    "xw": 0.0,\r\n\t\t"yx": 0.0,  "yy": 0.0,  "yz": -1.0,   "yw": 0.0,\r\n\t\t"zx": 0.0,  "zy": 1.0,  "zz": 0.0,    "zw": 0.0,\r\n\t\t"wx": -5.0, "wy": -1.5, "wz": -9.55, "ww": 1.0\r\n\t}\r\n}, "id": 10}\r\n<\/pre>\n

Attach the actual camera to the instance we made earlier with the instance_attach<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t"instance_name" : "exCameraInstance",\r\n\t"item_name" : "exCamera"\r\n}, "id": 11}\r\n<\/pre>\n

Attach the camera instance to the root group of the scene with the group_attach<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t"group_name" : "exRootGroup",\r\n\t"item_name" : "exCameraInstance"\r\n}, "id": 12}\r\n<\/pre>\n

Attach the camera instance to the scene definition with the scene_set_camera_instance<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "scene_set_camera_instance", "params": {\r\n\t"scene_name" : "exScene",\r\n\t"camera_instance" : "exCameraInstance"\r\n}, "id": 13}\r\n<\/pre>\n

Set the tone-mapping options on the camera definition with the element_set_attributes<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "element_set_attributes", "params": {\r\n\t"create" : true,\r\n\t"element_name" : "exCamera",\r\n\t"attributes" : {\r\n\t\t"tm_tonemapper" : {\r\n\t\t\t"type" : "String",\r\n\t\t\t"value" : "mia_exposure_photographic"\r\n\t\t},\r\n\t\t"mip_cm2_factor" : {\r\n\t\t\t"type" : "Float32",\r\n\t\t\t"value" : 1.0\r\n\t\t},\r\n\t\t"mip_film_iso" : {\r\n\t\t\t"type" : "Float32",\r\n\t\t\t"value" : 100.0\r\n\t\t},\r\n\t\t"mip_camera_shutter" : {\r\n\t\t\t"type" : "Float32",\r\n\t\t\t"value" : 10.0\r\n\t\t},\r\n\t\t"mip_f_number" : {\r\n\t\t\t"type" : "Float32",\r\n\t\t\t"value" : 2.8\r\n\t\t},\r\n\t\t"mip_gamma" : {\r\n\t\t\t"type" : "Float32",\r\n\t\t\t"value" : 2.2\r\n\t\t}\r\n\t}\r\n}, "id": 14}\r\n<\/pre>\n

Make the mesh object for the room using the generate_mesh<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "generate_mesh", "params": {\r\n\t"name" : "exObject",\r\n\t"mesh" : {"vectors":{"points":[{"x":0,"y":0,"z":0},{"x":0,"y":0,"z":3},{"x":0,"y":5,"z":0},{"x":0,"y":5,"z":3},{"x":10,"y":0,"z":0},{"x":10,"y":0,"z":3},{"x":10,"y":5,"z":0},{"x":10,"y":5,"z":3},{"x":0,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":3,"z":3},{"x":0,"y":3,"z":3},{"x":3,"y":2,"z":3},{"x":4,"y":2,"z":3},{"x":4,"y":3,"z":3},{"x":3,"y":3,"z":3},{"x":6,"y":2,"z":3},{"x":7,"y":2,"z":3},{"x":7,"y":3,"z":3},{"x":6,"y":3,"z":3},{"x":9,"y":2,"z":3},{"x":10,"y":2,"z":3},{"x":10,"y":3,"z":3},{"x":9,"y":3,"z":3}],"normals":[{"x":-1,"y":0,"z":0},{"x":0,"y":1,"z":0},{"x":1,"y":0,"z":0},{"x":0,"y":0,"z":-1},{"x":0,"y":0,"z":1},{"x":0,"y":-1,"z":0}],"uvs":[{"x":0,"y":0},{"x":0,"y":3},{"x":5,"y":0},{"x":5,"y":3},{"x":0,"y":0},{"x":0,"y":3},{"x":10,"y":0},{"x":10,"y":3},{"x":10,"y":0},{"x":0,"y":0},{"x":0,"y":5},{"x":10,"y":5},{"x":0,"y":2},{"x":0,"y":3},{"x":1,"y":2},{"x":1,"y":3},{"x":3,"y":2},{"x":3,"y":3},{"x":4,"y":2},{"x":4,"y":3},{"x":6,"y":2},{"x":6,"y":3},{"x":7,"y":2},{"x":7,"y":3},{"x":9,"y":2},{"x":9,"y":3},{"x":10,"y":2},{"x":10,"y":3}]},"vertices":[{"v":0,"n":0,"t":2},{"v":2,"n":0,"t":0},{"v":3,"n":0,"t":1},{"v":1,"n":0,"t":3},{"v":2,"n":1,"t":6},{"v":6,"n":1,"t":4},{"v":7,"n":1,"t":5},{"v":3,"n":1,"t":7},{"v":6,"n":2,"t":2},{"v":4,"n":2,"t":0},{"v":5,"n":2,"t":1},{"v":7,"n":2,"t":3},{"v":4,"n":3,"t":8},{"v":0,"n":3,"t":9},{"v":2,"n":3,"t":10},{"v":6,"n":3,"t":11},{"v":0,"n":5,"t":4},{"v":4,"n":5,"t":6},{"v":5,"n":5,"t":7},{"v":1,"n":5,"t":5},{"v":8,"n":4,"t":12},{"v":9,"n":4,"t":14},{"v":10,"n":4,"t":15},{"v":11,"n":4,"t":13},{"v":12,"n":4,"t":16},{"v":13,"n":4,"t":18},{"v":14,"n":4,"t":19},{"v":15,"n":4,"t":17},{"v":16,"n":4,"t":20},{"v":17,"n":4,"t":22},{"v":18,"n":4,"t":23},{"v":19,"n":4,"t":21},{"v":20,"n":4,"t":24},{"v":21,"n":4,"t":26},{"v":22,"n":4,"t":27},{"v":23,"n":4,"t":25},{"v":1,"n":4,"t":9},{"v":5,"n":4,"t":8},{"v":21,"n":4,"t":26},{"v":8,"n":4,"t":12},{"v":11,"n":4,"t":13},{"v":22,"n":4,"t":27},{"v":7,"n":4,"t":11},{"v":3,"n":4,"t":10}],"polygons":[[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15],[16,17,18,19],[20,21,22,23],[24,25,26,27],[28,29,30,31],[32,33,34,35],[36,37,38,39],[40,41,42,43]]}\r\n}, "id": 15}\r\n<\/pre>\n

Set the visible attribute on the mesh object using the element_set_attribute<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t"element_name" : "exObject",\r\n\t"attribute_type" : "Boolean",\r\n\t"attribute_name" : "visible",\r\n\t"attribute_value" : true,\r\n\t"create" : true\r\n}, "id": 16}\r\n<\/pre>\n

Create a new instance for the room mesh using the create_element<\/em> command with the type Instance<\/em>:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exObjectInstance",\r\n\t"element_type" : "Instance"\r\n}, "id": 17}\r\n<\/pre>\n

Attach the mesh object to the newly created instance with the instance_attach<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t"instance_name" : "exObjectInstance",\r\n\t"item_name" : "exObject"\r\n}, "id": 18}\r\n<\/pre>\n

Attach the instance of the room mesh to the root group with the group_attach<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t"group_name" : "exRootGroup",\r\n\t"item_name" : "exObjectInstance"\r\n}, "id": 19}\r\n<\/pre>\n

Call the import_scene_elements<\/em> command to load the architectural material definition from mdl::material_examples::architectural.mdl<\/em>:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "import_scene_elements", "params": {\r\n\t"filename": "${shader}\/material_examples\/architectural.mdl"\r\n}, "id": 20}\r\n<\/pre>\n

Create an instance of the architectural<\/em> material with the create_material_instance_from_definition<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "create_material_instance_from_definition", "params": {\r\n\t"arguments": {\r\n\t\t"diffuse" : { "r" : 0.7, "g" : 0.7, "b" : 0.7 },\r\n\t\t"reflectivity" : 0.0\r\n\t},\r\n\t"material_definition_name": "mdl::material_examples::architectural::architectural",\r\n\t"material_name": "exMaterial"\r\n}, "id": 21}\r\n<\/pre>\n

Call the instance_set_material<\/em> command the assign the material we just made to the instance of the room mesh:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "instance_set_material", "params": {\r\n\t"instance_name" : "exObjectInstance",\r\n\t"material_name" : "exMaterial"\r\n}, "id": 22}\r\n<\/pre>\n

Add a section plane to cut the front of the room away so we can see inside with the element_set_attribute<\/em> command:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t"element_name" : "exOptions",\r\n\t"attribute_name" : "section_planes",\r\n\t"attribute_type" : "Section_plane[]",\r\n\t"attribute_value": [\r\n\t\t{\r\n\t\t\t"origin": { "x": 0.0, "y": 0.0001, "z": 0.0 },\r\n\t\t\t"normal": { "x": 0.0, "y": -1.000, "z": 0.0 },\r\n\t\t\t"clip_light": true,\r\n\t\t\t"disabled": false\r\n\t\t}\r\n\t],\r\n\t"create": true\r\n}, "id": 23}\r\n<\/pre>\n<\/p><\/div><\/div>\n

This will give us a nice scene to test our lighting. Of course if you render it now, it will just come out black since there is nothing to light up the geometry we have made. So let’s move on to adding some light. We’ll add four types of lighting in this article, starting with a simple photometric light using a light profile.<\/p>\n

Adding a Light Profile Based Light<\/h3>\n

Light profiles in Iray are a special type of data which is loaded from an IES<\/a> file (usually with the extension .ies) which describes, for each outgoing direction of a light source, how much light is emitted. The IES data is typically provided by the lighting manufacturer however you can also find various tools for making these files yourself. So let’s add one of these nice lights to the left side of our room. Prior to doing this I have obtained an IES file called downlight.ies<\/em> and placed it in a new folder on my RealityServer called ies<\/em> in the\u00a0content_root<\/em> folder. With that file in place we can do the following:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "import_scene_elements", "params": {\r\n\t"filename": "${shader}\/material_examples\/lights_photometric.mdl"\r\n}, "id": 25},\r\n\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exIesLight",\r\n\t"element_type" : "Light"\r\n}, "id": 28},\r\n\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exIesLightProfile",\r\n\t"element_type" : "Lightprofile"\r\n}, "id": 29},\r\n\r\n{"jsonrpc": "2.0", "method": "lightprofile_set_filename", "params": {\r\n\t"file_name" : "ies\/downlight.ies",\r\n\t"lightprofile_name" : "exIesLightProfile"\r\n}, "id": 30},\r\n\r\n{"jsonrpc": "2.0", "method": "create_material_instance_from_definition", "params": {\r\n\t"material_definition_name" : "mdl::material_examples::lights_photometric::ies_light",\r\n\t"material_name" : "exIesLightShader",\r\n\t"arguments" : {\r\n\t\t"flux" : 1200,\r\n\t\t"profile" : "exIesLightProfile"\r\n\t}\r\n}, "id": 31},\r\n\r\n{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t"element_name" : "exIesLight",\r\n\t"attribute_type" : "Ref",\r\n\t"attribute_name" : "material",\r\n\t"attribute_value" : "exIesLightShader",\r\n\t"create" : true\r\n}, "id": 32},\r\n\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exIesLightInstance",\r\n\t"element_type" : "Instance"\r\n}, "id": 33},\r\n\r\n{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t"instance_name" : "exIesLightInstance",\r\n\t"item_name" : "exIesLight"\r\n}, "id": 34},\r\n\r\n{"jsonrpc": "2.0", "method": "instance_set_world_to_obj", "params": {\r\n\t"instance_name" : "exIesLightInstance",\r\n\t"transform" : {\r\n\t\t"xx": 1.0,  "xy": 0.0,  "xz": 0.0,   "xw": 0.0,\r\n\t\t"yx": 0.0,  "yy": 1.0,  "yz": 0.0,   "yw": 0.0,\r\n\t\t"zx": 0.0,  "zy": 0.0,  "zz": 1.0,   "zw": 0.0,\r\n\t\t"wx": -2.0, "wy": -4.75, "wz": -2.95, "ww": 1.0\r\n\t}\r\n}, "id": 35},\r\n\r\n{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t"group_name" : "exRootGroup",\r\n\t"item_name" : "exIesLightInstance"\r\n}, "id": 36}\r\n<\/pre>\n

First we load an MDL material (lights are actually just materials with emissive properties) from disk using the import_scene_elements<\/em> command. The light material definition we will use accepts a light profile as one of its parameters. You can define your own MDL file to handle the light profile differently however that is beyond the scope of this article.<\/p>\n

Next we use create_element<\/em> with the type Light<\/em> to make a light. We also need to call it again with the type Lightprofile<\/em> to make our light profile. Note that this light is just the light definition, it won’t actually get used until we make an instance of it (or multiple instances if we want many of the same light in the scene) and attach it to our scenes root group. Right now the light and light profile have nothing setup so we’ll do that next.<\/p>\n

Using the lightprofile_set_filename<\/em> we can tell RealityServer to load the light profile data from the IES file into memory so the renderer can use it. We have our file in its own directory but of course you can put it anywhere you like.<\/p>\n

We now need to make an instance (a material instance, not an element instance) of the\u00a0ies_light<\/em> material definition we loaded earlier using\u00a0create_material_instance_from_definition<\/em> so that we can attach it to the light with the\u00a0element_set_attribute<\/em> command. For this we set the material<\/em> attribute of the light we created earlier. When we call create_material_instance_from_definition\u00a0<\/em>we also provide the parameters for the light material, in our case we have given it a flux<\/em> (brightness) and set the profile<\/em> attribute to point at the light profile element we created earlier.<\/p>\n

The light is now all setup but not actually in the scene, for that we need to make an element instance using create_element<\/em> with the type Instance<\/em>. We then use instance_attach<\/em> to connect the light definition (which in turn references the light material which references the light profile) to the instance of the light.<\/p>\n

Right now the light is just sitting at the origin pointing down. We want to move it to the ceiling and the left of the room. We use the\u00a0instance_set_world_to_obj<\/em> command for this, providing a transformation matrix that positions the light accordingly.<\/p>\n

Finally we attach the light instance to the root group of the scene so that it will actually render. If we now render the scene we should get something that looks like this (for the commands needed to render the scene look at the end of the article).<\/p>\n

\"IES

IES Light Profile light rendered in our scene<\/p><\/div>\n

Notice the complex scalloping effect when the light hits the wall. This is all generated from the information in the light profile. Next time you see a small downlight in real life near a wall take a look and you are very likely to see a similar distribution of light.<\/p>\n

Adding a Rectangular Area Light<\/h3>\n

Another common type of light you might want to add is an area light. These are more realistic than lights with no area (point lights) since in the real-world point lights do not exist. Area lights result in soft shadows as well which usually look much nicer than the hard shadows from point lights. We will now put a rectangular area light into the middle of the scene to see what extra properties need to be set for this.<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exRectLight",\r\n\t"element_type" : "Light"\r\n}, "id": 401},\r\n\r\n{"jsonrpc": "2.0", "method": "light_set_area_shape", "params": {\r\n\t"light_name" : "exRectLight",\r\n\t"area_shape" : "rectangle"\r\n}, "id": 402},\r\n\r\n{"jsonrpc": "2.0", "method": "light_set_area_size", "params": {\r\n\t"light_name" : "exRectLight",\r\n\t"area_size" : {"x": 1.2, "y" : 0.01}\r\n}, "id": 403},\r\n\r\n{"jsonrpc": "2.0", "method": "create_material_instance_from_definition", "params": {\r\n\t"material_definition_name" : "mdl::material_examples::lights_photometric::diffuse_area_light",\r\n\t"material_name" : "exRectLightShader",\r\n\t"arguments" : {\r\n\t\t"flux" : 900\r\n\t}\r\n}, "id": 404},\r\n\r\n{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t"element_name" : "exRectLight",\r\n\t"attribute_type" : "Ref",\r\n\t"attribute_name" : "material",\r\n\t"attribute_value" : "exRectLightShader",\r\n\t"create" : true\r\n}, "id": 405},\r\n\r\n{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t"element_name" : "exRectLight",\r\n\t"attribute_type" : "Boolean",\r\n\t"attribute_name" : "visible",\r\n\t"attribute_value" : true,\r\n\t"create" : true\r\n}, "id": 410},\r\n\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exRectLightInstance",\r\n\t"element_type" : "Instance"\r\n}, "id": 406},\r\n\r\n{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t"instance_name" : "exRectLightInstance",\r\n\t"item_name" : "exRectLight"\r\n}, "id": 407},\r\n\r\n{"jsonrpc": "2.0", "method": "instance_set_world_to_obj", "params": {\r\n\t"instance_name" : "exRectLightInstance",\r\n\t"transform" : {\r\n\t\t"xx": 1.0,  "xy": 0.0,  "xz": 0.0,   "xw": 0.0,\r\n\t\t"yx": 0.0,  "yy": 1.0,  "yz": 0.0,   "yw": 0.0,\r\n\t\t"zx": 0.0,  "zy": 0.0,  "zz": 1.0,   "zw": 0.0,\r\n\t\t"wx": -5.0, "wy": -4.75, "wz": -2.95, "ww": 1.0\r\n\t}\r\n}, "id": 408},\r\n\r\n{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t"group_name" : "exRootGroup",\r\n\t"item_name" : "exRectLightInstance"\r\n}, "id": 409}\r\n<\/pre>\n

For our rectangular area light most of the steps are the same, almost identical in fact, except we leave out the parts having to do with the light profile creation and instead of creating in instance of the ies_light<\/em> material we will use the diffuse_area_light<\/em> material and just give it a flux. We also put it in a different position, in the middle of the back wall.<\/p>\n

We omit the\u00a0import_scene_elements<\/em> command to load the MDL here since we already did that for the photometric light and the light material definition is in the same file. If you are not adding the previous light, be sure to add the appropriate\u00a0import_scene_elements\u00a0<\/em>command to the above sequence.<\/p>\n

Note that with this MDL light type the flux is per unit area. So making the light larger will increase the amount of light in the scene. Often you might want to specify a total output independent of the size of the area light. You can do this in MDL using the mode<\/em> parameter on the material_emission<\/em> structure and setting it to intensity_power<\/em>. See the MDL Specifications<\/a> for more details.<\/p>\n

We also need to tell RealityServer that we want the light to be an area light. We use two special commands for this, light_set_area_shape<\/em> and light_set_area_size<\/em>. The shape can be either rectangle<\/em>, disc<\/em>, cylinder<\/em> or sphere<\/em>. The area_size<\/em> attribute takes a structure of two floating point numbers, what they mean varies depending on the type of shape used (note that sphere and disc have a different command, light_set_area_radius<\/em>). We are using a rectangle so it’s the length and width. We set this to 1.2m and 0.01m respectively.<\/p>\n

One other optional step we took was to set the visible attribute on the light (not the light instance) using element_set_attribute<\/em>. This allows you to see the area light when viewed directly (it always shows up in reflections and through transmission regardless). Adding the above to our command sequence should give the following image:<\/p>\n

\"Rectangle

Rectangular area light added to our scene<\/p><\/div>\n

Notice how the light is emitted from the entire area of the light rather than a single point. You can experiment with different shapes and sizes easily by changing a few commands.<\/p>\n

Adding a Spot Light<\/h3>\n

For our third light we are going to use a more traditional spot light with a simple spread angle and exponent based falloff. This type of light used to be used frequently before light profiles were available and can still be useful in some cases. We will make it a disc shaped area source as well. Here’s the code:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exSpotLight",\r\n\t"element_type" : "Light"\r\n}, "id": 501},\r\n\r\n{"jsonrpc": "2.0", "method": "light_set_area_shape", "params": {\r\n\t"light_name" : "exSpotLight",\r\n\t"area_shape" : "disc"\r\n}, "id": 502},\r\n\r\n{"jsonrpc": "2.0", "method": "light_set_area_radius", "params": {\r\n\t"light_name" : "exSpotLight",\r\n\t"area_radius" : 0.05\r\n}, "id": 503},\r\n\r\n{"jsonrpc": "2.0", "method": "create_material_instance_from_definition", "params": {\r\n\t"material_definition_name" : "mdl::material_examples::lights_photometric::spot_light",\r\n\t"material_name" : "exSpotLightShader",\r\n\t"arguments" : {\r\n\t\t"flux" : 1500,\r\n\t\t"spot_exponent" : 2.5,\r\n\t\t"spot_spread_angle" : 1.483530\r\n\t}\r\n}, "id": 504},\r\n\r\n{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t"element_name" : "exSpotLight",\r\n\t"attribute_type" : "Ref",\r\n\t"attribute_name" : "material",\r\n\t"attribute_value" : "exSpotLightShader",\r\n\t"create" : true\r\n}, "id": 505},\r\n\r\n{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t"element_name" : "exSpotLight",\r\n\t"attribute_type" : "Boolean",\r\n\t"attribute_name" : "visible",\r\n\t"attribute_value" : true,\r\n\t"create" : true\r\n}, "id": 410},\r\n\r\n{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t"element_name" : "exSpotLightInstance",\r\n\t"element_type" : "Instance"\r\n}, "id": 506},\r\n\r\n{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t"instance_name" : "exSpotLightInstance",\r\n\t"item_name" : "exSpotLight"\r\n}, "id": 507},\r\n\r\n{"jsonrpc": "2.0", "method": "instance_set_world_to_obj", "params": {\r\n\t"instance_name" : "exSpotLightInstance",\r\n\t"transform" : {\r\n\t\t"xx": 1.0,  "xy": 0.0,  "xz": 0.0,   "xw": 0.0,\r\n\t\t"yx": 0.0,  "yy": 1.0,  "yz": 0.0,   "yw": 0.0,\r\n\t\t"zx": 0.0,  "zy": 0.0,  "zz": 1.0,   "zw": 0.0,\r\n\t\t"wx": -8.0, "wy": -4.75, "wz": -2.95, "ww": 1.0\r\n\t}\r\n}, "id": 508},\r\n\r\n{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t"group_name" : "exRootGroup",\r\n\t"item_name" : "exSpotLightInstance"\r\n}, "id": 509}\r\n<\/pre>\n

Again, the general process is the same as the previous two lights. This one is positioned on the right side of the rear wall though. Instead of ies_light<\/em> or diffuse_area_light<\/em> we will use the spot_light<\/em> MDL material. This one takes some extra parameters, spot_exponent<\/em> and spot_spread_angle<\/em>.<\/p>\n

The spot exponent controls how soft the edge of the spot area is, higher numbers will give a more blended edge, it should however always be greater than 0. The spread angle is given in radians and specifies the maximum angle after which no light will be emitted. So a value of\u00a01.483530 will give a spot light with a beam\u00a0angle of 85 degrees.<\/p>\n

We also use a new command here,\u00a0light_set_area_radius<\/em> instead of the previous\u00a0light_set_area_size<\/em> command. This just takes a single value and is used for disc and sphere type area lights. Putting it all together and rendering it we should get the following image:<\/p>\n

\"Spot

Spot light with a disc area shape added to our\u00a0scene<\/p><\/div>\n

You might be wondering why the disc of the spotlight looks dark. This is due to how Iray treats the direct visibility of area light sources. Basically it shows you whatever light is emitted at the angle you are looking at it from. Since we are looking at it very side on and the spot light doesn’t emit any light in our direction the surface of the area source comes out dark. If you change the view to be from below looking directly up at the light it will look bright. If you do not care about accuracy you can compensate for this in the MDL material used by adding a constant illumination term so there is light coming out at all angles.<\/p>\n

Adding Daylight<\/h3>\n

So the room we made was created with 3 holes in the ceiling for a good reason, we want somewhere for some daylight to enter. Daylight is created using an environment function rather than a light element since the light from daylight comes from essentially everywhere. Adding daylight to our scene is actually relatively simple. Here is the code:<\/p>\n

\r\n{"jsonrpc": "2.0", "method": "import_scene_elements", "params": {\r\n\t"filename": "${shader}\/base.mdl"\r\n}, "id": 601},\r\n\r\n{"jsonrpc": "2.0", "method": "create_function_call_from_definition", "params": {\r\n\t"arguments" : {\r\n\t\t"multiplier" : 0.10132,\r\n\t\t"rgb_unit_conversion" : { \r\n\t\t\t"r" : 1.0,\r\n\t\t\t"g" : 1.0,\r\n\t\t\t"b" : 1.0\r\n\t\t},\r\n\t\t"sun_disk_intensity" : 1.0,\r\n\t\t"physically_scaled_sun" : true,\r\n\t\t"y_is_up" : false,\r\n\t\t"sun_direction" : { "x" : 0.0, "y" : 0.5, "z" : 1.0 },\r\n\t\t"on" : true\r\n\t},\r\n\t"function_definition_name" : "mdl::base::sun_and_sky(bool,float,color,float,float,float,float,float,color,color,float3,float,float,float,bool,int,bool)",\r\n\t"function_name" : "exSunSky"\r\n}, "id": 602},\r\n\r\n{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t"element_name" : "exOptions",\r\n\t"attribute_type" : "Ref",\r\n\t"attribute_name" : "environment_function",\r\n\t"attribute_value" : "exSunSky",\r\n\t"create" : true\r\n}, "id": 603},\r\n\r\n{"jsonrpc": "2.0", "method": "element_set_attributes", "params": {\r\n\t"create" : true,\r\n\t"element_name" : "exCamera",\r\n\t"attributes" : {\r\n\t\t"tm_tonemapper" : {\r\n\t\t\t"type" : "String",\r\n\t\t\t"value" : "mia_exposure_photographic"\r\n\t\t},\r\n\t\t"mip_cm2_factor" : {\r\n\t\t\t"type" : "Float32",\r\n\t\t\t"value" : 1.0\r\n\t\t},\r\n\t\t"mip_film_iso" : {\r\n\t\t\t"type" : "Float32",\r\n\t\t\t"value" : 100.0\r\n\t\t},\r\n\t\t"mip_camera_shutter" : {\r\n\t\t\t"type" : "Float32",\r\n\t\t\t"value" : 250.0\r\n\t\t},\r\n\t\t"mip_f_number" : {\r\n\t\t\t"type" : "Float32",\r\n\t\t\t"value" : 4.0\r\n\t\t},\r\n\t\t"mip_gamma" : {\r\n\t\t\t"type" : "Float32",\r\n\t\t\t"value" : 2.2\r\n\t\t}\r\n\t}\r\n}, "id": 603}\r\n<\/pre>\n

Iray has a built in, highly accurate and efficient daylighting model. We load this by loading base.mdl<\/em>. Note that there is no base.mdl on disk, this just triggers the internal loading. With this loaded we can access the sun_and_sky<\/em> MDL function.<\/p>\n

Since we want to make an MDL function call and not a material this time we call\u00a0create_function_call_from_definition<\/em>. We pass in the parameters for the daylight system, the ones shown in the code above are those that give you physically based lighting if your scene is in meters.<\/p>\n

For the sun_direction<\/em> we just set it to something quite high in the sky. In reality you would usually drive this from something which computes the correct value based on location, date and time. If you have the RealityServer extras plugins installed you can use the set_sun_position<\/em>\u00a0command for that.<\/p>\n

Once we have our function created we just need to use element_set_attribute<\/em> to set the environment_function<\/em> attribute on the options. Finally above we are resetting our tone-mapping values to be more appropriate for a scene lit by daylighting (if you skip this you may just get a white image).\u00a0If all went well you should get an image like this with all of our lights and daylighting:<\/p>\n

\"Daylight\"

Turning on daylighting for our scene<\/p><\/div>\n

The bright patches on the floor are where the direct sunlight is coming into the space. The daylight system includes both the sun and the sky. The bright patches on the floor generate a lot of indirect illumination.<\/p>\n

What Happened to the Lights?<\/h3>\n

Now, in the last image you might have noticed we can now barely see the lights (if you look closely they do still have an effect). This is normal and correct however it bares some explanation as it\u00a0is a very\u00a0common question we get not just for RealityServer but also for users of our other products that use Iray like Bloom Unit<\/a>.<\/p>\n

The important thing to keep in mind is that the tone-mapper we are using is simulating a camera, not the human eye. The tone-mapper is what compresses the range of values that we compute for the lighting into something that can be displayed on the conventional monitor (which has a much lower dynamic range than real-world scenes).<\/p>\n

It is possible to simulate the eye instead by using alternative tone-mapping techniques, such as Greg Ward’s Visibility Matching Tone Reproduction Operator<\/a>\u00a0for High Dynamic Range Scenes. In fact if you turn off tone-mapping in RealityServer and output a .hdr file you can give it to Radiance’s<\/a> pcond<\/em> utility which implements this tone-mapper. RealityServer has the ability to add tone-mapping plugins should you feel adventurous and want to implement this method.<\/p>\n

Most of the time however this is not what you want and any technique used will involve compressing the contrast in the image for display. To understand the issue a little better let’s look at the actual lighting levels. Below the IES light on the left, with daylighting off we see an illuminance of about 115 lux. When we turn on daylighting it goes up to around 6,115 lux. This means the IES light contributes less than a 2% change in illumination relative to the light provided by the daylight.<\/p>\n

This means there is very little contrast in this region, making the contribution from the light sources difficult to see. This can be frustrating since your eye is much better at seeing this type of variation than the camera we are simulating. However if you take out your camera phone and try to capture an image of artificial and daylighting together in a space you will see a similar\u00a0result to\u00a0the rendering above.<\/p>\n

Complete Command Set<\/h3>\n

Below is the complete command set which generates the last image shown above. You can modify it to experiment with different lighting options, just remember you may need to change the tone-mapping settings when switching between something with daylighting and something without. The command set below also shows you the commands needed to do the rendering part (the last few commands).<\/p>\n

Complete Light Creation Command Set<\/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" : 640, "y" : 200 }\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" : 3.2\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.0,  "xy": 0.0,  "xz": 0.0,    "xw": 0.0,\r\n\t\t\t"yx": 0.0,  "yy": 0.0,  "yz": -1.0,   "yw": 0.0,\r\n\t\t\t"zx": 0.0,  "zy": 1.0,  "zz": 0.0,    "zw": 0.0,\r\n\t\t\t"wx": -5.0, "wy": -1.5, "wz": -9.55, "ww": 1.0\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" : 100.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" : 250.0\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" : 4.0\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_mesh", "params": {\r\n\t\t"name" : "exObject",\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":0,"y":0,"z":0},  {"x":0,"y":0,"z":3},  {"x":0,"y":5,"z":0},  {"x":0,"y":5,"z":3},\r\n\t\t\t\t\t{"x":10,"y":0,"z":0}, {"x":10,"y":0,"z":3}, {"x":10,"y":5,"z":0}, {"x":10,"y":5,"z":3},\r\n\t\t\t\t\t{"x":0,"y":2,"z":3},  {"x":1,"y":2,"z":3},  {"x":1,"y":3,"z":3},  {"x":0,"y":3,"z":3},\r\n\t\t\t\t\t{"x":3,"y":2,"z":3},  {"x":4,"y":2,"z":3},  {"x":4,"y":3,"z":3},  {"x":3,"y":3,"z":3},\r\n\t\t\t\t\t{"x":6,"y":2,"z":3},  {"x":7,"y":2,"z":3},  {"x":7,"y":3,"z":3},  {"x":6,"y":3,"z":3},\r\n\t\t\t\t\t{"x":9,"y":2,"z":3},  {"x":10,"y":2,"z":3}, {"x":10,"y":3,"z":3}, {"x":9,"y":3,"z":3}\r\n\t\t\t\t],\r\n\t\t\t\t"normals" : [\r\n\t\t\t\t\t{"x":-1,"y":0,"z":0}, {"x":0,"y":1,"z":0},\r\n\t\t\t\t\t{"x":1,"y":0,"z":0},  {"x":0,"y":0,"z":-1},\r\n\t\t\t\t\t{"x":0,"y":0,"z":1},  {"x":0,"y":-1,"z":0}\r\n\t\t\t\t],\r\n\t\t\t\t"uvs" : [\r\n\t\t\t\t\t{"x":0,"y":0},  {"x":0,"y":3}, {"x":5,"y":0},  {"x":5,"y":3},\r\n\t\t\t\t\t{"x":0,"y":0},  {"x":0,"y":3}, {"x":10,"y":0}, {"x":10,"y":3},\r\n\t\t\t\t\t{"x":10,"y":0}, {"x":0,"y":0}, {"x":0,"y":5},  {"x":10,"y":5},\r\n\t\t\t\t\t{"x":0,"y":2},  {"x":0,"y":3}, {"x":1,"y":2},  {"x":1,"y":3},\r\n\t\t\t\t\t{"x":3,"y":2},  {"x":3,"y":3}, {"x":4,"y":2},  {"x":4,"y":3},\r\n\t\t\t\t\t{"x":6,"y":2},  {"x":6,"y":3}, {"x":7,"y":2},  {"x":7,"y":3},\r\n\t\t\t\t\t{"x":9,"y":2},  {"x":9,"y":3}, {"x":10,"y":2}, {"x":10,"y":3}\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":2},   {"v":2,"n":0,"t":0},   {"v":3,"n":0,"t":1},   {"v":1,"n":0,"t":3},\r\n\t\t\t\t{"v":2,"n":1,"t":6},   {"v":6,"n":1,"t":4},   {"v":7,"n":1,"t":5},   {"v":3,"n":1,"t":7},\r\n\t\t\t\t{"v":6,"n":2,"t":2},   {"v":4,"n":2,"t":0},   {"v":5,"n":2,"t":1},   {"v":7,"n":2,"t":3},\r\n\t\t\t\t{"v":4,"n":3,"t":8},   {"v":0,"n":3,"t":9},   {"v":2,"n":3,"t":10},  {"v":6,"n":3,"t":11},\r\n\t\t\t\t{"v":0,"n":5,"t":4},   {"v":4,"n":5,"t":6},   {"v":5,"n":5,"t":7},   {"v":1,"n":5,"t":5},\r\n\t\t\t\t{"v":8,"n":4,"t":12},  {"v":9,"n":4,"t":14},  {"v":10,"n":4,"t":15}, {"v":11,"n":4,"t":13},\r\n\t\t\t\t{"v":12,"n":4,"t":16}, {"v":13,"n":4,"t":18}, {"v":14,"n":4,"t":19}, {"v":15,"n":4,"t":17},\r\n\t\t\t\t{"v":16,"n":4,"t":20}, {"v":17,"n":4,"t":22}, {"v":18,"n":4,"t":23}, {"v":19,"n":4,"t":21},\r\n\t\t\t\t{"v":20,"n":4,"t":24}, {"v":21,"n":4,"t":26}, {"v":22,"n":4,"t":27}, {"v":23,"n":4,"t":25},\r\n\t\t\t\t{"v":1,"n":4,"t":9},   {"v":5,"n":4,"t":8},   {"v":21,"n":4,"t":26}, {"v":8,"n":4,"t":12},\r\n\t\t\t\t{"v":11,"n":4,"t":13}, {"v":22,"n":4,"t":27}, {"v":7,"n":4,"t":11},  {"v":3,"n":4,"t":10}\r\n\t\t\t],\r\n\t\t\t"polygons" : [\r\n\t\t\t\t[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15],\r\n\t\t\t\t[16,17,18,19],[20,21,22,23],[24,25,26,27],[28,29,30,31],\r\n\t\t\t\t[32,33,34,35],[36,37,38,39],[40,41,42,43]\r\n\t\t\t]\r\n\t\t}\r\n\t}, "id": 1017},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t\t"element_name" : "exObject",\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" : "exObjectInstance",\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" : "exObjectInstance",\r\n\t\t"item_name" : "exObject"\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" : "exObjectInstance"\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.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": "exMaterial"\r\n\t}, "id": 1023},\r\n\t{"jsonrpc": "2.0", "method": "instance_set_material", "params": {\r\n\t\t"instance_name" : "exObjectInstance",\r\n\t\t"material_name" : "exMaterial"\r\n\t}, "id": 1024},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t\t"element_name" : "exOptions",\r\n\t\t"attribute_name" : "section_planes",\r\n\t\t"attribute_type" : "Section_plane[]",\r\n\t\t"attribute_value": [\r\n\t\t\t{\r\n\t\t\t\t"origin": { "x": 0.0, "y": 0.0001, "z": 0.0 },\r\n\t\t\t\t"normal": { "x": 0.0, "y": -1.000, "z": 0.0 },\r\n\t\t\t\t"clip_light": true,\r\n\t\t\t\t"disabled": false\r\n\t\t\t}\r\n\t\t],\r\n\t\t"create": true\r\n\t}, "id": 1025},\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": 2001},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exIesLight",\r\n\t\t"element_type" : "Light"\r\n\t}, "id": 2002},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exIesLightProfile",\r\n\t\t"element_type" : "Lightprofile"\r\n\t}, "id": 2003},\r\n\t{"jsonrpc": "2.0", "method": "lightprofile_set_filename", "params": {\r\n\t\t"file_name" : "ies\/downlight.ies",\r\n\t\t"lightprofile_name" : "exIesLightProfile"\r\n\t}, "id": 2004},\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::ies_light",\r\n\t\t"material_name" : "exIesLightShader",\r\n\t\t"arguments" : {\r\n\t\t\t"flux" : 1200,\r\n\t\t\t"profile" : "exIesLightProfile"\r\n\t\t}\r\n\t}, "id": 2005},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t\t"element_name" : "exIesLight",\r\n\t\t"attribute_type" : "Ref",\r\n\t\t"attribute_name" : "material",\r\n\t\t"attribute_value" : "exIesLightShader",\r\n\t\t"create" : true\r\n\t}, "id": 2006},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exIesLightInstance",\r\n\t\t"element_type" : "Instance"\r\n\t}, "id": 2007},\r\n\t{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t\t"instance_name" : "exIesLightInstance",\r\n\t\t"item_name" : "exIesLight"\r\n\t}, "id": 2008},\r\n\t{"jsonrpc": "2.0", "method": "instance_set_world_to_obj", "params": {\r\n\t\t"instance_name" : "exIesLightInstance",\r\n\t\t"transform" : {\r\n\t\t\t"xx": 1.0,  "xy": 0.0,  "xz": 0.0,   "xw": 0.0,\r\n\t\t\t"yx": 0.0,  "yy": 1.0,  "yz": 0.0,   "yw": 0.0,\r\n\t\t\t"zx": 0.0,  "zy": 0.0,  "zz": 1.0,   "zw": 0.0,\r\n\t\t\t"wx": -2.0, "wy": -4.75, "wz": -2.95, "ww": 1.0\r\n\t\t}\r\n\t}, "id": 2009},\r\n\t{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t\t"group_name" : "exRootGroup",\r\n\t\t"item_name" : "exIesLightInstance"\r\n\t}, "id": 2010},\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": 3001},\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": 402},\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": 1.2, "y" : 0.01}\r\n\t}, "id": 403},\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" : 900\r\n\t\t}\r\n\t}, "id": 404},\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": 405},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t\t"element_name" : "exRectLight",\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": 410},\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": 406},\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": 407},\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": 1.0,  "xy": 0.0,  "xz": 0.0,   "xw": 0.0,\r\n\t\t\t"yx": 0.0,  "yy": 1.0,  "yz": 0.0,   "yw": 0.0,\r\n\t\t\t"zx": 0.0,  "zy": 0.0,  "zz": 1.0,   "zw": 0.0,\r\n\t\t\t"wx": -5.0, "wy": -4.75, "wz": -2.95, "ww": 1.0\r\n\t\t}\r\n\t}, "id": 408},\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": 409},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exSpotLight",\r\n\t\t"element_type" : "Light"\r\n\t}, "id": 501},\r\n\t{"jsonrpc": "2.0", "method": "light_set_area_shape", "params": {\r\n\t\t"light_name" : "exSpotLight",\r\n\t\t"area_shape" : "disc"\r\n\t}, "id": 502},\r\n\t{"jsonrpc": "2.0", "method": "light_set_area_radius", "params": {\r\n\t\t"light_name" : "exSpotLight",\r\n\t\t"area_radius" : 0.05\r\n\t}, "id": 503},\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::spot_light",\r\n\t\t"material_name" : "exSpotLightShader",\r\n\t\t"arguments" : {\r\n\t\t\t"flux" : 1500,\r\n\t\t\t"spot_exponent" : 2.5,\r\n\t\t\t"spot_spread_angle" : 1.483530\r\n\t\t}\r\n\t}, "id": 504},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t\t"element_name" : "exSpotLight",\r\n\t\t"attribute_type" : "Ref",\r\n\t\t"attribute_name" : "material",\r\n\t\t"attribute_value" : "exSpotLightShader",\r\n\t\t"create" : true\r\n\t}, "id": 505},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t\t"element_name" : "exSpotLight",\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": 506},\r\n\t{"jsonrpc": "2.0", "method": "create_element", "params": {\r\n\t\t"element_name" : "exSpotLightInstance",\r\n\t\t"element_type" : "Instance"\r\n\t}, "id": 507},\r\n\t{"jsonrpc": "2.0", "method": "instance_attach", "params": {\r\n\t\t"instance_name" : "exSpotLightInstance",\r\n\t\t"item_name" : "exSpotLight"\r\n\t}, "id": 508},\r\n\t{"jsonrpc": "2.0", "method": "instance_set_world_to_obj", "params": {\r\n\t\t"instance_name" : "exSpotLightInstance",\r\n\t\t"transform" : {\r\n\t\t\t"xx": 1.0,  "xy": 0.0,  "xz": 0.0,   "xw": 0.0,\r\n\t\t\t"yx": 0.0,  "yy": 1.0,  "yz": 0.0,   "yw": 0.0,\r\n\t\t\t"zx": 0.0,  "zy": 0.0,  "zz": 1.0,   "zw": 0.0,\r\n\t\t\t"wx": -8.0, "wy": -4.75, "wz": -2.95, "ww": 1.0\r\n\t\t}\r\n\t}, "id": 509},\t\r\n\t{"jsonrpc": "2.0", "method": "group_attach", "params": {\r\n\t\t"group_name" : "exRootGroup",\r\n\t\t"item_name" : "exSpotLightInstance"\r\n\t}, "id": 510},\r\n\t{"jsonrpc": "2.0", "method": "import_scene_elements", "params": {\r\n\t\t"filename": "${shader}\/base.mdl"\r\n\t}, "id": 601},\r\n\t{"jsonrpc": "2.0", "method": "create_function_call_from_definition", "params": {\r\n\t\t"arguments" : {\r\n\t\t\t"multiplier" : 0.10132,\r\n\t\t\t"rgb_unit_conversion" : { \r\n\t\t\t\t"r" : 1.0,\r\n\t\t\t\t"g" : 1.0,\r\n\t\t\t\t"b" : 1.0\r\n\t\t\t},\r\n\t\t\t"sun_disk_intensity" : 1.0,\r\n\t\t\t"physically_scaled_sun" : true,\r\n\t\t\t"y_is_up" : false,\r\n\t\t\t"sun_direction" : { "x" : 0.0, "y" : 0.5, "z" : 1.0 },\r\n\t\t\t"on" : true\r\n\t\t},\r\n\t\t"function_definition_name" : "mdl::base::sun_and_sky(bool,float,color,float,float,float,float,float,color,color,float3,float,float,float,bool,int,bool)",\r\n\t\t"function_name" : "exSunSky"\r\n\t}, "id": 602},\r\n\t{"jsonrpc": "2.0", "method": "element_set_attribute", "params": {\r\n\t\t"element_name" : "exOptions",\r\n\t\t"attribute_type" : "Ref",\r\n\t\t"attribute_name" : "environment_function",\r\n\t\t"attribute_value" : "exSunSky",\r\n\t\t"create" : true\r\n\t}, "id": 603},\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" : 5000\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" : 1200\r\n\t\t\t}\r\n\t\t}\r\n\t}, "id": 701},\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": 702},\r\n\t{"jsonrpc": "2.0", "method": "delete_scope", "params": {\r\n\t\t"scope_name" : "exScope"\r\n\t}, "id": 999}\r\n]\r\n<\/pre>\n<\/p><\/div><\/div>\n

Just note that you will need an IES file to make this work. \u00a0You can download our example file\u00a0from here<\/a>. It will need to be placed somewhere under your content_root<\/em> folder, in our example we put it into an ies<\/em> sub-folder.<\/p>\n

Summary<\/h3>\n

In this article we have covered all of the steps needed to programmatically add lighting to a scene, including different types of lights and many of the different ways to set them up. With the flexibility of using MDL for defining lights you can take things a lot further than shown here so you are definitely encouraged to explore both the MDL documentation and the general RealityServer documentation for more detail. As always, contact us<\/a> if you would like more information or would like to request an evaluation of RealityServer.<\/p>\n","protected":false},"excerpt":{"rendered":"

In this article I am going to show you how add light sources to your RealityServer scene using the Web-services API. You will learn how to add several different types of lights, including a photometric light using an IES data file, an area light, a spot light and daylight. This will be a very simple […]<\/p>\n","protected":false},"author":2,"featured_media":1520,"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\/08\/lightcreation-feature.png","_links":{"self":[{"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/posts\/1518"}],"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=1518"}],"version-history":[{"count":21,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/posts\/1518\/revisions"}],"predecessor-version":[{"id":1548,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/posts\/1518\/revisions\/1548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/media\/1520"}],"wp:attachment":[{"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/media?parent=1518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/categories?post=1518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.migenius.com\/wp-json\/wp\/v2\/tags?post=1518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}