3D ray tracing using raycast

@Ignatz - nice to see you are still following the forum - one of the best supporters of Codea and my first go to (your website) for ideas on how to deal with coding it. :+1:

1 Like

@Ignatz yes, really nice to read you again after so long! Gives me the opportunity to thank you for all your contributions in the early Codea days.

1 Like

What a thrill to get a note from you @ignatz! The stuff you guys did with shaders back in the day amazes me to this day. Thanks for all that. I’ve still got lots of your projects in my main documents folder and I go back and dig through them to remember how to do various tricks. Thanks again!

1 Like

Actually, I haven’t been following the forum, but I was notified of the current thread by email.

Thank you all for your kind words, but I can assure you I’m nothing special. I’d been programming before (mainly in Excel VBA) but never computer graphics, and I could never master C. So I found learning Codea quite hard, but I loved the language and what Codea could do. The rest was hard work, plus assistance from a number of fellow members. While I enjoyed making shaders because they were so powerful, I couldn’t do anything too complicated because I struggled with C. The most challenging and fascinating was quaternions, magical creatures that did wonderful things with rotations and made flying simulations possible. I never understood them though.

The one good thing I did was to document my learning through a series of blogs and sample projects, because I have always enjoyed discovering stuff and sharing it. I would probably have continued, but after three years, I seemed to have explored most of what I could do, and nobody seemed to want to collaborate on a project, and I didn’t think my blogs were getting read, so I moved on to something completely different.

The point I’d like to make in all this is that you shouldn’t be impressed by anything I did, except my hundreds of hours of hard work and determination. I am unlikely to be smarter than you, so if you want to develop good skills, you can achieve it and do everything I did and more.

And for the younger ones among you, curiosity is invaluable. It led me to poke in all sorts of corners of Codea to see what was there, and to find out how other people did cool stuff. And curiosity helped me throughout my career (I am retired), leading me to find and learn stuff that nobody else bothered with, and always try to improve jobs no matter how boring they were, so I became the office expert and was treated extremely well. I learned that if several other people can do your job, you are expendable, but if you are THE person for something, you are special. Also, the person with greatest skills is given the new and exciting work, while the others get to do keep doing the boring regular work. I was fortunate because I am naturally curious, but the rest was just hard work. So my advice is - in everything you do, always be questioning, never let your brain go to sleep!

Anyway, I thoroughly enjoyed my time with Codea and I hope you do too. And if you still can’t figure out that 3D trick, start with the basics of shaders and work up from there. Like a staircase, knowledge has to be built step by step.

All the very best to you all
Ignatz

5 Likes

@John , you say @ignatz’s method could be used in Craft, but isn’t there still a Craft issue of sub-meshes that can’t be accessed by shaders?

Models without submeshes could use the ignatz method, it seems, but would it also work on the Craft models that do have submeshes?

Had some time over xmas….introduced physics into the 3d ray tracing. This stops overlapping bodies. The emitters now generate photons which travel along the ray path. The photon speed takes into account the slowing down due to the refractive indices of the objects it passes through. If the ray hits a sensor it will count the received photons.

Can also interactively scale and rotate the 3d objects.

@LoopSpace the mesh for a sphereSegment seems to be have an internal ‘border’. I guess when a hemisphere is defined the two ‘ends’ do not get joined.

@LoopSpace there is a thing i would like to do with an 3D ellipsoid! Any chance you can add that object to the MeshExt library!

You can make an ellipsoid by passing an axes parameter when defining the sphere. These aren’t normalised so if they are of different lengths they will distort the sphere into an ellipsoid. So something like:

m:addSphere({axes = {vec3(1,0,0), vec3(0,1,0), vec3(0,0,3)} })

This is better than simply scaling the mesh since it ensures that the normals are correctly computed.

I don’t understand what you mean about the sphere segment, can you clarify for me?

@LoopSpace the ellipsoid works great-you thought of everything! See video below.

Also in this video you can the hemisphere made using sphereSegment. You will see there is a ‘internal plane’ inside the mesh-that is what i was refering to.

1 Like

Ah, I see what you mean. It’s because there’s no test for having the segment go all the way round in the longitude direction. I’ve added a test to my code, just need to upload it somewhere - where are you getting my code from? Github, or somewhere else?

An alternative fix is to swap longitude and lattitude, so use:

startLatitude=0,
endLatitude=180,
startLongitude=0,
endLongitude=180,

@LoopSpace maybe you could load it to the Codea WebRepro 2.0?
The alternative fix works fine-thanks.

I’ve just tried uploading it to WebRepo, it’s waiting for admin approval. Fingers crossed!

@LoopSpace Hey, sorry about the delay in approving it!

I’ve approved this version but I’m having some trouble with the update button due to this function:

function updateCode()
    local tabs = {
        "Library Graphics:MeshExt",
        "Library Graphics:PseudoMesh",
        "Library Maths:VecExt",
        "Library Base:ColourExt"
    }
    local code, tab
    for k,v in pairs(tabs) do
        print(string.format("Updating %s", v))
        code = readProjectTab(v)
        tab = v:gsub(".*:","")
        saveProjectTab(tab,code)
        print(string.format("Updated %s", v))
    end
end

I can see what you’re doing and honestly I think it’s a nice idea for development :slight_smile:
Sadly, if I install from WebRepo, these projects don’t exist so this throws an error if I tap the update button.

If/when you upload another version could I ask you to comment that button out for submission to avoid other users potentially thinking it should do something for them?

Thanks!

My intention wasn’t that this be a project that anyone actually run, it’s a way of sharing code for use in other projects, so I wasn’t thinking too much about what went in Main. But it nevertheless shouldn’t be something confusing, so I’ll make it so that it at least doesn’t crash!

What I ought to do – and will do if I get the time – is provide some demo code that shows off the capabilities of the various bits of code.

1 Like

I only saw the first few lines of your comment on a forum email and thought exactly the same with the usage demo.

They can be super helpful just to have as reference in the Main tab.

Semi-related but for future reference if you upload a project with dependencies to WebRepo, the dependencies should be included automatically. You can see it in your project already,_dep_Craft:Touches.

@LoopSpace are you sure the normals are correct when using different axes vectors with the addSphere to form an ellipsoid? I wanted to demonstrate that light emitted from one foci will be reflected to the other foci-but it doesn’t seem to work! Either the normals are wrong or i have a bug in my code (probably the latter!).

No, not sure at all! I may have not thought of distorting the axes so just assumed it would be spherical.

I’ll take a look. It’s an easy fix if it’s wrong.

@LoopSpace maybe you can help me….i use direction vectors for the raytracing reflection and refraction, this allows me to use the vector form of the Snell’s laws.

I want to able to define the direction of a ray depending on the rotation of a Codea entity, as defined by its .rotation or .eulerAngle properties. Do you have method (perhaps in vecExt) which allows to extract the corresponding direction vector from the entity orientation?

p.s. did you get a chance to look into the normals of the ellipsoid?

@LoopSpace i figured it out using the extendQuat() routines….

dir=vec3(0,1,0)^entity.rotation

simple with your code!

1 Like

Yay! Glad you got yourself sorted out.

I’ve just uploaded an update to the WebRepo that, I hope, contains a fix for the ellipse normals. Please test!