[Info] Ambient Occlusion -> Simple Rays

[Info] Ambient Occlusion -> Simple Rays

Postby qpik » Sat Aug 08, 2009 12:04 pm

This is a crude version of a raycasting plugin that realizes a very straightforward approach to adding soft shadows inside SketchUp. Since I'm no professional coder but an architect at this point it's very slow and is useless for complicated models. It takes two parameters - Precision, which determines probing resolution according to grid = model.bounds.diagonal / precision and Rays which denotes number of rays cast from each point of grid.

 Download it here! 

Here is an explanatory image. Shading took 2-3 seconds.

simple_rays.jpg

The plugin works like this:
1. Extract faces from selection and compute each of them
2. Make grid of points and compute each - lightmap method extending Sketchup::Face class
3. Cast an array of rays from a point invoking model.raytest method, calculate exposure translate to r, g, b values - exposure method extending Array class
4. Write 24-bit BMP texture for each face in a folder "filename_occlusion" along your SketchUp file - Bitmap class
5. Position textures on faces

I would appreciate any comments on how I can speed it up and whether this plugin might be of any use.

Version history:
Mon Aug 10, 2009 Modified ray casting algorithm - more even ray casting, resulting also in approx. 2 times more rays cast
Mon Aug 10, 2009 Modified lightmap algorithm - shading time reduced approx. 2 times by reducing number of occlusion calls, added Bitmap.expand method to do the same task, deleted some unused code
Tue Aug 11, 2009 Added Bitmap.blur - texture smoothing
Tue Aug 11, 2009 Added progress info, removed unused parts of code
Wed Aug 12, 2009 Removed Save panel, instead when model is not saved "temp_occlusion" folder is created in the Plugins folder, added Mac compatibility
Wed Nov 25, 2009 Added Sun Exposure Time mode and changed the name of the plugin, rewritten many parts of code, making it a bit faster.


NOTE -

See Dan Rathbun's Qpik::SimpleRays Tools
1
Last edited by qpik on Tue Nov 18, 2014 7:03 pm, edited 26 times in total.

qpik 
 

Re: [Plugin] Ambient Occlusion

Postby Dave R » Sat Aug 08, 2009 12:44 pm

Kuba, this is a very interesting plugin from the description. I've tried using it with a model much like the one in your example. I don't get any change, though. Hopefully some of the Ruby gurus will be along to answer your questions.
0
Inspecting mirrors is a job I could easily see myself doing.

If you are the smartest person in the room, you are in the wrong room.
User avatar
Dave R 
Global Moderator
 

Re: [Plugin] Ambient Occlusion

Postby Daniel S » Sat Aug 08, 2009 1:07 pm

Not working for me too.
This is what the ruby console says:

Error: #<NoMethodError: undefined method `+' for nil:NilClass>
C:/Archivos de programa/Google/Google SketchUp 7/Plugins/occlusion.rb:403:in `ambient_occlusion'
C:/Archivos de programa/Google/Google SketchUp 7/Plugins/occlusion.rb:435
C:/Archivos de programa/Google/Google SketchUp 7/Plugins/occlusion.rb:434:in `call'

Daniel S

Ps. I don´t know if it´s related... but will be possible to create faces for simulating shadows viewtopic.php?f=180&t=20396&p=170766 ?
0

Daniel S 
 

Re: [Plugin] Ambient Occlusion

Postby qpik » Sat Aug 08, 2009 1:26 pm

Dave R
I don't know what causes the problem. The plugin works only with faces front side only. Did you see any new material appear in Material window after running the plugin?

Daniel S
It's a problem with SketchUp file path. Are you using Windows or Mac SketchUp version?
I'm sure that ray casting algorithm can be modified to include sun position.

Kuba
0

qpik 
 

Re: [Plugin] Ambient Occlusion

Postby Dave R » Sat Aug 08, 2009 1:42 pm

Kuba, one thing I am very careful about in my SketchUp models is that face orientation is correct. There are only front faces exposed. Here's what I get from the console.

No. there are no new materials generated.

Error: #<NoMethodError: undefined method `+' for nil:NilClass>
C:/Program Files/Google/Google SketchUp 7/Plugins/occlusion.rb:403:in `ambient_occlusion'
C:/Program Files/Google/Google SketchUp 7/Plugins/occlusion.rb:435
C:/Program Files/Google/Google SketchUp 7/Plugins/occlusion.rb:434:in `call'

