schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.67k stars 709 forks source link

Trimesh performance #176

Closed recoshet closed 9 years ago

recoshet commented 9 years ago

Hello again) I use a complex geometry in your engine. But I have found a strong drop in performance when adding new simple objects (spheres). example: http://naviris.ru/3D2/cannon_phi.html I cut geometry for a few details. But productivity has increased only at the edges of the model.

Tell me how to handle a model to increase productivity?

I want to do this one: http://media.tojicode.com/q3bsp/physics-test.html

schteppe commented 9 years ago

Hello, The Trimesh is not very optimized yet. It does a naive lookups for triangles+edges+verts, which is extremely slow (if you mesure performance, you'll see that the bottleneck is Narrowphase.prototype.sphereTrimesh). I'm planning to make an octree lookup for it, and that will make it much faster.

Btw, is that de_dust2 from CounterStrike? Cool :)

schteppe commented 9 years ago

@recoshet Hey, check out the latest commits. I added an Octree and it should be fast enough for Sphere/Trimesh collisions now. One sphere against a 25.000 vertex trimesh works without problem on my macbook pro :)

recoshet commented 9 years ago

That'S Perfect! Very good performance. Thanks!

schteppe commented 9 years ago

@recoshet it's even more optimized now :)

sasha240100 commented 9 years ago

@schteppe, Hi there. I have a problem when constructing terrain with trimesh (i have only vertices and faces, thats why.) When number of segments is more than 125x125 all other vertices points to 0, 0, 0 point. And normals have 0 value. image

In normals array after normal №194325 all them have zero value

sasha240100 commented 9 years ago

image

Thats when i have 250x250 segments.

sasha240100 commented 9 years ago

@schteppe i found the solution. Int32Array for indeces in trimesh object. But it use more perfomance.

schteppe commented 9 years ago

Hi. The Trimesh currently use an Int16Array for the index data. If I got it right, you have 125 * 125 * 2 * 3 = 93750 indices which is more than a signed 16 bit integer can hold. You should probably split your Shape into pieces to get around this... Or switch to another index storage, as you already suggested.

Perhaps the Trimesh should throw an error when the index limit is reached..

sasha240100 commented 9 years ago

@schteppe Thank you for your answer. But how can i split a shape? I can give you a link with my project. Can you help me, please?

sasha240100 commented 9 years ago

@schteppe Here is a demo: http://sitepro.ga/proj/whsedit/

schteppe commented 9 years ago

If I were you I'd use Int32Array or just a normal Array instead as a quick fix. Performance difference is probably not noticeable. Or is it?

Trimesh splitting would be cool to have in the lib, maybe I'll have a look at that sometime in the future.