This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Yevano's profile picture

Distance Field Raymarching in CC

Started by Yevano, 19 May 2015 - 11:11 PM
Yevano #1
Posted 20 May 2015 - 01:11 AM
Today I wrote some code that can render distance fields in ComputerCraft. It's neat, because you can basically render any conceivable 3D object you want as long as you can define the distance function for it.



If you can't tell, that's a red box with a green sphere protruding out the sides. It runs pretty darn smoothly. The image doesn't really do it justice though, so you should try it out for yourself!
http://pastebin.com/vW4mmKxt

I structured the code a lot like a GPU shader. There's a loop which goes through all the pixels, and a function which returns a color based on what pixel you ask it for. Next up, I'll try making more complex scenes, and maybe do some shadows. Reflection is also possible, but honestly I don't think that would look good at all at such a low resolution.

For those wondering about distance field rendering: http://iquilezles.org/www/articles/distfunctions/distfunctions.htm

Also, anybody can edit the code and do whatever with it. Just give me credit if you use a significant portion of the original code in some release of your own. Thanks.
Bomb Bloke #2
Posted 20 May 2015 - 02:30 AM
Heh, took me a moment to figure out how it was rotating, since the loop at the bottom doesn't have any obvious involvement in the process - pretty cool. :)/>

When rendering, consider counting the number of consecutive "pixels" of a colour you get back from your raymarching functions, and when you hit the end of the line (or get a pixel of a different colour, whichever happens first), string.rep'ing a single string of the appropriate length and term.writing that in one go.
Yevano #3
Posted 20 May 2015 - 02:31 AM
Heh, took me a moment to figure out how it was rotating, since the loop at the bottom doesn't have any obvious involvement in the process - pretty cool. :)/>

When rendering, consider counting the number of consecutive "pixels" of a colour you get back from your raymarching functions, and when you hit the end of the line (or get a pixel of a different colour, whichever happens first), string.rep'ing a single string of the appropriate length and term.writing that in one go.

Ah neat. Thanks for the tip. Going to bed for now but I will try that later.
DannySMc #4
Posted 20 May 2015 - 11:09 AM
This is awesome,

Would you pssibly be able to make this work via a monitor? As I would love to add this on my Server as the main screen? And even better if you could make a way of making it on the biggest sized monitor and then maybe make the word shinexus like move around? That would be awesome if possible? or even the logo? and make it 3D? I have no idea how this works but I would love if I could put this on my server at spawn? I will put your name under if you could please??
Yevano #5
Posted 20 May 2015 - 04:37 PM
Things like letters are technically possible, though I think the distance functions and input domain transformations required might be too heavy to be performant in CC. 2D distance functions require no iteration though so it could be possible to create a 2D logo if one were able to make distance functions for all the letters. It would be able to be scaled, rotated, stretched, etc.

I'd definitely be interested to see what this would look like on a large screen, so I'll try this later today. Might even try doing shadows since they're actually really easy.
DannySMc #6
Posted 20 May 2015 - 05:28 PM
Things like letters are technically possible, though I think the distance functions and input domain transformations required might be too heavy to be performant in CC. 2D distance functions require no iteration though so it could be possible to create a 2D logo if one were able to make distance functions for all the letters. It would be able to be scaled, rotated, stretched, etc.

I'd definitely be interested to see what this would look like on a large screen, so I'll try this later today. Might even try doing shadows since they're actually really easy.

Well I have no idea how the images work? So could I give you an image and you try please? AS I would really love this on my server!
Yevano #7
Posted 20 May 2015 - 05:41 PM
Well I have no idea how the images work? So could I give you an image and you try please? AS I would really love this on my server!

Well the only problem with images is that the color conversions usually don't look that great. However, I will say that rendering a textured box is dead simple, so it's technically possible.

I don't really feel like doing the work for decoding the image pixels and stuff like that right now, but if you send me a PM with the image maybe I'll try something this weekend or so.
DannySMc #8
Posted 20 May 2015 - 06:37 PM
Well I have no idea how the images work? So could I give you an image and you try please? AS I would really love this on my server!

Well the only problem with images is that the color conversions usually don't look that great. However, I will say that rendering a textured box is dead simple, so it's technically possible.

I don't really feel like doing the work for decoding the image pixels and stuff like that right now, but if you send me a PM with the image maybe I'll try something this weekend or so.

No i can make it a CC image?
Yevano #9
Posted 20 May 2015 - 06:42 PM
No i can make it a CC image?

That would work.
DannySMc #10
Posted 20 May 2015 - 10:08 PM
No i can make it a CC image?

That would work.

So I can make it just a simple paint image? or?
Yevano #11
Posted 20 May 2015 - 10:18 PM
I'd prefer if you PM me about it. It's sorta off-topic for this thread.
Yevano #12
Posted 21 May 2015 - 11:03 PM
After a bit of messing around I got some basic shadows working!



Red is the floor and blue is a tall box on top of the floor. Green is the shadow cast by the point light to the left. In the demo, I have the light spinning around the model so it's easier to see how the shadow is getting cast.
http://pastebin.com/ycA7m6vp

Edit: Also, this is the shader code I wrote when I was figuring out how shadows work. https://www.shadertoy.com/view/4lBGWK#

Double edit:


Edited on 21 May 2015 - 09:56 PM
DannySMc #13
Posted 22 May 2015 - 12:32 AM
OMG!
Bomb Bloke #14
Posted 22 May 2015 - 02:55 AM
Come to think of it, checking the clock for every pixel has its drawbacks - at higher resolutions you tend to end up "changing the scene" mid-frame, resulting in a tearing/blurry effect.

Really I reckon the whole "pixel" function would be better off replaced with a "page" function, one which calculates rox/roy/roz once per frame, as opposed to once per pixel. Try and loop each calculation as few times as possible.
Yevano #15
Posted 22 May 2015 - 03:03 AM
Come to think of it, checking the clock for every pixel has its drawbacks - at higher resolutions you tend to end up "changing the scene" mid-frame, resulting in a tearing/blurry effect.

Really I reckon the whole "pixel" function would be better off replaced with a "page" function, one which calculates rox/roy/roz once per frame, as opposed to once per pixel. Try and loop each calculation as few times as possible.

Good idea. I actually changed the time to be calculated per frame when messing around with the big monitor because like you said the tearing effect did become quite noticeable. Honestly I doubt calculations outside of the march loop have a significant impact though. Will see what I can do tomorrow.