Jump to content

Parts shaders replacement, PBR, WIP release!


Recommended Posts

I thought it would fix my findVesselBounds() function, but no... At least it fix the crash with launchlamps, so here's a quick update.

 

V0.022 changelog :

-Fixed the crash when switching from a vessel to a launchclamp.

 

Download : https://www.dropbox.com/s/6u7uk2ni3xxh4rq/PBRShaderLoader_v0.022.zip?dl=0

Sources : https://www.dropbox.com/s/91pa99h3dtxmata/PBRShaderLoader_v0.022_src.zip?dl=0

Link to post
Share on other sites

 

So is this an in-development mod release, technically? It might get more eyeballs in the add-on development section, since the modelling area tends to only get viewed by part-makers. I can move the thread over if you want.

 

 

Link to post
Share on other sites
24 minutes ago, NecroBones said:

 I can move the thread over if you want.

That'd be great. I posted here because at the moment it's mostly a plugin intended to part modders, but it'd be more relevant in the plugin development section.

 

On an unrelated note, there's still some bugs when switching vessel with this last version. I'll fix it asap.

Link to post
Share on other sites

will test it out later today. got a part that should really shine with reflections. 

Is mask inputs still this?

R = Metal 1= full metal?
G = Gloss 1= full gloss?
B = Nothing
A = Occlude

R channel input seems to be broken on Lilleman/Specular; the rest seems fine. techinically you only need 1 PBR shader and set input textures to the proper channels on the shader no? I guess direct replacement by name is much easier to deal with. But no real need for specular property in any PBR shader if you are going with Metalness workflow.
KSP/Diffuse and Specular can both be replaced with Lilleman/Diffuse 
 

Edited by nli2work
Link to post
Share on other sites
4 hours ago, nli2work said:

R = Metal 1= full metal?

G = Gloss 1= full gloss?
B = Nothing
A = Occlude

Yep.

The R channel is not really broken in specular shaders, They use the StandardSpecular lighting model, where the Metal value isn't needed. http://docs.unity3d.com/Manual/SL-SurfaceShaders.html

I kept their appearence order, but I can change it, or even separate these three values in their own textures (with 32 bits precision, this time), if needed.

 

4 hours ago, nli2work said:

 techinically you only need 1 PBR shader and set input textures to the proper channels on the shader no? I guess direct replacement by name is much easier to deal with.

Maybe not a single shader for everything, but I can definitely reduce their count.

 

4 hours ago, nli2work said:

. But no real need for specular property in any PBR shader if you are going with Metalness workflow.

KSP/Diffuse and Specular can both be replaced with Lilleman/Diffuse 
 

I can do that for stock parts, but I'll let the specular shaders available, if someone want to use them.

For coherence between parts, we will probably need a "materials chart", with reference values, too. I'm still experimenting with masks values.

 

Still need to do some testing before another release, so here's a screenshot to keep you waiting.

BcwkoR3m.jpg

 

Edited by Lilleman
Link to post
Share on other sites

Getting self-reflection not much else. The big white area covers the large shield, which should be polished gold. Exported with KSP/DIffuse, I assume is replaced with Lilleman/Diffuse in game?

nJo8yrk.jpg

what does RP size do? 0.05 seems really small for RP bounds? Or is it actual size of the RP sphere? anything inside the sphere (part hosting the RP) shouldn't be reflected?

Lilleman/Diffuse in Unity: http://i.imgur.com/gsUHLVg.jpg

Edit: aside from self reflection, that actually that looks rather correct for mirror-like surface in space. :D 

Edited by nli2work
Link to post
Share on other sites
1 hour ago, nli2work said:

 anything inside the sphere (part hosting the RP) shouldn't be reflected?

I haven't found a proper way to avoid this yet. The part itself isn't reflected, thanks to the backculling, but all attached parts are.

The culling mask for the probe can be set to ignore them, but then most elements of the scene are not reflected. The solution until I can get something to work is to set the "near clip plane" value pretty high.

