Friday, 8 June 2018

The Degeneration game - Nyrva's shower - a simple case study in physics shapes in Mesh Studio

I often come across frustrated builders who have struggled along the right path but for various reasons not quite got to the destination they had hoped. It often reads like a storybook, as our wannabe mesh warrior stumbles into the local inn, seeking solace and with a plea for help.
Nyrva: Hello, question please. If I am trying to build a "sci-fi shower" for RP, that you have to be able to walk inside of, do I have to make it square and boxy in the mesh and physics in order to avoid the "Degenerate Triangles" problem on import? It seems I'm not allowed to do a cylindrical design.

We meet our hero part way through their quest. They've already confronted the uploader troll, and unable to answer the riddle of the degenerate triangle, have retreated for help.

Back in the village tavern, our quester gets a lot of well-meaning advice. It variously consists or perpetuating myths and old wives tales, or valid advice that helped someone once, but does not really help our hero.
helpful soul#1: cylinders can be a pain ... i have found it "cheaper" to make the sides from a number of "box" type prims  for the physics
 helpful soul #2: your physics can be made up of a number of cube prims arrounged in a circle... sounds a pain ...but it is cheaper LI wise ( and i havent a clue why that is so ). I once made a smoke stack for a steam boat .... mesh funnel ... for some reason .... i forget why i need it physical ...... i was lazy and added a normal ( transparent ) cylinder when i added the prim cylinder to my mesh funnel the LI blew through the roof ....???? ( so i made a mesh physics one)
helpful soul #3: Also zeroing out the low and lowest LOD helps lower the LI because you wouldn't be looking at the shower from across the sim
In this case, all good advice in the right place and time but missing the point. Our embattled builder hacks away for a little longer, gets frustrated and gives up, limping defeated to the marketplace to buy something less fulfilling.

So what was the problem?

The MAV errors reported by the Second Life are often considered as dark arts, a kind of voodoo curse that nobody understands, but mostly they are just annoying badly reported errors. MAV comes from Mesh Asset Validation, so this was failing during the validation phase. OK, but what is a degenerate triangle? In mathematics, a degenerate triangle has zero area. In SL and other games use cases a degenerate triangle is one with a very small area. The degenerate triangle check, in SL, goes a step further and penalises "long thin triangles" which are defined as triangles where any one side is more than 10 times the length of any other side.

So the problem is that we have some tiny, probably stretched triangles. This is one of the reasons why I keep repeating over and over that a physics shape must be as simple as possible. If you have a triangle that fails the degenerate check you have definitely not optimised that model.

In the case of our intrepid mesher, they'd not removed all the hidden surfaces and thin edges. This is a common error, people think "this is a physics mesh, not a visible mesh so I don't need to hide the geometry, but it brings us back to rule #1 of physics shapes, every triangle that does not need to be there is a triangle too many.

Right then, how do I find these things?

Once you know what the cause is, it is often quite obvious where the problem lies; however, what many people do not realise is that the much-maligned uploader tries to help you. When it finds a degenerate triangle it will highlight it in the preview window.

Happily ever after?

Does this story have a happy ending? We left our hapless builder being bombarded with advice over a beer in the local tavern, but how did it end?

Having been asleep when this tale started, I caught up with Nyrva when I woke to see how they'd got on. 

Me: Hi, did you get past your problem earlier?
Nyrva: no, never did. just wound up buying something off the MP for 55L$. I couldn't make anything below 5LI. For me, it's just another item on the "What MS can't be used for" list. lol!"

Never one to pass up a challenge I asked Nyrva to send me the item. I was able to get the shower to be 1LI with a full physics shape relatively easily. So what magic did I use? Nothing really just careful attention to that law of physics models, delete every triangle that is not absolutely necessary,

The rest of this blog is a brief step by step guide through the process.

The visible mesh is always a good starting point.
As we can see the structure is very simple, with no obvious reasons why we cannot get a very low LI. The only complexity is that it is cylindrical, in fact, the cubicle is just 3 cylinders, one of which is hollow and 1/4 cut. 

Cylinders are made up of a series of flat edges, a cylinder in SL is by default 24-sided, and in Mesh studio you can step down to as low as 3 sides (where your cylinder will appear as a triangular prism)

