Creating an ocean material using procedural textures

In this recipe, we will build an ocean surface material, using the Ocean modifier and procedural textures to create the foam, and establish a set of nodes to locate it on the higher parts of the waves:

Creating an ocean material using procedural textures

The final look of the ocean material, with three simple and brightly colored objects to be reflected by the surface

Getting ready

Before we start with creating the shaders, let's prepare the ocean scene:

  1. Start Blender and switch to the Cycles rendering engine. Select the default Cube, delete it (press X), and add a Plane (with the mouse arrow in the 3D window, press Shift + A and navigate to Mesh | Plane). In Object Mode, scale the plane smaller to 0.300. Don't apply a size.
  2. Go to the Object modifiers window and assign an Ocean modifier. Set these values of Geometry to Generate, Repeat X and Repeat Y to 4, Spatial Size to 20, and Resolution to 12.
  3. Press the N key, and in the Transform panel, set these values for the Plane in Location for X as -6.90000, Y as -7.00000, and Z as 0.00000.
  4. Make sure that you are at frame 1, and with the mouse arrow in the modifier's Time slot, press I to add a key for the animation. Go to frame 25, change the Time value from 1.00 to 2.00, and press the I key again to set a second key.
  5. In the Choose Screen layout button at the top, switch from Default to Animation. In the Graph Editor window, press T. In the Set Keyframe Interpolation pop-up menu, select the Linear item under Interpolation. Then press Shift + E, and in the Set Keyframe Extrapolation pop-up menu, select the Linear Extrapolation item to make the ocean animation constant and continuous.
  6. Go back to the Default screen and rename the Plane as Ocean_surface. Have a look at the following screenshot:
    Getting ready

    The Plane with the assigned Ocean modifier, and the settings to the right

  7. Place the Camera to have a nice angle on the ocean, and then go to the Camera view (press 0 on the numeric keypad).
  8. Add a Cube in the middle of the scene and, if you want, a UV Sphere in the foreground. Place these so that they float in the air. Their only purpose is to get reflected by the ocean surface during the shader setup.
  9. Go to the World window and click on the Use Nodes button under the Surface subpanel. Then click on the little square with a dot on the right side of the color slot. From the pop-up menu, navigate to Texture | Sky Texture.
  10. Select the Lamp, go to the Object data window, and click on the Use Nodes button under the Nodes subpanel. Set a yellowish color for the light (change the value of R to 1.000, G to 0.989, and B to 0.700). Turn it to Sun, set the Size value to 0.010, and set the Strength value to 2.500.
  11. Go to the Render window, and under the Sampling subpanel, set both the Clamp Direct and Clamp Indirect values to 1.00. Set the samples to 100 for Render and 50 for Preview (you can obviously change these values according to the power of your machine).

    Now, because the shader we are going to build is largely transparent, we need to simulate the water body as seen from above the surface.

  12. Add a new Plane in Edit Mode and scale it 10 times bigger (20 Blender units per side; press Tab, then press S, enter digit 10, and press Enter). Exit Edit Mode and move the Plane so that it is centered on the ocean Plane location, then move it 1 unit down on the z axis. You can do it like this: go to the Top view and move the new Plane of 7 Blender units first along the x axis and then along the y axis. Then press G, press Z, enter digit -1, and press Enter.
  13. In Edit Mode, press W to subdivide it by the Specials menu. Then press T to open the Tool Shelf panel on the left, and under Number of Cuts in the Operator panel at the bottom, and select 3.
  14. Go to the Vertex Paint mode and paint a very simple gray-scale gradient, changing from black at the vertices close to the Camera location to a plain white color on the opposite side.

    There are five rows of vertices on the Plane (ideally, all the rows are along the global x axis), so you can paint the first row with RGB value as 0.000, second with RGB value as 0.250, third with RGB value as 0.500, fourth with RGB value as 0.750, and fifth with RGB value as 1.000 to have a perfect gray-scale gradient.

  15. In the Object data window, under the Vertex Colors tab, rename the Vertex Color layer as Col_emit. Have a look at the following screenshot:
    Getting ready

    The Ocean_Bottom plane with the painted Vertex Colors layer

  16. Exit Vertex Paint mode and rename this second Plane Ocean_bottom.
  17. Split the 3D window into two horizontal rows. Change the upper row to a Node Editor window.
  18. Assign very simple colored materials to the Cube and the UV Sphere; plain Diffuse BSDF shaders are enough.

How to do it...

We will be performing this in four parts:

  • Creating the water surface and the bottom shaders
  • Creating the foam shader
  • Creating the stencil material for the location of foam
  • Putting everything together

Let's start!

Creating the water surface and the bottom shaders

