Procedural Content Generation

Procedural content generation (PCG) is a method used to create data algorithmically rather than manually. In game development, this technique is used to generate complex scenes, textures, or game levels dynamically. This document explains how to use our game engine’s Lua scripting API to generate a scene with various geometric objects, materials, lights, and textures procedurally.

Introduction

Procedural generation in our engine leverages Lua scripting to programmatically define the properties and behaviors of scene elements. The following example illustrates the procedural creation of a scene with multiple objects, lighting, and textures.

Code Overview

The provided script includes multiple sections, each responsible for setting up different parts of the scene:

  1. Camera Setup: Configures a perspective camera with a specific field of view and orientation.

  2. Texture and Material Loading: Loads textures and creates materials to be applied to the objects.

  3. Light Creation: Adds directional and point lights to the scene.

  4. Object Creation: Creates various geometric objects such as spheres, boxes, and more.

  5. Scene Completion: Ensures all objects are created and then sets a readiness flag.

The code utilizes promises to handle asynchronous operations, ensuring that all assets are loaded and ready before they are used.

Detailed Code Explanation

Procedural Scene Generation Script
-- Import necessary modules
local Events = require 'engine/events'
local Console = require 'engine/console'
local Loader = require 'engine/loader'
local Transform = require 'engine/math/transform'
local Color = require 'engine/data/color'
local Material = require 'engine/items/material'
local SgCamera = require 'engine/sceneobjects/sgcamera'
local SgLightsource = require 'engine/sceneobjects/sglightsource'
local SgSphere = require 'engine/sceneobjects/sgsphere'
-- Add other necessary imports...

local ProcGenTest = Class.new(Entity)

function ProcGenTest:init()
    -- Camera initialization
    local camPromise = SgCamera:create({
        active = true,
        transform = Transform.new(Vector3.new(0, 0, 6)),
        type = SgCamera.Projection.Perspective,
        -- Additional camera properties...
    })

    -- Load texture and create materials
    local texPromise = Loader.loadTexture("textureId")
    local materialPromise = Material:create()

    -- Ensure both the texture and the material are ready before applying the texture to the material
    Promise:all({texPromise, materialPromise}):next(function(texture, material)

        material:setAlbedoMap(texture)

        -- Now we can create objects using the fully prepared material
        local spherePromise = SgSphere:create({
            transform = Transform.new(),
            radius = 1,
            material = material,
            -- Other sphere properties...
        })

        -- Additional object creation can go here, using the material as needed...

        -- Create lights
        local dirLightPromise = SgLightsource:create({
            type = "Directional",
            intensity = 5,
            color = Color.new(255, 255, 255),
            -- Additional light properties...
        })

        -- Handle all promises for final scene setup
        Promise:all({spherePromise, dirLightPromise})
            :next(function()
                Console.log("All objects and lights created.")
            end)
            :catch(function(err)
                Console.error("Error during object creation: " .. Tools.dump(err))
            end)
    end):catch(function(error)
        Console.error("Error loading materials or textures: " .. Tools.dump(error))
    end)
end

return ProcGenTest

Key Concepts

  • Promise Handling: Used to manage asynchronous operations, crucial for loading external resources like textures.

  • Transforms: Define the position, rotation, and scale of objects in the scene.

  • Materials and Textures: Enhance the visual quality of objects.

  • Lighting: Critical for adding realism to the scene by simulating light interactions.

  • Logging and Debugging: Console logs help track the state and errors.

This example provides a foundation for understanding how procedural content can be dynamically generated and managed within our game engine. Users can expand upon this by integrating more complex geometries, advanced materials, and sophisticated interaction systems. Please see the API Reference for more detailed information about the possible properties and methods of items and scene objects