For the visible mesh in the High LOD model leaving the default setting will be fine. However, for the physics, we need to economise.  I chose to use the cylinder and set the number of side to 8 in Mesh Studio, with the quarter cut away this results in a 6 sided cut cylinder, which is 12 triangles on the inner face and 12 triangles on the outer. however, there are also two long thin triangles on the vertical edges and 12 small triangles on the top and bottom surfaces. These triangles are extraneous and also quite likely to be degenerate, I have marked them in RED to highlight them. All of these should be made transparent in the model before meshing.

The two "caps" top and bottom do not need to be curved at all. Leaving them as cylinders, even 8 sided cylinders would have resulted in 16 triangles ( 8 top, 8 bottom - assumes we remove the 16 around the sides). Unless there is a very specific need have the physics shape match the shower visual shape using a square and getting just 4 triangles is far more efficient.

Some of you will be shouting, "but you don't need the underside and the inside surfaces. It is true that the inner shower wall and the underside of the shower ceiling are probably superfluous and would have been the next optimisation if this had remained higher. Note that we cannot remove the underside surface of the floor because we need to have at least one triangle there to match the bounding box of the mesh and avoid stretching.

Uploading with the original visible mesh resulted in a 1LI cylindrical shower. 

Bonus feature

There was a curious outtake during the making of this blog. having duplicated the curved shower glass and used it as the basis of my physics, I made the long thin edges and the top surfaces transparent and generated a mesh.

It refused to accept this as the physics, giving a Degenerate Triangle error. Looking at the image in the upload preview it was clear that the problem was that long thin cut edge, but it was equally obvious that there were no triangles there.

The problem was a little more subtle. Sometime during the editing of the original cylinder Nyrva had picked up a rounding error. and the cut was set to 1.2495 instead of 1.25. When Mesh Studio generates the mesh it obeys this. 0.125 is 1/8 of course. the size of one of our segments in our physics, so what Mesh studio was actually generating was 6 full-size sides and a tiny slither of the 7th side, which was of course, degenerate. Correcting the rounding error fixed the issue.

Saturday, 20 January 2018

For LOD's sake stop!

TLDR; Increasing LOD Factor increases lag. Just say no!

Introduction - Bad design is slowing you down.

Is your Second Life slower than you'd like? Do things grind to a halt when you visit a busy region?
Have you read a notecard like this and followed its advice?

These things may well be related. Notecards such as the one above are sadly widespread, and I suspect that in many cases the designers suggesting this don't fully appreciate the impact their advice can have.

Higher LOD Factor = Lower FPS. 

Let's be very clear, the higher this LOD Factor setting, the longer it takes to draw the scene (frame), which means fewer frames per second (FPS). Lower FPS is a part of what many people call lag.

In the new release of Firestorm (5.0.11), the viewer will warn you if you have unusually high settings, and settings that are considered too high for general use will revert to defaults after a restart. This is done in the hope of improving the overall user experience by encouraging more creators to design efficient, well-behaved content. In 5.0.11 the limit for "normal" use will be 4; this may reduce further in the future.

The remainder of this post will try to de-mystify the "techie" terms LOD and LOD Factor and discuss why you should prefer content from creators who take the time to make Second Life–friendly products.

What is LOD?

LOD is an acronym representing "Levels of Detail", a standard mechanism in computer graphics that is used to reduce the amount of work required to draw a scene by using simpler forms of objects that are smaller or further away. The faster a single scene can be drawn, the more times per second it can be redrawn and the faster and smoother your in-game experience will be.

In Second Life objects are constructed from up to four versions of the same item, each with fewer details than the preceding one. These are the Levels of Detail (LODs), and you might see them as the object moves into the distance.
High LOD (2700 triangles)

Medium LOD (692 tris)

Low LOD (313 tris)

Lowest LOD (14 tris)

Each of the above shows a different level of detail for a 1920s style lamp made by me (click them to look closer.) 

How does LOD work?

As Loki Eliot once stated, the best way to visualise LOD is to consider a series of concentric rings around an object; as the camera passes from one ring to another the viewer changes to the next LOD model.

Here, with Loki's permission, is an animation of his original illustration.

As you can see, the further the viewer is away from the object the simpler the representation. The illustration exaggerates for effect, of course, and a well-designed object should decay gracefully as it vanishes into the distance.