Let's now create the water surface and the bottom shaders:

  1. Select the Ocean_bottom object. Click on the New button in the Material window under the Properties panel or in the Node Editor toolbar. Rename the new material as Ocean_bottom as well.
  2. Switch the Diffuse BSDF shader with a Mix Shader node. In the first and the second slots, load two Emission shaders.
  3. Add an Attribute node (press Shift + A and navigate to Input | Attribute) and connect the Color output to the Fac input of the Mix Shader node. In the Name slot of the Attribute node, write Col_emit, which is the name of the Vertex Color layer.
  4. Change the color of the first Emission node for R to 0.178, G to 0.150, and B to 0.085. Set the Strength value to 1.000.
  5. Change the color values of the second Emission node for R to 0.213, G to 0.284, and B to 0.380. Set the Strength value to 2.000.

    The Ocean_bottom material is ready. Have look at the following screenshot:

    Creating the water surface and the bottom shaders

    The Ocean_bottom material and the scene visible in the Solid viewport shading mode through the Camera view

  6. Now select Ocean_surface and click on New in the Material window under the Properties panel or in the Node Editor toolbar. Rename this material as Ocean_surface.
  7. Replace the Diffuse BSDF node with a Mix Shader node, and in the first Shader slot, assign a Transparent BSDF node. In the second slot, assign a Glass BSDF shader. In the Properties panel of the Node Editor window, label the Mix Shader node as Mix Shader01.
  8. Change the Transparent BSDF nodes Color values for R to 0.055, G to 0.124, and B to 0.042 (you can also do this by connecting an RGB node to the Color input socket, as shown in the example blend file provided). Set the Glass BSDF shader node's Roughness value to 0.900 and the IOR value to 1.333.
  9. Add a Layer Weight node (press Shift + A and navigate to Input | Layer Weight), connect the Facing output to the Fac input of the Mix Shader01 node, and set the Blend value to 0.050.
  10. Select the Mix Shader01 node and press Shift + D to duplicate it. Add a Glossy BSDF shader (press Shift + A and navigate to Shader | Glossy BSDF) and connect it to the second Shader input socket of the Mix Shader02 node. Connect the output of the Mix Shader01 node to the first Shader input socket of the Mix Shader02 node, and the output of this node to the Surface input of the Material Output node.
  11. Add a Fresnel node (press Shift + A and navigate to Input | Fresnel). Connect this to the Fac input of the Mix Shader02 node. Set the IOR value to 1.333 as shown in the following screenshot:
    Creating the water surface and the bottom shaders

    The Ocean_surface shader network

  12. Now select all the nodes except the Material Output node, and press Ctrl + G to make a group. Select and delete the Group Input node to the left (press the X key), and drag the Mix Shader02 node's output to the empty socket of the Group Output node.
  13. Press Tab to close the node group, and rename it as Ocean_water.

Creating the foam shader

