Showing posts with label materials. Show all posts
Showing posts with label materials. Show all posts

Monday, November 17, 2008

MOSAIC progress report on BBB related changes

Greetings all,

The good fellows over at Aqsis have put forth a rather daunting challenge... to render at least a frame if not a small animation of the Blender Foundations "Big Buck Bunny" movie. At first I wasn't very keen on the idea but after considering it for a while I decided it would be the perfect project to really push MOSAIC's integration with Blender and its ability to efficiently export a large animation. Most of the work over the last few months have been related to getting MOSAIC and Aqsis capable of rendering BBB but because of the nature of MOSAIC's RenderMan design and its goals to do all things non-renderer specific these improvements will benefit all RenderMan renderer's using MOSAIC :-)

One of the first big challenges required rewriting key sections of MOSAIC to better handle large scenes and objects with large amounts of data. In particular MOSAIC was dying a horrible death when exporting large numbers of particles, hair strands or high density meshes. The problem was MOSAIC was storing all generated RIB archives in memory before writing to file, now MOSAIC generates and exports the RIB one line at a time (managing archive interactions through open file handles) thus file size is now only limited by the OS. I've also optimized many areas of MOSAIC related to instancing and RIBset passes making occlusion map animations as well as much larger scene animations possible (or at least not as painfully slow as before).

One of the next big challenges has been dealing with the painfully slow export times in Python when dealing with millions of strands of hair :-( I'm still planning experimenting with Python wrappers in the near future to squeeze faster export directly out of MOSAIC, although I'm not keen on complicating compilation and installation of MOSAIC for the user. A far more RenderMan like way of dealing with the problem is to use DSO's (dynamic shared objects) for generating any procedural like object such as hair. A DSO is C library object that can be dynamically loaded and executed by the renderer. These DSO's are capable of accessing the render state as well as calling functions from within the render and can use this information to generate and return RIB code to be rendered (such as growing hair on another object). The advantages of this approach are huge not only because MOSAIC doesn't need handle these large amounts of data but also because neither does the RIB files. This is a big deal for render farms since several hundreds of megs for a single hair RIB for a single character for a single frame will kinda slow down traffic on the network for a large animation :-s The down side to DSO's is they have to be properly compiled to use (something non-programmers will be intimidated by) and often times they call on renderer specific functions making them often times non-universal to other renderer's (unless carefully designed). In either case I have added a new series of controls so that code fragments can now be attached to individual particles systems and geometry materials indices's as well as a toggle to determine if the code fragment "replaces" the exported RIB. So in the case of the BBB hair problem you just make a code fragment to call a hair DSO, select the body mesh of the character and finally attach the code fragment to the particles system (toggling "R" on to not export any hair from MOSAIC). Chris (c42f) over at Aqsis is in the process of writing an excellent hair DSO that will eventually mimic enough of Blender's hair behavior to properly render several of the hairy characters, although I think it may be useful for grass or other such things with tweaking and maybe some modifications :-)

There are also several new modifications related to the DSO stuff mentioned previously. The exporter now always export particle and geometry RIB into a cache file that is always generated regardless of the archiving type of the object. This was originally done so DSO's that require a RIB of the geometry are always guaranteed one, but has several side benefits such as improved export times of motion blur and more efficient sharing or geometry data throughout the export system :-) The geometry cache RIB path/file name can be returned in a code fragment using the new <GeometryCache_S> token and particles cache RIB path/file name can be returned using the new <ParticleCache_S> token. Also there are some improvements to the token system for more flexibility in the returned token string. Originally the tokens were only designed for return values inside shader calls that are always in square brackets but this isn't very useful when in a code fragment. Now its possible to leave the type off to return a straight value or use the "_Q" type to return a quoted value. So for instance <Test_F> = [ 1.0 ], <Test> = 1.0, <Test_Q> = "1.0". Another big new feature added to help with DSO's in MOSAIC is the inclusion of what I'm calling Python tokens. A Python token is just a token including the name of a Python text loaded in Blender that writes RIB text into a global variable tokenReturn that MOSAIC then places in the export stream. So if you made a Blender text called "test.py" and put tokenReturn = "[ \"Hello World!\" ]" in it and then created a fragment and put a token <test.py>, then "Hello World" would be placed in the RIB that the fragment was attached to. This is mainly helpful for accessing data in Blender through Python that MOSAIC doesn't have a token for, such as accessing a control for a parameter to a DSO that MOSAIC doesn't have, it could also be very handy for procedurally generating RIB with python in Blender (although if doing something intense it would be better to use a DSO).

There are a few small changes made to MOSAIC to help with managing larger more complex projects. The "Generate Shader Fragment" utility now only lists shader source files instead of all shader sources and library shaders. This is because when dealing with a lot of shaders the menu was becoming to large to use. Instead I've added a "Fragment" button to each shader library in the "MOSAIC Settings" tab that allows you to create fragments for only those shaders in each library. This has not only the advantage of making the menus smaller but makes it possible to deal with shaders in logical groups. I've also added a "list all tokens" utility that will generate a text file called "tokens.txt" in Blender that automatically lists all MOSAIC tokens grouped according to there datablock and type using there full format, this is not only handy for seeing what tokens are available outside the shader editor but is great for copy/pasting tokens into code fragments ;-) I've also had to make several changes to how MOSAIC interprets dupli systems, in particular how it manages dupli groups conected to empties. This is important to how Blender manages content linked to external libraries in large projects and such.

