• Resources
  • Cubemap reflections (cubic environment mapping)

It seems like every other game engine has this built-in by now, so here's a cubemap reflection shader written in Godot's shading language. It makes things really shiny.<br><br><img src="https://lut.im/Lcq9n2y40l/GPDuaWCZV0YOpbu7.png" alt="" height="400" width="500"><br><br>I just took GlaDOSik's original shader and used some magic math to make it so the only necessary parameter is the actual cubemap.<br>

// -- VERTEX SHADER -- //<br><br>//Based on: http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter07.html<br>//By GlaDOSik with help from romulox_x (http://forum.godotdevelopers.org/index.php?topic=8988)<br>//Shader-based camera position by fluffrabbit with help from falsaber and Hiesetsu<br><br>vec4 invcamx = INV_CAMERA_MATRIX.x;<br>vec4 invcamy = INV_CAMERA_MATRIX.y;<br>vec4 invcamz = INV_CAMERA_MATRIX.z;<br>vec4 invcamw = INV_CAMERA_MATRIX.w;<br><br>vec3 CameraPosition = -invcamw.xyz * mat3( invcamx.xyz, invcamy.xyz, invcamz.xyz );<br><br>vec3 vertexW = WORLD_MATRIX * SRC_VERTEX; //vertex from model to world space<br>vec3 N = normalize(WORLD_MATRIX * vec4(SRC_NORMAL.x, SRC_NORMAL.y, SRC_NORMAL.z, 0.0)).xyz; //normal from model space to world space<br>vec3 I = normalize(vertexW - CameraPosition); //incident vector (from camera to vertex)<br>vec3 R = reflect(I, N); //reflection vector (from vertex to cube map)<br>R.z *= -1;<br>VAR2.xyz = R;<br><br>// -- FRAGMENT SHADER -- //<br><br>uniform cubemap cube;<br>color envColor = texcube(cube,VAR2.xyz);<br>DIFFUSE = envColor.xyz;

<p>Attached are an example project and a Blender file to generate a cubemap.</p><br>

Really cool. (I moved the topic from 3D grafics to tools and assets.)

Nice thanks for sharing! Now we just need a way to set them up so as todo parallax correction(lerping between them according to player location) too. :)