This second short animation loop shows a real object, the same table lamp that was shown above in its constituent models, this time viewed in-world on a platform of rings that represent Loki's concentric circles.

The viewer can be seen switching between these versions as the camera retreats away and approaches again.

The embedded image barely shows the changes at all; when viewing it full size, the switch is noticeable, but not so much as to distract you if you were focussed on nearby objects. 

How is LOD used?

It is the job of the creator of an object to specify what an object looks like in each of these simpler forms when they upload it to Second Life. A well-designed product should be recognisable at a distance, and the changes between models should not be so noticeable as to distract the user.

To encourage good design behaviour, and prevent someone setting all the LOD models to the same high detail version, Linden Lab levies a higher Land Impact penalty on objects with more complex, lower-detail models. The idea was to reward efficient object design with lower land impact. It never quite worked out

Where did it all go wrong?

Many creators understand how their creations affect the user experience and carefully craft models for each LOD to ensure that the user experience is good. However, for a long time now, there has been an unfortunate tendency with some designers towards skimping on the low detail models (often specifying just a single triangle) to artificially lower the Land Impact and make them seem "more efficient". This, of course, means that the object will crumple quickly as you move away from it, so they compensate for this by telling the users to "adjust their settings" to "see the product as intended." This has a significant impact on the overall performance capability of Second Life.

What is LOD Factor?

Remember those concentric circles I mentioned above? Each circle represents the boundary line either side of which a different LOD model is shown. How far away those boundaries are from the object is controlled by the size of the object and a thing called the LOD Factor. Irrespective of the size of an object, the larger the LOD Factor, the further apart those rings will be, and the wider the area that the models are visible for. With LOD factors that are high, everyday objects are drawn in "full detail" even if they are barely visible on the screen.

Why is changing LOD Factor so bad?

The LOD Factor setting is a global adjustment; it affects everything that you see. Every object in view, no matter how large or small, will have its LOD behaviour altered according to that setting.

By the act of setting an arbitrarily high LOD Factor, because your favourite sofa at home crumples when you stand by the door, the viewer will be forced to draw the high detail model for all objects a lot more often. Consider the extreme case of a jewelled earing worn by one of the crowd in a shopping mall, perhaps no more than a few pixels on your screen. In spite of the size, the viewer will have to try to draw every facetted jewel and tiny metal clasp just because a designer was unwilling or unable to design a proper LOD model and told you to use a debug setting instead. 

How do you find better content?

"Ok, ok, enough already, I get it. Bad content, has bad LODs. But how do I find the good stuff?"

One simple rule of thumb is to avoid content that tells you to adjust your viewer settings to see it properly. If an object comes with a notecard or other "advice" to increase "RenderVolumeLODFactor", then the chances are that the object will not have well designed LOD models. However, with the new release of Firestorm, you will also have better tools and be able to inspect an item in-world to see exactly how it behaves.

The new Mesh Info panel is described in my previous blog post. Using the LOD display function, you can look at the different LOD models, and in the information table, you can now also see what distance the concentric LOD rings would be at for both LL and FS default LOD Factor settings.

When evaluating an item, consider the way it will be used. Outdoor objects such as cars are likely to be seen from far greater distances than a piece of furniture, and remember too that even if you adjust your LOD Factor higher, your friends may not and will not be so impressed by the pile of crumpled triangles parked outside.

An indoor item may never be expected to be seen outside of a room so the designer may have made legitimate choices to economise, use the tools to decide. The chest of drawers in the image to the right will start to collapse at 5m for anyone on the Linden Lab viewer default settings. In a small house this is fine, but in a stately bedroom this may not be so desirable.

Will this situation change?

I hope that with these changes people will be empowered to start to take control of their SL performance and make better choices, but there is a stronger incentive too. As Second Life continues to grow and evolve, new features are being added and old features modernised. With each new development, there are extra demands on the viewer to draw them. To balance this equation and to keep the world accessible to as many as possible, more needs to be done to encourage efficient Second Life optimised content. There are very strong suggestions that the way that Land Impact is calculated will change, penalising poor content in favour of well-designed content. How this will be achieved is as yet unknown, but the writing is on the wall for bad content.

I hope this has helped to explain a complex and somewhat technical topic without too much techno-babble.

See you soon.