Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Wednesday, 27 June 2012

Retained Shape for Toxiclibs Mesh

I've recently posted a sketch to Open Processing, which demonstrates the huge performance improvement possible using PShape retained shape for processing-2.0. Unfortunately this cannot be run in the browser online (at present), but will work if you download it to your computer. Here is the important code for translating the mesh to PShape. Do this in setup (as I have done in sketch above) to get maximum performance benefit.
66 PShape meshToRetained(Mesh3D mesh, boolean smth) {        
67   PShape retained = createShape(TRIANGLES);
68   retained.enableStyle();
69   retained.fill(200, 200, 200);
70   retained.ambient(50);
71   retained.emissive(10);
72   retained.specular(50);
73   if (smth) {
74     mesh.computeVertexNormals();
75     for (Face f : mesh.getFaces()) {
76       retained.normal(f.a.normal.x, f.a.normal.y, f.a.normal.z);
77       retained.vertex(f.a.x, f.a.y, f.a.z);
78       retained.normal(f.b.normal.x, f.b.normal.y, f.b.normal.z);
79       retained.vertex(f.b.x, f.b.y, f.b.z);
80       retained.normal(f.c.normal.x, f.c.normal.y, f.c.normal.z);
81       retained.vertex(f.c.x, f.c.y, f.c.z);
82     }
83   } 
84   else {
85     for (Face f : mesh.getFaces()) {
86       retained.normal(f.normal.x, f.normal.y, f.normal.z);
87       retained.vertex(f.a.x, f.a.y, f.a.z);
88       retained.vertex(f.b.x, f.b.y, f.b.z);
89       retained.vertex(f.c.x, f.c.y, f.c.z);
90     }
91   }
92   retained.end();
93   return retained;
94 }
In this sketch I also discovered how to update the PShape root, which is very nice feature and necessary, if for example you have made changes to the underlying mesh, eg laplacian transform.
58 void keyPressed() {
59   if (key == 'l') {
60     new LaplacianSmooth().filter(mesh, 1);
61     PShape changed = meshToRetained(mesh, true);
62     retained.updateRoot(changed);
63   }
64 }

Implicit Function Script running in processing-2.0a6 IDE

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2