SOS - Simple OBJect Shading ***Updated***(again - 6/14/05)

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

What I was talking about is what I would assume is the bottom of the sub. I figured the larger pojection with the antenna or periscope or whatever was the conning tower.

And just to prove I could waste time in a ridiculous manner as well as the next guy, I wanted to see how far I could push things... all of these were with the brightness set at 0, the edge offset in the Layer Properties>3D Options set to 0 (turned off), and a 2 pixel, no offset-no blur black shadow on the layer.
Image
I know it messes with the design, but I smoothed the model a few (3 I think) times, ending up with almost 50,000 faces. This was with the Shading Levels at 500, shading inc 0.2%.
Image
Same model, but with the Shading Levels at 6 and the Increment at 15%. Woohoo, toon shading!
Image
I went back to the original model, an triangulated it before I smoothed it. 3 triangulates, 2 smooths. almost the same number of faces but arranges quite differently. Again, 500 shades at 0.2%.
Image
Same triangulated model, with 8 shades at 10%. The edges are a little random but it eliminates the obvious blockiness that you get in the models that are just smoothed.

And boy does a model that size cut down the programs responsiveness! I know part of the problem is I have to streamline how I'm doing things because I'm probably creating 3 times as many points as I should, I'll fix that. I also fixed the little bug with the base shading, I'll put the update at the top.
User avatar
jakob polack
Posts: 29
Joined: Thu Mar 17, 2005 8:08 pm
Location: copenhagen denmark
Contact:

Post by jakob polack »

hi there - all this seems interesting....but..how do I get your lua script inside moho ?

jakobpcph
hey look here its my private webspace:
http://uk.geocities.com/jakobpcph/
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

In the first post of the topic, the 2 lines in red are links to the script files. Right click (or equivalent, depending on what OS your on) and save the files to your computer. You need to find your Moho installation directory. Inside that is another directory named "scripts", and inside that another 3, called "menu", "tool" and "utility". The Library script (sf_utilities.lua) should be placed in the "utility" folder. Inside the "menu" folder are all the subfolders where the scripts you can access from the "Scripts" menu in Moho live. You can put the sf_sos.lua file pretty much anywhere in the "menu" directory you want, but it kinda makes sense to put it in the "3D" folder. After you've done that, either start Moho, or if it is already open, press <CTRL><F5>. That reloads all of the scripts. Then you can access it from Scripts>3D>Simple OBJect Shading.

Have fun with it. It's kind of nifty if I do say so myself. And in general, you'll usually be told if something is a "menu" or "tool" script, and that tells you where they should go. The sf_utility.lua script is a bit of an exception, and it's basically there to keep me from having to type the same code over and over again. It also both makes the other scripts code more understandable (if you're into that sort of thing) and some handy functions available to other people who might want to whip up a script.
User avatar
jorgy
Posts: 779
Joined: Sun Sep 05, 2004 8:01 pm
Location: Colorado, USA

Post by jorgy »

Damn! That is so cool! Especially the variations, allowing the toon shading and what-not.

