Rework environment.

This commit is contained in:
Roz K 2022-12-18 06:35:20 +01:00
parent 1e2b666af3
commit 8db60a0c01
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
2 changed files with 53 additions and 39 deletions

View File

@ -14,9 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from math import radians, cos from math import radians, cos
from engine import vec3, vec3_up, mat4_mul_vec3, resolve_input, set_input_vec3
from engine import *
from game.math import vec3_add, vec3_sub, vec3_scale, vec3_mul, vec3_dot from game.math import vec3_add, vec3_sub, vec3_scale, vec3_mul, vec3_dot
def _angles(start, end): def _angles(start, end):
@ -30,6 +28,10 @@ def _floats(a, b):
def _colors(a, b): def _colors(a, b):
return (a, vec3_sub(b, a)) return (a, vec3_sub(b, a))
_light_power = (
(_angles(180.0, 0.0), _floats(1.0, 1.0)),
)
_light_color = ( _light_color = (
(_angles(180.0, 0.0), _colors((1.0, 1.0, 1.0), (1.0, 1.0, 1.0))), (_angles(180.0, 0.0), _colors((1.0, 1.0, 1.0), (1.0, 1.0, 1.0))),
) )
@ -46,11 +48,6 @@ _sun_color = (
(_angles(180.0, 0.0), _colors((8.0, 8.0, 4.0), (8.0, 8.0, 4.0))), (_angles(180.0, 0.0), _colors((8.0, 8.0, 4.0), (8.0, 8.0, 4.0))),
) )
_light_power = (
(_angles(180.0, 90.0), _floats(0.0, 0.0)),
(_angles( 90.0, 0.0), _floats(0.0, 1.0))
)
def _resolve(ranges, c): def _resolve(ranges, c):
for (cmin, cmax, crng), ab in ranges: for (cmin, cmax, crng), ab in ranges:
if c >= cmin and c <= cmax: if c >= cmin and c <= cmax:
@ -65,24 +62,41 @@ def _resolve_color(ranges, c):
w, (a, b) = _resolve(ranges, c) w, (a, b) = _resolve(ranges, c)
return vec3_add(a, vec3_scale(b, w)) return vec3_add(a, vec3_scale(b, w))
def resolve_inputs(shader): class _Inputs:
light_direction = resolve_input(shader, b'u_light_direction') __slots__ = 'light_direction', 'light_color', 'horizon_color', 'sky_color', 'sun_color'
light_color = resolve_input(shader, b'u_light_color')
horizon_color = resolve_input(shader, b'u_horizon_color')
sky_color = resolve_input(shader, b'u_sky_color')
sun_color = resolve_input(shader, b'u_sun_color')
return (light_direction, light_color, horizon_color, sky_color, sun_color)
def from_sun(view, sun_direction, sun_power): def __init__(self, shader):
c = vec3_dot(sun_direction, vec3_up) self.light_direction = resolve_input(shader, b'u_light_direction')
light_power = _resolve_float(_light_power, c) * sun_power self.light_color = resolve_input(shader, b'u_light_color')
light_color = vec3_scale(_resolve_color(_light_color, c), sun_power) # vec3_scale(_resolve_color(_light_color, c), light_power) self.horizon_color = resolve_input(shader, b'u_horizon_color')
horizon_color = vec3_mul(_resolve_color(_horizon_color, c), light_color) self.sky_color = resolve_input(shader, b'u_sky_color')
sky_color = vec3_mul(_resolve_color(_sky_color, c), light_color) self.sun_color = resolve_input(shader, b'u_sun_color')
sun_color = _resolve_color(_sun_color, c)
light_direction = vec3()
mat4_mul_vec3(light_direction, view, sun_direction, 0.0)
return (light_direction, vec3(light_color), vec3(horizon_color), vec3(sky_color), vec3(sun_color))
def update_inputs(inputs, values): class Environment:
list(map(set_input_vec3, inputs, values)) __slots__ = 'light_direction', 'light_color', 'horizon_color', 'sky_color', 'sun_color'
def __init__(self):
self.light_direction = vec3()
self.light_color = vec3()
self.horizon_color = vec3()
self.sky_color = vec3()
self.sun_color = vec3()
def from_sun(self, view, sun_direction, sun_power):
c = vec3_dot(sun_direction, vec3_up)
light_power = _resolve_float(_light_power, c) * sun_power
mat4_mul_vec3(self.light_direction, view, sun_direction, 0.0)
self.light_color = vec3(vec3_scale(_resolve_color(_light_color, c), light_power))
self.horizon_color = vec3(vec3_scale(_resolve_color(_horizon_color, c), light_power))
self.sky_color = vec3(vec3_scale(_resolve_color(_sky_color, c), light_power))
self.sun_color = vec3(_resolve_color(_sun_color, c))
def resolve_inputs(self, shader):
return _Inputs(shader)
def update_inputs(self, inputs):
set_input_vec3(inputs.light_direction, self.light_direction)
set_input_vec3(inputs.light_color, self.light_color)
set_input_vec3(inputs.horizon_color, self.horizon_color)
set_input_vec3(inputs.sky_color, self.sky_color)
set_input_vec3(inputs.sun_color, self.sun_color)

