Creating underwater environment materials

In this recipe, we will create an underwater environment as shown in the following screenshot, looking especially at a fake caustic effect projected by the water's wavy surface and from an atmospheric perspective, obtained by a per material dedicated node group:

Creating underwater environment materials

The underwater environment in the final rendering

Note

Note that this atmospheric perspective effect is actually a fake and it is not obtained by a volume material. Volumetric shaders will be explained in Chapter 9, Special Materials, of this Cookbook.

Getting ready

Let's start by preparing the scene:

  1. Start Blender and switch to the Cycles rendering engine. Select the Cube and go to Edit Mode. Scale it 21 times bigger (Press A to select all the vertices, then press S, enter digit 21, and finally press Enter). Then scale it on the z axis of 0.230 (press S, then press Z, enter .23, and press Enter).
  2. Go to the Top view (press 7 on the numeric keypad) and press Ctrl + R to add three edge loops along the global x axis. Then press Ctrl + R again to add three edge loops along the y global axis. The Cube is now subdivided into 16 equal parts.
  3. Select all the faces, go to the Shading tab under the Tool Shelf panel to the left, and click on the Flip Direction button to invert the normals (which must look inwards).
  4. Exit Edit Mode, switch to the Objects modifiers window, and assign a Subdivision Surface modifier. Set the type of subdivision to Simple and the Subdivisions to 4 both for the View and the Render levels.
  5. Assign a second Subdivision Surface modifier. Again, set the type of subdivision to Simple but the Subdivisions to 2 for both the View and the Render levels.
  6. Now assign an Ocean modifier. Set the values of Geometry to Displace, Spatial Size to 20, and Resolution to 12.
  7. Go to the Tool Shelf again, and in the Edit subpanel under the Tools tab, click on the Smooth button below the Shading item.
  8. Go to the Object data window and click on the + icon under the UV Maps subpanel to add a set of UV coordinates. There is no need to unwrap the Cube. Check the Double Sided item in the Normals subpanel at the top.
  9. Make sure you are at frame 1, go to the Object modifiers window, and move the mouse over the Ocean modifier Time slot. Press the I key to add a key for the animation. Go to frame 25, change the Time value from 1.00 to 2.00, and press I again to set a second key.
  10. In the Choose Screen layout button at the top, switch from Default to Animation. In the Graph Editor window, press T, and 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.
  11. Rename the Cube as Ocean_surface as shown in the following screenshot:
    Getting ready

    The Cube with the assigned Ocean modifier

  12. Move the Camera to a place below the ocean surface Set the Location value of X to 16.80000, Y to -2.64000, and Z to 0.95000. Then set the Rotation value of X to 92°, Y to , and Z to 90°. Next, go to the Camera view (press 0 on the numeric keypad).
  13. Add a Cube, a UV Sphere, and whatever other objects you want to add floating under the ocean surface. Assign them very simple and colored Diffuse BSDF materials. Add a big Cylinder to the background, close to the far side of the Ocean_surface object. Immerse half of it in water (and half will be in the air). Assign a simple Diffuse BSDF material to this item too. Smooth the Cylinder and the UV Sphere. If you wish, assign a Subdivision Surface modifier to the UV Sphere.
  14. Now add a Plane. Place it at Location values of X as -3.22600, Y as -2.79600, and Z as -2.24463. Enter Edit Mode and scale it 30 times bigger (press A to select all of the geometry, then press S, enter 30 and press Enter). Using the Specials menu (press W) divide the Plane five or six times. Activate the PET (Proportional Editing Tool), randomly select vertices, and move them up to model the dunes of the ocean bed. Exit Edit Mode, smooth it by the Tools tab under the Tool Shelf panel, and assign a Subdivision Surface modifier at level 2. Disable the modifier visibility in the viewport by clicking on the eye icon. Rename it as Ocean_bed.
  15. Add a Cube. In Edit Mode, divide it a couple of times (press W, and Subdivide Smooth), in Proportional Editing mode and by selecting vertices quickly model a big round rock. Replicate it three or four times by rotating and scaling the copies. Place them in a scattered manner on the Ocean_bed. Smooth it and assign a Subdivision Surface modifier. Disable the modifier visibility in the viewport.
  16. Go to the World window and click on Use Nodes. Then click on the little square with a dot on the right side of the color slot. From the menu, select Sky Texture. Click on the Sky Type button above the little window and switch to the Preetham type.
  17. Select the Lamp. In the Object data window, click on the Use Nodes button and set a yellowish color for the light (set the values for R to 1.000, G to 0.989, and B to 0.700). Change it to a Sun. Set the Size value to 0.010 and the Strength value to 2.500. Then set the Rotation values of X to 22°, Y to -7°, and Z to 144°. You might know that for a Sun Lamp, the location doesn't matter.
  18. Go to the Render window. Under the Sampling subpanel, set the Clamp Direct and the Clamp Indirect values to 1.00. Then set the Samples to 25 for both Preview and Render. Under the Light Paths subpanel, disable both the Reflective Caustics and Refractive Caustics items.