Another big problem is how to deal with complex characters with multiple materials applied to the same mesh. Since standard RiSpec doesn't have a method for dealing with this in the past MOSAIC just separated the mesh where each material is applied however this process is slow to export and when dealing with SDS it is still required to export the entire mesh for each material (using hole tags so curvature matches between materials) making for very large RIB file sizes. MOSAIC now supports three different methods for separating materials using a control called "Material Set Mathod" located in the Geometry tab, the first two methods will work with all renderer's however the most efficient and elegant method will only work in Aqsis or Gelato, heres a breakdown of the pros and cons of each:

Separate Geometry
Separates the geometry per material either using Os 0/1 values for polygons or hole tags for SDS.
PROS:
  • the only benefit of this approach is it guarantees that all renderers and shaders will work with multiple materials.
CONS:
  • Since the geometry is copied for every material this can produce massive project sizes- Since the geometry is re-processed for every material this can be very slow to export
  • All that data ends up needing to be loaded into the render which consume large amounts of memory and will render slowly

Multipass Materials
Calls the cache with read archives per material using a user parameter in the geometry to adjust Os values in the shader per face.
PROS:
  • Requires the geometry only be processed once greatly increasing export times
  • Produces far less overall RIB size since geometry's only used once (unless datablock is set to "Inline Code")
CONS:
  • Requires that all shaders be modified to recognize the geometries user parameters, however this only effects polygons (particles are still separated)
  • Depending on the renderer still consumes large amounts of memory for each copy of the mesh per material
  • Still very slow to render since each material has a complete mesh even if its not visible per material

Layered Materials
This elegant approach will only work in Aqsis or Gelato and uses the user parameter in the geometry to shade or skip each face in a chain or layered shaders.
PROS:
  • Only requires the geometry to be exported once, so exports fast
  • Only requires the geometry to be loaded into the renderer once, so uses less memory
  • Only requires one geometry for all materials, so renders faster
CONS:
  • Will only work in Aqsis or Gelato
  • Requires all shaders be modified to recognize the geometries user parameters, and unlike the previous method will mess up both particles and geometry shading if not.

The last method required adding Aqsis/Gelato style layered shading support to the internal structure of MOSAIC so it was a small step to add user control of layered shading to materials. MOSAIC is using the concept of "chaining" Blender materials together to create layered shading for surface and displacement shaders. How this works is fairly straightforward, open up the materials tab and select the material you want to be the "root" material in the chain then select a material with "Next Material Layer" menu to link to, then goto that material and select another material to link to, so of so forth... All materials in a chain are exported together into each materials archive separately so multiple objects can intercept different parts of the chain.
You can also use the "Surf Output Variables" and "Disp Output Variables" to specify surface and displace output variables to use for the current materials shaders. If more then one variable is entered (separated by spaces) then separate connect calls are chained together for each so for example...

if Material.001 "layer1" used "a1 a2 a3" and called Material.002 and
if Material.002 "layer2" used "b1 b2 b3" then MOSAIC would export
ConnectShaderLayers "surface" "layer1" "a1" "layer2" "b1"
ConnectShaderLayers "surface" "layer1" "a2" "layer2" "b2"
ConnectShaderLayers "surface" "layer1" "a3" "layer2" "b3"

Also note that you can have varying numbers of parameters per control and can use None selections to disable export of certain shaders in a chain (such as only building a displace chain, or skipping a surface shader in a long chain).

Here's a simple example using all these features together. In particular this is demonstrating combining highly refined or integrated shader (such as MOSAIC's integrated shaders to Blender) with fast customized shaders (such as procedural shaders). This in my mind is a key benefit of layered shading because you don't have to re-create the shaders for every new surface (such as having to be sure every new shader can use occlusion, or env maps, or SSS).