It's the same thing Daniel got.
0
Inspecting mirrors is a job I could easily see myself doing.

If you are the smartest person in the room, you are in the wrong room.
User avatar
Dave R 
Global Moderator
 

Re: [Plugin] Ambient Occlusion

Postby qpik » Sat Aug 08, 2009 2:13 pm

Ok. The problem is obvious.
I worked on an already saved model. I got the same error when working on a new model.
The plugin looked for the path via model.path method and returned nil since it couldn't find it. Maybe better solution would be to create a temporary folder in SketchUp/Plugins and then delete it after all the textures are inside the model.

Kuba
0

qpik 
 

Re: [Plugin] Ambient Occlusion

Postby Dave R » Sat Aug 08, 2009 2:23 pm

Saving the file before hand does make it work. The file doesn't need to be saved in the Plugins directory, either. You might want to add a note about that in your first post.
0
Inspecting mirrors is a job I could easily see myself doing.

If you are the smartest person in the room, you are in the wrong room.
User avatar
Dave R 
Global Moderator
 

Re: [Plugin] Ambient Occlusion

Postby Dave R » Sat Aug 08, 2009 2:34 pm

Here's an example. This was with the values set at 50 & 50. Maybe about 30 seconds or so. Probably something set too high, though.
AO Test1.jpg


Is there any way to control the angle or direction of the shadows?
0
Inspecting mirrors is a job I could easily see myself doing.

If you are the smartest person in the room, you are in the wrong room.
User avatar
Dave R 
Global Moderator
 

Re: [Plugin] Ambient Occlusion

Postby qpik » Sat Aug 08, 2009 2:51 pm

Dave R
Nice image. Thanks for posting
I have to work on blurring and smoothing the shadows as well as on casting rays.


I've re-uploaded occlusion.rb. (don't know if this is a good way of posting rewritten code)

Now the plugin will ask you to save the file so you don't have to worry about it.

Kuba
0

qpik 
 

Re: [Plugin] Ambient Occlusion

Postby Daniel S » Sat Aug 08, 2009 3:01 pm

Kuba

Saving the model before running the plugin works very well.
I think that will be better to make faces simulating shadows instead of writing textures. The result will not be fantastic, but you can then change the color of shadows or change the opacity, or use a texture...
Anyway I like a lot your plugin
Thank you for sharing it with us!!!.

Daniel S
0

Daniel S 
 

Re: [Plugin] Ambient Occlusion

Postby Dave R » Sat Aug 08, 2009 3:08 pm

Thanks, Kuba.

As to posting updates, yes, you should replace the file in your original post. You should also make a note of the version number and date of update in the title to make it easier for folks to tell there has been an update.

Thanks for making the change.
0
Inspecting mirrors is a job I could easily see myself doing.

If you are the smartest person in the room, you are in the wrong room.
User avatar
Dave R 
Global Moderator
 

Re: [Plugin] Ambient Occlusion

Postby qpik » Sat Aug 08, 2009 3:09 pm

Daniel S
I thought about it first. Before I learned reading/writing BMP files.
But my goal was to render soft shadows.
I can only imagine creating hundreds of tiny faces to simulate smooth color transition.

If you have better idea I could implement it.

Kuba
0

qpik 
 

Re: [Plugin] Ambient Occlusion

Postby Daniel S » Sat Aug 08, 2009 3:55 pm

For soft shadows your method is better, but with faces and a lot of offsets you can make a good transition.

Here i attach a very quick example (with not very good result, but is to explain the idea).. it will have better results if you make more faces and with not big changes on the value of opacity between each face.

Daniel S
0

Daniel S 
 

Re: [Plugin] Ambient Occlusion

Postby Pixero » Sat Aug 08, 2009 5:55 pm

Very interesting. :thumb:

Do you have a "max distance" when calculating occlusion?
That would also help the speed a bit I think since it wouldnt need to continue calculating rays that doesnt hit anything within the given max distance.

Also I think it would be great to have an option for it to discard materials or include/blend with the materials/textures in the scene.

Great job so far!
0
User avatar
Pixero 
Premium Member
Premium Member
 

Re: [Plugin] Ambient Occlusion

Postby xrok1 » Sun Aug 09, 2009 7:31 pm

its cool thats its actually "baking" the AO onto the faces :D . now if it could overlay the baked AO onto the existing material instead of replacing it :ecstatic:
0
“There are three classes of people: those who see. Those who see when they are shown. Those who do not see.”

