Gear VR template: /project.lua — code sample - Stingray Lua API Reference

Gear VR template: /project.lua — code sample

Code

-----------------------------------------------------------------------------------
-- This implementation uses the default SimpleProject and the Project extensions are
-- used to extend the SimpleProject behavior.

-- This is the global table name used by Appkit Basic project to extend behavior
Project = Project or {}

require 'script/lua/flow_callbacks'

require 'script/lua/gear_vr'
GearVRSystem = GearVRSystem or {}

vr_system = nil

Project.level_names = {
    menu = "content/levels/main_menu",
    basic = "content/levels/basic"
}

-- Can provide a config for the basic project, or it will use a default if not.
local SimpleProject = require 'core/appkit/lua/simple_project'
SimpleProject.config = {
    standalone_init_level_name = Project.level_names.basic,
    camera_unit = "core/appkit/units/camera/camera",
    camera_index = 1,
    shading_environment = nil, -- Will override levels that have env set in editor.
    create_free_cam_player = true, -- Project will provide its own player.
    exit_standalone_with_esc_key = true
    -- Loading screen disabled until 2D gui VR is fully supported.
    -- loading_screen_materials = {"core/appkit/materials/loading_screen"},
    -- loading_screen_start_package = "loading_screen",
    -- loading_screen_end_package = "main",
    -- loading_screen_shading_env = "core/stingray_renderer/environments/midday/midday" -- Controls the shading environment used by default by the loading screen.
}

function Project.on_init_complete()
    -- Create and setup a GearVR device for the project
    if Appkit.is_standalone() then
        local managed_world = Appkit.managed_world
        vr_system = GearVRSystem(managed_world.world)
        if vr_system then
            vr_system:initialize(0.01, 1000.0, "vr_left_target", "vr_right_target")
            vr_system:enable()
        end
    end
end

-- This optional function is called by SimpleProject after level, world and player is loaded
-- but before lua trigger level loaded node is activated.
function Project.on_level_load_pre_flow()
    local vr_player_height = 1.7
    local vr_eye_height = 1.7

    -- If a GearVR profile is found use its data to initialize local values
    local vr_profile = vr_system and vr_system:profile()
    if vr_profile then
        vr_player_height = vr_profile.player_height
        vr_eye_height = vr_profile.eye_height
    end

    -- Set free cam position and orientation
    local player_start_pose = Appkit.PlayerUtil.get_player_start_pose(SimpleProject.world)

    local view_position, view_rotation

    if player_start_pose then
        view_position = stingray.Matrix4x4.translation(player_start_pose)
        view_rotation = stingray.Matrix4x4.rotation(player_start_pose)
    else
        view_position = stingray.Vector3(0, -14, vr_player_height)
        view_rotation = stingray.Quaternion.identity()
    end

    local Player = require 'script/lua/player'
    Player.set_default_character_eye_height(vr_eye_height)
    Player.spawn_player(SimpleProject.level, view_position, view_rotation)
end

function Project.on_level_shutdown_post_flow()
end

-- Optional function called by SimpleProject after world update (we will probably want to split to pre/post appkit calls)
function Project.update(dt)
    if not vr_system then return end

    -- Update GearVR tracking space pose
    local managed_world = Appkit.managed_world;
    local cam, cam_unit = managed_world.get_enabled_camera(managed_world)
    vr_system:set_tracking_space_pose(stingray.Camera.world_pose(cam, cam_unit))

    if not vr_system:is_enabled() then return end

    -- Update the Wwise Listener to include the GearVR device pose
    if stingray.Wwise then
        local poses = vr_system:hmd_world_pose()
        stingray.WwiseWorld.set_listener(stingray.Wwise.wwise_world(managed_world.world), stingray.Wwise.LISTENER_0, poses.left_eye)
    end
end

-- Optional function called by SimpleProject *before* appkit/level/player/world shutdown
function Project.shutdown()
    if vr_system then
        vr_system:shutdown()
        vr_system = nil
    end
end

function Project.render()
    if vr_system and vr_system:is_enabled() then
        local managed_world = Appkit.managed_world
        vr_system:render(managed_world.shading_env)
        return true -- Override the regular SimpleProject rendering with VR rendering
    end
end

return Project