Skip to content

Instantly share code, notes, and snippets.

@pfirsich
Last active August 23, 2018 21:39
Show Gist options
  • Save pfirsich/9063926c3e51e6dbef66629a8bf0aa1e to your computer and use it in GitHub Desktop.
Save pfirsich/9063926c3e51e6dbef66629a8bf0aa1e to your computer and use it in GitHub Desktop.
Sketches for high-level library for kaun (chu)
-- resources (kaun)
local paddleMesh = kaun.newMesh("paddle.obj")
local paddleTexture = kaun.newTexture("paddle.png")
local ballMesh = kaun.newSphereMesh(1.0, 12, 12)
local ballTexture = kaun.newTexture("ball.png")
local level = kaun.newMesh("level.obj")
local skyboxMesh = kaun.newBoxMesh(1, 1, 1)
local skyboxTexture = kaun.newCubeTexture("posx.png", "negx.png", "posy.png",
"negy.png", "posz.png", "negz.png")
-- actual scene setup (chu)
local scene = chu.Scene() -- derives from chu.ecs.World
local paddleMaterial = chu.PhongMaterial(paddleTexture)
-- chu.Object derives from chu.ecs.Entity, chu.MeshComponent and chu.TransformComponent
local paddleL = scene:Object(paddleMesh, paddleMaterial)
paddleL:setPosition(-10, 0, 0)
local paddleR = scene:Object(paddleMesh, paddleMaterial)
paddleR:setPosition(10, 0, 0)
local ball = scene:Object(ballMesh)
ball.material.baseTexture = ballTexture
ball:addComponent(chu.PointLightComponent) -- addComponent returns the component
local levelMaterial = chu.DisneyPbrMaterial()
-- these maps could be kaun Texture objects too
levelMaterial.albedo = "level_albedo.png" -- could also put a color (table) here
levelMaterial.roughness = "level_roughness.png" -- could also put a number here
levelMaterial.metalness = "level_metalness.png"
local level = scene:Object(levelMesh, levelMaterial)
local skybox = scene:Object(skyboxMesh, chu.SkyboxMaterial(skyboxTexture))
-- chu.DirectionalLight derives from chu.ecs.Entity and chu.DirectionalLightComponent and chu.TransformComponent
local sun = scene:DirectionalLight()
sun:lookAt(lightDir)
sun:addComponent(chu.PcfShadowComponent, 4096, 4096)
local camera = chu.PerspectiveCamera(45, 0.1, 100.0)
function love.update()
-- the game
end
function love.draw()
scene:render(camera) -- calls chu.World:emit("render") or something
end
-- resources (kaun)
local paddleMesh = kaun.newMesh("paddle.obj")
local paddleTexture = kaun.newTexture("paddle.png")
local ballMesh = kaun.newSphereMesh(1.0, 12, 12)
local ballTexture = kaun.newTexture("ball.png")
local level = kaun.newMesh("level.obj")
local skyboxMesh = kaun.newBoxMesh(1, 1, 1)
local skyboxTexture = kaun.newCubeTexture("posx.png", "negx.png", "posy.png",
"negy.png", "posz.png", "negz.png")
-- actual scene setup (chu)
local world = chu.World()
local paddleMaterial = chu.PhongMaterial(paddleTexture)
local paddleL = world:Entity()
paddleL:addComponent(chu.MeshComponent, paddleMesh, paddleMaterial)
paddleL:getComponent(chu.TransformComponent):setPosition(-10, 0, 0)
local paddleR = world:Entity()
paddleR:addComponent(chu.MeshComponent, paddleMesh, paddleMaterial)
paddleR:getComponent(chu.TransformComponent):setPosition(10, 0, 0)
local ball = world:Entity()
-- addComponent returns the component
ball:addComponent(chu.MeshComponent, ballMesh).material.baseTexture = ballTexture
ball:addComponent(chu.PointLightComponent)
local levelMaterial = chu.DisneyPbrMaterial()
-- these maps could be kaun Texture objects too
levelMaterial.albedo = "level_albedo.png" -- could also put a color (table) here
levelMaterial.roughness = "level_roughness.png" -- could also put a number here
levelMaterial.metalness = "level_metalness.png"
local level = world:Entity()
level:addComponent(chu.MeshComponent, levelMesh, levelMaterial)
local skybox = world:Entity()
skybox:addComponent(chu.MeshComponent, skyboxMesh, chu.SkyboxMaterial(skyboxTexture))
local sun = world:Entity()
sun:addComponent(chu.DirectionalLightComponent)
sun:getComponent(chu.TransformComponent):lookAt(lightDir)
sun:addComponent(chu.PcfShadowComponent, 4096, 4096)
local camera = chu.PerspectiveCamera(45, 0.1, 100.0)
function love.update()
-- the game
end
function love.draw()
chu.render(world, camera)
end
-- resources (kaun)
local paddleMesh = kaun.newMesh("paddle.obj")
local paddleTexture = kaun.newTexture("paddle.png")
local ballMesh = kaun.newSphereMesh(1.0, 12, 12)
local ballTexture = kaun.newTexture("ball.png")
local level = kaun.newMesh("level.obj")
local skyboxMesh = kaun.newBoxMesh(1, 1, 1)
local skyboxTexture = kaun.newCubeTexture("posx.png", "negx.png", "posy.png",
"negy.png", "posz.png", "negz.png")
-- actual scene setup (chu)
local scene = chu.Scene()
local paddleMaterial = chu.PhongMaterial(paddleTexture)
local paddleL = chu.Object(paddleMesh, paddleMaterial)
paddleL:setPosition(-10, 0, 0)
local paddleR = chu.Object(paddleMesh, paddleMaterial)
paddleR:setPosition(10, 0, 0)
scene.add(paddleL, paddleR)
local ballLight = chu.PointLight()
local ball = chu.Object(ballMesh)
ball.material.baseTexture = ballTexture -- PhonMaterial added by default
ball.add(ballLight)
scene.add(ball)
local levelMaterial = chu.DisneyPbrMaterial()
-- these maps could be kaun Texture objects too
levelMaterial.albedo = "level_albedo.png" -- could also put a color (table) here
levelMaterial.roughness = "level_roughness.png" -- could also put a number here
levelMaterial.metalness = "level_metalness.png"
local level = chu.Object(levelMesh, levelMaterial)
scene.add(level)
local skyboxMaterial = chu.SkyboxMaterial(skyboxTexture)
local skybox = chu.Object(skyboxTexture, skyboxMaterial)
scene.add(skybox)
local sun = chu.DirectionalLight()
sun:lookAt(lightDir)
sun.shadow = chu.PcfShadow(4096, 4096)
local camera = chu.PerspectiveCamera(45, 0.1, 100.0)
function love.update()
-- the game
end
function love.draw()
chu.render(scene, camera)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment