-
Notifications
You must be signed in to change notification settings - Fork 11
/
scene_light_priority.py
82 lines (60 loc) · 2.97 KB
/
scene_light_priority.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Dynamically assign lights to the fixed pipeline slots by adjusting their priority
import harfang as hg
import math
hg.InputInit()
hg.WindowSystemInit()
res_x, res_y = 1280, 720
win = hg.RenderInit('Light priority relative to a specific world position', res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X)
hg.AddAssetsFolder("resources_compiled")
pipeline = hg.CreateForwardPipeline()
res = hg.PipelineResources()
# create models
vtx_layout = hg.VertexLayoutPosFloatNormUInt8()
light_mdl = hg.CreateSphereModel(vtx_layout, 0.05, 8, 16)
light_ref = res.AddModel('light', light_mdl)
orb_mdl = hg.CreateSphereModel(vtx_layout, 1, 16, 32)
orb_ref = res.AddModel('orb', orb_mdl)
ground_mdl = hg.CreateCubeModel(vtx_layout, 100, 0.01, 100)
ground_ref = res.AddModel('ground', ground_mdl)
# create materials
shader = hg.LoadPipelineProgramRefFromAssets('core/shader/default.hps', res, hg.GetForwardPipelineInfo())
mat_light = hg.CreateMaterial(shader, 'uDiffuseColor', hg.Vec4(0, 0, 0), 'uSpecularColor', hg.Vec4(0, 0, 0))
hg.SetMaterialValue(mat_light, "uSelfColor", hg.Vec4(1, 0.9, 0.75))
mat_orb = hg.CreateMaterial(shader, 'uDiffuseColor', hg.Vec4(1, 1, 1), 'uSpecularColor', hg.Vec4(1, 1, 1))
hg.SetMaterialValue(mat_orb, "uSelfColor", hg.Vec4(0, 0, 0))
mat_ground = hg.CreateMaterial(shader, 'uDiffuseColor', hg.Vec4(1, 1, 1), 'uSpecularColor', hg.Vec4(1, 1, 1))
hg.SetMaterialValue(mat_ground, "uSelfColor", hg.Vec4(0, 0, 0))
# setup scene
scene = hg.Scene()
cam = hg.CreateCamera(scene, hg.Mat4LookAt(hg.Vec3(5, 4, -7), hg.Vec3(0, 1.5, 0)), 0.01, 1000)
scene.SetCurrentCamera(cam)
orb_node = hg.CreateObject(scene, hg.TranslationMat4(hg.Vec3(0, 1, 0)), orb_ref, [mat_orb])
hg.CreateObject(scene, hg.TranslationMat4(hg.Vec3(0, 0, 0)), ground_ref, [mat_ground])
# create an array of dynamic lights
light_obj = scene.CreateObject(light_ref, [mat_light]) # sphere model to visualize lights
light_nodes = []
for i in range(16):
node = hg.CreatePointLight(scene, hg.Mat4.Identity, 1.5, hg.Color(1, 0.85, 0.25, 1), hg.Color(1, 0.9, 0.5, 1))
node.SetObject(light_obj)
light_nodes.append(node)
# main loop
angle = 0
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
dt = hg.TickClock()
# animate lights
angle = angle + hg.time_to_sec_f(dt)
for i, node in enumerate(light_nodes):
a = angle + i * hg.Deg(15)
node.GetTransform().SetPos(hg.Vec3(math.cos(a * -0.6) * math.sin(a) * 5, math.cos(a * 1.25) * 2 + 2.15, math.sin(a * 0.5) * math.cos(-a * 0.8) * 5))
# update light priorities according to their distance to the orb
for node in light_nodes:
priority = hg.Dist(orb_node.GetTransform().GetPos(), node.GetTransform().GetPos())
#priority = node.GetTransform().GetPos().y # uncomment to prioritize lights near the ground
node.GetLight().SetPriority(-priority)
scene.Update(dt)
vid, passId = hg.SubmitSceneToPipeline(0, scene, hg.IntRect(0, 0, res_x, res_y), True, pipeline, res)
hg.Frame()
hg.UpdateWindow(win)
hg.DestroyForwardPipeline(pipeline)
hg.RenderShutdown()
hg.DestroyWindow(win)