21

It was a shock to me when I found that in Graphics3D a surface is always transparent to the light sources:

lightSources = {{"Directional", Red, Scaled[{1/2, 1, 1}]}, 
                {"Directional", Green, Scaled[{1, 1/2, 1}]}, 
                {"Directional", Blue, Scaled[{0, 0, 0}]}};
Show[Plot3D[Sin[x y], {x, 0, Pi}, {y, 0, Pi}, 
  Lighting -> lightSources], 
 Graphics3D[{PointSize[Large], 
   Point[lightSources[[All, 3]], 
    VertexColors -> lightSources[[All, 2]]]}]]

plot

One can see the surface on the right is blue-illumunated although this surface is completely shaded from the blue light source located at the point {0,0,-1}.

I feel such rendering is unacceptable. Is it possible to produce shadows in Mathematica?

UPDATE

To clear up: by "shadows" I mean that the surfaces on the scene must not be transparent to the light sources (or, better, their transparency could be defined by a user). I need not drop shadows shown in the linked threads: (1), (2).

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • 6
  • 1
    If it does have to be in Mathematica, you might try exporting to POV-Ray: Export["file.pov", plot] (Untested: POV-Ray has issues with Intel Macs) – Michael E2 Jan 14 '13 at 13:55
  • @Michael I just tried exporting to the "POV" format of the plot in the question. I did not worked with POV-Ray before and just opened the exported file in it and pressed the "Run" button. This is what I have got as the output. Is it possible to get something better using POV-Ray? (I am working under Windows). – Alexey Popkov Jan 14 '13 at 14:23
  • Possible duplicate: http://mathematica.stackexchange.com/questions/938/how-to-make-a-drop-shadow-for-graphics3d-objects and http://stackoverflow.com/questions/6955692/shadows-in-mathematica-graphics3d. There is a big difference between surface rendering and rendering drop shadows. The former needs raytracing whereas the latter can be done by the answers given in the referenced questions. If your question is only about the latter it certainly is a duplicate. – Sjoerd C. de Vries Jan 14 '13 at 14:35
  • 1
    @Alexey I think you'll have to spend some time learning about POV-Ray and add the light sources and surface characteristics (material) yourself. Or perhaps you can try to export to some other 3D format and try and easier to use renderer. Maybe try http://www.kerkythea.net I used it once but not with Mathematica. – Szabolcs Jan 14 '13 at 15:52
  • 1
    @AlexeyPopkov That's certainly disappointing. I was just typing something like what Szaboics said, when his comment showed up. Mathematica apparently does not export the lighting and surface characteristics. – Michael E2 Jan 14 '13 at 15:55
  • 1
    @Sjoerd I need surface rendering but with non-transparent surfaces for the light sources. I neen not drop shadows shown in the questions you referenced. – Alexey Popkov Jan 15 '13 at 05:39
  • 1
    Modern graphics cards can do real-time shadows to one degree or another as evidenced by a variety of games if nothing else. This really should be possible from Mathematica, but alas the option may be absent. – Mr.Wizard Jan 15 '13 at 08:23
  • @Mr.Wizard quite often this involves trickery of some kind. Those shadows are not really ray traced, do not interact with object in between and or are not anti-aliased as the rest of the scene. – Sjoerd C. de Vries Jan 16 '13 at 06:41
  • 1
    @Sjoerd I don't know a lot about it. I am aware that many tricks are used to simplify rendering of a complex scene at 30-60FPS but from various demos I've seen I think reasonably high fidelity shadows are possible from the hardware. Certainly simple light source occlusion I think. This should be practical as a high frame rate is not necessary. I'll see if I can find a video of the kind of demonstration I'm recalling. – Mr.Wizard Jan 16 '13 at 08:13
  • @Sjoerd here is a rather poor quality video but it is a point of reference. I am trusting that is an actual graphics accelerator render as I did not run a demo myself, but I seem to recall running something similar when I got a DirectX 10 card. Would not that kind of shadow rendering, anti-aliasing or not, be a nice addition to Mathematica graphics? – Mr.Wizard Jan 16 '13 at 08:23
  • @Sjoerd And here is an executable demo that includes realistic real-time shadows. I cannot vouch for the safety of this file so I recommend that you run it in Sandboxie. Clearly Mathematica graphics are not taking full advantage of modern graphics cards. – Mr.Wizard Jan 16 '13 at 08:38