As an alternative, just open the 9931OS_05_underwater_start.blend file and use the prepared scene.

How to do it...

First, let's perform the easy steps by appending the materials that are already made so that we can reuse them:

  1. From the 9931OS_03_Rock_procedurals.blend file, append the Rock_proc01 material. Select the Rocks object and assign the newly appended material.
  2. From the 9931OS_03_Ground.blend file, append the Ground_01 material. Select the Ocean_bed object and assign the material.

Now let's move on to the more complex steps:

  1. From the 9931OS_05_Ocean.blend file, append the Ocean_surface object, material. Select the Ocean_surface object and assign the material. Rename it as Ocean_surface_under.
  2. With the Ocean_surface object still selected, enter Edit Mode. Go to the Face selection mode and select only the upper faces. Then press Ctrl + I to invert the selection. In the Material window under the Properties panel, click on the + icon on the right (Add a new material slot), rename the new material as Null, and click on the Assign button. Now the Ocean_surface object has two different materials: the transparent water surface and the opaque sides and bottom (a simple white Diffuse BSDF material). Exit Edit Mode.
  3. In the Material window, click on the Ocean_surface_under material to select it. In the Node Editor window, delete the Foam and the Foam_location node groups. Also delete the two MixRGB nodes. Just leave the Ocean_water node group connected to the second Shader input socket of the Mix Shader node, which in turn is connected to the Material Output node.
  4. Add a Texture Coordinate node (press Shift + A and navigate to Input | Texture Coordinate), a Mapping node (press Shift + A and navigate Vector | Mapping), and an Image Texture node (press Shift + A and navigate to Texture | Image Texture). Connect the UV output of the Texture Coordinate node to the Vector input of the Mapping node, and the Vector output of this node to the Vector input socket of the Image Texture node.
  5. In the Image Texture node, load the caustics_tileable_low.png texture and set the Color Space to Non-Color Data.
  6. Add a Diffuse BSDF, a Transparent BSDF, and a Mix Shader node (press Shift + A and navigate to Shader | Diffuse BSDF, and repeat the same for the other two nodes). Label them as Diffuse_Caustics, Transparent_Caustics, and Mix Shader_Caustics.
  7. Connect the Diffuse_Caustics node's output to the first Shader input socket of the Mix Shader_Caustics node, and the Transparent_Caustics output to the second Shader input socket. Then connect the Color output of the Image Texture node to the Color input socket of the Transparent_Caustics shader node, and the Alpha output of the Image Texture node to the Fac input of the Mix Shader_Caustics node.
  8. Now connect the output of the Mix Shader_Caustics node to the first (and still empty) Shader input socket of the first Mix Shader node.
  9. Add a Light Path node (press Shift + A and navigate to Input | Light Path). Connect the Is Camera Ray output to the Fac input of the first Mix Shader node. Add Frame (press Shift + A and navigate to Layout | Frame) and parent these last nodes to it. Then label it as FAKE_CAUSTICS as shown in the following screenshot:
    How to do it...

    The FAKE_CAUSTICS frame mixed with the Ocean_Water node group on the ground of the Is Camera Ray output of the Light Path node

    The following screenshot shows where we are so far:

    How to do it...

    The point where we are so far

    What is missing now is the underwater atmospheric perspective effect. There are several ways to obtain this, for example, by compositing a Mist pass rendered in Blender Internal or by using a volumetric shader. However, we are going to do this with a simple node group added to every one of the different materials.

  10. Add a Camera Data node (press Shift + A and navigate to Input | Camera Data), a Math node (press Shift + A and navigate to Converter | Math), an Emission node (press Shift + A and navigate to Shader | Emission), and a Mix Shader node (press Shift + A and navigate to Shader | Mix Shader). Label the Mix Shader node as Mix Shader_Fog.
  11. Connect the View Z Depth output of the Camera Data node to the first Value input of the Math node. Set the Math node's Operation to Multiply and the second Value to 0.030. Check the Clamp option. Connect the Multiply node output to the Fac input socket of the Mix Shader_Fog node.
  12. Connect the Emission output to the second Shader input of the Mix Shader_Fog node. Set the Color values for R to 0.040, G to 0.117, and B to 0.124.
  13. Select all the new nodes and press Ctrl + G to make a group. Click and drag the first Shader input socket of the Mix Shader_Fog node into the empty socket of the Group Input node on the left and repeat this step by connecting the Shader output socket on the right. Press Tab to close the group. Then rename it as Fog_underwater as shown in the following screenshot:
    How to do it...

    The FOG_UNDERWATER node group in Edit Mode

  14. Add and paste the Fog_underwater node (press Shift + A and navigate to Group | Fog_underwater) just before the Material Output node of every material (in our scene, the Fog_underwater node will show eight users if the fake user button is selected) as shown in the following screenshot:
    How to do it...

    The Fog_underwater node group pasted at the end of the shader

