-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmaterial_update_value.lua
79 lines (56 loc) · 2.73 KB
/
material_update_value.lua
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
-- Create textured material with pipeline shader
hg = require("harfang")
hg.InputInit()
hg.WindowSystemInit()
res_x, res_y = 1280, 720
win = hg.RenderInit('Modify material pipeline shader uniforms', 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()
cube_mdl = hg.CreateCubeModel(vtx_layout, 1, 1, 1)
cube_ref = res:AddModel('cube', cube_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_cube = hg.CreateMaterial(shader, 'uDiffuseColor', hg.Vec4(1, 1, 1), 'uSpecularColor', hg.Vec4(1, 1, 1))
mat_ground = hg.CreateMaterial(shader, 'uDiffuseColor', hg.Vec4(1, 1, 1), 'uSpecularColor', hg.Vec4(0.1, 0.1, 0.1))
-- setup scene
scene = hg.Scene()
cam = hg.CreateCamera(scene, hg.Mat4LookAt(hg.Vec3(-1.30, 0.27, -2.47), hg.Vec3(0, 0.5, 0)), 0.01, 1000)
scene:SetCurrentCamera(cam)
hg.CreateLinearLight(scene, hg.TransformationMat4(hg.Vec3(0, 2, 0), hg.Deg3(27.5, -97.6, 16.6)), hg.ColorI(64, 64, 64), hg.ColorI(64, 64, 64), 10)
hg.CreateSpotLight(scene, hg.TransformationMat4(hg.Vec3(5, 4, -5), hg.Deg3(19, -45, 0)), 0, hg.Deg(5), hg.Deg(30), hg.ColorI(255, 255, 255), hg.ColorI(255, 255, 255), 10, hg.LST_Map, 0.0001)
cube_node = hg.CreateObject(scene, hg.TranslationMat4(hg.Vec3(0, 0.5, 0)), cube_ref, {mat_cube})
hg.CreateObject(scene, hg.TranslationMat4(hg.Vec3(0, 0, 0)), ground_ref, {mat_ground})
-- material update states
mat_has_texture = false
mat_update_delay = 0
texture_ref = hg.LoadTextureFromAssets('textures/squares.png', 0, res)
-- main loop
while not hg.ReadKeyboard():Key(hg.K_Escape) and hg.IsWindowOpen(win) do
dt = hg.TickClock()
mat_update_delay = mat_update_delay - dt
if mat_update_delay <= 0 then
-- set or remove cube node material texture
mat = cube_node:GetObject():GetMaterial(0)
if mat_has_texture then
hg.SetMaterialTexture(mat, 'uDiffuseMap', hg.InvalidTextureRef, 0)
else
hg.SetMaterialTexture(mat, 'uDiffuseMap', texture_ref, 0)
end
-- update the pipeline shader variant according to the material uniform values
hg.UpdateMaterialPipelineProgramVariant(mat, res)
-- reset delay and flip flag
mat_update_delay = mat_update_delay + hg.time_from_sec(1)
mat_has_texture = not mat_has_texture
end
scene:Update(dt)
hg.SubmitSceneToPipeline(0, scene, hg.IntRect(0, 0, res_x, res_y), true, pipeline, res)
hg.Frame()
hg.UpdateWindow(win)
end
hg.RenderShutdown()
hg.DestroyWindow(win)