The RPSize values did nothing in 0.022, but it found a purpose again. It uses to be very small when I ran some tests with a probe per part (I wanted the probe to contain only the part's meshrenderer). Now it needs to cover the whole vessel (50 by default, wich is pretty low).

Thanks for your patience!

 

V0.03 changelog:

-more stable (fingers crossed)

-add reflection probe when switching vessel (without crashing, most of the time)

-use highest probe resolution only for active vessel (other are refreshed less often, with a 1/4 resolution)

-added a "Near Clip Plane" value in the config file. Set it higher if inside of the vessel is reflected. Temporary until I figure it out.

Download : https://www.dropbox.com/s/dmt8uhcnog3vdpo/PBRShaderLoader_v0.03.zip?dl=0

Sources : https://www.dropbox.com/s/a47ho8yn1sq1n0i/PBRShaderLoader_v0.03_src.zip?dl=0

Edited by Lilleman
Link to post
Share on other sites

This is really interesting! The reflection probe turns out to be a pretty effective way of a behind the scenes look of how things work in KSP :D

I did a chromeball test, amped up brightness and saturation to 2; 2048 reflection map; timeslice mode 0. I think the biggest problem is there's no skybox at all in the surface reference frame, and terrain doesn't get reflected. This is bad for metalness workflow as metal surface gets most of its colors from the environment (the 2 black squares on the chromeball have 255 R). you see the grass that's part of KSC static mesh, and water; you don't see reflection of Kerbin till about 60km up (not sure what the real in game distance is); when the planet becomes a mesh object instead of terrain object. This could be a culling mask thing. Skybox fades in by 60km as well, but the reflection skybox doesn't get updated till you switch away to Map then back. and there's gaps between the cubemap faces. TRReflection suffered from the same problems apparently, in surface mode the reflection clear flag appears to be white. Two of the 6 faces in reflection cube map are rotated for some reason.

http://forum.kerbalspaceprogram.com/index.php?/topic/84273-unity-layers-and-tags-constantly-updated/ Maybe this can help figuring out which layers to reflect and which ones not.

I think you can get much better performance by adding a skybox for surface reference frame, replace the gradient thing whatever it is. PBR shaders should already account for skybox without the need for reflection probes, plus a good skybox will make surface look much nicer. Granted it might be something better as part of EVE mod. reflection probes can be added as a Part Module to specific parts similar to TR. other PBR shaded parts nearby will pick up on it when in the same vessel.

easiest fix though is probably set clear flag to the sky color from the surface mode atmosphere gradient; and switch clear flag to skybox at the same time KSP switches to skybox.

 

 

Edited by nli2work
Link to post
Share on other sites

@nli2work, the terrain is on layer 15, the atmosphere is on layer 9, the scaled planets (bodies viewed from afar, such as the mun in the sky) are on layer 10, and the skybox is on layer 18, and is a hollow sphere at the origin with a camera in the center for rendering the skybox. There's also layers 19, 23, and 30 which are for disconnected parts, the sun, and SurfaceFX (liftoff/landing particles, etc.) respectively.

I'm not sure if reflection probes would world with the skybox, but they'd probably work with the atmosphere/scaled/sun layers, and they'd definitely work with the terrain/surfaceFX/disconnected-parts layers.

Edit: I'm getting my information from this post:

I haven't updated it for 1.1 yet, but there isn't any reason I can think of that any of the layers I've listed would have changed.

I also see that you apparently already have the skybox working, which confuses me, but I guess it works. :P

Edited by MrHappyFace
Link to post
Share on other sites

wait wait, so KSP's starmap skybox is not really a skybox, but a sphere mesh object attached to the game camera???? by real skybox I mean the cubemap in Unity's lighting panel... that's what PBR shaders need to work properly. I guess that explains why PBR generally looks poor in KSP in it's current state.

Edited by nli2work
Link to post
Share on other sites
12 hours ago, nli2work said:

http://forum.kerbalspaceprogram.com/index.php?/topic/84273-unity-layers-and-tags-constantly-updated/ Maybe this can help figuring out which layers to reflect and which ones not.

That's some useful informations, thanks! I does not solve the problem of the terrain's reflection, but now I can prevent it to render the navball, the IVA view, and ship's parts (this also means close ships won't be reflected...)