How it works...

First of all, why should we choose a Cube for the ocean surface instead of the simpler Plane?

The reason is very simple: in Cycles, the World emits light, and the only way to avoid this is to set the color to pitch black (or by a combination of a Light Path node with the World materials, but this is another story). In our scene, the World is set to a bright blue sky color, and with a Plane, the underwater objects and the ocean bed would have been lit too much from the sides and the bottom the result look natural. A Cube, on the other hand, envelops all the underwater elements, limiting the lighting to the Sun Lamp passing through the surface, and projecting the image textured caustics. This gives a more natural-looking result.

The image texture we assigned to the water material is used to obtain a textured transparency effect. Right now, the water surface is actually opaque and transparent according to the black and white values of the textures, so as to allow the Sun Lamp light to pass through and project the caustics.

Thanks to the Is Camera Ray output of the Light Path node, the caustics image texture is not directly renderable on the ocean surface, but it nevertheless has some effect on the other materials. Because the value of Is Camera Ray is 1, the rays starting from the Camera and directly hitting the ocean surface can render only the clean water material plugged into the second input socket of the Mix Shader node, while the transmitted caustics (plugged in the first Shader socket = 0) get rendered.

The Fog_underwater node group is simply an emitter material serving as the background (deep green in this case) and mapped on every underwater material according to the z depth of the Camera (it also works with the Camera frame, in the viewport). The density of the fog is set by the Multiply node's second Value. For the ocean body, a value of 0.030 is good enough.

Note

The Camera z axis must not be confused with the global coordinate z axis, which is the vertical blue line visible in the 3D view. The Camera z axis, on the other hand, is the ideal line connecting the starting point of view to any visible element in the scene.

Note that we didn't expose the values of the nodes in the Fog_underwater group. This is because in Edit Mode, we can tweak the internal values of just one node to automatically update all the fog group instances assigned to the other materials. Besides, we know that the values exposed on the group interface would overwrite the internal settings and work only for that single node instance.

How it works...

The final underwater environment rendered from a different point of view

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

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