http://www.Twilightrender.com try it!

xrok1 
 

Re: [Plugin] Ambient Occlusion

Postby qpik » Sun Aug 09, 2009 11:29 pm

Daniel S
I will think about your idea. I would have to trace lightmap for "isolines" of equal brightness.

Pixero
But it would have to check for each ray this max_distance condition, am I right? Rays here don't bounce, they check only if they hit nothing/background.

xrok1
I planned this as next stage of developement for it seems quite complicated to me. The simplest way would be to create faces offset by small distance from the original textured face (the same way Daniel S overlays shadow in his model). But then brightness would have to be translated to opacity, which requires PNG format which I'm not familiar with (yet). The other way would be to merge shadow texture with existing one aquired by something similar to 'Make unique texture'.

Kuba
0

qpik 
 

Re: [Plugin] Ambient Occlusion

Postby alexschreyer » Mon Aug 10, 2009 4:59 am

qpic, this is an excellent plugin. Keep working at it. I am looking forward to the finished version.

I like the idea of merging both the occlusion and the original textures. Maybe it is possible to do that on a pixel-by-pixel basis for each texture. I don't know about standard ruby methods for this, but it may be as trivial as subtracting brightness values.
0
Author of "Architectural Design with SketchUp":
http://sketchupfordesign.com/
User avatar
alexschreyer 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Ambient Occlusion

Postby Pixero » Mon Aug 10, 2009 8:03 am

qpik wrote:But it would have to check for each ray this max_distance condition, am I right? Rays here don't bounce, they check only if they hit nothing/background.


If it doesnt hit anything within the given max distance it presumes the surface isnt occluded and moves on. That way you also can control how much spread the occlusion should have. Meaning you could set a low value and just get occlusion in tight corners. Can be nice to have this also.
0
User avatar
Pixero 
Premium Member
Premium Member
 

Re: [Plugin] Ambient Occlusion

Postby pep75 » Mon Aug 10, 2009 9:16 am

...even that it is still in a beta stage, I would really like to stress that the approach is brilliant!! Great work Qpik!!!
0
User avatar
pep75 
 

Re: [Plugin] Ambient Occlusion

Postby qpik » Mon Aug 10, 2009 12:47 pm

Pixero
So you mean to cut down on time by reducing single ray casting procedure, not number of them? That means that instead of using existing model.raytest method I would have to write my own. I'll think about it.
About ambient occlusion method. As i know it from Wikipedia it's based on casting rays from point evenly over a "hemisphere". I plan adding narrowing that angle, but combined with changing it's direction will produce different effect, it will give soft sunlight (fixed direction) or point light (point.vector_to source direction) shadows, where the size of the light source (softness of shadows) will be determined by that angle.

Speeding up is crucial for this plugin. I haven't even tried it with complex geometry, because I expect it to run for ages. I don't wan't to add too much functionality before finding out weak points and rewriting it .
I need someone to point out those areas of code which slow it down the most. Maybe Ruby can't do it much faster.

Kuba

ps. About values for Resolution and Rays - do you think those default 10,10 are optimal?
0

qpik 
 

Re: [Plugin] Ambient Occlusion - Twice faster

Postby majid » Tue Aug 11, 2009 5:41 am

just to thank you for such a plugin, it would be handy if progress.
0
User avatar
majid 
Top SketchUcator
 

Re: [Plugin] Ambient Occlusion - Twice faster

Postby kwistenbiebel » Tue Aug 11, 2009 12:59 pm

This plugin is a great idea.
I would use the AO to overlay it on Vray renders.
Unfortunately I can't render the screen size 'as is' in Vray so I can't perfectly overlay them.
(I can't figure out how to use thomthom's ruby that is made to render the exact viewport using vray 1.00.74)
0

kwistenbiebel 
 

Re: [Plugin] Ambient Occlusion - Blur

Postby qpik » Tue Aug 11, 2009 1:10 pm

I have added Bitmap.blur method to smooth out textures. I helps to save on calculation time, since now fairly good result can be obtained setting Rays to 5.

Here is an image showing the difference. Both shaded using Precision 10, Rays 10, but right one uses blur 3 times.

blur.jpg

Does anyone have an idea how to replace model.raytest method with faster counterpart? Maybe there is a way to access OpenGL from Ruby?

Kuba