At this point in development I'm finally at the point that I've begun to work on re-writing the built in shaders for more complete support of Blender's material system. I'm currently working on adding support for multiple layered textures using some if not all blending modes and UV layers. This is important because currently I can export the characters geometry into RenderMan (even using linked libraries) but without layered textures, alpha and layered UV's it won't be possible to get accurate renders of the charaters :-( The trickiest part wont be putting this together but getting the top 5 RenderMan renderer's compiling the shaders without problems :-s
Anyway I'm hoping this will only take a few weeks to do and if all goes well I also hope to add some fancy techniques such as better SSS support for skin and faked soft shadows using depth maps (should be considerably faster then raytracing).

Thanks for reading, WHiTeRaBBiT

Saturday, July 19, 2008

MOSAIC feature ramblings - part 11

Part 11 - The Materials Setup tab and raytrace attributes:


MaterialsThe "Materials Setup" tab allows you to control different aspects of how MOSAIC exports Blender materials.

MOSAIC uses Blender materials as the place to apply RenderMan shaders and attributes related to surface shading, in particular you can apply surface, displacement, interior volume, exterior volume and area light shaders as well as both integer and string based raytrace attributes.

Since MOSAIC exports all material associations to geometry including multiple materials per object and datablock, there is a wide range of possibilities.

Traditionally RenderMan shaders are applied to individual objects, but with MOSAIC you can apply shaders to materials and assign multiple materials to portions of the same mesh allowing much more complex setup and rigging. The exporter automatically separates the mesh into different objects to be shaded, when using an area light shader the geometry is placed with the light declarations before any other geometry (area light shaders are for true geometry based illumination and is not the same as the area light type which is only a light array).



The "Materials Setup" tab also has a set of attribute menus for setting the material's raytracing behavior. In RenderMan you can use these attributes to define whether a surface is visible to specular (shadows), diffuse (occlusion) and photons (caustics). There are two forms of these settings with varying support by different renderers, the older string type and the newer integer type (see your renderer's documentation for detailed instructions on how these attributes work). MOSAIC uses your renderer preset selection (in "MOSAIC Settings" tab) to automatically setup all new material settings to use the type best suited for your renderer. By default all options are on so you can immediately begin using raytraced shadows, occlusion and caustics :)

Since MOSAIC provides tokens that can hook Blender controls to shader parameters (see "Shader Parameters" in the "MOSAIC Utilities" tab) the built in shaders automatically use the most common Blender material controls. At the moment only the most important controls are linked, and not all of them behave the same as Blender... but after this series of blogs my next big project is to add almost all light, material and world controls into the shader system with near identical behavior :)

