Sunday 15 March 2015

behavior - change color of a colorCube from dynamic inputs in java3D -



behavior - change color of a colorCube from dynamic inputs in java3D -

i'm able alter color of cube @ beginning. want alter according changing value dynamically if run next code color of cube greenish , not alter 1 time again since run 1 time. wondering if there's way alter colors few times using class extends behavior. please allow me know i'm missing. thanx in advance.

protected branchgroup getcube() { branchgroup bg = new branchgroup(); boundingsphere bounds = new boundingsphere(new point3d(0.0, 0.0, 0.0), 10000.0); tg = new transformgroup(); t3d = new transform3d(); t3d.settranslation(new vector3d(-35.0f, -15.0f, 40.0f)); tg.settransform(t3d); cc = new colorcube(3.0); simplebehavior4d3 b = new simplebehavior4d3(tg, gui);//changes position of cube b.setschedulingbounds(bounds); b.setenable(true); tg.addchild(b); quadarray cube = new quadarray(24, quadarray.coordinates | quadarray.color_3); cube = (quadarray) cc.getgeometry(); cube.setcolors(0, creategreen()); if (b.liftcapacitystat().equals("full")) { cube.setcolors(0, createred()); } else if (b.liftcapacitystat().equals("half")) { quadarray cube1 = new quadarray(24, quadarray.coordinates | quadarray.color_3); cube1 = (quadarray) cc.getgeometry(); cube1.setcolors(0, createyellow()); } tg.addchild(cc); bg.addchild(tg); bg.setuserdata("cube"); homecoming bg; }

here code:

(note snippet

final geometryarray geometryarray = gi.getgeometryarray(); { geometryarray.setcapability(geometryarray.allow_color_read); geometryarray.setcapability(geometryarray.allow_color_write); }

and part interpolator defined

final interpolator colchange = new interpolator() { ....

)

bundle examples; import java.util.enumeration; import javax.media.j3d.alpha; import javax.media.j3d.ambientlight; import javax.media.j3d.appearance; import javax.media.j3d.boundingsphere; import javax.media.j3d.bounds; import javax.media.j3d.branchgroup; import javax.media.j3d.directionallight; import javax.media.j3d.geometry; import javax.media.j3d.geometryarray; import javax.media.j3d.indexedtrianglearray; import javax.media.j3d.interpolator; import javax.media.j3d.node; import javax.media.j3d.polygonattributes; import javax.media.j3d.rotationinterpolator; import javax.media.j3d.shape3d; import javax.media.j3d.transform3d; import javax.media.j3d.transformgroup; import javax.media.j3d.trianglearray; import javax.media.j3d.view; import javax.media.j3d.viewplatform; import javax.vecmath.color3f; import javax.vecmath.point3d; import javax.vecmath.vector3f; import com.sun.j3d.utils.geometry.geometryinfo; import com.sun.j3d.utils.geometry.normalgenerator; import com.sun.j3d.utils.universe.multitransformgroup; import com.sun.j3d.utils.universe.simpleuniverse; import com.sun.j3d.utils.universe.viewingplatform; public class illustration { public static void main(final string[] args) { new example(); } public example() { final simpleuniverse universe = new simpleuniverse(); final branchgroup grouping = new branchgroup(); final transformgroup tg = new transformgroup(); final boundingsphere bounds = new boundingsphere(new point3d(0.0, 0.0, 0.0), 100.0); final node blob = createmonster(); { //to enable rotation tg.setcapability(transformgroup.allow_transform_write); tg.setcapability(transformgroup.allow_transform_read); } // { // tg.addchild(colchange); // } // create rotation see sides of object final transform3d btrans = new transform3d(); btrans.rotx(1); final transform3d ztrans = new transform3d(); ztrans.rotz(-1); btrans.mul(ztrans); final rotationinterpolator ri = createrotation(tg, bounds, btrans, 7000); tg.addchild(ri); { final interpolator colchange = new interpolator() { int colindex = 0; color3f[] colors = new color3f[] { new color3f(0, 0, 0), new color3f(1, 0, 0), new color3f(0, 0, 1), }; { final long milis = 9000; super.setalpha(new alpha(-1, alpha.increasing_enable, 0, 0, milis, 0, 0, 0, 0, 0)); super.setschedulingbounds(new boundingsphere(new point3d(0.0, 0.0, 0.0), 100.0)); } @override public void processstimulus(final enumeration criteria) { final alpha alpha = getalpha(); if (alpha != null) { final float value = alpha.value(); // system.out.println("alpha=" + value); final geometry geo = ((shape3d) blob).getgeometry(0); colindex = (int) (value * colors.length); final color3f color = colors[colindex]; ((trianglearray) geo).setcolor(0, color); ((trianglearray) geo).setcolor(1, color); ((trianglearray) geo).setcolor(2, color); } wakeupon(defaultwakeupcriterion); } }; tg.addchild(colchange); } tg.addchild(blob); // group.addchild(tg); // addlight(group, new color3f(1f, 1f, 0f), new vector3f(4.0f, +7.0f, -12.0f)); addlight(group, new color3f(1f, 1f, 1f)); // final viewingplatform viewingplatform = universe.getviewingplatform(); final viewplatform view = viewingplatform.getviewplatform(); view.setviewattachpolicy(view.nominal_screen); viewingplatform.setnominalviewingtransform(); final multitransformgroup mtg = viewingplatform.getmultitransformgroup(); final transformgroup vtg = mtg.gettransformgroup(0); final transform3d t1 = btrans; vtg.gettransform(t1); t1.setscale(2); vtg.settransform(t1); group.compile(); // add together grouping of objects universe universe.addbranchgraph(group); } private node createmonster() { final shape3d kid = createshape(); final appearance fillappnode = new appearance(); final polygonattributes patt = new polygonattributes(); // avoid problem clockwise faces patt.setcullface(polygonattributes.cull_none); patt.setbackfacenormalflip(true); fillappnode.setpolygonattributes(patt); child.setappearance(fillappnode); homecoming child; } private shape3d createshape() { final point3d[] points = new point3d[] {// new point3d(-1, 1, 0), // new point3d(1, 1, 0), // new point3d(1, -1, 0), // new point3d(-1, -1, 0), // new point3d(0, 0, 1), // }; final int[] idxs = new int[] {// triangle faces 0, 1, 2,// 2, 3, 0,// 1, 0, 4,// 1, 2, 4,// 2, 3, 4,// 3, 0, 4 }; final indexedtrianglearray ita = new indexedtrianglearray(points.length, geometryarray.coordinates | geometryarray.color_3, idxs.length); ita.setcoordinates(0, points); ita.setcoordinateindices(0, idxs); //per vertex colors per vertex reference //so each vertex can have different color each of faces final color3f[] colors = new color3f[] {// new color3f(1, 0, 1),// new color3f(0, 1, 0),// new color3f(0, 0, 1),// new color3f(1, 1, 0),// new color3f(1, 1, 1),// }; ita.setcolors(0, colors); ita.setcolorindices(0, idxs); ita.setcolorindices(0, new int[] { 0, 0, 0, 1, 1, 1 }); // final geometryinfo gi = new geometryinfo(ita); //also generate normal vectors surface can lite final normalgenerator normalgenerator = new normalgenerator(); normalgenerator.generatenormals(gi); final geometryarray geometryarray = gi.getgeometryarray(); { geometryarray.setcapability(geometryarray.allow_color_read); geometryarray.setcapability(geometryarray.allow_color_write); } homecoming new shape3d(geometryarray); } private rotationinterpolator createrotation(final transformgroup objscale, final bounds bounds, final transform3d btrans, final long milis) { final alpha alpha = new alpha(-1, alpha.increasing_enable, 0, 0, milis, 0, 0, 0, 0, 0); final rotationinterpolator ri = new rotationinterpolator(alpha, objscale, btrans, 0.0f, (float) math.pi * 2.0f); ri.setschedulingbounds(bounds); homecoming ri; } private void addlight(final branchgroup group, final color3f light1color, final vector3f ligthdirection) { { final boundingsphere bounds = new boundingsphere(new point3d(0.0, 0.0, 0.0), 100.0); final directionallight light1 = new directionallight(light1color, ligthdirection); light1.setinfluencingbounds(bounds); group.addchild(light1); } } private void addlight(final branchgroup group, final color3f light1color) { { final boundingsphere bounds = new boundingsphere(new point3d(0.0, 0.0, 0.0), 100.0); final ambientlight light1 = new ambientlight(light1color); light1.setinfluencingbounds(bounds); group.addchild(light1); } } }

behavior java-3d cubes

No comments:

Post a Comment