GLSL PBR Shader for viewport

Hello everyone!

This thread is about the development of the PBR viewport.

My first goal was reach somewhat the quality / feature of Marmoset 2 inside the viewport to easily preview game assets and work on shaders inside Blender node editor like you would with Unreal editor for instance.

But recently I oriented my development towards supporting Cycles shading in realtime viewport. This provides a more intuitive and flexible approache to the features. I also started the support of physical lights.

A lot of things are approximated. For instance, the world casts no shadows and Refraction is done only for front facing faces.

But you can edit your mesh

Downloads (Win64 - OSX64 - Linux64):

V0.4 - 06/06/2016

  • Oren-nayar Diffuse (Env lighting)
  • Velvet Shader (Env lighting + Point lights)
  • Toon Shader (Env lighting + Point lights)
  • Ashikhmin & Beckmann distributions for isotropic and anisotropic
  • Closest Filtering for Texture node
  • Material layer overide but only for material. So you can preview one material on every objects without assigning it to each object.
  • Linearly Transformed Cosines to shade Area lights with the Glossy GGX (other distributions use the same LUT)
  • Material AO to apply occlusion during the shading stage an not at the post process stage. Algorithm is slow but correct (matches cycles)
  • Screen Space Reflections for Sharp Glossy shader. Algorithm is from Morgan McGuire blog and the step is always 1px so reflections can’t go far.
  • Backface buffer to improve screen space effects.
  • Bias parameter to control the accuracy of the Environment Sampling.
  • Performance : Use a jitter texture + a sample texture to generate sampling random vectors instead of computing everything inside the shader. (this takes 2 texture slots though)
  • Performance : Lights shadowing (and lamp nodetree) are evaluated only once per Material.
  • Codebase Change : major thing I did is not using the common uniform interface for passing uniforms related to the sampling and PBR. I also splited the glsl files into more managable ones.

V0.3 - 20/03/2016

  • Introduced a probe system that generates probe from the world environment or specific objects
  • Spherical harmonics are computed on the gpu and lower resolution (much faster)
  • Soft shadows of area lamps : shadow “FOV” controled by near clip plane
V0.2 - 18/01/2016
  • Better Default preview shader for Glossy and Diffuse
  • Support Sphere / Spot / Sun (partial) / Area (partial) lights
  • Glossy Shader (only Sharp / GGX)
  • Diffuse Shader (No roughness in Realistic mode)
  • Translucent Shader (Approximated as backlighted Diffuse)
  • Refraction Shader (only Sharp)
  • Glass Shader (only Sharp)
  • Velvet Shader (only Point Lights)
  • World Environement Reflection (still not perfect with some caveats)
  • Soft shadows of spot lights
  • Support Lamp Nodes
  • Normal Map Node (now works as expected)
  • Color Management settings previewed in the viewport (still WIP)
  • Other small things…

V0.1 - 21/06/2014

So what I’ve done so far:

  • Rectified some GLSL node to output what they should output.
  • IBL specular reflection
  • Irradiance from spherical harmonics computed from the HDR (now weighted)
  • Lambertian diffusion
  • GGX specular distribution
  • Dielectric and Metallic
  • Newton ring reflection (just a simple little node graph)
  • Cubemap support. (Cubemap should be in blender conventional orientation).
  • Standard Lights support
  • Multithreaded filtering of cubemap mip : You can prefilter the cubemap and have “better” light approximation.
  • Fresnel effect intensity input.
  • Mapping Node support
  • Envmap intensity input.
  • Separated outputs for better control.
  • Glossiness / Specular Model.
  • Energy conservation. (should be off most of the times)
  • Choosing Sample Number.
  • Horizon occlusion.
  • Sampling Setting are stored in Images not in Nodes.

Basicaly everything could be done in node if there is a proper reflection vector and a LOD sample value in the textures node but it would be tedious to do “for” loops with nodes.

My modification of blender’s code are:

  • New PBR node type: IBL input and compute Spherical Harmonics.
  • GLSL re/coding of the folowing nodes: Texture Coordinate, Geometry, Normal Map, Checker Texture (I found someone who already did afterward)
  • Fixed color correction inside the Image Texture Node (was not applied in GLSL).
  • Unclamped rectfloat before passing hdr texture to OpenGL and use GL_RGBA16F to have the “whole” (actualy half) hdr range in GLSL
  • Disable Canceling Mipmap in paint mode to actualy paint mask for the pbr shader (if not the reflection are not blured anymore).
  • Added Cooktorr GGX specular shader for energy preservation and specular distribution. (Only works in GLSL)
  • [New] Added cubemap support to blender’s viewport.
  • [New] Added normalmap node support to blender’s viewport.

Attachments




2 Likes

Amazing work… but, should there be a link or…?

Sorry for not replying I was busy trying to compile for windows.

So here is the build for win64 : https://www.dropbox.com/s/yy1p3bq886tr8mo/Blender-PBR_win64.zip

And here is the test scene : https://www.dropbox.com/s/z7nrf6c9ca22vs9/pbr.blend

Here you have some test with a real model.

