def __init__(self, distance_min=0, distance_max=1000, tile_size=10, margin=1., focal_margin=1.1): self.distance_min = distance_min self.distance_max = distance_max self.tile_size = tile_size self.O = hg.Vector2() self.A = hg.Vector2() self.B = hg.Vector2() self.focal_margin = focal_margin self.margin = margin self.Oint = hg.IntVector2() self.Aint = hg.IntVector2() self.Bint = hg.IntVector2() self.indiceA, self.indiceB = 0, 0 self.ymin, self.ymax = 0, 0 self.dAB, self.dBC, self.dAC = hg.IntVector2(), hg.IntVector2( ), hg.IntVector2() self.detAB, self.detBC, self.detAC = 0, 0, 0 self.obs = None self.obs2D = None self.vertices = [] self.case = 0 self.send_position = self.default_send
def render_aim_cursor(plus, scn, angle): radius = screen_width / 8 angle = 90 - angle a = hg.Vector2(cos(radians(angle)), sin(radians(angle))) * radius * 1.15 b = hg.Vector2(cos(radians(angle - 5)), sin(radians(angle - 5))) * radius c = hg.Vector2(cos(radians(angle + 5)), sin(radians(angle + 5))) * radius plus.Triangle2D(screen_width * 0.5 + a.x, screen_height * 0.15 + a.y, screen_width * 0.5 + b.x, screen_height * 0.15 + b.y, screen_width * 0.5 + c.x, screen_height * 0.15 + c.y, hg.Color.Green, hg.Color.Green, hg.Color.Green)
def get_2d(camera, renderer, point3d: hg.Vector3): f, pos = hg.Project(camera.GetTransform().GetWorld(), camera.GetCamera().GetZoomFactor(), renderer.GetAspectRatio(), point3d) if f: return hg.Vector2(pos.x, 1 - pos.y) else: return None
def __init__(self, plus, scene, main_light, resolution, parameters): self.layers = [] for layer_params in parameters["layers"]: self.layers.append(CloudsLayer(plus, scene, layer_params)) self.name = parameters["name"] self.t = 0 self.cam_pos = None self.bitmap_clouds = hg.Picture() self.bitmap_clouds_file = parameters["bitmap_clouds_file"] hg.LoadPicture(self.bitmap_clouds, self.bitmap_clouds_file) self.map_size = hg.IntVector2(self.bitmap_clouds.GetWidth(), self.bitmap_clouds.GetHeight()) self.map_scale = list_to_vec2(parameters["map_scale"]) self.map_position = list_to_vec2(parameters["map_position"]) self.sun_light = main_light self.ambient_color = scene.GetEnvironment().GetAmbientColor( ) * scene.GetEnvironment().GetAmbientIntensity() self.v_wind = hg.Vector2(60, 60) if "v_wind" in parameters: self.v_wind = list_to_vec2(parameters["v_wind"]) self.update_layers_cloud_map() self.update_layers_environment() self.update_particles()
def set_particle(self, position: hg.Vector2): self.num_tiles += 1 # x = int(position.x / self.map_scale.x * self.map_size.x) # y = int(position.y / self.map_scale.y * self.map_size.y) # c = self.bitmap_clouds.GetPixelRGBA(int(x % self.map_size.x), int(y % self.map_size.y)) c = self.get_pixel_bilinear( (position + self.offset * self.morph_level) / self.map_scale) scale_factor = pow(c.x, self.scale_falloff) # id = int(max(0,scale_factor - self.layer_2_alpha_threshold) / (1 - self.layer_2_alpha_threshold) * 7) id = int( (sin(position.x * 1735.972 + position.y * 345.145) * 0.5 + 0.5) * (self.num_geometries - 1)) if self.particle_index[id] < self.num_particles[id]: particle = self.particles[id][self.particle_index[id]] if c.x > self.alpha_threshold: smooth_alpha_threshold = min(1, (c.x - self.alpha_threshold) / self.smooth_alpha_threshold_step) s = self.particles_scale_range.x + scale_factor * self.scale_size py = self.altitude + s * self.altitude_floor + ( self.perturbation * (1 - c.x) * sin(position.x * 213)) pos = hg.Vector3(position.x - self.offset.x, py, position.y - self.offset.y) d = (hg.Vector2(pos.x, pos.z) - hg.Vector2(self.cam_pos.x, self.cam_pos.z)).Len() layer_n = abs( max( 0, min(1, (d - self.distance_min) / (self.distance_max - self.distance_min))) * 2 - 1) self.pc.a = (1 - min(pow(layer_n, 8), 1)) * ( 1 - pow(1. - scale_factor, self.alpha_scale_falloff)) * smooth_alpha_threshold particle[1] = hg.Matrix4(hg.Matrix3.Identity) particle[1].SetScale(hg.Vector3(s, s, s)) particle[1].SetTranslation(pos) material = particle[2] material.SetFloat2("pos0", position.x, position.y) material.SetFloat("alpha_cloud", self.pc.a) self.renderable_system.DrawGeometry(particle[0], particle[1]) self.particle_index[id] += 1
def __init__(self, plus, scene, parameters: dict): ViewTrame.__init__(self, parameters["distance_min"], parameters["distance_max"], parameters["tile_size"], parameters["margin"], parameters["focal_margin"]) self.name = parameters["name"] self.billboard_type = parameters["billboard_type"] self.particles_scale_range = list_to_vec2( parameters["particles_scale_range"]) self.num_tiles = 0 self.num_particles = parameters["num_particles"] # List ! self.num_geometries = parameters["num_geometries"] self.particle_index = [0] * self.num_geometries self.particle_index_prec = [0] * self.num_geometries self.particles_files_names = parameters[ "particles_files_names"] # List ! self.particles = [] for i in range(0, self.num_geometries): particles = self.create_particles(plus, scene, self.particles_files_names[i], self.num_particles[i], self.name + "." + str(i)) self.particles.append(particles) self.absorption = parameters["absorption"] self.altitude = parameters["altitude"] self.altitude_floor = parameters["altitude_floor"] self.alpha_threshold = parameters["alpha_threshold"] self.scale_falloff = parameters["scale_falloff"] self.alpha_scale_falloff = parameters["alpha_scale_falloff"] self.altitude_falloff = parameters["altitude_falloff"] self.perturbation = parameters["perturbation"] self.particles_rot_speed = 0.1 if "particles_rot_speed" in parameters: self.particles_rot_speed = parameters["particles_rot_speed"] # map: self.map_size = None self.map_scale = None self.bitmap_clouds = None # Environment: self.sun_dir = None self.sun_color = None self.ambient_color = None # Updates vars self.rot_hash = hg.Vector3(313.464, 7103.3, 4135.1) self.scale_size = 0 self.pc = hg.Color(1, 1, 1, 1) self.cam_pos = None self.t = 0 self.morph_level = 1.2 if "morph_level" in parameters: self.morph_level = parameters["morph_level"] self.offset = hg.Vector2(0, 0) #Used for clouds wind displacement self.renderable_system = scene.GetRenderableSystem() self.smooth_alpha_threshold_step = 0.1
def __init__(self, plus, scene): self.lumiere_soleil = scene.GetNode("Sun") self.lumiere_ciel = scene.GetNode("SkyLigth") self.couleur_horizon = hg.Color(255. / 255. * 0.75, 221. / 255. * 0.75, 199 / 255. * 0.75, 1.) # self.couleur_zenith=hg.Color(255/255.,252./255.,171./255.,1.) self.couleur_zenith = hg.Color(70. / 255. / 2, 150. / 255. / 2, 255. / 255. / 2, 1.) self.couleur_ambiante = hg.Color(78. / 255., 119. / 255., 107. / 255., 1.) # self.couleur_ambiante=hg.Color(1,0,0,1) renderer = plus.GetRenderer() # -------------- Init le shader de rendu de terrain: self.shader_terrain = renderer.LoadShader( "assets/shaders/terrain_marching_montagnes.isl") self.texture_terrain1 = renderer.LoadTexture( "assets/textures/terrain_1.png") self.texture_terrain2 = renderer.LoadTexture( "assets/textures/terrain_256.png") self.texture_terrain3 = renderer.LoadTexture( "assets/textures/terrain_2048.png") self.texture_cosmos = renderer.LoadTexture( "assets/textures/cosmos_b.png") self.facteur_echelle_terrain_l1 = hg.Vector2(20000, 20000) self.facteur_echelle_terrain_l2 = hg.Vector2(1000, 1000) self.facteur_echelle_terrain_l3 = hg.Vector2(100, 100) self.amplitude_l1 = 1000 self.amplitude_l2 = 90 self.amplitude_l3 = 1.5 self.terrain_intensite_ambiante = 0.2 self.offset_terrain = hg.Vector3(0, -50, 0) self.facteur_precision_distance = 1.01 self.couleur_neige = hg.Color(0.91 * 0.75, 0.91 * 0.75, 1 * 0.75) self.couleur_cristaux = hg.Color(133 / 255, 225 / 255, 181 / 255, 1) self.couleur_eau = hg.Color(117. / 255., 219. / 255., 211. / 255.) self.altitude_eau = 0 self.reflexion_eau = 0.5 self.intensite_cristaux = 3
def update_radial_post_process(acceleration): if acceleration<0: Main.RadialBlur_postProcess.SetStrength(Main.radial_blur_strength*pow(min(1, abs(acceleration*Main.deceleration_blur_strength)), 2)) else: Main.RadialBlur_postProcess.SetStrength(Main.radial_blur_strength*pow(min(1, abs(acceleration*Main.acceleration_blur_strength)), 4)) if Main.gun_sight_2D is not None and Main.current_view==back_view: Main.RadialBlur_postProcess.SetCenter(Main.gun_sight_2D) else: Main.RadialBlur_postProcess.SetCenter(hg.Vector2(0.5,0.5))
def fill_case(self, ymin, ymax, p_x0, p_x1, d1, d2): x0 = p_x0 x1 = p_x1 for y in range(ymin, ymax + 1): for x in range(int(x0), int(x1)): pos = hg.Vector2(x * self.tile_size, y * self.tile_size) if (pos - self.obs2D).Len() >= self.distance_min: self.send_position(pos) x0 += d1 x1 += d2
def begin_render(self, plus): renderer = plus.GetRenderer() renderer.SetWorldMatrix(hg.Matrix4.Identity) self.projection_matrix_mem = renderer.GetProjectionMatrix() self.view_matrix_mem = renderer.GetViewMatrix() self.projection_matrix_ortho = hg.ComputeOrthographicProjectionMatrix( 1., 500., 2, hg.Vector2(1, 1)) renderer.SetProjectionMatrix(self.projection_matrix_ortho) renderer.SetViewMatrix(hg.Matrix4.Identity)
def update_target_sight(Main, plus, aircraft: Aircraft): tps = hg.time_to_sec_f(plus.GetClock()) target = aircraft.get_target() f = Main.render_to_texture.value if target is not None: p2D = get_2d(Main.scene.GetCurrentCamera(), plus.GetRenderer(), target.get_parent_node().GetTransform().GetPosition()) if p2D is not None: a_pulse = 0.5 if (sin(tps * 20) > 0) else 0.75 if aircraft.target_locked: c = hg.Color(1., 0.5, 0.5, a_pulse) msg = "LOCKED - " + str(int(aircraft.target_distance)) x = (p2D.x - 32 / 1600) a = a_pulse else: msg = str(int(aircraft.target_distance)) x = (p2D.x - 12 / 1600) c = hg.Color(0.5, 1, 0.5, 0.75) c.a *= f plus.Sprite2D(p2D.x * Main.resolution.x, p2D.y * Main.resolution.y, 32 / 1600 * Main.resolution.x, "assets/sprites/target_sight.png", c) if aircraft.target_out_of_range: plus.Text2D((p2D.x - 40 / 1600) * Main.resolution.x, (p2D.y - 24 / 900) * Main.resolution.y, "OUT OF RANGE", 0.012 * Main.resolution.y, hg.Color(0.2, 1, 0.2, a_pulse * f)) else: plus.Text2D(x * Main.resolution.x, (p2D.y - 24 / 900) * Main.resolution.y, msg, 0.012 * Main.resolution.y, c) if aircraft.target_locking_state > 0: t = sin(aircraft.target_locking_state * pi - pi / 2) * 0.5 + 0.5 p2D = hg.Vector2(0.5, 0.5) * (1 - t) + p2D * t plus.Sprite2D(p2D.x * Main.resolution.x, p2D.y * Main.resolution.y, 32 / 1600 * Main.resolution.x, "assets/sprites/missile_sight.png", c) c = hg.Color(0, 1, 0, f) plus.Text2D(0.05 * Main.resolution.x, 0.93 * Main.resolution.y, "Target dist: %d" % (aircraft.target_distance), 0.016 * Main.resolution.y, c) plus.Text2D(0.05 * Main.resolution.x, 0.91 * Main.resolution.y, "Target cap: %d" % (aircraft.target_cap), 0.016 * Main.resolution.y, c) plus.Text2D(0.05 * Main.resolution.x, 0.89 * Main.resolution.y, "Target alt: %d" % (aircraft.target_altitude), 0.016 * Main.resolution.y, c)
def __init__(self, name, plus, scene, node_file_name, num_particles, start_scale, end_scale, stream_angle,color_label="teint"): self.name = name self.color_label=color_label self.particles_cnt = 0 self.particles_cnt_f = 0 self.num_particles = num_particles self.flow = 8 self.particles_delay = 3 self.particles = [] self.create_particles(plus, scene, node_file_name) self.start_speed_range = hg.Vector2(800, 1200) self.delay_range = hg.Vector2(1, 2) self.start_scale = start_scale self.end_scale = end_scale self.scale_range = hg.Vector2(1,2) self.stream_angle = stream_angle self.colors = [hg.Color(1, 1, 1, 1), hg.Color(1, 1, 1, 0)] self.start_offset = 0 self.rot_range_x = hg.Vector2(0, 0) self.rot_range_y = hg.Vector2(0, 0) self.rot_range_z = hg.Vector2(0, 0) self.gravity=hg.Vector3(0,-9.8,0) self.linear_damping = 1 self.loop=True self.end=False #True when loop=True and all particles are dead self.num_new=0 self.reset()
def GameControlCamera(dt): mouse_device = hg.GetInputSystem().GetDevice("mouse") mouse_wheel = mouse_device.GetValue(hg.InputRotY) if mouse_wheel != 0.0: gg.camera_zoom_target += gg.zoom_speed * hg.time_to_sec_f(dt) * mouse_wheel gg.camera_zoom_target = max(min(gg.camera_zoom_target, gg.zoom_min_max.y), gg.zoom_min_max.x) print(gg.camera_zoom_target) if mouse_device.IsButtonDown(hg.Button0): gg.mouse = hg.Vector2(mouse_device.GetValue(hg.InputAxisX), mouse_device.GetValue(hg.InputAxisY)) mouse_speed = gg.prev_mouse - gg.mouse gg.prev_mouse = hg.Vector2(gg.mouse) # print("Mouse X: %f, Mouse Y: %f" % (mouse_speed.x, mouse_speed.y)) gg.camera_target_angle.x -= radians(mouse_speed.y * hg.time_to_sec_f(dt) * gg.camera_rot_x_speed) gg.camera_target_angle.x = max(min(gg.camera_target_angle.x, gg.camera_rot_x_min_max.y), gg.camera_rot_x_min_max.x) gg.camera_target_angle.y += radians(mouse_speed.x * hg.time_to_sec_f(dt) * gg.camera_rot_y_speed) else: gg.mouse = hg.Vector2(mouse_device.GetValue(hg.InputAxisX), mouse_device.GetValue(hg.InputAxisY)) gg.prev_mouse = hg.Vector2(gg.mouse) cam_dt = gg.camera_target_angle - gg.camera_angle zoom_dt = gg.camera_zoom_target - gg.camera_zoom if gg.camera_lazyness == 0: cam_dt *= hg.time_to_sec_f(dt) zoom_dt *= hg.time_to_sec_f(dt) else: cam_dt *= hg.time_to_sec_f(dt) * (1.0 / gg.camera_lazyness) zoom_dt *= hg.time_to_sec_f(dt) * (1.0 / gg.camera_lazyness) gg.camera_angle += cam_dt gg.camera.GetTransform().SetRotation(gg.camera_angle * hg.Vector3(1.0, 0.0, 0.0)) gg.camera_y_controller.GetTransform().SetRotation(gg.camera_angle * hg.Vector3(0.0, 1.0, 0.0)) gg.camera_zoom += zoom_dt gg.camera.GetCamera().SetZoomFactor(gg.camera_zoom)
def load_json_script(self, file_name="assets/scripts/terrain_parameters.json"): json_script = hg.GetFilesystem().FileToString(file_name) if json_script != "": script_parameters = json.loads(json_script) self.facteur_echelle_terrain_l1 = hg.Vector2( script_parameters["map1_scale_x"], script_parameters["map1_scale_y"]) self.facteur_echelle_terrain_l2 = hg.Vector2( script_parameters["map2_scale_x"], script_parameters["map2_scale_y"]) self.facteur_echelle_terrain_l3 = hg.Vector2( script_parameters["map3_scale_x"], script_parameters["map3_scale_y"]) self.amplitude_l1 = script_parameters["map1_amplitude"] self.amplitude_l2 = script_parameters["map2_amplitude"] self.amplitude_l3 = script_parameters["map3_amplitude"] self.facteur_precision_distance = script_parameters[ "distance_factor"] self.altitude_eau = script_parameters["water_altitude"] self.reflexion_eau = script_parameters["water_reflexion"] self.offset_terrain.x = script_parameters["offset_x"] self.offset_terrain.y = script_parameters["offset_y"] self.offset_terrain.z = script_parameters["offset_z"]
def affiche_courbe(cls, plus): resolution = hg.Vector2(float(plus.GetScreenWidth()), float(plus.GetScreenHeight())) num = len(cls.courbe) if num > 10: x_step = resolution.x / (num - 1) x1 = 0 x2 = x_step y1 = (cls.courbe[0] - cls.ymin) / (cls.ymax - cls.ymin) * resolution.y for i in range(num - 1): y2 = (cls.courbe[i + 1] - cls.ymin) / (cls.ymax - cls.ymin) * resolution.y plus.Line2D(x1, y1, x2, y2, hg.Color.Yellow, hg.Color.Yellow) x1 = x2 x2 += x_step y1 = y2
def __init__(self, plus, scene, resolution: hg.Vector2, texture_width=256): renderer = plus.GetRenderer() render_system = plus.GetRenderSystem() # Parameters: self.color = hg.Color(1, 1, 0, 1) self.reflect_level = 0.75 # Shaders: # self.shader_water_reflection = render_system.LoadSurfaceShader("assets/shaders/water_reflection.isl") # Reflection plane, just to get normal & origine: # self.plane=plus.AddPlane(scene, hg.Matrix4.TransformationMatrix(hg.Vector3(0,0,0), hg.Vector3(radians(0), radians(0), radians(0))), 1, 1) # self.plane.SetName("Water_Reflection_Plane") # Création des textures de rendu: tex_res = hg.Vector2(texture_width, texture_width * resolution.y / resolution.x) self.render_texture = renderer.NewTexture() renderer.CreateTexture(self.render_texture, int(tex_res.x), int(tex_res.y), hg.TextureRGBA8, hg.TextureNoAA, 0, False) self.render_depth_texture = renderer.NewTexture() renderer.CreateTexture(self.render_depth_texture, int(tex_res.x), int(tex_res.y), hg.TextureDepth, hg.TextureNoAA, 0, False) # Création des frameBuffer objects: self.render_target = renderer.NewRenderTarget() renderer.CreateRenderTarget(self.render_target) renderer.SetRenderTargetColorTexture(self.render_target, self.render_texture) renderer.SetRenderTargetDepthTexture(self.render_target, self.render_depth_texture) self.projection_matrix_mem = None self.view_matrix_mem = None self.projection_matrix_ortho = None # Reflection camera: self.camera_reflect = plus.AddCamera( scene, hg.Matrix4.TranslationMatrix(hg.Vector3(0, -2, 0))) self.camera_reflect.SetName("Camera.water_reflection") self.clear_reflect_map(plus)
def affiche_vecteur(cls, plus: hg.Plus, camera, position, direction, unitaire=True, c1=hg.Color.Yellow, c2=hg.Color.Red): resolution = hg.Vector2(float(plus.GetScreenWidth()), float(plus.GetScreenHeight())) if unitaire: position_b = position + direction.Normalized() else: position_b = position + direction pA = cls.get_2d(camera, plus.GetRenderer(), position) pB = cls.get_2d(camera, plus.GetRenderer(), position_b) if pA is not None and pB is not None: plus.Line2D(pA.x * resolution.x, pA.y * resolution.y, pB.x * resolution.x, pB.y * resolution.y, c1, c2)
def gui_ScreenModeRequester(): global flag_windowed global current_monitor, current_mode, monitors_names, modes hg.ImGuiSetNextWindowPosCenter(hg.ImGuiCond_Always) hg.ImGuiSetNextWindowSize(hg.Vector2(res_w, res_h), hg.ImGuiCond_Always) if hg.ImGuiBegin( "Choose screen mode", hg.ImGuiWindowFlags_NoTitleBar | hg.ImGuiWindowFlags_MenuBar | hg.ImGuiWindowFlags_NoMove | hg.ImGuiWindowFlags_NoSavedSettings | hg.ImGuiWindowFlags_NoCollapse): if hg.ImGuiBeginCombo("Monitor", monitors_names[current_monitor]): for i in range(len(monitors_names)): f = hg.ImGuiSelectable(monitors_names[i], current_monitor == i) if f: current_monitor = i hg.ImGuiEndCombo() '''if hg.ImGuiBeginCombo("Screen size", modes[current_monitor][current_mode].name): for i in range(len(modes[current_monitor])): f = hg.ImGuiSelectable(modes[current_monitor][i].name+"##"+str(i), current_mode == i) if f: current_mode = i hg.ImGuiEndCombo() f, d = hg.ImGuiCheckbox("Windowed", flag_windowed) if f: flag_windowed = d''' # 指定解析度視窗畫 current_mode = 1 flag_windowed = True ok = hg.ImGuiButton("Ok") hg.ImGuiSameLine() cancel = hg.ImGuiButton("Quit") hg.ImGuiEnd() return "ok" '''if ok:
def rotate_vector_2D(cls, p: hg.Vector2, angle): cos_angle = cos(angle) sin_angle = sin(angle) return hg.Vector2(p.x * cos_angle - p.y * sin_angle, p.x * sin_angle + p.y * cos_angle)
def list_to_vec2(v: list): return hg.Vector2(v[0], v[1])
# add a new scene, which is a container of all objects you want to draw/update scn = plus.NewScene() plus.LoadScene(scn, "@assets/geometrics/geometrics.scn") scn.UpdateAndCommitWaitAll() cam = scn.GetCurrentCamera() pos = cam.GetTransform().GetPosition() pos.y = 1.65 cam.GetTransform().SetPosition(pos) simple_graphic = hg.SimpleGraphicSceneOverlay(False) simple_graphic.SetDepthTest(False) simple_graphic.SetBlendMode(hg.BlendAlpha) scn.AddComponent(simple_graphic) teleporter.setup_teleporter(scn, hg.Vector2(200, 200)) # check if there is VR available and Initialise the frame renderer try: openvr_frame_renderer = hg.CreateFrameRenderer("VR") # we have the vr frame renderer # Initialize it to open steamVR if openvr_frame_renderer.Initialize(plus.GetRenderSystem()): # add the new frame renderer to the scene. # the scene will automatically use it to render the scene. scn.GetRenderableSystem().SetFrameRenderer(openvr_frame_renderer) print("!! Use VR") else: openvr_frame_renderer = None print("!! No VR detected")
def update_triangle(self, resolution, position: hg.Vector3, direction: hg.Vector3, zoomFactor): self.obs = position self.obs2D = hg.Vector2(self.obs.x, self.obs.z) dir3D = direction dir2D = hg.Vector2(dir3D.x, dir3D.z).Normalized() focal_distance = zoomFactor * self.focal_margin view_width = 2 * resolution.x / resolution.y # 2 because screen xmin=-1, screen xmax=1 distAB = self.distance_max * view_width / focal_distance VUab = hg.Vector2(-dir2D.y, dir2D.x) dir2D *= self.distance_max VUab *= distAB / 2 self.O = hg.Vector2(self.obs.x, self.obs.z) self.A = hg.Vector2(self.obs.x + dir2D.x - VUab.x, self.obs.z + dir2D.y - VUab.y) self.B = hg.Vector2(self.obs.x + dir2D.x + VUab.x, self.obs.z + dir2D.y + VUab.y) # Margin: cx = (self.O.x + self.A.x + self.B.x) / 3 cy = (self.O.y + self.A.y + self.B.y) / 3 self.O.x = (self.O.x - cx) * self.margin + cx self.O.y = (self.O.y - cy) * self.margin + cy self.A.x = (self.A.x - cx) * self.margin + cx self.A.y = (self.A.y - cy) * self.margin + cy self.B.x = (self.B.x - cx) * self.margin + cx self.B.y = (self.B.y - cy) * self.margin + cy # Tiled triangle: self.Oint = hg.IntVector2(int(round(self.O.x / self.tile_size)), int(round(self.O.y / self.tile_size))) self.Aint = hg.IntVector2(int(round(self.A.x / self.tile_size)), int(round(self.A.y / self.tile_size))) self.Bint = hg.IntVector2(int(round(self.B.x / self.tile_size)), int(round(self.B.y / self.tile_size))) self.vertices = [self.Oint, self.Aint, self.Bint] self.indiceA = 0 self.ymin = self.Oint.y self.ymax = self.ymin if self.Aint.y < self.ymin: self.ymin = self.Aint.y self.indiceA = 1 if self.Bint.y < self.ymin: self.ymin = self.Bint.y self.indiceA = 2 if self.Aint.y > self.ymax: self.ymax = self.Aint.y if self.Bint.y > self.ymax: self.ymax = self.Bint.y self.indiceB = (self.indiceA + 1) % 3 self.indiceC = (self.indiceA + 2) % 3 if self.vertices[self.indiceA].y == self.vertices[self.indiceC].y: self.indiceA = self.indiceC self.indiceB = (self.indiceA + 1) % 3 self.indiceC = (self.indiceA + 2) % 3 self.dAB.x = self.vertices[self.indiceB].x - self.vertices[ self.indiceA].x self.dAB.y = self.vertices[self.indiceB].y - self.vertices[ self.indiceA].y self.dBC.x = self.vertices[self.indiceC].x - self.vertices[ self.indiceB].x self.dBC.y = self.vertices[self.indiceC].y - self.vertices[ self.indiceB].y self.dAC.x = self.vertices[self.indiceC].x - self.vertices[ self.indiceA].x self.dAC.y = self.vertices[self.indiceC].y - self.vertices[ self.indiceA].y if self.dAB.y == 0: self.detAB = 0 else: self.detAB = float(self.dAB.x) / float(self.dAB.y) if self.dBC.y == 0: self.detBC = 0 else: self.detBC = float(self.dBC.x) / float(self.dBC.y) if self.dAC.y == 0: self.detAC = 0 else: self.detAC = float(self.dAC.x) / float(self.dAC.y)
terrain.load_parameters() def update_view(plus, scene, fps, delta_t): camera = scene.GetNode("Camera") fps.UpdateAndApplyToNode(camera, delta_t) # ================================================================================================== # Program start here # ================================================================================================== # Display settings resolution = hg.Vector2(1600, 900) antialiasing = 4 screenMode = hg.Windowed # System setup plus = hg.GetPlus() hg.LoadPlugins() plus.Mount("./") # Run display plus.RenderInit(int(resolution.x), int(resolution.y), antialiasing, screenMode) plus.SetBlend2D(hg.BlendAlpha) # Setup dashboard: scene, fps = init_scene(plus) render_to_texture = RenderToTexture(plus, resolution)
def set_rot_range(self,xmin,xmax,ymin,ymax,zmin,zmax): self.rot_range_x = hg.Vector2(xmin, xmax) self.rot_range_y = hg.Vector2(ymin, ymax) self.rot_range_z = hg.Vector2(zmin, zmax)
def init_game(plus): init_scene(plus) Aircraft.main_node = Main.main_node Main.audio = hg.CreateMixer() Main.audio.Open() # Clear color alpha = 0 Main.scene.GetEnvironment().SetBackgroundColor(hg.Color(0, 0, 0, 0)) # Aircrafts & Cie: Main.p1_aircraft = Aircraft("TangoCharly", 1, "aircraft", plus, Main.scene, hg.Vector3(0, 3000, 0), hg.Vector3(0, 0, 0)) Main.p2_aircraft = Aircraft("Zorglub", 2, "ennemyaircraft", plus, Main.scene, hg.Vector3(4000, 3000, 4000), hg.Vector3(0, 0, 0)) Main.carrier = Carrier("Charles_de_Gaules", 1, plus, Main.scene) for i in range(4): Main.p1_missiles[i] = Missile("sidewinder_" + str(i), 1, plus, Main.scene, Main.audio, "assets/weaponry/enemymissile_low.geo", "assets/weaponry/enemymissile_smoke") Main.p2_missiles[i] = Missile("ennemy_sidewinder_" + str(i), 2, plus, Main.scene, Main.audio, "assets/weaponry/enemymissile_low.geo", "assets/weaponry/enemymissile_smoke") # Machine guns : Main.bullets = Main.p1_aircraft.gun_machine Main.ennemy_bullets = Main.p2_aircraft.gun_machine Main.p1_aircraft.set_destroyable_targets([Main.p2_aircraft, Main.carrier]) Main.p2_aircraft.set_destroyable_targets([Main.p1_aircraft, Main.carrier]) # Fps Main.fps = hg.FPSController(0, 0, 0) Main.controller=find_controller("xinput.port0") Main.scene.Commit() Main.scene.WaitCommit() #plus.UpdateScene(Main.scene) load_game_parameters() Main.texture_hud_plot_aircraft = plus.LoadTexture("assets/sprites/plot.png") Main.texture_hud_plot_missile = plus.LoadTexture("assets/sprites/plot_missile.png") Main.texture_hud_plot_ship = plus.LoadTexture("assets/sprites/plot_ship.png") Main.texture_hud_plot_dir = plus.LoadTexture("assets/sprites/plot_dir.png") Main.texture_noise = plus.LoadTexture("assets/sprites/noise.png") # ---- Blend settings: renderer = plus.GetRenderer() renderer.SetBlendFunc(hg.BlendSrcAlpha, hg.BlendOneMinusSrcAlpha) # --- Sfx: Main.p1_sfx = AircraftSFX(Main.p1_aircraft) Main.p2_sfx = AircraftSFX(Main.p2_aircraft) # P2 engine sound is different: Main.p2_sfx.set_air_pitch(0.75) Main.p2_sfx.set_pc_pitch(1.5) Main.p2_sfx.set_turbine_pitch_levels(hg.Vector2(1.5, 2.5)) # ---- Camera: Main.scene.SetCurrentCamera(Main.camera) # ---- PostProcess: Main.MotionBlur_postProcess=hg.MotionBlurPostProcess() Main.HSL_postProcess=hg.HSLPostProcess() Main.RadialBlur_postProcess=hg.RadialBlurPostProcess() Main.camera.AddComponent(Main.RadialBlur_postProcess) Main.camera.AddComponent(Main.HSL_postProcess) post_processes_load_parameters()
# path finding sample, left click to change start point, right click to change end point. import harfang as hg hg.LoadPlugins() hg.MountFileDriver(hg.StdFileDriver("_data/")) resolution = hg.Vector2(1280, 720) antialiasing = 4 plus = hg.GetPlus() plus.RenderInit(int(resolution.x), int(resolution.y), antialiasing) # setup scene scn = plus.NewScene(False, False) plus.AddEnvironment(scn,hg.Color.Black, hg.Color(0.25, 0.25, 0.3)) cam = plus.AddCamera(scn, hg.Matrix4.TransformationMatrix(hg.Vector3(0, 65, -55), hg.Vector3(0.9, 0, 0))) lgt = plus.AddLight(scn, hg.Matrix4.TransformationMatrix(hg.Vector3(40, 50, -60), hg.Vector3(0.75, -0.6, 0)), hg.LightModelSpot) lgt.GetLight().SetConeAngle(0.49) lgt.GetLight().SetEdgeAngle(0.1) gfx = hg.SimpleGraphicSceneOverlay(False) scn.AddComponent(gfx) # create the navigation system and component nav_sys = hg.NavigationSystem() nav_sys.SetDebugVisuals(False) scn.AddSystem(nav_sys)
class Main: resolution = hg.Vector2(1600, 900) antialiasing = 2 screenMode = hg.FullscreenMonitor1 main_node = hg.Node() controller = None scene = None camera = None satellite_camera = None camera_matrix = None camera_v_move = hg.Vector3(0, 0, 0) # Camera velocity for sfx fps = None sea_render = None ligth_sun = None ligth_sky = None sea_render_script = None clouds_render_script = None water_reflection = None p1_aircraft = None p2_aircraft = None p1_success = False carrier = None carrier_radar = None island = None p1_missiles = [None] * 4 p2_missiles = [None] * 4 p1_missiles_smoke_color = hg.Color(1, 1, 1, 1) p2_missiles_smoke_color = hg.Color(1, 1, 1, 1) p1_targets = [] bullets = None ennemy_bullets = None title_font = "assets/fonts/destroy.ttf" hud_font = "assets/fonts/Furore.otf" texture_hud_plot = None texture_noise = None fading_cptr = 0 fading_start_saturation = 0 fadout_flag = False fadout_cptr = 0 audio = None p1_sfx = None p2_sfx = None title_music = 0 title_music_settings = None clouds = None render_volumetric_clouds = True show_debug_displays = False display_gui = False satellite_view = False HSL_postProcess=None MotionBlur_postProcess=None RadialBlur_postProcess=None flag_MotionBlur=False radial_blur_strength = 0.5 deceleration_blur_strength=1/6 acceleration_blur_strength=1/3 gun_sight_2D=None current_view=None
renderer.SetViewport(hg.Rect(0, 0, width, height)) # set perspective matrix persp_mat = hg.ComputePerspectiveProjectionMatrix(0.1, 100, 3.2, renderer.GetAspectRatio()) renderer.SetProjectionMatrix(persp_mat) # clear the viewport with green color renderer.Clear(hg.Color(0.05, 0.05, 0.05, 0)) # blit the picture with the clock in a texture draw_clock() renderer.BlitTexture(tex, pic) # draw the triangle using the render system x, y, z, w = 2.0, 1.5, 10, 0.55 vertices = [hg.Vector3(-x, -y + w, z*0.5), hg.Vector3(-x, y + w, z), hg.Vector3(x, y + w, z), hg.Vector3(-x, -y + w, z*0.5), hg.Vector3(x, y + w, z), hg.Vector3(x, -y + w, z*0.5)] uvs = [hg.Vector2(1, 1), hg.Vector2(1, 0), hg.Vector2(0, 0), hg.Vector2(1, 1), hg.Vector2(0, 0), hg.Vector2(0, 1)] render_system.DrawTriangleAuto(2, vertices, [], uvs, tex) renderer.DrawFrame() renderer.ShowFrame() hg.UpdateWindow(win) hg.EndFrame() render_system.Free() renderer.Close()
import harfang as hg from math import radians scale = 0.75 width, height = int(1280 * scale), int(720 * scale) scene = None spawn_point = None osd = None camera_y_controller = None camera = None camera_angle = None camera_target_angle = None camera_lazyness = 0.1 # [0:1] camera_zoom = 0.5 camera_zoom_target = 3.0 zoom_speed = 15.0 zoom_min_max = hg.Vector2(1.5, 10.0) prev_mouse = hg.Vector2(0, 0) mouse = hg.Vector2(0, 0) camera_rot_x_speed = (height / 35.0) camera_rot_y_speed = (width / 50.0) camera_rot_x_min_max = hg.Vector2(radians(-20.0), radians(30.0))
def update_radar(Main, plus, aircraft, targets): value = Main.render_to_texture.value t = hg.time_to_sec_f(plus.GetClock()) rx, ry = 150 / 1600 * Main.resolution.x, 150 / 900 * Main.resolution.y rs = 200 / 1600 * Main.resolution.x rm = 6 / 1600 * Main.resolution.x radar_scale = 4000 plot_size = 12 / 1600 * Main.resolution.x / 2 plus.Sprite2D(rx, ry, rs, "assets/sprites/radar.png", hg.Color(1, 1, 1, value)) mat = aircraft.get_parent_node().GetTransform().GetWorld() aZ = mat.GetZ() aZ.y = 0 aZ.Normalize() aY = mat.GetY() if aY.y < 0: aY = hg.Vector3(0, -1, 0) else: aY = hg.Vector3(0, 1, 0) aX = hg.Cross(aY, aZ) mat_trans = hg.Matrix4.TransformationMatrix(mat.GetTranslation(), hg.Matrix3(aX, aY, aZ)).InversedFast() for target in targets: if not target.wreck and target.activated: t_mat = target.get_parent_node().GetTransform().GetWorld() aZ = t_mat.GetZ() aZ.y = 0 aZ.Normalize() aY = hg.Vector3(0, 1, 0) aX = hg.Cross(aY, aZ) t_mat_trans = mat_trans * hg.Matrix4.TransformationMatrix( t_mat.GetTranslation(), hg.Matrix3(aX, aY, aZ)) pos = t_mat_trans.GetTranslation() v2D = hg.Vector2(pos.x, pos.z) / radar_scale * rs / 2 if abs(v2D.x) < rs / 2 - rm and abs(v2D.y) < rs / 2 - rm: if target.type == Destroyable_Machine.TYPE_MISSILE: plot = Main.texture_hud_plot_missile elif target.type == Destroyable_Machine.TYPE_AIRCRAFT: plot = Main.texture_hud_plot_aircraft elif target.type == Destroyable_Machine.TYPE_SHIP: plot = Main.texture_hud_plot_ship t_mat_rot = t_mat_trans.GetRotationMatrix() ps = plot_size a = 0.5 + 0.5 * abs(sin(t * uniform(1, 500))) else: if target.type == Destroyable_Machine.TYPE_MISSILE: continue dir = v2D.Normalized() v2D = dir * (rs / 2 - rm) ps = plot_size plot = Main.texture_hud_plot_dir aZ = hg.Vector3(dir.x, 0, dir.y) aX = hg.Cross(hg.Vector3.Up, aZ) t_mat_rot = hg.Matrix3(aX, hg.Vector3.Up, aZ) a = 0.5 + 0.5 * abs(sin(t * uniform(1, 500))) cx, cy = rx + v2D.x, ry + v2D.y if target.nationality == 1: c = hg.Color(0.25, 1., 0.25, a) elif target.nationality == 2: c = hg.Color(1., 0.5, 0.5, a) c.a *= value p1 = t_mat_rot * hg.Vector3(-plot_size, 0, -ps) p2 = t_mat_rot * hg.Vector3(-plot_size, 0, ps) p3 = t_mat_rot * hg.Vector3(plot_size, 0, ps) p4 = t_mat_rot * hg.Vector3(plot_size, 0, -ps) plus.Quad2D(cx + p1.x, cy + p1.z, cx + p2.x, cy + p2.z, cx + p3.x, cy + p3.z, cx + p4.x, cy + p4.z, c, c, c, c, plot) c = hg.Color(1, 1, 1, value * max(pow(1 - aircraft.health_level, 2), 0.05)) plus.Quad2D(rx - rs / 2, ry - rs / 2, rx - rs / 2, ry + rs / 2, rx + rs / 2, ry + rs / 2, rx + rs / 2, ry - rs / 2, c, c, c, c, Main.texture_noise, 0.25 + 0.25 * sin(t * 103), (0.65 + 0.35 * sin(t * 83)), 0.75 + 0.25 * sin(t * 538), 0.75 + 0.25 * cos(t * 120)) c = hg.Color(1, 1, 1, value) plus.Sprite2D(rx, ry, rs, "assets/sprites/radar_light.png", hg.Color(1, 1, 1, 0.3 * value)) plus.Sprite2D(rx, ry, rs, "assets/sprites/radar_box.png", c)