ps. Just a tip. I turn on 'Use sun for shading' and set both 'Dark' and 'Light' to 100 just to remove Sketchup shading.
0

qpik 
 

Re: [Plugin] Ambient Occlusion - Smoothing

Postby tbd » Tue Aug 11, 2009 1:39 pm

Kuba: nice work !

some suggestions:
* store directory and don't ask the user on each AO run for a directory
* make it Mac compatible - use Ruby functions instead of rindex for \\
* to speed up things you need to profile the code and see what part takes most of the time
* add some progress info - Sketchup.set_status_text with % will do
* there is no way to access OpenGL from Ruby (except hijacking it but that will slow down SU)
0
SketchUp Ruby Consultant | Podium 1.x developer
http://plugins.ro
User avatar
tbd 
 

Re: [Plugin] Ambient Occlusion - Smoothing

Postby qpik » Tue Aug 11, 2009 2:22 pm

TBD
Thank you for your suggestions. When the file is saved the script shouldn't ask for directory any more.
My biggest concern is model.raytest, it takes up to 90% of calculation time.
Can you tell me if you see any chance of speeding this plugin up? Is there a way to replace model.raytest? Is Ruby just to slow?


Another example showing different settings for different sizes of faces rendered separately.

example.jpg

Kuba
0

qpik 
 

Re: [Plugin] Ambient Occlusion - Smoothing

Postby tbd » Tue Aug 11, 2009 3:07 pm

qpik wrote:My biggest concern is model.raytest, it takes up to 90% of calculation time. Is there a way to replace model.raytest?


unfortunately no :(
the only way is to take the 3d data and do the ray testing yourself, but definitely not in Ruby as it is too slow.
0
SketchUp Ruby Consultant | Podium 1.x developer
http://plugins.ro
User avatar
tbd 
 

Re: [Plugin] Ambient Occlusion - Smoothing

Postby qpik » Tue Aug 11, 2009 6:12 pm

TBD wrote:unfortunately no :(
the only way is to take the 3d data and do the ray testing yourself, but definitely not in Ruby as it is too slow.


That is to say that this plugin has very little usability. It's just to slow. :(

TBD: It's probably a trivial question, but how do I extract file name from path on both Windows and Mac?

Anyway. I found that there is a strange thing with SketchUp resizing textures. Sometimes it's smother and sometimes it produces 'waves'. For example it gives nice result while using P:10, R:10 or P:25, R:10, but not with P:20, R:10 or P:50, R:10. (where P stands for Precision and R for Rays in AO settings)

Kuba
0

qpik 
 

Re: [Plugin] Ambient Occlusion - Smoothing

Postby tbd » Tue Aug 11, 2009 8:07 pm

qpik wrote:It's probably a trivial question, but how do I extract file name from path on both Windows and Mac?

Code: Select all
File.basename(Sketchup.active_model.path)


and if you want just the name, without the .skp extension:
Code: Select all
File.basename(Sketchup.active_model.path,".skp")


qpik wrote:That is to say that this plugin has very little usability. It's just too slow


if you add overlaying on textures (adding support for .jpg/.png textures is enough) then it can be a free alternative to LightUp 8-)
0
SketchUp Ruby Consultant | Podium 1.x developer
http://plugins.ro
User avatar
tbd 
 

Re: [Plugin] Ambient Occlusion - Smoothing

Postby DJMX1 » Wed Aug 12, 2009 9:29 am

Hello, I'm french this is a google translation.
This plugin is very promising, it interests me greatly, it will have on it manages colors, textures and shades diffuse projections in the future. But bravo, I am waiting for new version. :thumb:
0

DJMX1 
 

Re: [Plugin] Ambient Occlusion - Smoothing

Postby qpik » Wed Aug 12, 2009 2:05 pm

Thank you TBD. That saved me some time checking Ruby documentation :) I hope i fixed the bugs you pointed out in your first post. Let me know if it works on Mac.

Also thanks to all for motivating posts. I cannot resist though to see dead end for this plugin.
Has anyone tried it on a normal model which has more than 10 faces? How long does it take to render?

I still consider porting it to C++ (which I would have to learn :)) but it seems pointless, as it's much easier I think to add texture baking to Podium or Kerkythea (if it's not there yet).

Kuba
0

qpik 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 

Next


 

Return to Plugins

Who is online

Users browsing this forum: _domiomi, astro19930807, Google Bot, sokhet and 9 guests

Visit our sponsors: