Skip to content

Commit

Permalink
Added displacement option to SDF geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
favreau committed Sep 20, 2021
1 parent 0f359d8 commit 58312d5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
23 changes: 14 additions & 9 deletions brayns/common/geometry/SDFGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ enum class SDFType : uint8_t
struct SDFGeometry
{
uint64_t userData;
Vector3f center; // TO BE REMOVED, for v3 cache compatibility only
Vector3f userParams; // Currently used exclusively for displacement
Vector3f p0;
Vector3f p1;
float r0 = -1.f;
Expand All @@ -46,21 +46,25 @@ struct SDFGeometry
};

inline SDFGeometry createSDFSphere(const Vector3f& center, const float radius,
const uint64_t data = 0)
const uint64_t data = 0,
const Vector3f& userParams = Vector3f(0.f))
{
SDFGeometry geom;
geom.userData = data;
geom.userParams = userParams;
geom.p0 = center;
geom.r0 = radius;
geom.type = SDFType::Sphere;
return geom;
}

inline SDFGeometry createSDFPill(const Vector3f& p0, const Vector3f& p1,
const float radius, const uint64_t data = 0)
const float radius, const uint64_t data = 0,
const Vector3f& userParams = Vector3f(0.f))
{
SDFGeometry geom;
geom.userData = data;
geom.userParams = userParams;
geom.p0 = p0;
geom.p1 = p1;
geom.r0 = radius;
Expand All @@ -70,10 +74,12 @@ inline SDFGeometry createSDFPill(const Vector3f& p0, const Vector3f& p1,

inline SDFGeometry createSDFConePill(const Vector3f& p0, const Vector3f& p1,
const float r0, const float r1,
const uint64_t data = 0)
const uint64_t data = 0,
const Vector3f& userParams = Vector3f(0.f))
{
SDFGeometry geom;
geom.userData = data;
geom.userParams = userParams;
geom.p0 = p0;
geom.p1 = p1;
geom.r0 = r0;
Expand All @@ -89,12 +95,11 @@ inline SDFGeometry createSDFConePill(const Vector3f& p0, const Vector3f& p1,
return geom;
}

inline SDFGeometry createSDFConePillSigmoid(const Vector3f& p0,
const Vector3f& p1, const float r0,
const float r1,
const uint64_t data = 0)
inline SDFGeometry createSDFConePillSigmoid(
const Vector3f& p0, const Vector3f& p1, const float r0, const float r1,
const uint64_t data = 0, const Vector3f& userParams = Vector3f(0.f))
{
SDFGeometry geom = createSDFConePill(p0, p1, r0, r1, data);
SDFGeometry geom = createSDFConePill(p0, p1, r0, r1, data, userParams);
geom.type = SDFType::ConePillSigmoid;
return geom;
}
Expand Down
5 changes: 3 additions & 2 deletions engines/ospray/ispc/geometry/RayMarching.isph
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ inline vec3f computeNormal(const vec3f& pos,
const SDFParams& params)
{
// tetrahedron technique (4 evaluations)
const uniform vec3f k0 = make_vec3f(1, -1, -1), k1 = make_vec3f(-1, -1, 1),
k2 = make_vec3f(-1, 1, -1), k3 = make_vec3f(1, 1, 1);
const uniform float t = 2.f;
const uniform vec3f k0 = make_vec3f( t, -t, -t), k1 = make_vec3f(-t, -t, t),
k2 = make_vec3f(-t, t, -t), k3 = make_vec3f(t, t, t);

return normalize(k0 * sdfDistance(pos + e * k0, geo, prim, params) +
k1 * sdfDistance(pos + e * k1, geo, prim, params) +
Expand Down
24 changes: 18 additions & 6 deletions engines/ospray/ispc/geometry/SDFGeometries.ispc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ inline float smootherstep(const float x)
return x * x * x * (x * (x * 6 - 15) + 10);
}

inline float opDisplacement(const vec3f& p, const float a, const float b)
{
return a * sin(b * p.x) * sin(b * p.y) * sin(b * p.z);
}

inline float sdSphere(const vec3f& p, const vec3f c, float r)
{
return length(p - c) - r;
Expand Down Expand Up @@ -83,12 +88,12 @@ inline float sdConePill(const vec3f& p, const vec3f p0, const vec3f p1,

// distance to p0 along cone axis
const float c1 = dot(w, v);
if (c1 <= 0)
if (c1 <= 0) // p0 Sphere
return length(p - p0) - r0;

// cone length
const float c2 = dot(v, v);
if (c2 <= c1)
if (c2 <= c1) // p1 Sphere
return length(p - p1) - r1;

const float b = c1 / c2;
Expand All @@ -108,7 +113,7 @@ inline float sdConePill(const vec3f& p, const vec3f p0, const vec3f p1,
struct SDFGeometry
{
uint64 userData;
vec3f center; // TO BE REMOVED, for v3 cache compatibility only
vec3f userParams;
vec3f p0;
vec3f p1;
float r0;
Expand Down Expand Up @@ -179,14 +184,20 @@ const uniform SDFGeometry* varying getPrimitiveVarying(

inline float calcDistance(const uniform SDFGeometry& primitive, const vec3f& p)
{
float displacement = 0.f;
if (primitive.userParams.x > 0.f)
displacement = opDisplacement(p, primitive.r0 * primitive.userParams.x,
primitive.userParams.y);
if (primitive.type == SDF_TYPE_SPHERE)
return sdSphere(p, primitive.p0, primitive.r0);
return displacement + sdSphere(p, primitive.p0, primitive.r0);
if (primitive.type == SDF_TYPE_PILL)
return sdCapsule(p, primitive.p0, primitive.p1, primitive.r0);
return displacement +
sdCapsule(p, primitive.p0, primitive.p1, primitive.r0);
if (primitive.type == SDF_TYPE_CONE_PILL ||
primitive.type == SDF_TYPE_CONE_PILL_SIGMOID)
{
return sdConePill(p, primitive.p0, primitive.p1, primitive.r0,
return displacement +
sdConePill(p, primitive.p0, primitive.p1, primitive.r0,
primitive.r1,
primitive.type == SDF_TYPE_CONE_PILL_SIGMOID);
}
Expand Down Expand Up @@ -221,6 +232,7 @@ float sdfDistance(const vec3f& p, uDataPtr_t geo_, uDataPtr_t prim_,
const Geo_ptr geo = (const Geo_ptr)geo_;
const Prim_ptr prim = (const Prim_ptr)prim_;

// Primitive
float d = calcDistance(*prim, p);

// TODO don't blend soma if far enough from eye
Expand Down

0 comments on commit 58312d5

Please sign in to comment.