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

Google 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'

-- GoogleVR device
require 'script/lua/google_vr'
GoogleVRSystem = GoogleVRSystem or {}

Project.level_names = {
    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 = false, -- 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 an Oculus device for project
    if Appkit.is_standalone() then
        local managed_world = Appkit.managed_world
        vr_system = GoogleVRSystem(managed_world.world)
        if vr_system then
            GoogleVRSystem.initialize(vr_system, 0.01, 1000.0)
        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

    -- 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)
    local player = Player.spawn_player(SimpleProject.level, view_position, view_rotation)

    -- Disable touch controls on GoogleVR
    if player then
        Player.set_controls_enabled(player, false)
    end
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

    -- Check daydream controller status before reading state
    if GoogleVRSystem.check_controller_status(vr_system) then
        GoogleVRSystem.print_buttons_state(vr_system)
        GoogleVRSystem.print_touch_position(vr_system)
        -- Uncomment to print values
        --GoogleVRSystem.print_orientation(vr_system)
        --GoogleVRSystem.print_gyroscope(vr_system)
        --GoogleVRSystem.print_acceleration(vr_system)
    else
        -- Check cardboard magnet clicker
        local click = GoogleVRSystem.update_magnet_clicker(vr_system)
        if click then
            print("Magnet Click")
        end
    end

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

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

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

return Project