Bladeren bron

Improve parallax mapping support

rdb 16 jaren geleden
bovenliggende
commit
51378e31a0

+ 11 - 0
panda/src/pgraphnodes/config_pgraphnodes.cxx

@@ -46,6 +46,17 @@ ConfigVariableEnum<LODNodeType> default_lod_type
  PRC_DESC("Set this to either 'pop' or 'fade' to determine the type of "
  PRC_DESC("Set this to either 'pop' or 'fade' to determine the type of "
           "LODNode that is created by LODNode::make_default_lod()."));
           "LODNode that is created by LODNode::make_default_lod()."));
 
 
+ConfigVariableInt parallax_mapping_samples
+("parallax-mapping-samples", 3,
+ PRC_DESC("Sets the amount of samples to use in the parallax mapping "
+          "implementation. A value of 0 means to disable it entirely."));
+
+ConfigVariableDouble parallax_mapping_scale
+("parallax-mapping-scale", 0.1,
+ PRC_DESC("Sets the strength of the effect of parallax mapping, that is, "
+          "how much influence the height values have on the texture "
+          "coordinates."));
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: init_libpgraphnodes
 //     Function: init_libpgraphnodes
 //  Description: Initializes the library.  This must be called at
 //  Description: Initializes the library.  This must be called at

+ 3 - 0
panda/src/pgraphnodes/config_pgraphnodes.h

@@ -18,6 +18,7 @@
 #include "pandabase.h"
 #include "pandabase.h"
 #include "lodNodeType.h"
 #include "lodNodeType.h"
 #include "configVariableEnum.h"
 #include "configVariableEnum.h"
+#include "configVariableDouble.h"
 
 
 class DSearchPath;
 class DSearchPath;
 
 
@@ -25,6 +26,8 @@ ConfigureDecl(config_pgraphnodes, EXPCL_PANDA_PGRAPHNODES, EXPTP_PANDA_PGRAPHNOD
 NotifyCategoryDecl(pgraphnodes, EXPCL_PANDA_PGRAPHNODES, EXPTP_PANDA_PGRAPHNODES);
 NotifyCategoryDecl(pgraphnodes, EXPCL_PANDA_PGRAPHNODES, EXPTP_PANDA_PGRAPHNODES);
 
 
 extern ConfigVariableEnum<LODNodeType> default_lod_type;
 extern ConfigVariableEnum<LODNodeType> default_lod_type;
+extern ConfigVariableInt parallax_mapping_samples;
+extern ConfigVariableDouble parallax_mapping_scale;
 
 
 extern EXPCL_PANDA_PGRAPHNODES void init_libpgraphnodes();
 extern EXPCL_PANDA_PGRAPHNODES void init_libpgraphnodes();
 
 

+ 14 - 6
panda/src/pgraphnodes/shaderGenerator.cxx

@@ -893,7 +893,7 @@ synthesize_shader(const RenderState *rs) {
     }
     }
   }
   }
   text << "\t // Fetch all textures.\n";
   text << "\t // Fetch all textures.\n";
-  if (_map_index_height >= 0) {
+  if (_map_index_height >= 0 && parallax_mapping_samples > 0) {
     Texture *tex = texture->get_on_texture(texture->get_on_stage(_map_index_height));
     Texture *tex = texture->get_on_texture(texture->get_on_stage(_map_index_height));
     nassertr(tex != NULL, NULL);
     nassertr(tex != NULL, NULL);
     text << "\t float4 tex" << _map_index_height << " = tex" << texture_type_as_string(tex->get_texture_type());
     text << "\t float4 tex" << _map_index_height << " = tex" << texture_type_as_string(tex->get_texture_type());
@@ -910,17 +910,25 @@ synthesize_shader(const RenderState *rs) {
     } else {
     } else {
       text << ".rgb";
       text << ".rgb";
     }
     }
-    text << " * 0.2 - 0.1);\n";
+    text << " * 2.0 - 1.0) * " << parallax_mapping_scale << ";\n";
+    // Additional samples
+    for (int i=0; i<parallax_mapping_samples-1; i++) {
+      text << "\t parallax_offset = l_eyevec.xyz * (parallax_offset + (tex" << _map_index_height;
+      if (_map_height_in_alpha) {
+        text << ".aaa";
+      } else {
+        text << ".rgb";
+      }
+      text << " * 2.0 - 1.0)) * " << 0.5 * parallax_mapping_scale << ";\n";
+    }
   }
   }
   for (int i=0; i<_num_textures; i++) {
   for (int i=0; i<_num_textures; i++) {
     if (i != _map_index_height) {
     if (i != _map_index_height) {
       Texture *tex = texture->get_on_texture(texture->get_on_stage(i));
       Texture *tex = texture->get_on_texture(texture->get_on_stage(i));
       nassertr(tex != NULL, NULL);
       nassertr(tex != NULL, NULL);
       // Parallax mapping pushes the texture coordinates of the other textures away from the camera.
       // Parallax mapping pushes the texture coordinates of the other textures away from the camera.
-      // The normal map coordinates aren't pushed (since that would give inconsistent behaviour when
-      // the height map is packed with the normal map together).
-      if (_map_index_height >= 0 && i != _map_index_normal) {
-        text << "\t l_texcoord" << i << ".xyz += parallax_offset;\n";
+      if (_map_index_height >= 0 && parallax_mapping_samples > 0) {
+        text << "\t l_texcoord" << i << ".xyz -= parallax_offset;\n";
       }
       }
       text << "\t float4 tex" << i << " = tex" << texture_type_as_string(tex->get_texture_type());
       text << "\t float4 tex" << i << " = tex" << texture_type_as_string(tex->get_texture_type());
       text << "(tex_" << i << ", l_texcoord" << i << ".";
       text << "(tex_" << i << ", l_texcoord" << i << ".";