View File

@ -24,19 +24,19 @@ from game import shader
from game import resources from game import resources
from game import batch from game import batch
from game import triangles from game import triangles
from game import environment
from game.camera import Camera
from game import sea from game import sea
from game.camera import Camera
from game.environment import Environment
proj_hfov = pi * 0.25 proj_hfov = pi * 0.25
proj_ratio = 16.0 / 9.0 proj_ratio = 16.0 / 9.0
proj_near_z = 8.0 proj_near_z = 8.0
proj_far_z = 3000.0 proj_far_z = 3000.0
sun_direction = vec3(math.vec3_normalize((1.0, 0.0, 1.0))) sun_direction = math.vec3_normalize((1.0, 0.0, 1.0))
sun_power = 1.0 sun_power = 1.0
camera_origin = vec3((0.0, -1200.0, 500.0)) camera_origin = (0.0, -1200.0, 500.0)
camera_lookat = vec3((0.0, 500.0, -500.0)) camera_lookat = (0.0, 500.0, -500.0)
def main(): def main():
print("Generating terrain...") print("Generating terrain...")
@ -138,6 +138,7 @@ def main():
tests_camera_inputs = camera.resolve_inputs(tests_shader) tests_camera_inputs = camera.resolve_inputs(tests_shader)
sky_camera_inputs = camera.resolve_inputs(sky_shader) sky_camera_inputs = camera.resolve_inputs(sky_shader)
environment = Environment()
terrain_environment_inputs = environment.resolve_inputs(terrain_shader) terrain_environment_inputs = environment.resolve_inputs(terrain_shader)
tests_environment_inputs = environment.resolve_inputs(tests_shader) tests_environment_inputs = environment.resolve_inputs(tests_shader)
sky_environment_inputs = environment.resolve_inputs(sky_shader) sky_environment_inputs = environment.resolve_inputs(sky_shader)
@ -182,15 +183,14 @@ def main():
tests_batch.set_param(1, clouds_id, _clouds_orientation) tests_batch.set_param(1, clouds_id, _clouds_orientation)
mat3_rotation(_rotation, up, (current_time * 0.05) % tau) mat3_rotation(_rotation, up, (current_time * 0.05) % tau)
mat3_mul_vec3(_origin, _rotation, camera_origin) mat3_mul_vec3(_origin, _rotation, vec3(camera_origin))
mat3_mul_vec3(_lookat, _rotation, camera_lookat) mat3_mul_vec3(_lookat, _rotation, vec3(camera_lookat))
camera.set_view(_origin, _lookat) camera.set_view(_origin, _lookat)
environment_values = environment.from_sun(camera.view, sun_direction, sun_power) environment.from_sun(camera.view, vec3(sun_direction), sun_power)
select_shader(terrain_shader) select_shader(terrain_shader)
camera.update_inputs(terrain_camera_inputs) camera.update_inputs(terrain_camera_inputs)
environment.update_inputs(terrain_environment_inputs, environment_values) environment.update_inputs(terrain_environment_inputs)
select_texture(tiles_texture, tiles_sampler) select_texture(tiles_texture, tiles_sampler)
select_texture(heightmap, terrain_heightmap_sampler) select_texture(heightmap, terrain_heightmap_sampler)
select_texture(normalmap, terrain_normalmap_sampler) select_texture(normalmap, terrain_normalmap_sampler)
@ -206,7 +206,7 @@ def main():
select_shader(tests_shader) select_shader(tests_shader)
camera.update_inputs(tests_camera_inputs) camera.update_inputs(tests_camera_inputs)
environment.update_inputs(tests_environment_inputs, environment_values) environment.update_inputs(tests_environment_inputs)
select_texture(tests_texture, tests_sampler) select_texture(tests_texture, tests_sampler)
select_texture(heightmap, tests_heightmap_sampler) select_texture(heightmap, tests_heightmap_sampler)
select_texture(normalmap, tests_normalmap_sampler) select_texture(normalmap, tests_normalmap_sampler)
@ -222,7 +222,7 @@ def main():
select_shader(sky_shader) select_shader(sky_shader)
camera.update_inputs(sky_camera_inputs) camera.update_inputs(sky_camera_inputs)
environment.update_inputs(sky_environment_inputs, environment_values) environment.update_inputs(sky_environment_inputs)
set_input_float(sea_phase, (current_time * 0.023) % 1.0) set_input_float(sea_phase, (current_time * 0.023) % 1.0)
select_texture(sea_polar_textures, sea_polar_sampler) select_texture(sea_polar_textures, sea_polar_sampler)
select_texture(sea_detail_texture, sea_detail_sampler) select_texture(sea_detail_texture, sea_detail_sampler)