Scripting ========== Alongside the Sequencer tool, trueSKY can be driven and configured via Unity scripting. Similarly to using the sequencer, the settings and properties in trueSKY for Unity are split between the `Keyframes`_ properties (temporal values for clouds and sky), `Layers`_ properties (values for clouds and sky that persist throughout a sequence) and the properties of the `trueSKY object`_ itself (global configuration settings). trueSKY Object -------- To modify trueSKY object via scripting, a reference to the active object in the scene must first be obtained (there should only be one). This can be done upon initialisation; the code to do so is as follows: .. code-block:: cpp simul.trueSKY trueSky=simul.trueSKY.GetTrueSky(); All editor properties of the trueSKY object are publicly accessible, so once a reference to the trueSKY object has been obtained, they can also be accessed and modified via scripting. For example, to disable depth blending: .. code-block:: cpp private simul.trueSKY tS; void Start () { tS = simul.trueSKY.GetTrueSky (); tS.DepthBlending = false; } Layers -------- Layer (Also called Keyframers) properties are those accessible in the sequencer by clicking on the layer name on the left of the timeline. These affect the entire sequence and the values will remain until the user changes them manually. This differs in nature from keyframe properties, which are active only between specified times and will be automatically modified via interpolation (for example if two adjacent keyframes have cloudiness set to 0.4 and 0.6 respectively, trueSKY will interpolate these values for times in between, increasing gradually from 0.4 to 0.6). The functions needed to Get and Set layer properties are as follows: * **GetKeyframerValue(uint uid, string name)**: Set a layer value specified by the name string. * **SetKeyframerValue(uint uid, string name, object value)**: Gets the layer value specified by the name string. **Creating and Deleting Layers via Scripting** You can create and delete cloud layers at runtime. There is only one sky layer for each sequence, so you cannot edit sky layers. * **CreateCloudKeyframer(string name)**: Creates a new Cloud layer with the given name, returns the UID * **DeleteCloudKeyframer(uint uid)**: Deletes the Cloud layer with the given UID Keyframes -------- Keyframes in trueSKY can be accessed, counted, modified, created and deleted via scripting. To delete a keyframe or change its properties, the Unique Identifier (UID) will need to be known. These can be retrieved either by index (the keyframes in the sequencer, which are zero-indexed) or by retrieving the current/interpolated keyframe (not viewable in the sequencer, as they are interpolated). UIDs are unique and persistent for the session, but not across saves or restarts, so they should not be stored. **Note**: For any functions with a layer parameter, the layer indexes are as follows: **0** for the sky, **1** for the first cloud layer, **2** for the second cloud layer and so on. This is not the same as UID, which can vary for layers. **Sky**: * **GetSkyKeyframeByIndex (int index)**: Get the sky keyframe with specified index. * **GetInterpolatedSkyKeyframe ()**: Retrieve the interpolated (current) sky keyframe. * **InsertSkyKeyframe (float t)**: Insert a sky keyframe at specified time t. * **GetNumSkyKeyframes ()**: Returns the total number of sky keyframes. **Clouds**: * **GetCloudKeyframeByIndex (int index, int layer)**: Get the cloud keyframe with specified index. * **GetInterpolatedCloudKeyframe (int layer = 1)**: Retrieve the interpolated (current) cloud keyframe. * **InsertCloudKeyframe (float t)**: Insert a cloud keyframe at specified time t. * **GetNumCloudKeyframes ()**: Returns the total number ofcloud keyframes. To set or get keyframe values, you need to use: * **GetKeyframeValue(keyframeUID, "VariableName")** * **SetKeyframeValue(keyframeUID, "VariableName", value)** For example, to increase the cloudiness of a cloud keyframe, you would use: .. code-block:: cpp public simul.trueSKY trueSky=null; void Update() { tS = simul.trueSKY.GetTrueSkypublic simul.trueSKY trueSky=null;(); //this will get the first cloud keyframe on the first cloud layer. uint ckUid = ts.GetCloudKeyframeByIndex(0,0); float _cloudiness = ts.GetKeyframeValue(ckUid, "cloudiness"); ts.SetKeyframeValue(ckUid, "cloudiness", _cloudiness + 0.2); } For a list of variable names, see the variable pages such as :doc:`Cloud Variables <../Tutorials/Clouds/cloudsvariables>` or :doc:`Sky Variables <../Tutorials/Sky/skyvariables>`. Be sure to use the correct variable type when passing the value. Other Functions --------------- **Queries** TrueSKY for Unity also provides some built in functions to carry out queries at given positions. It is possible to carry out cloud queries at a given position, and cloud line queries across two positions. These will return a VolumeQueryResult and a LineQueryResult respectively. Additionally there are more simplified functions for when you want just one value, for example, the amount of rain at a given position. Where applicable, id parameters should be given any unique integer (calling another query with the same id will overwrite the former). The functions are as follows: * **GetCloudQuery (int id, Vector3 pos)**: Given an id and a position, returns a VolumeQueryResult struct. * **CloudLineQuery (int id, Vector3 startpos, Vector3 endpos)**: Given an id, a start position and an end position, returns a LineQueryResult struct. * **GetCloudAtPosition (Vector3 pos)**: Returns the amount of cloud at the given position, as a float value 0.0 to 1.0. * **GetCloudShadowAtPosition (Vector3 pos)**: Returns the amount of cloud shadow at the given position, as a float value 0.0 to 1.0. * **GetPrecipitationAtPosition (Vector3 pos)**: Returns the amount of rain/snow at the given position, as a float value 0.0 to 1.0. The contents of the structs are as follows: **VolumeQueryResult**: * vec3 pos_m: The position used for the query (this may be modified from the original position, and projected elsewhere to avoid testing areas we know to not have cloud). * int valid: Whether the query is valid (1) or not (0). Returns 0 for the first 3 frames. * float density: The cloud density (0.0 to 1.0). * float direct_light: The amount of direct light (0.0 to 4.0). * float indirect_light: The amount of indirect light (0.0 to 4.0). * float ambient_light: The amount of ambient light (0.0 to 4.0). * float precipitation: The amount of rain/snow (0.0 to 1.0). **LineQueryResult**: * vec3 pos1_m: The start position used for the query (this may be modified from the original position, and projected elsewhere to avoid testing areas we know to not have cloud). * vec3 pos2_m: The end position used for the query (this may be modified from the original position, and projected elsewhere to avoid testing areas we know to not have cloud). * int valid: Whether the query is valid (1) or not (0). Returns 0 for the first 3 frames. * float density: The cloud density (0.0 to 1.0). * float visibility: The visibility distance, in km (between 0.1 and 10000.0). * float optical_thickness_metres: The amount of cloud filling the distance between the start and end positions. If completely cloudy = distance in metres, if none = 0. * float first_contact_metres: The distance to the nearest cloud, in metres. **Storms** Just like keyframes, storm UID's are accessible through their index or by querying the time. * **GetStormUidByIndex(uid index)** * **GetStormUidAtTime(float time)** For setting values for a storm, you again have similar functions to keyframes. * **GetStormValue(uint uid, string name)** * **SetStormValue(uint uid, string name, object value)** The centre of a storm can be set via the SetStormCentre (float x, float y) function. This is equivalent to setting the storm centre via the map window in the sequencer (hence only two axes). **Unity <-> trueSKY Coordinate Conversions** As Unity and trueSKY treat coordinates differently (which axis is considered "up"), these functions are provided to convert between them when needed: * **Vector3 TrueSkyToUnityPosition(Vector3 ts_pos)**: Takes a trueSKY position and returns the converted Unity position. * **Vector3 TrueSkyToUnityDirection(Vector3 ts_dir)**: Takes a trueSKY direction and returns the converted Unity direction. * **Vector3 UnityToTrueSkyPosition(Vector3 upos)**: Takes a Unity position and returns the converted trueSKY position. * **Vector3 UnityToTrueSkyDirection(Vector3 u_dir)**: Takes a Unity direction and returns the converted trueSKY direction.