The reflection probes will use layer 9 (atmosphere), 10 (Scaled scenery), 15 (local scenery), and 23 (not in the link, but it's used for distant celestial bodies) . Also not in the link, the navball is on layer 25. It's a bit better now, if it wasn't for the terrain and flipped faces glitches.

 

I made a quick search for the flipped faces, Surprinsingly it's a common problem. I'll probably have to create the cubemap from a camera, and get rid of reflection probes (the idea is to render each face, then flip the two wrong faces, it can't be done with probes).

This will look a lot like TR's visors reflections function... In fact, since the shader loader is almost the same too, this plugin might become a fork of TR at some point.

5 minutes ago, MrHappyFace said:

@nli2work, the terrain is on layer 15, the atmosphere is on layer 9, the scaled planets (bodies viewed from afar, such as the mun in the sky) are on layer 10, and the skybox is on layer 18, and is a hollow sphere at the origin with a camera in the center for rendering the skybox. There's also layers 19, 23, and 30 which are for disconnected parts, the sun, and SurfaceFX (liftoff/landing particles, etc.) respectively.

I'm not sure if reflection probes would world with the skybox, but they'd probably work with the atmosphere/scaled/sun layers, and they'd definitely work with the terrain/surfaceFX/disconnected-parts layers.

That adds layer 18 to the current culling mask, thanks. Maybe I can find this skybox camera and render the cubemap to a rendertexture, then assign it as a skybox of the probe's camera? I'll see how it turns out.

Link to post
Share on other sites

All this makes me more convinced the real issue isn't reflection probes, but KSP's environment and light setup. 

Reflection probes are really meant to be part of the game level instead of attached to moving objects. When moving objects are withing the RP's bounds box, they get the RP's cube map applied to their shader so they appear to reflect the immediate environment and other objects in the same RP bounds.

Maybe a better way to approach all this is to update the RP once per KSP reference frame change? and set the bounds to contain the physics bubble, something like 3000? you'd only need 1 RP for all loaded vessels; and should be much better performing than 1 RP per vessel.

Edited by nli2work
Link to post
Share on other sites
27 minutes ago, nli2work said:

wait wait, so KSP's starmap skybox is not really a skybox, but a sphere mesh object attached to the game camera? by real skybox I mean the cubemap in Unity's lighting panel... that's what PBR shaders need to work properly. I guess that explains why PBR generally looks poor in KSP in it's current state.

As far as I know, there are actually 4 cameras, all of which are composited onto the screen to get the final output:

  1. Local scenery camera which can see the terrain and ships. The KSC and easter eggs are on the same layer as the terrain, so they are also seen by this camera.
  2. Another local scenery camera. Not sure what the difference between the two local cameras is, but they're both essential. Both local cameras usually orbit the ship's center of mass, and move around the scene.
  3. Scaledspace camera, which can see atmospheres, far-away bodies and the sun. This camera is not tied to the local camera at all, but instead moves around in scaled space.
  4. Galaxy camera, which can see the skybox. This camera is stationary and is surrounded by a hollow cube with the skybox painted on.

Most mods I've seen that add their own cameras (BDArmory, Cacteye, Tarsier) just make carbon-copies of these cameras and slap them together into a working camera setup.

I'm not exactly sure on how the reflection probe works, but it seems to just be a single camera rendering a cubemap and passing that cubemap to all renderers within it's bounds. Perhaps you could replicate this effect by rendering 4 cubemaps from your own camera setup, compositing that into one cubemap, then passing it manually to every part's material within the bounds of the fake probe.

I have some code sitting around from an old project where I tried to make a telescope mod, it creates the cameras from scratch and composites them into the single RenderTexture, but it also does some trickery to allow it to support lens flares, atmospheres viewed from custom angles (atmospheres look glitchy otherwise), and some other things I can't remember that otherwise wouldn't be possible with a custom camera. I'll see if I can upload some of that later tonight.

Edited by MrHappyFace
grammar is fun
Link to post
Share on other sites

I was trying to clarify the difference between a mesh and skybox and figure out what KSP is really doing. You can take a flipped box or sphere, apply a cube map material to it, center it to a camera, and it would look exactly the same as a skybox material assigned in the lighting panel in unity 5. However PBR shaders will only react to the skybox material from lighting panel and look correct. Of course you can add a reflection probe to force the PBR shader to reflect the mesh geometry with cube map material, but that's a really expensive way fake something that is normally done already with a proper lighting setup.

 

With the current way KSP is setup it looks like there's a real skybox that's the Galaxy cube map. Explains why metal PBR is pitch back, because that skybox is mostly black. This means there two ways to make PBR shaders work properly in game...
1. Fade clear flag from color to skybox as vessel transitions from surface to orbit, same time when KSP fades in the real skybox. Probably the easier way.
2. add a skybox while on surface, and fade to galaxy skybox when transitioning to orbit. Maybe harder, but would make surface look much nicer, and should be more accurate as far as vessel lighting goes.

Edited by nli2work
Link to post
Share on other sites
13 minutes ago, nli2work said:

I was trying to clarify the difference between a mesh and skybox. You can take a box or sphere, apply a cube map material to it, attach it to a camera, and it would look exactly the same as a skybox material assigned in the lighting panel in unity 5. However PBR shaders will only react to the skybox material from lighting panel and look correct. Of course you can add a reflection probe to force the PBR shader to reflect the mesh geometry with cube map material, and that's a really expensive way to get PBR shaders to look correct.

Well given the complexity of KSP's flight scene, I'm not sure there's any other way to get it to look good without using a custom reflection cubemap or reflection probe.

You can set a custom reflection map by setting RenderSettings.customReflection to a cubemap composited from the method I posted above so that the standard shader can react to something other than the default skybox or the inaccurate results of reflection probes.

Edit: Alternatively, use a custom shader and have your own cubemap property in the shader so you can assign the reflection cubemap per part instead of globally to the whole scene.

Edited by MrHappyFace
Link to post
Share on other sites

Yeah I agree... I imagine it'd be very difficult to get a reflection probe cubemap that combines all the different flight scene camera views properly.

as far as reflection probe, only 1 is really necessary, placed at the active vessel COM, near clip plane large enough to exclude half the vessel if not all of it... this would produce most accurate reflection for most instances. 

Edit: the Lilleman/Specular shaders should still read (A) from the Diffuse map to control Shininess I think.

Edited by nli2work
Link to post
Share on other sites
21 hours ago, MrHappyFace said:

I have some code sitting around from an old project where I tried to make a telescope mod, it creates the cameras from scratch and composites them into the single RenderTexture, but it also does some trickery to allow it to support lens flares, atmospheres viewed from custom angles (atmospheres look glitchy otherwise), and some other things I can't remember that otherwise wouldn't be possible with a custom camera. I'll see if I can upload some of that later tonight.

Thanks, I'm looking for this kind of snippets right now. I'm getting better with Unity but I still have a lot to learn about KSP scenes.

 

20 hours ago, nli2work said:

the Lilleman/Specular shaders should still read (A) from the Diffuse map to control Shininess I think.

I made a quick modification on specular shaders Forgot to include them in the last build, but I'll do it for next version. You can extract the compiled versions to the mod if they work as you expect (instead of being "globalShininess * Specularcolor", the specular output is now "diffuse.alpha * SpecularColor").

https://www.dropbox.com/s/si0df9kd4glirfc/specularshaders.zip?dl=0

 

Small update, so it does not get a real version incrementation.

V0.031 changelog :

-Changed the reflection probe's culling mask to hide vessel reflection

-Fixed the background color value from config file (and changed it to black by default)

 

Download : https://www.dropbox.com/s/0t3vq8uo7gs5d5p/PBRShaderLoader_v0.031.zip?dl=0

Sources : https://www.dropbox.com/s/jmfvc662b56sxll/PBRShaderLoader_v0.031_src.zip?dl=0

Link to post
Share on other sites

the culling of self-reflection is definitely working! there's some kind of switch going on ... on normal speed, I only get reflection of planets; on time warp, I only get reflection of skybox. in the video you can see the reflection switching between Kerbin and Skybox when I switch from normal time to timewarp.

 

Edited by nli2work
Link to post
Share on other sites

The GlobalBrightness/Saturation values in the INI is for reflections only yes? 

In the specular shader it's blowing up the albedo, I think it's because of this line? Albedo should remain the same and not get multiplied by the reflection brightness I think. Haven't checked diffuse shader sources yet. 
o.Albedo = pow(c.rgb, _ColorSaturation) * _GlobalBrightness;

How do you compile the shaders?

PS: the loader doesn't reload shaders with database reload in game using F12 or ModuleManager F11; so you have to restart the game to see any changes after editing mask texture.

Edited by nli2work
Link to post
Share on other sites

GlobalBrightness and saturation should affect only albedo, not reflections. In the original standard shader this line is just "o.Albedo = c.rgb;". It's not really color saturation, either. more of a contrast.

And the the textures tends to become dark with this, hence the GlobalBrightness value. The default in the config file should be "1.0" for these two variables.

 

To compile a shader, import it in Unity, select it, and you should have "Compile and show code" in the inspector. Click on the arrow next to it to select only the platforms we need (OpenGl & DirectX9/11/11_9x), and compile. The compiled shader will be saved in your project directory, in the "temp" folder.

I'll add a way to reload masks at runtime. I also need to make a small interface to set the reflection probe's properties, instead of the config file.

 

 

Edited by Lilleman
Link to post
Share on other sites

I got around to uploading the camera code I was talking about earlier.

https://gist.github.com/HappyFaceIndustries/d997e268d38bd638881e4033606216f9

The rendering takes place in the RenderCameras method, you can change the Render function to RenderToCubemap to get Cubemaps instead of RenderTextures. The skybox exposure stuff is just to dim the skybox because this was a telescope mod, and the skybox is really bright if you zoom in too far, but I can't imagine reflections would zoom in, so it's not really needed. The UpdateVisibleObjects function is also not needed because it just makes far-away planets visible to the camera.

I haven't tested this in 1.1.x yet, but I don't see any reason it wouldn't work with little to no modification. You may need to reference Assembly-CSharp-firstpass.dll for it to work.

Edited by MrHappyFace
Link to post
Share on other sites

does't solve the problem that there just isn't a whole lot to reflect while on a planets surface... especially one with atmosphere. Squad's got their work cut out for them.

Edited by nli2work
Link to post
Share on other sites
This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...