Here's a list of which material controls are currently hooked to the default shaders:

  • Halo (this will turn all geometry including hairs into point clouds)

  • HaloSize (this not only controls point size but also the base width for hairs)

  • Add (this controls hair tip width relative to base width)

  • HaloPuno (if enabled MOSAIC attempts to export normals for hairs to create ribbons)

  • Shadeless

  • Shad A

  • Col RGB

  • Spec RGB

  • Mir RGB

  • A (Alpha)
  • Ref

  • Spec

  • Hard

  • GR: (light groups do not use "Exclusive")

  • Tralu

  • Amb (this controls both hemi light and raytraced and mapped occlusion)

  • Emit

  • Ray Mirror (this will enable raytraced reflections but if "Enable Environment Mapping" is enabled on any object using this material it will be used for reflections regardless of this toggle)

  • RayMir (this controls both raytracing and environment maps)

  • Fresnel (this controls both raytracing and environment maps)

  • Ray Transp (this will enable raytraced refractions but if "Enable Environment Mapping" is enabled on any object using this material it will be used for refractions regardless of this toggle)

  • IOR (this controls both raytracing and environment maps)

  • Fresnel (this controls both raytracing and environment maps)

  • Sub Surface Scattering (this enabled a depth map based sss effect therefore it's recommended you use this with shadow mapped lights, however you can manually adjust the SSSWidth parameter for ls_MOSAIClight to get similar result with raytracing)

  • Scale (adjusts the SSS strength)

  • Radius R

  • Radius G

  • Radius B

  • IOR (adjusts the sss map blurring)

  • Error (adjusts the sss map bias)

  • Scattering color

  • Col

  • Tex

  • Front

  • Back

  • Also the world "Ambient Occlusion" toggle enables a non cached raytraced occlusion using the worlds HorRGB color (mapped based occlusion is enabled in the "Scene Setup" tab independently of this toggle.

The surface shader also support texture channels. All "map to" channels are currently supported and work similar to Blender with one exception, only one texture of each channel type can be used for each material (I intend on changing this to match Blender's behavior by using arrays of texture names in the future). Something else to keep in mind with textures is that MOSAIC only supports the "image" texture type not any of the procedurals (that's what RSL is for), and that when loading an image you should probably stick with .TIFF since most RenderMan systems have problems with anything else. Also remember to use the "Texture Export Options" in the "Project Setup" tab to disable any textures you're not using.

Here's a list of which material texture controls are currently hooked to the default shaders:

  • UV (only uses this coordinate type, any other is default to primitives type)

  • ofsXYZ

  • sizeXYZ

  • All "Map To" channels supported

  • Col

  • Nor

  • Disp (controls displacement map height)

That covers most of the high points of the materials tab so let's go ahead and list its groups and controls:

Navigation and RIBset management:Materials Setup
This group of controls is at the top of the tab and allows you to select available cameras, updating all controls and Blender selections accordingly. It also has controls for creating, deleting and selecting RIBsets for this tab.

Here's a breakdown of the controls in this group:

  • Select Material - This will show a list of available materials for selection, once selected it will update all controls in this tab.

  • Select RIBset -This will show a list of available RIBsets for selection, once selected it will update all controls in this tab.

  • Create New RIBset -This will create a new RIBset from the current RIBset control settings and select it.

  • Delete This RIBset - This will delete the currently selected RIBset and select the DEFAULT RIBset.


Code management:
This group of controls is for attaching user created code fragments (text files with the "cf_" prefix in their names) to the beginning and end of different RIB blocks. As with the previous group, this group of settings is available in various configurations for all the remaining tabs.

Here's a breakdown of the controls in this group:

  • Material Archive -This menu allows you to specify whether the exported RIB code for cameras is exported into separate RIB archives or inlined into the object RIB.

  • Material Begin Code - This allows the insertion of custom code fragments into the beginning of the material block before anything MOSAIC exports.

  • Material End Code - This allows the insertion of custom code fragments into the end of the material block after anything MOSAIC exports.


Shader management:
This group of settings is available in various configurations for all tabs and is for selecting shaders for the selected material.

Here's a breakdown of the controls in this group:

  • Surface Shader - This allows you to select a surface shader from a list of available system or loaded shaders.

  • Notes - This will display a popup of any notes attached to shader fragments by the author (usually containing information about which Blender controls are used by this shader).

  • Displacement Shader - This allows you to select a displacement shader from a list of available system or loaded shaders.

  • Int Volume Shader - This allows you to select an interior volume shader from a list of available system or loaded shaders. Volume shaders are similar to atmosphere shaders but for filling volumes of space.

  • Ext Volume Shader - This allows you to select an exterior volume shader from a list of available system or loaded shaders. Volume shaders are similar to atmosphere shaders but for filling volumes of space.

  • Area Light Shader - This allows you to select an area light shader from a list of available system or loaded shaders. Area light shaders are for turning geometry into true area lights, but are only supported by a few renderers and usually require special shader ops.


Options and attributes:
This group of controls is for setting material related options and attributes for this material.

Here's a breakdown of the controls in this group:

  • transmission - Is this material visible to transmission rays (casts shadows)? This control uses both the new integer style (on or off) and the older string style (sets how shadows cast through material such as "transparent", "opaque", "shader", "Os").

  • specular - Is this material visible to rays created by trace() (receives shadows)? This control uses both the new integer style (on or off) and the older trace command.

  • diffuse - Is this material visible to color bleeding and occlusion (new integer style)?

  • photon - Is this material visible to photons such as caustics (new integer style)?

  • camera - Is this material visible to the camera (new integer style)?

  • Shading Model - Which shading model to use when scattering photons (newer style)?

  • transmissionhitmode - How is opacity for transmission rays determined?

  • specularhitmode - How is opacity and color for specular rays determined?

  • diffusehitmode - How is opacity and color for diffuse rays determined?

  • camerahitmode - Allows you to control culling of geometry behind camera's visibility.

  • Use Material in Animations - This option allows you to manually take advantage of a "speed hack" in MOSAIC. This option can drastically improve export times in an animation for any material that is not animated. If enabled then MOSAIC will only export this material on the first frame of this animation and will then just re-use it without recalculating any of its data.

  • Flip "U" Tex Coor - This allows you to manually flip the exported "U" UV value for this material (handy when certain image formats are working but inverted in the U direction on your renderer).

  • Flip "V" Tex Coor - This allows you to manually flip the exported "V" UV value for this material (handy when certain image formats are working but inverted in the V direction on your renderer).

  • Shader MBlur - This enables the motion blurring of the material's parameters. This is particularly helpful for blurring displacement animations (if your renderer supports this).

  • frames - This specifies the number of frames to blur across.

    Note : MOSAIC begins the blur by this number before the current frame so blur leads to current frame.


Well, that's it for the materials tab... next up a deeper explanation of using the mapping utilities :)

Thanks for reading, WHiTeRaBBiT