2 Answers2

5

Im sorry i have a bit limited time at the moment so the answer will need to be revisited later.

While its true that the hardware of your computer could do this, and it could be included in by wolfram making shadows is usually considered a next step in rendering. I am not dwelling on why this doesn't work, as you can use any of the existing free raytracers out there. There are plenty heres a (really) partial list (by preference and suitability in this case, path of least resistance):

Highend stuff

Midend

  • pixie R free
  • Aqsis R free
  • yafaray free
  • ... lots and lots of other software and hardware renderers out there that can easily do the job

There are a lot more. The ones marked with R conform to the RI spec making it easy to swap between the systems. On top of that Mathematica supports RIB export. It also brings your pipeline quite a bit o future proof that no other solution would give.

Note, I think POV ray is not a suitable tool by todays standard.

Exporting the geometry to rib should be as simple as:

Export["s:\\temp\\test.rib", Plot3D[Sin[x y], {x, 0, Pi}, {y, 0, Pi}]]

over this you need to append the lights and shaders. Now the RIB file at this point is unrenderable. Because its missing

  • Camera
  • Materials
  • Lights
  • World

Lets add those. A suitable camera environment would be ( Im using 3Delight so the notation may differ very slightly for auto shadows feature)

#this is rib
PixelSamples 3 3
PixelFilter "sinc" 2 2
ShadingRate 1

Display "output.tif" "tiff" "rgb"

Format 400 400 1
Projection "perspective" "fov" 20

Rotate 180 0 1 0

Translate 2.5 -0.5 -20
Rotate -50 1 0 0
Rotate 118 0 0 1

After this we need the world where the object resides:

 #still rib
 WorldBegin
 ... items in world...
 WorldEnd 

inside this world each light needs something like this:

#rib also beween World
AttributeBegin 
    Translate 0 0 -1
    Color   0 0 1
    Surface "constant" 
    Sphere 0.1 -0.1  0.1 360.0
    Attribute "light" "shadows" "on"
    LightSource "pointlight" "l1" 
    "intensity" 5 
    "lightcolor" [0 0 1]
AttributeEnd 
Illuminate "l1" 1

and finally the object which we can include test.rib:

#still rib
Attribute "visibility" "int transmission" [1]
Surface "matte" 
ReadArchive "test.rib"

so far

Image 1: This is whats being produced. Note scene contains a weak extra fill light

Thats it now to write this into a function in Mathematica. But again i got to go

joojaa
  • 780
  • 7
  • 14
  • I was not able to reproduce the image: it still gives a lot of identical errors: 3DL WARNING P1037: camera to screen matrix too general, reverting to identity, 3DL ERROR P1051: [d:/Desktop/test.rib:7]: invalid context for 'RiPointsPolygons'. It would help if you post somewhere a complete working .rib file with comments (for example, on PasteBin). – Alexey Popkov Jun 29 '14 at 16:13
  • 1
    @AlexeyPopkov Sure http://pastebin.com/4Lmnbk7T you need the test rib generated in the same folder. For heavens sake dont run anything from desktop. Let me know if you have problems. – joojaa Jun 29 '14 at 16:20
  • With this file the scene renders without errors. But I am not sure if the projection used by 3Delight is the same which Mathematica uses? Looks like 3Delight looses the perspective. – Alexey Popkov Jun 29 '14 at 19:30
  • @AlexeyPopkov its not because i havent had time searching for the matrix plot uses. I just quikly eyballed the result. you can change the focal view to any angle now its 20 degrees. But im not really good with all mathematica functions. Much better at rendering stuff. – joojaa Jun 29 '14 at 19:40
  • It is not clear how to change the view angle and the distance from the object consistently. Probably I should multiply numbers in the Translate 2.5 -0.5 -12 line by a constant when I lower the view angle (the line Projection "perspective" "fov" 20)? – Alexey Popkov Jun 29 '14 at 19:55
0

Try changing "Directional" to "Point" in your definition of the lightSources. If I understand your question that solves the problem.

N.J.Evans
  • 5,093
  • 19
  • 25
  • 2
    That puts the lights in the right positions, but the problem that "the surface on the right is blue-illuminated although this surface is completely shaded from the blue light source" remains. –  May 25 '14 at 19:35
  • It sure is. What a pain. – N.J.Evans May 25 '14 at 19:58