Skip to content

Commit dbddb52

Browse files
committed
Fixes crash if there are no normals generated and a normal map/tangent space is requested (it'll generate its own per-vertex normals).
1 parent 42d9bdf commit dbddb52

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

src/GafferCycles/IECoreCyclesPreview/MeshAlgo.cpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "GafferCycles/IECoreCyclesPreview/AttributeAlgo.h"
3838
#include "GafferCycles/IECoreCyclesPreview/ObjectAlgo.h"
3939

40+
#include "IECoreScene/MeshNormalsOp.h"
4041
#include "IECoreScene/MeshPrimitive.h"
4142
#include "IECoreScene/MeshAlgo.h"
4243

@@ -64,7 +65,7 @@ namespace
6465

6566
struct MikkUserData {
6667
MikkUserData( const char *layer_name,
67-
const ccl::Mesh *mesh,
68+
ccl::Mesh *mesh,
6869
ccl::float3 *tangent,
6970
float *tangent_sign )
7071
: mesh( mesh ), texface( NULL ), tangent( tangent ), tangent_sign( tangent_sign )
@@ -73,6 +74,12 @@ struct MikkUserData {
7374
mesh->attributes;
7475

7576
ccl::Attribute *attr_vN = attributes.find( ccl::ATTR_STD_VERTEX_NORMAL );
77+
if( !attr_vN )
78+
{
79+
mesh->add_face_normals();
80+
mesh->add_vertex_normals();
81+
attr_vN = attributes.find( ccl::ATTR_STD_VERTEX_NORMAL );
82+
}
7683
vertex_normal = attr_vN->data_float3();
7784

7885
ccl::Attribute *attr_uv = attributes.find( ccl::ustring( layer_name ) );
@@ -82,7 +89,7 @@ struct MikkUserData {
8289
}
8390
}
8491

85-
const ccl::Mesh *mesh;
92+
ccl::Mesh *mesh;
8693
int num_faces;
8794

8895
ccl::float3 *vertex_normal;
@@ -609,10 +616,46 @@ ccl::Mesh *convertCommon( const IECoreScene::MeshPrimitive *mesh )
609616

610617
// Convert Normals
611618
PrimitiveVariable::Interpolation nInterpolation = PrimitiveVariable::Invalid;
612-
if( const V3fVectorData *normals = normal( mesh, nInterpolation ) )
619+
if( !triangles )
613620
{
614-
ccl::Attribute *attr_N = attributes.add( nInterpolation == PrimitiveVariable::Uniform ? ccl::ATTR_STD_FACE_NORMAL : ccl::ATTR_STD_VERTEX_NORMAL, ccl::ustring("N") );
615-
convertN( mesh, normals, attr_N, nInterpolation );
621+
if( const V3fVectorData *normals = normal( trimesh.get(), nInterpolation ) )
622+
{
623+
ccl::Attribute *attr_N = attributes.add( nInterpolation == PrimitiveVariable::Uniform ? ccl::ATTR_STD_FACE_NORMAL : ccl::ATTR_STD_VERTEX_NORMAL, ccl::ustring("N") );
624+
convertN( trimesh.get(), normals, attr_N, nInterpolation );
625+
}
626+
else if( mesh->interpolation() != "catmullClark" )
627+
{
628+
IECoreScene::MeshNormalsOpPtr normalOp = new IECoreScene::MeshNormalsOp();
629+
normalOp->inputParameter()->setValue( trimesh );
630+
normalOp->copyParameter()->setTypedValue( false );
631+
normalOp->operate();
632+
if( const V3fVectorData *normals = normal( trimesh.get(), nInterpolation ) )
633+
{
634+
ccl::Attribute *attr_N = attributes.add( ccl::ATTR_STD_VERTEX_NORMAL, ccl::ustring("N") );
635+
convertN( trimesh.get(), normals, attr_N, PrimitiveVariable::Vertex );
636+
}
637+
}
638+
}
639+
else
640+
{
641+
if( const V3fVectorData *normals = normal( mesh, nInterpolation ) )
642+
{
643+
ccl::Attribute *attr_N = attributes.add( nInterpolation == PrimitiveVariable::Uniform ? ccl::ATTR_STD_FACE_NORMAL : ccl::ATTR_STD_VERTEX_NORMAL, ccl::ustring("N") );
644+
convertN( mesh, normals, attr_N, nInterpolation );
645+
}
646+
else if( mesh->interpolation() != "catmullClark" )
647+
{
648+
IECoreScene::MeshPrimitivePtr normalmesh = mesh->copy();
649+
IECoreScene::MeshNormalsOpPtr normalOp = new IECoreScene::MeshNormalsOp();
650+
normalOp->inputParameter()->setValue( normalmesh );
651+
normalOp->copyParameter()->setTypedValue( false );
652+
normalOp->operate();
653+
if( const V3fVectorData *normals = normal( normalmesh.get(), nInterpolation ) )
654+
{
655+
ccl::Attribute *attr_N = attributes.add( ccl::ATTR_STD_VERTEX_NORMAL, ccl::ustring("N") );
656+
convertN( normalmesh.get(), normals, attr_N, PrimitiveVariable::Vertex );
657+
}
658+
}
616659
}
617660

618661
// Convert primitive variables.

0 commit comments

Comments
 (0)