CAVEAT: Changing a value in the shader chain will recompute SH lighting so it’ll be laggy when picking color and stuff. I need to find a way to recompute SH only on File Change/Loading.

It would be neat if this could become the start of a new ultra-fast and high quality GLSL engine where you have modes for Cycles, BI, and the BGE as well as a custom mode where you plug in shaders for [insert your favorite render engine or game engine here].

I would also wait to really flesh it out until it can take advantage of the new Viewport FX code, as it will just have a much nicer base to work with (a lot of the current viewport code is OpenGL 1.x stuff from more than 12 years ago)

@Monio - seen this? https://www.youtube.com/watch?v=BvcHKR6sSPI

Yes. Nice but limited comparing to my solution.

Amazing.
hypersomniac !!!

Dude, this is great. Really great. I’ve been using Substance Painter / Designer to do my PBR work for UE4, but it’d be nice to be able to do a lot of it in Blender. Not sure how it would work with, say, the painting toolset. Maybe if you get farther with it you could add some nodes/presets that work like modern game engines like UE4 or Unity 5.

New version!

I separated Specular environment and SH in 2 nodes so you can chose the one you need and tweak their intensity and such. Just plug a LagLong EnvMap inside (it should be the same map obviously). I may get cubemaps to work.

Well i’ve got the lamps working. It’s hacky but it works. I added a specular type only defined for GLSL mode it’s called CookTorr GGX under the standard (non Cycles) material tab (it reverts to toon shading everywhere else).

You need to hook up a material using this spec shader in the PBR Lighting Node. This node overides the spec/diffuse color and the roughness of the material. You plug it in the right output and activate GLSL shading and voila!
Just note that Hemi Lights don’t follow the same spec function so disable spec on theses lights.

Normal maps must be tangent space (for now), Non-color data, and pluged in a NormalMap Node (Cycle node).

To combine Env Lighting and Lamp Lighting just follow this scheme (I made some annotations i found important):


This download is still here.

Take note that it’s not backward compatible with the previous build.

It is already working with paint mode! I’m also going to provide node presets for some shaders like you mentioned.

Holycrapholycrapholycrapholycrap, this is fantastic! I thought we would have to wait 3-5 years before we could have PBR-based viewport shading in Blender.

Thanks a ton for the hard work and the build. (Where is the link to the Linux build by the way?)

EDIT: So I got on my windows machine and tried this build out a bit and I must confess that I find it extremely confusing to set up. By looking at your new example it seems I need to first switch to Cycles to add the necessary nodes and then switch back to Blender game to change the specularity type to CockTorr GGX and set the viewport to textured GLSL shading.
I just don’t understand how to get any results to show in the viewport, everything I try just shows up as a black silhouette. Only IBL lighting, only lamps and both combined shows the same result.

Your old example file does not work in this new version, do you have a new one to share please?
Or it can just be that the build doesn’t work properly, that would explain a lot. (but most likely the reason is that I am an idiot and have missed something glaringly obvious)

Looks awesome. I hope this can integrate with the new Viewport FX code that is coming.

EDIT #2: Okay something seems to actually be broken. Not even the regular GLSL shading works now, even though it worked before. Not even if I restart Blender. Anyone else experiencing this?

wow! I was not expecting that much trouble!

Does it work with the a normal build (not mine)?

Some thoughts about the problem:

  • roughness of 0 may give black IBL spec result on windows (i’m working on it).
  • roughness of 0 give no spec from PBR lighting node (it’s expected i think).

Concerning your 1st question : yes It is REALY confusing to setup but i’ll do node groups so you won’t have to deal with these problems! but image input need to be cycles one so I don’t know how to solve this mess :confused:

I noticed in Cycles “GLSL” material view mode the lamps does not update.

Also i get crash too so it’s not a safe build!

Anyway here is a new test scene. Stupid Simple shader. It should work!

Linux build should come soon. I still have to build a portable version of it.

@hypersomniac Yes the regular builds work as expected. Yours “work” only as long as I don’t switch to GLSL shading.

This is what I get straight when I open your new example. If I deselect Suzanne I get a pure black silhouette instead. I also tried to start up a new instance of Blender and recreate your scene from scratch but still the same result.


I will try the Linux build later when you upload it. Thank you for your hard work :slight_smile:

@NinthJake I forgot to say that this shader needs opengl 2.10 if i’m not mistaken. I may ask you to check your graphic driver. Also you could check the console for GLSL errors.

What graphic card do you have?

Edit : Btw the graphic error you get is because the object is filled by the selected/unselected color. So it realy mean GLSL errors. Please check the console (top bar>window>show console) and paste them here.

This is very interesting, and I hope you get this sorted out after the Viewport improvements come in. Question: will this be more liek an advanced version of GLSL for painting in the viewport and getting realistic materials? I haven’t tried it yet, just reading the thread right now.

I run an Nvidia GTX 560Ti (driver version: 335.23) which is supposed to support OpenGL 4.1

This is the errors I get in the console.

I have the same problem on GTX 570

same error here, both builds
win7sp1 x64, intel Q9300, 8GB, GTS450 1GB (OpenGL 4.4 / tested with GPU Caps Viewer)