Let's now create the shader for the foam:

  1. Add a Noise Texture node (press Shift + A and navigate to Texture | Noise Texture) and a Voronoi Texture node (press Shift + A and navigate to Texture | Voronoi Texture) nodes. Select them and press Shift + D to duplicate them. Label them as Noise Texture01, Noise Texture02, Voronoi Texture01, and Voronoi Texture02.
  2. Add four ColorRamp nodes (press Shift + A and navigate to Converter | ColorRamp, then press Shift + D to duplicate them). Label them as ColorRamp01, ColorRamp02, ColorRamp03, and ColorRamp04. Place the four texture nodes in a vertical column and arrange the ColorRamp nodes to their side. Connect the Color output of each texture node to the Fac input of the respective ColorRamp node.
  3. Set Interpolation of the ColorRamp01 node to B-Spline, ColorRamp02 and ColorRamp03 to Ease, and ColorRamp04 to B-Spline again.
  4. Go to the ColorRamp01 node. Move the black color stop to position 0.345 and the white color stop to position 0.633.
  5. Go to the ColorRamp02 and ColorRamp03 nodes. Move the black color stop to position 0.159 and the white color stop to position 0.938 for both nodes. Leave the ColorRamp04 color stops as they are.
  6. Set the Scale value of the Noise Texture01 node to 500.000, the Noise Texture02 node to 100.000, the Voronoi Texture01 node to 100.000, and the Voronoi Texture02 node to 90.000.
  7. Add a Texture Coordinate node (press Shift + A and navigate to Input | Texture Coordinate) and a Mapping node (press Shift + A and navigate to Vector | Mapping). Connect the UV output of the Texture Coordinate node to the Vector input socket of the Mapping node. Then connect the Vector output of this node to the Vector input sockets of the four texture nodes.
  8. Add a MixRGB node (press Shift + A and navigate to Color | MixRGB), set the Blend Type to Subtract, and label it as Subtract01. Set the Fac value to 1.000. Connect the Color outputs of the ColorRamp01 and ColorRamp02 nodes to the Color1 and Color2 input sockets of the Subtract01 node.
  9. Select the Subtract01 node, press Shift + D to duplicate it, and set the Blend Type to Multiply. Label it as Multiply and connect the Color outputs of the ColorRamp03 and ColorRamp04 nodes to its Color1 and Color2 input sockets.
  10. Duplicate a MixRGB node again, set the Blend Type to Difference, and name it Difference as well. Then connect the Color outputs of the ColorRamp03 and ColorRamp04 nodes to the Color1 and Color2 input sockets of this Difference node.
  11. Duplicate one of the MixRGB nodes one more time. Set the Blend Type to Lighten and label it as Lighten. Lower the Fac value to 0.500. Connect the Color output of the Multiply node to the Color1 input of the Lighten node, and the Color output of the Difference node to the Color2 input socket.
  12. Add an Invert node (press Shift + A and navigate to Color | Invert) and move it on the link connecting the Difference and the Lighten nodes to be automatically pasted in between.
  13. Add a new ColorRamp node, label it as ColorRamp05, and connect the Lighten node output to its Fac input. Then move the black color stop to position 0.298 and the white color stop to position 0.486.
  14. Add a new MixRGB node (press Shift + A and navigate to Color | MixRGB), set the Blend Type to Subtract, and label it as Subtract02. Set the Fac value to 1.000. Connect the ColorRamp05 node's Color output to the Color1 input of Subtract02 node and the output of the Subtract01 node to the Color2 input socket.
  15. Add an RGB to BW node (press Shift + A and navigate to Converter | RGB to BW), a Bump node (press Shift + A and navigate to Vector | Bump), and a Diffuse BSDF shader (press Shift + A and navigate to Shader | Diffuse BSDF).
  16. Connect the Subtract02 node's output to the RGB to BW node, the output of this node to the Height input socket of the Bump node, and the Normal output of the Bump node to the Normal input of the Diffuse BSDF shader. Set the Bump node's Strength value to 1.000 as shown in the following screenshot:
    Creating the foam shader

    The network for the foam shader

  17. Select all of these nodes and press Ctrl + G. Delete the Group Input node on the left.
  18. Drag the BSDF output of the Diffuse BSDF shader onto the right side, to the empty socket of the Group Output node. Repeat this for the Color output of the Subtract02 node.
  19. Press Tab to close the group. Rename it as Foam.

Creating the stencil material for the foam location

What we need now is a way to limit both the amount and the presence of the foam in the upper parts of the waves:

  1. Add Gradient (press Shift + A and navigate to Texture | Gradient Texture) and Voronoi Texture (press Shift + A and navigate to Texture | Voronoi Texture) texture nodes, select them, and press Shift + D to duplicate them. Label them as Gradient Texture01, Gradient Texture02, Voronoi Texture03, and Voronoi Texture04.
  2. Set the Gradient Types to Easing for both the nodes, the Scale value of the Voronoi Texture03 node to 250.000, and the Scale value of the Voronoi Texture04 node to 50.000.
  3. Add three Mapping nodes (press Shift + A and navigate to Vector | Mapping, then press Shift + D to duplicate them). Label them as Mapping01, Mapping02, and Mapping03.
  4. Add a Texture Coordinate node (press Shift + A and navigate to Input | Texture Coordinate) and a Geometry node (press Shift + A and navigate to Input | Geometry).
  5. Connect the Normal output of the Geometry node to the Vector input of the Mapping01 node. Next, connect the Position output of the Geometry node to the Vector input of the Mapping02 node. Then connect the UV output of the Texture Coordinate node to the Vector input of the Mapping03 node.
  6. In the Mapping02 node, change the Location value of X to 0.500 and the Rotation value of Y to 90°.
  7. Connect the output of the Mapping01 to the input of the Gradient Texture01, the output of the Mapping02 to the input of the Gradient Texture02, and the output of the Mapping03 to both the Vector inputs of the last two Voronoi Texture nodes.
  8. Add a ColorRamp node (press Shift + A and navigate to Converter | ColorRamp), label it as ColorRamp06, and connect the Color output of the Voronoi Texture04 node to its Fac input. Set Interpolation to B-Spline. On the ColorRamp06 node, click on the little + icon to add a new color stop (medium gray) in the middle of the slider. Change its color to total black and move it to position 0.068.
  9. Add a Math node (press Shift + A and navigate to Converter | Math), set the Operation to Multiply, and label it as Multiply02. Connect the Color output of the Gradient Texture02 node to the first Value input socket of the Multiply02 node.
  10. Add a MixRGB node (press Shift + A and navigate to Color | MixRGB), set the Blend Type to Difference, and label it as Difference02. Set the Fac value to 1.000. Connect the Color output of the Gradient Texture01 to the Color1 input socket, and the Value output of the Multiply02 node to the Color2 input socket of the Difference02 node.
  11. Press Shift + D to duplicate the MixRGB node, set the Blend Type to Subtract, and label it as Subtract03. Connect the ColorRamp06 node's Color output to both the Color2 and to the Fac input sockets. Then connect the Color output of the Voronoi Texture03 node to the Color1 input socket.
  12. Add a new ColorRamp node (press Shift + A and navigate to Converter | ColorRamp), label it as ColorRamp07, and connect the output of the Difference02 node to the Fac input. Then move the white color stop to position 0.344.
  13. Duplicate one of the MixRGB nodes, set the Blend Type to Burn, and label it as Burn as well. Connect the Color output of the ColorRamp07 node to the Color1 input of the Burn node. Then connect the output of the Subtract03 node to the Color2 input socket of the Burn node as shown in the following screenshot:
    Creating the stencil material for the foam location

    The stencil network

  14. Now select all of these nodes, press Ctrl + G, and drag the Burn node output on the right to connect it to the empty socket of the Group Output node. Press Tab to close the group. Rename it as Foam_location.

