- AVS
- Raytracing Space Curves...
Archive: Raytracing Space Curves...
UIUC85
20th February 2004 07:40 UTC
Raytracing Space Curves...
Ok so I had a few new ideas but I'm not sure if they are even possible. I was thinking about raytracing space curves (by that I mean tracing the ray until it is a certain distance from the space curve). I guess this could get really nasty going down with derivatives to find the closest value of t... But technically you could setup the distance equation and take the derivative and solve for t in terms of x,y,z to find the closest point... Hmm the more I type this out the more I become aware of how complicated this could get if I use a complex equations... Any thoughts on this idea? UnConeD?
Ex. Say I had the following for my curve:
(cos(t), sin(t), 0)
then the distance for that is:
f(t)=((x-cos(t))^2+(y-sin(t))^2+z^2)^(1/2)
take the derivative in terms of t and you get:
f'(t)=(sin(t)x-cos(t)y)/f(t)
if you set that equal to zero and solved for t you'd get:
t=atan(y/x)
Which makes sense if you think about it. Then you could plug that back into to get the distance and set a threshold value like raytracing metaballs. That's a really simple equation and it could get pretty difficult if you try and use complicated space curves but I think it's a plausible idea.
horse-fly
20th February 2004 17:51 UTC
that looks quite nasty... the code, that is, i haven't tried it out in avs...
UIUC85
20th February 2004 19:51 UTC
Well of course it'd be kinda nasty but if you use Whittaker iteration to solve it then I you could bypass some of that. I dont think it's been attempted in AVS at all anyways. I'll work on that and see what happens. Any other opinions?
UIUC85
20th February 2004 20:04 UTC
Here we go. It's a good old raytraced torus using my method (I've never seen this used anywhere so I dont know this has a name or something). :)
I'll work on some more complex curves and maybe some shading.
fragmer
20th February 2004 22:40 UTC
I don't know anything about the coding part (looks cool though) but here's a better looking alpha code :p
alpha=2-abs(st-t*1.2)*3;
fragmer
20th February 2004 22:51 UTC
oh and replacing */$PI* with a variable *p* where "p=1/$PI" gives a 3fps b00st! :P
UIUC85
21st February 2004 03:55 UTC
No offense to you because I really do appreciate the comments and all, but I know it's not optimized. I'm just working with the method right now. Seeing how far I can push this trick. The current alpha coding just finds out whether the iterations are actually zeroing in on anything or not. If they aren't then I dont want to see the garbage at that point.
UIUC85
21st February 2004 04:11 UTC
Ok so here's an update on the torus. Figured out a convenient way of implementing some shading which will work for all raytraced space curves (if some more can be solved) ;) So here's what the torus looks like now. Changed the whittaker constant because this function can be approximated rather quickly with a few iterations. You'll notice the center is pretty crisp and clean as well. I tried working with a few other equations but I'm not so sure that's gonna work for most... I'll keep trying though. Comments!? Ideas!?
[Ishan]
21st February 2004 04:52 UTC
um....do you speak twice? :p btw, nice preset.
UIUC85
21st February 2004 04:56 UTC
Oh just caught what you were saying... Fixed it.
On a different note, I had another idea I might work on. If I created a second function to go along to define the radius of the shape about the space curve. Hmm...
[Ishan]
21st February 2004 05:09 UTC
i'm not a very techie AVSer but i think its turning out good :up:
UIUC85
23rd February 2004 04:55 UTC
The surface equation idea works. So now the radius of the torus can vary along the ring. But I cant seem to solve any more space curve equations. I could numerically approximate them but then I'd be doing that for each iteration of the other approximation and thats just not gonna happen. I'll see if I can find any more but I dont know...
UIUC85
25th February 2004 08:00 UTC
Uhh haven't fixed the normals on this new shape but I think it looks awesome.
Had a new idea as well! Similar direction but different effect. I'll work on it tomorrow and post it if I get it to work. I'm thinking I should be able to create some pretty crazy shapes!
[Ishan]
25th February 2004 09:41 UTC
hey nice work! maybe you can get them to morph between different shapes.
UnConeD
25th February 2004 12:03 UTC
I hadn't had time to read up on this. Your explanation is kind of chaotic ;)
What you seem to be doing now is first finding the curve's parameter value whose point comes closest to the current intersection guess. and then constructing a sphere around that point to raytrace with a whittaker iteration.
Indeed, the main problem will be finding out the value of p where df(p)/p = 0 (where p is the curve parameter). Up to now, your shapes haven't in fact been much more than just implicit equations written in a roundabout way. Once you try complicated curves, I think you will run into stability and speed problems.
I did have an idea which might help speed up the whittaker iteration. Instead of considering the smallest distance from the curve to the current intersection guess, consider the distance from a curvepoint to the current ray.
The distance from a point P to a ray O + k*D can be found by finding the point along the ray which comes closest to the point P. Then you check the distance between the two. By projecting P-O onto D, you get 'k':
k = D * (P - O)
(D*D)
(* is dot product)
In AVS, you can easily normalize D (using invsqrt) once, so then you get (D*D = 1):
k = D * (P - O)
In this equation, P will be in parametric form (the curve).
The calculated 'k' is automatically the value 'k' for the point which is closest to the curvepoint.
This obviously homes in a lot faster, and seems to work well as an accelerating iteration. Of course, this 'k' will try to get as close to the curve as possible, rather than staying a certain radius away from it. So it would seem good to use this to get close to the desired area fast, and then using your current method for respecting the radius.
For your variable-radius donut:
('k' = t, 'p' = at, 'dx,dy,dz' = rx,ry,rz)
// Normalize once at the beginning
rn=invsqrt(sqr(rx)+sqr(ry)+sqr(rz));
rx=rx*rn;ry=ry*rn;rz=rz*rn;
// One iteration step
ex=rx*t+ox; ey=ry*t+oy; ez=rz*t+oz;
at=atan2(ey,ex);
t=(cos(at)-ox)*rx+(sin(at)-oy)*ry+(-oz)*rz;
This can be optimized:
ex=rx*t+ox; ey=ry*t+oy;
atd=invsqrt(sqr(ex)+sqr(ey));
t=(ex*atd-ox)*rx+(ey*atd-oy)*ry-oz*rz;
It might be best to multiply 't' by 0.9 or to subtract a small value, in order to keep it from going past the curve (this would make your sphere whittaker iterations diverge).
UIUC85
25th February 2004 15:14 UTC
I hadn't thought of that. Yeah sorry about the chaotic explanation. I was working out the details as I was typing it more or less... That idea you is awesome! I'll put that in later today when I get the chance.
Here maybe I can explain this all a little better to everyone on how this works.
So basically I wrote out a space curve. If you dont know what that is, more or less every 3D shape you create with SSC's is one. So what I do is start out my rays (I'm gonna add in UnConeD's idea so read that). And the object is to find the first point on the ray that is within a certain distance of the space curve. So you use the distance formula along with the current approximated guess.
Thats where thist stuff came in.
Ex. Say I had the following for my curve:
(cos(t2), sin(t2), 0)
then the distance for that is:
f(t2)=((x-cos(t2))^2+(y-sin(t2))^2+z^2)^(1/2)
take the derivative in terms of t and you get:
f'(t2)=(sin(t2)x-cos(t2)y)/f(t2)
if you set that equal to zero and solved for t you'd get:
t2=atan(y/x)
When you take the derivative of the distance formula and solve it for zero, you can find the value of t2 on the space curve that is closest to that point. Then substitute that back into the equation. So we have:
r=((rx*t1+ox-cos(t2))^2+(ry*t1+oy-sin(t2))^2+(rz*t1+oz)^2)^(1/2)
That's just the distance from the ray and the closest point on the space curve. So you subtract r from both sides and you get the equation so it's equal to zero and you use Whittake to approximate your "intersection."
That should help. :)
S-uper_T-oast
25th February 2004 17:51 UTC
Nifty.
UIUC85
26th February 2004 02:32 UTC
Here's the idea I had in action. The top and bottom of the sphere were leveled off because the shape gets too dynamic for whittaker iteration making it hard to zero in on a specific solution. Yeah I know the code isn't optimized. I just wanted to give out an example. I can make the surface change with the music. The shading isn't right yet but it works well enough. Ideas?
UIUC85
16th March 2004 04:50 UTC
I'm still working with this idea. Anybody know any nice way of skewing those normals on the lumpy torus shape? I figure its dependent on the angle from the center and the angle from the closest point on the curve but I haven't really had the time to think it through fully. Any ideas? UnConeD? Jheriko? Anybody?
jheriko
16th March 2004 08:57 UTC
Skewing? Normals? Why? That torus looks fine as it is.. the edge fading looks okay imo, besides wouldn't better edge fading result in more ugly black 'occlusions' that shouldn't really happen? I assume you want normals for the edge fading since the lighting effect looks like distance fog to me.
If you want more 'space curves' to try here are some parametric surfaces that I like:
f8 klein bottle:
x=(1+cos(u/2)*sin(v)-sin(u/2)*sin(2*v))*cos(u)
y=(1+cos(u/2)*sin(v)-sin(u/2)*sin(2*v))*sin(u)
z=sin(u/2)*sin(v)+cos(u/2)*sin(2*v)
moebius strip:
x=cos(u)+v*cos(.5*u)*cos(u)
y=sin(u)+v*cos(.5*u)*sin(u)
z=v*sin(.5*u)
I made a minipack a while ago containing quite a few different parameteric surfaces, its called 6point5 and can be found in my deviantart gallery, be warned though some of the functions might be messy since I derived a couple of them myself.
UIUC85
16th March 2004 16:46 UTC
The normals I'm using on the lumpy torus are exactly the same as the normals on the normal torus, but they dont account for the lumps so the lighting doesn't look quite right. That's why I wanted to skew the normals.
D12
10th April 2004 06:57 UTC
My experience & my information about advanced method of writing hard DM's & SSc's is very little.
But I try to learn them in AVS.
TomyLobo
10th April 2004 07:00 UTC
D12: Can you please stop digging up old threads and adding absolutely NOTHING to them?