[quote author=Megalomaniak link=topic=15406.msg15737#msg15737 date=1460594216]Nice thanks for sharing! Now we just need a way to set them up so as todo parallax correction(lerping between them according to player location) too. :)[/quote]I&#039;m pretty new to all this shader stuff. What&#039;s parallax correction for? Does it enhance the realism? Is there a guide on this somewhere?By the way, your water shader looks delicious. I&#039;m definitely putting that in my game.

[quote author=fluffrabbit link=topic=15406.msg15739#msg15739 date=1460595925]I&#039;m pretty new to all this shader stuff. What&#039;s parallax correction for? Does it enhance the realism? Is there a guide on this somewhere?[/quote]Basically, yes. It improves the precision of reflections by blending between multiple according to viewpoint/camera location since they are relative to the point of capture.[url=

[/url][url=https://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/]https://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/[/url][quote author=fluffrabbit link=topic=15406.msg15739#msg15739 date=1460595925]By the way, your water shader looks delicious. I&#039;m definitely putting that in my game.[/quote]Thanks, make sure to let me know if you find any issues though!

[quote]It improves the precision of reflections by blending between multiple according to viewpoint/camera location since they are relative to the point of capture.[/quote]I&#039;m not going to say it&#039;s overkill because it&#039;s definitely worth at least trying to implement, but I&#039;m in the middle of a project that has certain deadlines and this isn&#039;t a very high priority for me right now, but it seems like it could be very useful for when shiny things travel through different rooms. I&#039;ll keep it in mind but I don&#039;t have time to work on it right now. I only did the basic cubemap thing because that&#039;s an absolute necessity. Sorry.

[quote author=fluffrabbit link=topic=15406.msg15741#msg15741 date=1460599416]...I&#039;m in the middle of a project that has certain deadlines and this isn&#039;t a very high priority for me right now, but it seems like it could be very useful for when shiny things travel through different rooms. I&#039;ll keep it in mind but I don&#039;t have time to work on it right now.[/quote]Haha, sorry. I never meant to imply that you should do(implement) it. :)

[quote author=Megalomaniak link=topic=15406.msg15742#msg15742 date=1460602104][quote author=fluffrabbit link=topic=15406.msg15741#msg15741 date=1460599416]...I&#039;m in the middle of a project that has certain deadlines and this isn&#039;t a very high priority for me right now, but it seems like it could be very useful for when shiny things travel through different rooms. I&#039;ll keep it in mind but I don&#039;t have time to work on it right now.[/quote]Haha, sorry. I never meant to imply that you should do(implement) it. :)[/quote]Maybe this is something that you may be interested in doing. Not even GTA 5 uses this technique. It uses a low-resolution realtime cubemap rendered from where the player is.

[quote author=fluffrabbit link=topic=15406.msg15743#msg15743 date=1460602736]It uses a low-resolution realtime cubemap rendered from where the player is.[/quote] yes &amp; no, it uses a Dual-Paraboloid Map, basically 2 spherical maps stitched together, it captures the environment by projecting the environment to a cubemap first, then converting it. Most likely because they need to support the ps3 &amp; xbox360(of the 2 ps3 is the lower common denominator with it&#039;s limited ram, thankfully it&#039;s quite fast by it&#039;s generation&#039;s standards)And I never mentioned realtime capture either, in that case I would have referenced [url=http://devlog-martinsh.blogspot.com.ee/2011/09/box-projected-cube-environment-mapping.html]bpcem[/url], but sure, that would be cool and I have considered implementing it too, but I&#039;m thinking lower hanging fruit first, maybe? Since you know, the parallax correction would be needed any way&#039;s. :)Also I&#039;d imagine the smart implementation to be pre-rendering/baking those &#039;reflection probes&#039; while baking the environment lighting(need time of day? bake more lightmaps &amp; reflection probes, lerp for TOD change)but that&#039;s just how I&#039;d go about it. To be clear my interest is only in targeting PC, though. :)eh, getting too off topic tho, sorry. Cool project, and again, thanks for sharing.

[quote author=Megalomaniak link=topic=15406.msg15744#msg15744 date=1460612281][quote author=fluffrabbit link=topic=15406.msg15743#msg15743 date=1460602736]It uses a low-resolution realtime cubemap rendered from where the player is.[/quote] yes &amp; no, it uses a Dual-Paraboloid Map, basically 2 spherical maps stitched together, it captures the environment by projecting the environment to a cubemap first, then converting it. Most likely because they need to support the ps3 &amp; xbox360(of the 2 ps3 is the lower common denominator with it&#039;s limited ram, thankfully it&#039;s quite fast by it&#039;s generation&#039;s standards)And I never mentioned realtime capture either, in that case I would have referenced [url=http://devlog-martinsh.blogspot.com.ee/2011/09/box-projected-cube-environment-mapping.html]bpcem[/url], but sure, that would be cool and I have considered implementing it too, but I&#039;m thinking lower hanging fruit first, maybe? Since you know, the parallax correction would be needed any way&#039;s. :)Also I&#039;d imagine the smart implementation to be pre-rendering/baking those &#039;reflection probes&#039; while baking the environment lighting(need time of day? bake more lightmaps &amp; reflection probes, lerp for TOD change)but that&#039;s just how I&#039;d go about it. To be clear my interest is only in targeting PC, though. :)eh, getting too off topic tho, sorry. Cool project, and again, thanks for sharing.[/quote]If we&#039;re going so far as to implement bpcem I&#039;d just render a spheremap in realtime (only one angle) per object at something like 256x256. For any seriously reflective object such as a mirror or ball bearing doing that sort of thing would work well, then for less reflective objects I&#039;d just render with the skybox and muddy them up with shader effects.

this a good thread, but that discussion would probably be better off split into its own thread. :)