Normal and bump mapping are techniques used in 3D graphics to fake additional surface detail on an object without adding any additional polygons. Whilst bump mapping uses simple greyscale images (where light areas appear raised) normal mapping, used by SceneKit, requires RGB images where the red, green and blue channels define the displacement along the x, y and z axes respectively. 

Core Image includes tools that are ideal candidates to create bump maps: gradients, stripes, noise and my own Voronoi noise are examples. However, they need an additional step to convert them to normal maps for use in SceneKit. Interestingly, Model I/O includes a class that will do this, but we can take a more direct route with a custom Core Image kernel. 

The demonstration project for this blog post is SceneKitProceduralNormalMapping.

Creating a Source Bump Map


The source bump map is created using a CIGaussianGradient filter which is chained to a CIAffineTile:


    let radialFilter = CIFilter(name: "CIGaussianGradient", withInputParameters: [
        kCIInputCenterKey: CIVector(x: 50, y: 50),
        kCIInputRadiusKey : 45,
        "inputColor0": CIColor(red: 1, green: 1, blue: 1),
        "inputColor1": CIColor(red: 0, green: 0, blue: 0)
        ])

    let ciCirclesImage = radialFilter?
        .outputImage?
        .imageByCroppingToRect(CGRect(x:0, y: 0, width: 100, height: 100))
        .imageByApplyingFilter("CIAffineTile", withInputParameters: nil)
        .imageByCroppingToRect(CGRect(x:0, y: 0, width: 500, height: 500))

Creating a Normal Map Filter

The kernel to convert the bump map to a normal map is fairly simple: for each pixel, the kernel compares the luminance of the pixels to its immediate left and right and, for the red output pixel, returns the difference of those two values added to one and divided by two. The same is done for the pixels immediately above and below for the green channel The blue and alpha channels of the output pixel are both set to 1.0:


        float lumaAtOffset(sampler source, vec2 origin, vec2 offset)
        {
            vec3 pixel = sample(source, samplerTransform(source, origin + offset)).rgb;
            float luma = dot(pixel, vec3(0.2126, 0.7152, 0.0722));
            return luma;
        }
            
        kernel vec4 normalMap(sampler image) \n" +
       
            vec2 d = destCoord();" +
            
            float northLuma = lumaAtOffset(image, d, vec2(0.0, -1.0));
            float southLuma = lumaAtOffset(image, d, vec2(0.0, 1.0));
            float westLuma = lumaAtOffset(image, d, vec2(-1.0, 0.0));
            float eastLuma = lumaAtOffset(image, d, vec2(1.0, 0.0));
            
            float horizontalSlope = ((westLuma - eastLuma) + 1.0) * 0.5;
            float verticalSlope = ((northLuma - southLuma) + 1.0) * 0.5;
            
            return vec4(horizontalSlope, verticalSlope, 1.0, 1.0);
        }

Wrapping this up in a Core Image filter and bumping up the contrast returns a normal map:




Implementing the Normal Map

A SceneKit material's normal content can be populated with a CGImage instance, so we can update the code above to chain the tiled radial gradients to the new filter and, should we want to, a further color controls filter to tweak the contrast:


    let ciCirclesImage = radialFilter?
        .outputImage?
        .imageByCroppingToRect(CGRect(x:0, y: 0, width: 100, height: 100))
        .imageByApplyingFilter("CIAffineTile", withInputParameters: nil)
        .imageByCroppingToRect(CGRect(x:0, y: 0, width: 500, height: 500))
        .imageByApplyingFilter("NormalMap", withInputParameters: nil)
        .imageByApplyingFilter("CIColorControls", withInputParameters: ["inputContrast": 2.5])
    
    let context = CIContext()

    let cgNormalMap = context.createCGImage(ciCirclesImage!,
                                            fromRect: ciCirclesImage!.extent)

Then, simply define a material with the normal map:


    let material = SCNMaterial()
    material.normal.contents = cgNormalMap

Core Image for Swift

All the code to accompany this post os available from my GitHub repository. However, if you'd like to learn more about how to wrap the Core Image Kernel Language code in a Core Image filter or explore the awesome power of custom kernels, may I recommend my book, Core Image for Swift.
Core Image for Swift is available from both Apple's iBooks Store or, as a PDF, from Gumroad. IMHO, the iBooks version is better, especially as it contains video assets which the PDF version doesn't.




0

Add a comment

It's been a fairly busy few months at my "proper" job, so my recreational Houdini tinkering has taken a bit of a back seat. However, when I saw my Swarm Chemistry hero, Hiroki Sayama tweeting a link to How a life-like system emerges from a simple particle motion law, I thought I'd dust off Houdini to see if I could implement this model in VEX.

The paper discusses a simple particle system, named Primordial Particle Systems (PPS), that leads to life-like structures through morphogenesis. Each particle in the system is defined by its position and heading and, with each step in the simulation, alters its heading based on the PPS rule and moves forward at a defined speed. The heading is updated based on the number of neighbors to the particle's left and right.
5

This blog post discusses a technique for rendering SideFX Houdini FLIP fluids as sparse fields of wormlike particles (hence my slightly over-the-top Sparse Vermiform moniker) with their color based on the fluid system's gas pressure field.

The project begins with an oblate spheroid that is converted to a FLIP Fluid using the FLIP Fluid from Object shelf tool. The fluid object sits within a box that's converted to a static body with its volume inverted to act as a container.
3

This post continues from my recent blog entry, Particle Advection by Reaction Diffusion in SideFX Houdini. In this project, I've done away with the VEX in the DOP network, and replaced it with a VDB Analysis node to create a vector field that represents the gradient in the reaction diffusion volume. This allows me to use a POP Advect by Volumes node in the DOP network rather than hand coding by own force wrangle.

After watching this excellent tutorial that discusses advecting particles by magnetic fields to create an animation of the sun, I was inspired to use the same technique to advect particles by fields that are a function of reaction diffusion systems. 

The source for my reaction diffusion vector field is a geometry node.
1

I played with animating mitosis in Houdini last year (see Simulating Mitosis in Houdini), but the math wasn't quite right, so I thought I'd revisit my VEX to see if it could be improved. After some tinkering, the video above shows my latest (hopefully improved) results.
4

This post describes a simple way to create a system comprising of a regularly surfaced fluid and a faux grain system. The video above contains three clips using the same basic technique: creating a single point source for the FLIP SOP initial data but using groups to render some as a fluid and some as individual tiny spheres - the grains. 

The first clip shows a granular sphere dropping into a fluid tank.
1

Fibonacci spheres are created from a point set that follows a spiral path to form a sphere (you can see an example, with code at OpenProcessing).

This video contains five clips using SideFX Houdini's Grain Solver with an attached POP Wrangle that uses VEX to generate custom forces. Here's a quick rundown of the VEX I used for each clip (please forgive the use of a variable named oomph).

Clip One "Twin Peaks"

Here, I compare each grain's current angle to the scene's origin to the current time.

The Rayleigh-Taylor instability is the instability between two fluids of different densities. It can appear as "fingers" of a denser liquid dropping into a less dense liquid or as a mushroom cloud in an explosion.

The phenomenon "comes for free" in SideFX Houdini FLIP Fluids.
1

Following on from my recent blog post, Mixing Fluids in Houdini, I wanted to simulate a toroidal eddy effect where the incoming drip takes the form of a torus and the fluid flows around the circumference of its minor radius. 

My first thought was to use a POP Axis Force, but that rotates particles around the circumference of the major radius. So, I took another approach: create lots of curves placed around a circle and use those as the geometry source for a POP Curve Force.
About Me
About Me
Labels
Labels
Blog Archive
Loading