I see what you mean on the bottom. How can I get rid of that "hook protrusion"? I had a fin there but deleted it for that bump thing. But I can't seem to select it with point, vertice, or face tool.
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Jorgy - what I did was select all the edges (maybe points, I don't remember) of the old fin, and moved it away from the rest of the model. Deselect everything, and then select only the two endpoints. Connect them to make a face. Triangulate the face, because theres something else wierd going on there. Delete all these new faces. If I remember right, there are still 4 vertexes and 3 sides left, there must have been something overlapping. Again, select the endpoints, connect them, triangulate, and delete the whole mess. Then it should be gone for good. If you just try to delete that whole thing without the extra steps, the whole model goes away. Why? I haven't got a clue.
User avatar
jorgy
Posts: 779
Joined: Sun Sep 05, 2004 8:01 pm
Location: Colorado, USA

Post by jorgy »

7ft,

I'm still having trouble with your script. I'm still getting errors:


invalid type in variable assignment
value is 'nil'; 'number' is expected


I've managed to narrow down my complicated examples to a rather simple box with bevelled top that still exhibits this behavior.

http://65.98.71.13/moho/broken.wings

http://65.98.71.13/moho/broken.obj

http://65.98.71.13/moho/broken.mtl


Any help would be appreciated.

Thanks!

jorgy

[/i]
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

I tried those specific files and everything looked fine, and the script ran without a hitch on the OBJ on my machine. Maybe you don't have the last version on you machine, I was making running bugfixes so maybe you have one of the intermediate versions in there? If not, I'll see what I can do.
User avatar
jorgy
Posts: 779
Joined: Sun Sep 05, 2004 8:01 pm
Location: Colorado, USA

Post by jorgy »

Hi,

The versions that I have (which I thought were the latest) are:

sf_utilities.lua
-- v1.04 4/26/2005 715:pm

sf_sos.lua
1.01


I'm running on linux, if that makes any difference.

Thanks!

jorgy
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

It shouldn't, as that script doesn't do anything external, but I'll give it the once over. Is there a line number for the error? How far does it get? If need be, I'll whip some debugging code in there so we can see exactly where it's crapping out. I don't like things not workin' the way their supposed to.
User avatar
jorgy
Posts: 779
Joined: Sun Sep 05, 2004 8:01 pm
Location: Colorado, USA

Post by jorgy »

I commented in some of your debugging statements, and it looks like:

http://putfile.com/pic.php?pic=6/16017165528.gif&s=x2

Unfortunately, it doesn't give a line number, but it is part way (on face 40 of 70) through.
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

This is just a first shot in the dark, but try using this lib:
http://www.sharemation.com/Moho/MenuScr ... sV1.05.zip

Maybe I did something dumb in there. I actually have a few minutes right now, so I'll give it the once over. I don't see a damn thing wrong with the initial file, and as I said it's working on my box.

This is a version that will print out the script line numbers as it runs through the offending loop. That should help to nail it down :
http://www.sharemation.com/Moho/MenuScr ... s_test.lua

Give that one a shot and tell me where it dies. I'm really clueless at this point. I hate that part...
User avatar
jorgy
Posts: 779
Joined: Sun Sep 05, 2004 8:01 pm
Location: Colorado, USA

Post by jorgy »

Okay, the last message that it prints is line 355. The next statement is:

newFace.matID = materialListID[mat][pickShade]

Is that a hint, or a bigger question? :-)
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

I think I found the problem, and I think it's due to a bit of a bug in Lua itself. Not to get too crazy in the math, but under some circumstances (in particular where the script would crap out on you) the length of the normal of the face (FNLength)would end up being zero. Consequently, the dot product of the light vector and the faces normal would be zero. Gah. So the line:

Code: Select all

angle = dot / (LVLength * FNLength)
ends up dividing zero by zero, and coming up with a number that lua showed as " -1.#IND ". Which doesn't look a whole lot like anything to me. Just for the hell of it I tried the math.abs function on it to try to make it positive, and got " -1.#QNAN ". So I think theres a bug in there. So...

In the test version, line #274 reads:

Code: Select all

faceNormal = faceNormal--:Norm()
Wasn't sure if I had to normalize it at the time. What I seem to have gotten working is changing that line to:

Code: Select all

faceNormal:NormMe()
It at least gets rid of the meaningless "number", and I think that screwy angle was what was mucking up the indexing of the material ID's. Windows seemed to just see it as a zero anyway, maybe in Linux it was properly interpreting it as complete shite and looked at it as nil. That's my guess. Try that fix out, and if it works, I'll put up a proper update. If not, I'll find it eventually.

Also, around line#250 in the test version there is this block:

Code: Select all

	
	----------------------------------------------
	-- Copy all original materials to the new mesh
	----------------------------------------------
	for i = 0, (mesh:CountMaterials() - 1) do
		local newMaterial = newMesh:CreateNewMaterial()
		local oldMat = mesh:Material(i)
		newMaterial.color = oldMat.color
	end
That whole bit should probably be commented out. It might be mucking up the indexing to the shades. Anyway, there's no good reason to just copy over all the original materials when they might not even be used in the shaded version of the OBJ.

Hopefully some of this will help. If not, I'll figure it out eventually.
User avatar
jorgy
Posts: 779
Joined: Sun Sep 05, 2004 8:01 pm
Location: Colorado, USA

Post by jorgy »

Bingo - that did it! (the normalization - I left the material block in).

Thanks so much, this is WAY COOL.
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Man, am I glad. It was really driving me nuts. Wonder why iy didn't throw a divide by zero error. Dang pesky machines... I'll put the updated version in the top post for anyone else who was having problems.
Post Reply