-
Notifications
You must be signed in to change notification settings - Fork 11
/
scene_coroutine.py
130 lines (103 loc) · 4.43 KB
/
scene_coroutine.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import harfang as hg
import asyncio
import math
# make the cube rotate in async function
async def async_cube_rotation(cube, dt_getter):
rot = cube.GetRot()
while True:
#need to convert dt to seconds to have a decent rotation
dt_sec = hg.time_to_sec_f(dt_getter()) * 2
rot.y += math.pi * dt_sec
cube.SetRot(rot)
await asyncio.sleep(dt_sec)
# make cube translate in async function during the cube_rotation is running
async def async_cube_translation(cube, dt_getter, steps, speed):
pos = cube.GetPos()
while True:
# Translation on +X
for i in range(steps):
dt_sec = hg.time_to_sec_f(dt_getter())
pos.x += dt_sec * speed
cube.SetPos(pos)
await asyncio.sleep(dt_sec)
# Translation on +Z
for i in range(steps):
dt_sec = hg.time_to_sec_f(dt_getter())
pos.z += dt_sec * speed
cube.SetPos(pos)
await asyncio.sleep(dt_sec)
# Translation on -X
for i in range(steps):
dt_sec = hg.time_to_sec_f(dt_getter())
pos.x -= dt_sec * speed
cube.SetPos(pos)
await asyncio.sleep(dt_sec)
# Translation on -Z
for i in range(steps):
dt_sec = hg.time_to_sec_f(dt_getter())
pos.z -= dt_sec * speed
cube.SetPos(pos)
await asyncio.sleep(dt_sec)
def create_material(diffuse, specular, self, prg_ref):
mat = hg.CreateMaterial(prg_ref, 'uDiffuseColor', diffuse, 'uSpecularColor', specular)
hg.SetMaterialValue(mat, 'uSelfColor', self)
return mat
async def async_main():
def get_dt():
return dt
hg.InputInit()
hg.WindowSystemInit()
res_x, res_y = 1280, 720
win = hg.RenderInit('Harfang - Scene Coroutine', res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X)
# Link precompiled assets folder to the project
hg.AddAssetsFolder('resources_compiled')
# Create Pipeline
pipeline = hg.CreateForwardPipeline()
res = hg.PipelineResources()
# create materials
vtx_layout = hg.VertexLayoutPosFloatNormUInt8()
prg_ref = hg.LoadPipelineProgramRefFromAssets('core/shader/default.hps', res, hg.GetForwardPipelineInfo())
mat_objects = create_material(hg.Vec4(0.5, 0.5, 0.5), hg.Vec4(1, 1, 1), hg.Vec4(0, 0, 0), prg_ref)
# setup scene
scene = hg.Scene()
scene.canvas.color = hg.ColorI(22, 56, 76)
scene.environment.fog_color = scene.canvas.color
scene.environment.fog_near = 20
scene.environment.fog_far = 80
cam_mtx = hg.TransformationMat4(hg.Vec3(0, 7, -10), hg.Deg3(30, 0, 0))
cam = hg.CreateCamera(scene, cam_mtx, 0.01, 3000)
scene.SetCurrentCamera(cam)
hg.CreateLinearLight(scene, hg.TransformationMat4(hg.Vec3(0, 0, 0), hg.Deg3(30, 59, 0)),
hg.Color(1, 0.8, 0.7), hg.Color(1, 0.8, 0.7), 10, hg.LST_Map, 0.002,
hg.Vec4(50, 100, 200, 400))
hg.CreatePointLight(scene, hg.TranslationMat4(hg.Vec3(0, 10, 10)), 100, hg.ColorI(94, 155, 228),
hg.ColorI(94, 255, 228))
# create models
cube_model_1 = hg.CreateCubeModel(vtx_layout, 1, 1, 1)
cube_ref_1 = res.AddModel('cube', cube_model_1)
cube_instance_1 = hg.CreateObject(scene, hg.TranslationMat4(hg.Vec3(0, 0, 0)), cube_ref_1,
[mat_objects])
cube_transform_1 = cube_instance_1.GetTransform()
cube_model_2 = hg.CreateCubeModel(vtx_layout, 1, 1, 1)
cube_ref_2 = res.AddModel('cube', cube_model_2)
cube_instance_2 = hg.CreateObject(scene, hg.TranslationMat4(hg.Vec3(0, 3, 0)), cube_ref_2,
[mat_objects])
cube_transform_2 = cube_instance_2.GetTransform()
# create async tasks
asyncio.create_task(async_cube_rotation(cube_transform_1, get_dt))
asyncio.create_task(async_cube_translation(cube_transform_2, get_dt, steps=100, speed=2.0))
asyncio.get_event_loop()
# main loop
while not hg.ReadKeyboard().Key(hg.K_Escape) and hg.IsWindowOpen(win):
dt = hg.TickClock()
# update the scene
scene.Update(dt)
hg.SubmitSceneToPipeline(0, scene, hg.IntRect(0, 0, res_x, res_y), True, pipeline, res)
# give async tasks time to do their job
await asyncio.sleep(0)
hg.Frame()
hg.UpdateWindow(win)
hg.DestroyForwardPipeline(pipeline)
hg.RenderShutdown()
hg.DestroyWindow(win)
asyncio.run(async_main())