How to model a molecule with Unity using a script?

Hi,

I am new to unity and am trying to import and visualise a molecule with it. Basically I have got the 3D positions of the atoms of the molecule in a file and I want to create a unity script that draws either spheres at the position of these atoms or additionnally cylinders between them.

Even with very small proteins (300 atoms) I run into heavy performance problems, so I think I am doing something wrong… any help/suggestion would be appreciated!

My current approach uses this:

temp=GameObject.CreatePrimitive(PrimitiveType.Sphere);
temp.transform.position = Vector3(3.4094, 2.8198, 0.725);

Thanks very much in advance,
bam

You’re not doing anything wrong per se, but you should use a different approach. 300 objects is kind of a lot of objects to draw, if each one is a separate draw call.

What you should do instead: look into the Particle API. Instead of 300 spheres, draw 300 particles (in the same particle system). Give them a texture that looks like a sphere, and basically fake their being 3D objects.

Try something like this (where particlePositions is an array of the positions of your atoms, and you have a particle emitter with emit set to false, and a particle renderer.

function Update() {
var pe : ParticleEmitter = GetComponent(ParticleEmitter);
pe.particles  = new Particle[particlePositions.length];
for (p=0;p<particlePositions.length;p++) {
pe.particles[p] = Particle();
pe.particles[p].position = particlePositions[p];
pe.particles[p].size = 1.0;
}
}

Hi,

Thanks for your suggestion which I might try. But the problem that I would foresee is shadows. Actually the aim of this is to get a very realistic (in a 3D shape perception sense) visual representation of the molecule. And 300 was just a start, I’m more aiming at 300 000 atoms (spheres).

I guess with a sphere texture anything such as ambient occlusion lighting is impossible, isn’t it?

You can see an example of what I am trying to achieve with Unity here: http://qutemol.sourceforge.net/fxs/par_big_2b.png
That is from a program called Qutemol.

Cheers,
Marc

Well, you won’t get the performance you want with 300,000 draw calls, nor will you get the detail you want with a particle system.

The only decent compromise I can think of: Maybe you can figure out which atoms are closest to the focal point on screen (maybe by using Camera.main.ScreenToWorldPoint(particlePosition), and comparing those points to a point in the middle of the screen). For the closest, say, 30 particles to the center, you set their mesh renderers to true, and they’ll be pretty and shadowed. For all the other ones, use the particle trick.

If you have Pro, you can use a render texture on the particles with a camera that floats around a “model particle” at the same angle as your main camera, so that the shading on the particles doesn’t look so awkward.