def update_ubo(): # Upload uniforms data to memory data_ptr = hvk.map_memory(api, device, uniforms_mem, 0, sizeof(uniforms_data_type)) uniforms = uniforms_data_type.from_address(data_ptr.value) ubo_data, light, mat = uniforms.ubo, uniforms.light, uniforms.mat # UBO width, height = window.dimensions() ubo_data[0] = Mat4.perspective(radians(60), width / height, 0.1, 256.0) # Perspective ubo_data[1] = Mat4.from_translation(0.0, 0.0, -zoom) # View model = Mat4.from_rotation(rotation[0], (1.0, 0.0, 0))\ .rotate(rotation[1], (0.0, 1.0, 0.0))\ .rotate(rotation[2], (0.0, 0.0, 1.0)) ubo_data[2] = model # Model ubo_data[3] = model.invert().transpose() # Normal # Light stuff light.viewPos[:3] = view light.pos[:3] = light_direction light.color = light_color light.ambient = light_ambient # Material stuff material = materials[material_index] mat.color = material['color'] mat.strenght = material['strength'] mat.shininess = material['shininess'] hvk.unmap_memory(api, device, uniforms_mem)
def look(self): cam = self.camera px, py, pz = self.position rx, ry = self.rotation center, up = Vec3(), Vec3.from_data(0, 1, 0) # Rotations eye = Vec3.from_data(pz * sin(rx) * cos(ry), pz * sin(ry), pz * cos(rx) * cos(ry)) n = (eye - center).normalize() u = Vec3.cross_product(up, n).normalize() v = Vec3.cross_product(n, u).normalize() # Truck u.scale(px) eye += u center += u # Pedestal v.scale(py) eye += v center += v look = Mat4.look_at(eye, center, up) cam.update_view(look)
def update_ubo(): # Upload uniforms data to memory data_ptr = hvk.map_memory(api, device, uniforms_mem, 0, sizeof(uniforms_data_type)) uniforms = uniforms_data_type.from_address(data_ptr.value) ubo_data = uniforms.ubo # Perspective width, height = window.dimensions() ubo_data[0] = Mat4.perspective(radians(60), width / height, 0.1, 256.0) # View ubo_data[1] = Mat4.from_translation(0.0, 0.0, -zoom) # Model ubo_data[2] = Mat4.from_rotation(rotation, (0.0, -1.0, 0.5)) hvk.unmap_memory(api, device, uniforms_mem)
def update_ubo(): # Upload uniforms data to memory data_ptr = hvk.map_memory(api, device, uniforms_mem, 0, sizeof(uniforms_data_type)) uniforms = uniforms_data_type.from_address(data_ptr.value) ubo_data, light = uniforms.ubo, uniforms.light # Perspective width, height = window.dimensions() ubo_data[0] = Mat4.perspective(radians(60), width / height, 0.1, 256.0) # View ubo_data[1] = Mat4.from_translation(0.0, 0.0, -zoom) # Model ubo_data[2] = Mat4.from_rotation(rotation, (0.0, -1.0, 0.5)) # Light stuff light.reverseLightDirection[:3] = Vec3.normalize(reverse_light_direction) hvk.unmap_memory(api, device, uniforms_mem)
def __init__(self, fov, width, height, position=(0,0,0), rotation=(0,0,0)): self.position = pos = list(position) self.rotation = rot = list(rotation) rotx = Mat4.from_rotation(radians(rot[0]), (1,0,0)) roty = Mat4.from_rotation(radians(rot[1]), (0,1,0)) rotz = Mat4.from_rotation(radians(rot[2]), (0,0,1)) cam = rotx * roty * rotz cam.translate(*pos) self.camera = cam self.view = view = cam.clone().invert() # Vulkan clip space has inverted Y and half Z. self.clip = Mat4.from_data(( 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 1.0 )) proj = Mat4.perspective(radians(fov), width/height, 0.001, 1000.0) self.projection = self.clip * proj
def _setup_assets(self): scene = self.scene # Images helmet_f = KTXFile.open("damaged_helmet.ktx") helmet_f = helmet_f.slice_array(slice(2, 3)) # Only keep the normal maps helmet_f = helmet_f[1:2] # Only keep the first mipmap helmet_f.cast_single( ) # Interpret the image as a single texture (not an array) helmet_maps = Image.from_ktx(helmet_f, name="HelmetTextureMaps") # Sampler helmet_sampler = Sampler.from_params( max_lod=helmet_maps.mipmaps_levels) # Shaders shader_normals_map = { "POSITION": "pos", "NORMAL": "normal", "TANGENT": "tangent", "TEXCOORD_0": "uv" } shader2_normals_map = { "POSITION": "pos", "NORMAL": "normal", "TEXCOORD_0": "uv" } shader_normals = Shader.from_files( f"debug_normals/debug_normals.vert.spv", f"debug_normals/debug_normals.frag.spv", f"debug_normals/debug_normals.map.json", name="DebugNormals") shader2_normals = Shader.from_files( f"debug_normals2/debug_normals2.vert.spv", f"debug_normals2/debug_normals2.frag.spv", f"debug_normals2/debug_normals2.map.json", name="DebugNormalsNoTangent") shader_normals.uniforms.debug = {"debug1": (0, 1, 0, 0)} shader2_normals.uniforms.debug = {"debug1": (0, 1, 0, 0)} # Meshes helmet_m = Mesh.from_gltf(GLBFile.open("damaged_helmet.glb"), "HelmetMesh", attributes_map=shader_normals_map, name="HelmetMesh") helmet_m2 = Mesh.from_gltf(GLTFFile.open("DamagedHelmet.gltf"), "HelmetMesh", attributes_map=shader2_normals_map, name="HelmetMesh2") # Objects helmet = GameObject.from_components(shader=shader_normals.id, mesh=helmet_m.id, name="Helmet") helmet.model = Mat4.from_rotation(radians(360), (0, 1, 0)).translate(-1, 0, 0) helmet.uniforms.normal_maps = CombinedImageSampler( image_id=helmet_maps.id, view_name="default", sampler_id=helmet_sampler.id) helmet2 = GameObject.from_components(shader=shader2_normals.id, mesh=helmet_m2.id, name="Helmet") helmet2.model = Mat4().from_rotation(radians(90), (1, 0, 0)).translate(1, 0, 0) helmet2.uniforms.normal_maps = CombinedImageSampler( image_id=helmet_maps.id, view_name="default", sampler_id=helmet_sampler.id) scene.shaders.extend(shader_normals, shader2_normals) scene.meshes.extend(helmet_m, helmet_m2) scene.images.extend(helmet_maps) scene.samplers.extend(helmet_sampler) scene.objects.extend(helmet, helmet2) self.objects = ( helmet, helmet2, ) self.shaders = (shader_normals, shader2_normals)
def _setup_assets(self): scene = self.scene # Images helmet_f = KTXFile.open("damaged_helmet.ktx") if __debug__: helmet_f = helmet_f[ 2: 3] # Speed up load time by only keeping a low res mipmap in debug mode specular_env_f = KTXFile.open("storm/specular_cubemap.ktx") irradiance_env_f = KTXFile.open("storm/irr_cubemap.ktx") with (IMAGE_PATH / "brdf.bin").open("rb") as f: brdf_args = { "format": vk.FORMAT_R16G16_UNORM, "extent": (128, 128, 1), "default_view_type": vk.IMAGE_VIEW_TYPE_2D } brdf_f = f.read() helmet_i = Image.from_ktx(helmet_f, name="HelmetTextureMaps") brdf_i = Image.from_uncompressed(brdf_f, name="BRDF", **brdf_args) env_i = Image.from_ktx(specular_env_f, name="CubemapTexture") env_irr_i = Image.from_ktx(irradiance_env_f, name="CubemapIrradianceTexture") # Sampler brdf_s = Sampler.new() env_s = Sampler.from_params(max_lod=env_i.mipmaps_levels) helmet_s = Sampler.from_params(max_lod=helmet_i.mipmaps_levels) # Shaders n = "pbr2/pbr2" shader_map = { "POSITION": "pos", "NORMAL": "normal", "TEXCOORD_0": "uv" } shader = Shader.from_files(f"{n}.vert.spv", f"{n}.frag.spv", f"{n}.map.json", name="PBRShader") color_factor = 1.0 emissive_factor = 1.0 exposure = 1.5 gamma = 2.2 shader.uniforms.render = { "light_color": (1.0, 1.0, 1.0), "env_lod": (0, env_i.mipmaps_levels), "factors": (color_factor, emissive_factor, exposure, gamma) } shader.uniforms.brdf = CombinedImageSampler(image_id=brdf_i.id, view_name="default", sampler_id=brdf_s.id) shader.uniforms.env_specular = CombinedImageSampler( image_id=env_i.id, view_name="default", sampler_id=env_s.id) shader.uniforms.env_irradiance = CombinedImageSampler( image_id=env_irr_i.id, view_name="default", sampler_id=brdf_s.id) # Meshes helmet_m = Mesh.from_gltf(GLTFFile.open("DamagedHelmet.gltf"), "HelmetMesh", attributes_map=shader_map, name="HelmetMesh") # Objects helmet = GameObject.from_components(shader=shader.id, mesh=helmet_m.id, name="Helmet") helmet.model = Mat4().from_rotation(radians(90), (1, 0, 0)) helmet.uniforms.texture_maps = CombinedImageSampler( image_id=helmet_i.id, view_name="default", sampler_id=helmet_s.id) # Packing scene.images.extend(helmet_i, brdf_i, env_i, env_irr_i) scene.samplers.extend(helmet_s, brdf_s, env_s) scene.shaders.extend(shader) scene.meshes.extend(helmet_m) scene.objects.extend(helmet) self.objects = (helmet, ) self.shaders = (shader, )
def _setup_assets(self): scene = self.scene engine = self.engine # Images w, h = self.heightmap_size heightmap_i = Image.empty( name = "HeightmapImage", extent=(w, h, 1), format=vk.FORMAT_R8G8B8A8_SNORM, usage=DEFAULT_IMAGE_USAGE | vk.IMAGE_USAGE_STORAGE_BIT, default_view_type=vk.IMAGE_VIEW_TYPE_2D, layout=ImageLayout.ShaderWrite ) placeholder_i = Image.empty( name = "PlaceholderImage", extent=(1,1,1), format=vk.FORMAT_R8G8B8A8_SNORM, default_view_type=vk.IMAGE_VIEW_TYPE_2D ) # Samplers heightmap_sm = Sampler.from_params( address_mode_V=vk.SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, address_mode_U=vk.SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, mag_filter=vk.FILTER_NEAREST, min_filter=vk.FILTER_NEAREST ) # Shaders dt = "debug_texture/debug_texture" debug_texture_attributes_map = {"POSITION": "pos", "TEXCOORD_0": "uv"} debug_texture_s = Shader.from_files(f"{dt}.vert.spv", f"{dt}.frag.spv", f"{dt}.map.json", name="DebugTexture") # Compute shaders compute_queue = "render" if "compute" in engine.queues: compute_queue = "compute" ch = "compute_heightmap/compute_heightmap" local_x, local_y = self.compute_local_size compute_heightmap_c = Compute.from_file(f"{ch}.comp.spv", f"{ch}.map.json", name="ComputeHeightmap", queue=compute_queue) compute_heightmap_c.set_constant("local_size_x", local_x) compute_heightmap_c.set_constant("local_size_y", local_y) compute_heightmap_c.uniforms.heightmap = CombinedImageSampler(image_id=heightmap_i.id, view_name="default", sampler_id=heightmap_sm.id) # Meshes plane_m = Mesh.from_prefab(MeshPrefab.Plane, attributes_map=debug_texture_attributes_map, name="PlaneMesh") # Game objects preview_heightmap_o = GameObject.from_components(shader = debug_texture_s.id, mesh = plane_m.id, name = "ObjTexture") preview_heightmap_o.model = Mat4() preview_heightmap_o.uniforms.color_texture = CombinedImageSampler(image_id=placeholder_i.id, view_name="default", sampler_id=heightmap_sm.id) scene.images.extend(heightmap_i, placeholder_i) scene.samplers.extend(heightmap_sm) scene.shaders.extend(debug_texture_s) scene.computes.extend(compute_heightmap_c) scene.meshes.extend(plane_m) scene.objects.extend(preview_heightmap_o) self.objects = (preview_heightmap_o,) self.shaders = () self.compute_heightmap = compute_heightmap_c self.heightmap_texture = heightmap_i self.heightmap_sampler = heightmap_sm self.heightmap_preview = preview_heightmap_o
def _setup_assets(self): scene = self.scene # Textures texture = Image.from_ktx(KTXFile.open("vulkan_logo.ktx"), name="Texture") array_texture = Image.from_ktx(KTXFile.open("array_test.ktx"), name="ArrayTexture") cubemap_texture = Image.from_ktx(KTXFile.open("storm/specular_cubemap.ktx"), name="CubemapTexture") with (IMAGE_PATH/"brdf.bin").open("rb") as f: texture_raw_data = f.read() texture_args = {"format": vk.FORMAT_R16G16_UNORM, "extent": (128, 128, 1), "default_view_type": vk.IMAGE_VIEW_TYPE_2D} raw_texture = Image.from_uncompressed(texture_raw_data, name="TextureRaw", **texture_args) # Samplers sampler = Sampler.from_params( address_mode_V=vk.SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, address_mode_U=vk.SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE ) sampler_lod = Sampler.from_params( address_mode_V=vk.SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, address_mode_U=vk.SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, max_lod=cubemap_texture.mipmaps_levels ) # Shaders simple_name = "debug_texture/debug_texture" array_name = "debug_texture_array/debug_texture_array" cube_name = "debug_texture_cube/debug_texture_cube" shader_attributes_map = {"POSITION": "pos", "TEXCOORD_0": "uv"} shader_simple = Shader.from_files(f"{simple_name}.vert.spv", f"{simple_name}.frag.spv", f"{simple_name}.map.json", name="DebugTexture") shader_array = Shader.from_files(f"{array_name}.vert.spv", f"{array_name}.frag.spv", f"{array_name}.map.json", name="DebugArrayTexture") shader_cube = Shader.from_files(f"{cube_name}.vert.spv", f"{cube_name}.frag.spv", f"{cube_name}.map.json", name="DebugCubeTexture") # Meshes plane_m = Mesh.from_prefab(MeshPrefab.Plane, attributes_map=shader_attributes_map, invert_y=True, name="PlaneMesh") plane_m2 = Mesh.from_prefab(MeshPrefab.Plane, attributes_map=shader_attributes_map, name="PlaneMesh") sphere_m = Mesh.from_gltf(GLBFile.open("test_sphere.glb"), "Sphere.001", attributes_map=shader_attributes_map, name="SphereMesh") # Objects plane1 = GameObject.from_components(shader = shader_simple.id, mesh = plane_m.id, name = "ObjTexture") plane1.model = Mat4() plane1.uniforms.color_texture = CombinedImageSampler(image_id=texture.id, view_name="default", sampler_id=sampler.id) plane2 = GameObject.from_components(shader = shader_simple.id, mesh = plane_m2.id, name = "ObjRawTexture", hidden=True) plane2.model = Mat4() plane2.uniforms.color_texture = CombinedImageSampler(image_id=raw_texture.id, view_name="default", sampler_id=sampler.id) plane3 = GameObject.from_components(shader = shader_array.id, mesh = plane_m.id, name = "ObjArrayTexture", hidden=True) plane3.model = Mat4() plane3.uniforms.color_texture = CombinedImageSampler(image_id=array_texture.id, view_name="default", sampler_id=sampler.id) sphere = GameObject.from_components(shader = shader_cube.id, mesh = sphere_m.id, name = "ObjCubeTexture", hidden=True) sphere.model = Mat4.from_rotation(radians(180), (1, 0, 0)) sphere.uniforms.cube_texture = CombinedImageSampler(image_id=cubemap_texture.id, view_name="default", sampler_id=sampler_lod.id) # Add objects to scene scene.shaders.extend(shader_simple, shader_array, shader_cube) scene.samplers.extend(sampler, sampler_lod) scene.images.extend(texture, array_texture, raw_texture, cubemap_texture) scene.meshes.extend(plane_m, plane_m2, sphere_m) scene.objects.extend(plane1, plane2, plane3, sphere) self.cubemap_max_mipmap = cubemap_texture.mipmaps_levels self.visible_index = 0 self.objects.extend((plane1, plane2, plane3, sphere))
def update_perspective(self, fov, width, height): self.projection = Mat4.perspective(radians(fov), width/height, 0.001, 1000.0) self.projection = self.clip * self.projection self.view_projection = self.projection * self.view