Putting everything together

What is left now is just to connect these three groups to build the final shader:

  1. Add a Mix Shader node (press Shift + A and navigate to Shader | Mix Shader). Label it as Mix Shader03 and connect its output to the Surface input socket of the Material Output node.
  2. Connect the Shader output of the Ocean_water group to the first Shader input of the Mix Shader03. Then connect the BSDF output of the Foam group to the second Shader input.
  3. Add two MixRGB nodes (press Shift + A and navigate to Color | MixRGB). Set the Blend Type of the first node to Multiply and the Fac value to 0.550. Then label it as Multiply03. Set the second node Blend Type to Burn and the Fac to 0.200. Then label it as Burn02.
  4. Connect the Color output of the Foam_location group to the Color1 input of the Multiply03 node, and the Color output of the Foam group to the two Color2 inputs of both the Multiply03 and Burn02 nodes.
  5. Connect the Multiply03 node's output to the Color1 input of the Burn02 node. Connect the output of the Burn02 node to the Fac input of the Mix Shader03 node as shown in the following screenshot:
    Putting everything together

    The overall view of the network with the connected node groups

How it works...

This material, which looks quite complex, is actually easily understandable by splitting the entire process in three stages corresponding to the three group nodes:

  • In the first stage, we created the basic ocean water shader by mixing a Glass node with a Transparent BSDF shader on the ground of the Facing value of the Layer Weight node and then also with a Glossy BSDF shader driven by the index of refraction of water (the IOR value of the Fresnel node, which is 1.333 for water at 20°C). In other words, the ocean surface nicely reflects the environment but for the faces looking towards the Camera (the Facing factor), it is transparent. Very important is the Bottom_ocean Plane, which is used to mimic the volume of the water and the underwater perspective and also emitting light to enhance the effect of the sun bouncing from the ocean surface to any floating object. The result of this first stage is shown in the following rendering:
    How it works...

    Only the water shader rendered

  • In the second stage, we created the material for the foam—a simple, white Diffuse BSDF shader. In fact, the peculiarity of the foam shader is mostly in the frothy bumpiness (and in the lacy-shaped outline cut by the procedural textures of the Foam_location shader). Have a look at the rendered foam shader:
    How it works...

    Only the foam shader rendered

  • In the third stage, this group of nodes establishes the location of the foam that is mainly formed in the higher parts of waves in the real world, behaving as a gray-scale stencil map. Basically, a gradient texture is mapped on the Position (vertices) and multiplied for the Normal coordinates of the ocean mesh that, being created by the Ocean modifier, is constantly changing. So, only as the waves rise do they show foam at the top. This effect has been lessened and made a bit random to show some foam scattered around the rest of the surface as well. This works not only for stills but also in animation.

In the following screenshot, you can see the rendering of the resulting black-and-white mask that we used as a stencil for foam location (the image obtained by simply connecting the mask output to an Emission shader node to get a quick rendering and preview):

How it works...

The only stencil material rendered

See also

The Blender Ocean modifier is able to create its own foam effect, generated as Vertex Colors and baked to a series of images (frames) saved in a directory. These images are then automatically mapped on the surface. They can be used as stencil masks instead of the Foam_location group node.

To know more about the Ocean modifier, you can take a look at the wiki documentation at http://wiki.blender.org/index.php/Doc:2.6/Manual/Modifiers/Simulate/Ocean.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.15.217