diff --git a/binary/src/flex_renderer.cpp b/binary/src/flex_renderer.cpp index 5c3fa2d..889a05b 100644 --- a/binary/src/flex_renderer.cpp +++ b/binary/src/flex_renderer.cpp @@ -6,6 +6,7 @@ extern IVEngineClient* engine = NULL; //extern IMaterialSystem* materials = NULL; // stops main branch compile from bitching #define min(a, b) a < b ? a : b +#define max(a, b) a > b ? a : b float u[3] = { 0.5 - SQRT3 / 2, 0.5, 0.5 + SQRT3 / 2 }; float v[3] = { 1, -0.5, 1 }; @@ -97,22 +98,18 @@ IMesh* _build_diffuse(int id, FlexRendererThreadData data) { // calculate triangle rotation //Vector forward = (eye_pos - particle_pos).Normalized(); - Vector forward = (particle_pos - data.eye_pos).Normalized(); - Vector right = forward.Cross(Vector(0, 0, 1)).Normalized(); - Vector up = right.Cross(forward); - Vector local_pos[3] = { (-up - right * SQRT3), up * 2.0, (-up + right * SQRT3) }; Vector4D ani0 = data.particle_ani0[particle_index]; float scalar = data.radius * data.particle_positions[particle_index].w; for (int i = 0; i < 3; i++) { - Vector pos_ani = local_pos[i]; // Warp based on velocity - pos_ani = pos_ani + (data.particle_ani0[particle_index].AsVector3D() * pos_ani.Dot(data.particle_ani0[particle_index].AsVector3D()) * 0.0016).Min(Vector(3, 3, 3)).Max(Vector(-3, -3, -3)); + //Vector pos_ani = local_pos[i]; // Warp based on velocity + //pos_ani = pos_ani + (data.particle_ani0[particle_index].AsVector3D() * pos_ani.Dot(data.particle_ani0[particle_index].AsVector3D()) * 0.0016).Min(Vector(3, 3, 3)).Max(Vector(-3, -3, -3)); - Vector world_pos = particle_pos + pos_ani * scalar; + //Vector world_pos = particle_pos + pos_ani * scalar; mesh_builder.TexCoord2f(0, u[i], v[i]); - mesh_builder.Position3f(world_pos.x, world_pos.y, world_pos.z); - mesh_builder.Normal3f(-forward.x, -forward.y, -forward.z); + //mesh_builder.Position3f(world_pos.x, world_pos.y, world_pos.z); + //mesh_builder.Normal3f(-forward.x, -forward.y, -forward.z); mesh_builder.AdvanceVertex(); } } @@ -125,8 +122,9 @@ IMesh* _build_diffuse(int id, FlexRendererThreadData data) { // Launches 1 thread for each mesh. particles are split into meshes with MAX_PRIMATIVES number of primatives void FlexRenderer::build_meshes(FlexSolver* flex, float diffuse_radius) { + update_meshes(); // Clear previous imeshes since they are being rebuilt - destroy_meshes(); + //destroy_meshes(); int max_particles = flex->get_active_particles(); if (max_particles == 0) return; @@ -139,28 +137,24 @@ void FlexRenderer::build_meshes(FlexSolver* flex, float diffuse_radius) { render_context->GetMatrix(MATERIAL_PROJECTION, &projection_matrix); MatrixMultiply(projection_matrix, view_matrix, view_projection_matrix); - // Get eye position for sprite calculations - Vector eye_pos; render_context->GetWorldSpaceCameraPosition(&eye_pos); - Vector4D* particle_positions = flex->get_parameter("smoothing") != 0 ? (Vector4D*)flex->get_host("particle_smooth") : (Vector4D*)flex->get_host("particle_pos"); - Vector4D* particle_ani0 = (Vector4D*)flex->get_host("particle_ani0"); - Vector4D* particle_ani1 = (Vector4D*)flex->get_host("particle_ani1"); - Vector4D* particle_ani2 = (Vector4D*)flex->get_host("particle_ani2"); - bool particle_ani = flex->get_parameter("anisotropy_scale") != 0; // Should we do anisotropy calculations? float radius = flex->get_parameter("radius"); // thread data FlexRendererThreadData data; - data.eye_pos = eye_pos; data.view_projection_matrix = view_projection_matrix; data.particle_positions = particle_positions; data.max_particles = max_particles; data.radius = radius; data.render_buffer = water_buffer; if (flex->get_parameter("anisotropy_scale") != 0) { // Should we do anisotropy calculations? - data.particle_ani0 = particle_ani0; - data.particle_ani1 = particle_ani1; - data.particle_ani2 = particle_ani2; + data.particle_ani0 = (Vector4D*)flex->get_host("particle_ani0"); + data.particle_ani1 = (Vector4D*)flex->get_host("particle_ani1"); + data.particle_ani2 = (Vector4D*)flex->get_host("particle_ani2");; + } else { + data.particle_ani0 = nullptr; + data.particle_ani1 = nullptr; + data.particle_ani2 = nullptr; } int max_meshes = min(ceil(max_particles / (float)MAX_PRIMATIVES), allocated); @@ -169,6 +163,14 @@ void FlexRenderer::build_meshes(FlexSolver* flex, float diffuse_radius) { queue[mesh_index] = threads->enqueue(_build_water_anisotropy, mesh_index, data); } + // Remove meshes which wont be built + for (int mesh = max_meshes; mesh < allocated; mesh++) { + if (meshes[mesh] == nullptr) continue; + + render_context->DestroyStaticMesh(meshes[mesh]); + meshes[mesh] = nullptr; + } + /* // Diffuse particles max_particles = flex->get_active_diffuse(); if (max_particles == 0) return; @@ -191,7 +193,7 @@ void FlexRenderer::build_meshes(FlexSolver* flex, float diffuse_radius) { // Launch thread queue[mesh_index + allocated] = threads->enqueue(_build_diffuse, mesh_index, data); - } + }*/ }; void FlexRenderer::update_meshes() { @@ -205,7 +207,7 @@ void FlexRenderer::update_meshes() { } void FlexRenderer::draw_water() { - update_meshes(); // Update status of water meshes (join threads) + //update_meshes(); // Update status of water meshes (join threads) IMatRenderContext* render_context = materials->GetRenderContext(); for (int mesh = 0; mesh < allocated; mesh++) { @@ -216,6 +218,7 @@ void FlexRenderer::draw_water() { }; void FlexRenderer::draw_diffuse() { + /* update_meshes(); IMatRenderContext* render_context = materials->GetRenderContext(); @@ -223,7 +226,7 @@ void FlexRenderer::draw_diffuse() { if (meshes[mesh] == nullptr) continue; meshes[mesh]->Draw(); - } + }*/ }; void FlexRenderer::destroy_meshes() { diff --git a/binary/src/flex_renderer.h b/binary/src/flex_renderer.h index 3966689..244408c 100644 --- a/binary/src/flex_renderer.h +++ b/binary/src/flex_renderer.h @@ -16,7 +16,6 @@ struct FlexRendererThreadData { //IMesh*& water; - Vector eye_pos; VMatrix view_projection_matrix; Vector4D* particle_positions; Vector4D* particle_ani0; diff --git a/binary/src/shaders/GWaterFinalpass.h b/binary/src/shaders/GWaterFinalpass.h index 1a58bb7..6849819 100644 --- a/binary/src/shaders/GWaterFinalpass.h +++ b/binary/src/shaders/GWaterFinalpass.h @@ -51,8 +51,7 @@ SHADER_DRAW { bool bHasFlashlight = UsingFlashlight(params); SHADOW_STATE { // Note: Removing VERTEX_COLOR makes the shader work on all objects (Like props) - unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_TEXCOORD0_2D; - pShaderShadow->VertexShaderVertexFormat(flags, 1, 0, 0); + pShaderShadow->VertexShaderVertexFormat(VERTEX_GWATER2, 1, 0, 0); pShaderShadow->EnableTexture(SHADER_SAMPLER0, true); // Smoothed normals texture pShaderShadow->EnableTexture(SHADER_SAMPLER1, true); // Screen texture pShaderShadow->EnableTexture(SHADER_SAMPLER2, true); // Cubemap @@ -132,6 +131,7 @@ SHADER_DRAW { pShaderAPI->SetPixelShaderConstant(0, scr_s); pShaderAPI->SetPixelShaderConstant(1, &radius); + pShaderAPI->SetVertexShaderConstant(5, &radius); pShaderAPI->SetPixelShaderConstant(2, &ior); pShaderAPI->SetPixelShaderConstant(3, &reflectance); pShaderAPI->SetPixelShaderConstant(12, color2_normalized); // used to be 4, but that was overlapping with ambient cube. diff --git a/binary/src/shaders/GWaterNormals.h b/binary/src/shaders/GWaterNormals.h index 4fa3077..f1e5442 100644 --- a/binary/src/shaders/GWaterNormals.h +++ b/binary/src/shaders/GWaterNormals.h @@ -25,11 +25,8 @@ SHADER_FALLBACK{ SHADER_DRAW { SHADOW_STATE { - - // Note: Removing VERTEX_COLOR makes the shader work on all objects (Like props) - unsigned int flags = VERTEX_GWATER2; - - pShaderShadow->VertexShaderVertexFormat(flags, 1, 0, 0); + pShaderShadow->VertexShaderVertexFormat(VERTEX_GWATER2, 1, 0, 0); + //pShaderShadow->EnableAlphaWrites(true); DECLARE_STATIC_VERTEX_SHADER(GWaterNormals_vs30); SET_STATIC_VERTEX_SHADER(GWaterNormals_vs30); @@ -53,7 +50,6 @@ SHADER_DRAW { DECLARE_DYNAMIC_PIXEL_SHADER(GWaterNormals_ps30); SET_DYNAMIC_PIXEL_SHADER_COMBO(DEPTH, depthfix); SET_DYNAMIC_PIXEL_SHADER(GWaterNormals_ps30); - } diff --git a/lua/autorun/gwater2_init.lua b/lua/autorun/gwater2_init.lua index cd09b5c..6576c70 100644 --- a/lua/autorun/gwater2_init.lua +++ b/lua/autorun/gwater2_init.lua @@ -172,8 +172,10 @@ local function gwater_tick2() gwater2.solver:IterateMeshes(gwater2.update_meshes) hook.Run("gwater2_pretick") - if gwater2.solver:Tick(FrameTime(), 0) then - gwater2.renderer:BuildMeshes(gwater2.solver, 0.15) + + gwater2.renderer:BuildMeshes(gwater2.solver, 0.15) + if gwater2.solver:Tick(limit_fps, 0) then + end end @@ -186,7 +188,7 @@ hook.Add("Think", "gwater_tick", function() gwater2.solver:IterateMeshes(gwater2.update_meshes) end) -timer.Create("gwater2_tick", 0, 0, function() +timer.Create("gwater2_tick", limit_fps, 0, function() if !gwater2.new_ticker then return end gwater_tick2() end) diff --git a/lua/gwater2_shaders.lua b/lua/gwater2_shaders.lua index c6f84d7..631c3e3 100644 --- a/lua/gwater2_shaders.lua +++ b/lua/gwater2_shaders.lua @@ -123,15 +123,15 @@ hook.Add("PostDrawOpaqueRenderables", "gwater2_render", function(depth, sky, sky end -- grab normals - water_normals:SetFloat("$radius", radius * 0.5) + water_normals:SetFloat("$radius", radius) render.SetMaterial(water_normals) - --render.PushRenderTarget(cache_normals) + render.PushRenderTarget(cache_normals) render.SetRenderTargetEx(1, cache_depth) render.ClearDepth() gwater2.renderer:DrawWater() - --render.PopRenderTarget() + render.PopRenderTarget() render.SetRenderTargetEx(1, nil) - /* + -- Blur normals water_blur:SetFloat("$radius", radius) water_blur:SetTexture("$depthtexture", cache_depth) @@ -165,7 +165,7 @@ hook.Add("PostDrawOpaqueRenderables", "gwater2_render", function(depth, sky, sky render.OverrideAlphaWriteEnable(false, false) render.SetMaterial(water_mist) - gwater2.renderer:DrawDiffuse()*/ + --gwater2.renderer:DrawDiffuse() -- Debug Draw local dbg = 0 diff --git a/materials/gwater2/finalpass.vmt b/materials/gwater2/finalpass.vmt index 3b55dfb..4b63741 100644 --- a/materials/gwater2/finalpass.vmt +++ b/materials/gwater2/finalpass.vmt @@ -2,7 +2,7 @@ GWaterFinalpass { $envmap "env_cubemap" $radius 10 $ior 1.333 - $reflectance 0.63 + $reflectance 0.8 $screentexture "_rt_fullframefb" $color2 "[209 237 255 25]" $flashlight 1