diff --git a/intern/cycles/render/mesh_subdivision.cpp b/intern/cycles/render/mesh_subdivision.cpp index 3b4841f5b20..0ae5ff742c2 100644 --- a/intern/cycles/render/mesh_subdivision.cpp +++ b/intern/cycles/render/mesh_subdivision.cpp @@ -16,12 +16,14 @@ #include "mesh.h" #include "attribute.h" +#include "camera.h" #include "subd_split.h" #include "subd_patch.h" #include "subd_patch_table.h" #include "util_foreach.h" +#include "util_algorithm.h" CCL_NAMESPACE_BEGIN @@ -177,7 +179,7 @@ public: Far::TopologyRefinerFactory::Options(type, options)); /* adaptive refinement */ - int max_isolation = 10; + int max_isolation = calculate_max_isolation(); refiner->RefineAdaptive(Far::TopologyRefiner::AdaptiveOptions(max_isolation)); /* create patch table */ @@ -248,6 +250,42 @@ public: } } + int calculate_max_isolation() + { + /* loop over all edges to find longest in screen space */ + const Far::TopologyLevel& level = refiner->GetLevel(0); + Transform objecttoworld = mesh->subd_params->objecttoworld; + Camera* cam = mesh->subd_params->camera; + + float longest_edge = 0.0f; + + for(size_t i = 0; i < level.GetNumEdges(); i++) { + Far::ConstIndexArray verts = level.GetEdgeVertices(i); + + float3 a = mesh->verts[verts[0]]; + float3 b = mesh->verts[verts[1]]; + + float edge_len; + + if(cam) { + a = transform_point(&objecttoworld, a); + b = transform_point(&objecttoworld, b); + + edge_len = len(a - b) / cam->world_to_raster_size((a + b) * 0.5f); + } + else { + edge_len = len(a - b); + } + + longest_edge = max(longest_edge, edge_len); + } + + /* calculate isolation level */ + int isolation = (int)(log2f(max(longest_edge / mesh->subd_params->dicing_rate, 1.0f)) + 1.0f); + + return min(isolation, 10); + } + friend struct OsdPatch; friend class Mesh; };