[Lua] Interpreter Quickstart


#1

HARFANG3D® Lua quick starter

HARFANG3D® can be deployed via different languages. We will describe here how to implement the Lua stand alone engine.
You can download HARFANG Lua here : https://www.harfang3d.com/downloads

Why using Lua ?

Adding Lua scripting engine to a standalone C++ application is the most direct way to combine the performance of a dedicated engine with the expertise of your power users.

No matter the field of you standalone application, thanks to the ability to write scripts, a power user will usually be able to get the most of it by creating dedicated or project-centric workflow.
The script interface brings a safe environment that is suited for such users whose primary skills is generally not to deal with C/C++.

How to edit Lua code ?

ON WINDOWS

The easiest way to edit in Lua is to use a text editor like NotePad ++, with syntax highlight.

For debugging, you can use a more advanced editor like Visual Studio Code.

Notepad++ setup

Edition

To write the code under NotePad ++, there is not really any difficulty.
Take care to set synthaxic recognition.

Execution

  • First, you need to copy the lua stand alone and its dependencies into a directory of your project.
    e.g.: my_project/lua_standalone/

  • You can save your lua source in a my_project/source directory.

  • Then from the console, you launch your code with the following command:
    .\lua_standalone\lua.exe ./source/my_code.lua

Visual Studio Code implementation

  • Once the software is installed and started, first create a new workspace folder.

  • In the browser, you can create the folders for your project.
  • As mentioned above, you can copy the necessary files to the Harfang Stand Alone in the folder “lua”

  • Once your code is written, to run it and debug it from VSC, you have to add a configuration file.

  • Go to “Debug-> Add Configuration”

  • VSC creates a new “.vscode” folder, and a “launch.json” file, which contains the parameters needed to execute your code.

  • For your code to be correctly executed by the HARFANG Stand alone, modify the configuration script as follows:
  • runtimeExecutable specifies the lua Stand alone commande.
  • runtimeArgs is the script you want to execute and/or debug.

  • Finally, you can run your code (F5 or CTRL-F5)

Lua basic 3D environment

To conclude this training, here is an example of the use of HARFANG3D® in Lua :
To learn more, please refere to the documentation : https://www.harfang3d.com/doc/1.1.0/man.Index.html

-- Basic scene - Lua

hg = require('harfang')


function init_scene(plus)
    scene = plus:NewScene()
    camera = plus:AddCamera(scene, hg.Matrix4.TranslationMatrix(hg.Vector3(20, 10, 10)))
    camera:SetName("Camera")
    init_lights(plus, scene)

    -- Ground:
    ground = plus:AddPlane(scene, hg.Matrix4.TransformationMatrix(hg.Vector3(0, 0, 0), hg.Vector3(0, 0, 0)), 100, 100)
    cube = plus:AddCube(scene, hg.Matrix4.TransformationMatrix(hg.Vector3(0, 3, 0), hg.Vector3(0, 0, 0)), 3, 3, 3)
    fps = hg.FPSController(0, 2, -15)

    return scene, cube, fps
end

function init_lights(plus, scene)
    -- Main light:
    ligth_sun = plus:AddLight(scene, hg.Matrix4.RotationMatrix(hg.Vector3(math.rad(22), math.rad(-45), 0)), hg.LightModelLinear)
    ligth_sun:SetName("Sun")
    ligth_sun:GetLight():SetDiffuseColor(hg.Color(255. / 255., 255. / 255., 255. / 255., 1.))
    ligth_sun:GetLight():SetShadow(hg.LightShadowMap)
    ligth_sun:GetLight():SetShadowRange(50)
    ligth_sun:GetLight():SetDiffuseIntensity(1.)
    ligth_sun:GetLight():SetSpecularIntensity(1.)

    -- Ambient:
    ambient_color = hg.Color(103. / 255., 157. / 255., 141. / 255., 1.)
    environment = hg.Environment()
    environment:SetAmbientColor(ambient_color)
    environment:SetAmbientIntensity(0.3)
    environment:SetFogColor(ambient_color * 0.3)
    environment:SetFogNear(1)
    environment:SetFogFar(50)
    environment:SetBackgroundColor(ambient_color * 0.3)
    scene:AddComponent(environment)
end

---- Program starts here ----

-- Display settings
resolution = hg.Vector2(1600, 900)
antialiasing = 8
screenMode = hg.Windowed

-- System setup
plus = hg.GetPlus()
hg.LoadPlugins()
plus:Mount("./")

-- Run display
plus:RenderInit(resolution.x, resolution.y, antialiasing, screenMode)
plus:SetBlend2D(hg.BlendAlpha)

-- Setup scene:
scene, cube, fps= init_scene(plus)

scene:UpdateAndCommitWaitAll();
camera = scene:GetNode("Camera")
rot=hg.Vector3(0,0,0)

-- Main loop:

while not plus:KeyDown(hg.KeyEscape) and not plus:IsAppEnded() do
    delta_t = plus:UpdateClock()
    dts = hg.time_to_sec_f(delta_t)

    fps:UpdateAndApplyToNode(camera, delta_t)
    rot.x=rot.x+0.01
    rot.y=rot.y+0.045
    rot.z=rot.z+0.03146
    cube:GetTransform():SetRotation(rot)

    plus:UpdateScene(scene, delta_t)
    plus:Flip()
    plus:EndFrame()
end
plus:RenderUninit()

pinned #2