rk_island/game/sea.py

72 lines
3.2 KiB
Python

# Copyright (C) 2022 RozK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from itertools import product
from math import tau, cos, sin, copysign
from array import array
from engine import (create_texture,
TEXTURE_FORMAT_RGB10_A2, TEXTURE_FORMAT_TYPECODE, TEXTURE_FLAG_MIN_LINEAR, TEXTURE_FLAG_MAG_LINEAR)
from game.math import vec3_scale, vec3_direction, vec3_cross, vec3_normal_rgb10a2
from game.resources import load_png
_format = TEXTURE_FORMAT_RGB10_A2
_typecode = TEXTURE_FORMAT_TYPECODE[TEXTURE_FORMAT_RGB10_A2]
_conv = vec3_normal_rgb10a2
_flags = TEXTURE_FLAG_MIN_LINEAR | TEXTURE_FLAG_MAG_LINEAR
def load_polar_textures(paths, waves_height = 0.008):
def load_texture(_path):
width, height, data = load_png(_path)
assert data.typecode == 'H'
assert len(data) == width * height
def polar(_y, _x, _h):
d = 1.0 + (_y / height)
a = (_x / width) * tau
return (d * sin(a), d * cos(a), (_h / 65535.0) * waves_height)
def normal(_pos, _h):
y, x = _pos
o = polar(y, x, _h)
n = vec3_cross(
vec3_direction(o, polar(y, x + 1, data[y * width + ((x + 1) % width)])),
vec3_direction(o, polar(y + 1, x, data[((y + 1) % height) * width + x])))
return vec3_scale(n, copysign(1.0, n[2]))
return (width, height, list(map(normal, product(range(height), range(width)), data)))
width, height, normals = load_texture(paths[0])
data = array(_typecode, list(map(_conv, normals)))
for path in paths[1:]:
_width, _height, normals = load_texture(path)
assert _width == width and _height == height
data.extend(list(map(_conv, normals)))
return create_texture(0, b'u_sea_polar_sampler', _format, width, height, len(paths), _flags, data)
def load_detail_texture(path, scale = 0.5, waves_height = 0.002):
width, height, data = load_png(path)
assert data.typecode == 'H'
assert len(data) == width * height
def coord(_y, _x, _h):
return ((_x / width) * scale, (_y / height) * scale, (_h / 65535.0) * waves_height)
def normal(_pos, _h):
y, x = _pos
o = coord(y, x, _h)
n = vec3_cross(
vec3_direction(o, coord(y, x + 1, data[y * width + ((x + 1) % width)])),
vec3_direction(o, coord(y + 1, x, data[((y + 1) % height) * width + x])))
return vec3_scale(n, copysign(1.0, n[2]))
normals = list(map(normal, product(range(height), range(width)), data))
data = array(_typecode, list(map(_conv, normals)))
return create_texture(1, b'u_sea_detail_sampler', _format, width, height, 0, _flags, data)