作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 java3d,我怎样才能不基于每个顶点而是基于每个面进行着色?
我尝试了解 java3d,但我生成的 Shape3d 看起来并不像预期的那样。我想用不同的颜色给不同的三角形上色,但我不知道该怎么做。
Texture 看起来有点大材小用,更何况这部分我完全没有掌握。据我所见,Materials 在一个完整的 GeometryArray 上工作,所以在这里并不好。逐顶点颜色无法解决问题。
最佳答案
我喜欢这个解决方案。 “每个顶点颜色”实际上是每个顶点引用,因此每个顶点的每个面都可以有不同的颜色。
这是一个工作示例(遗憾的是不是很小):
package examples;
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.GeometryArray;
import javax.media.j3d.IndexedTriangleArray;
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.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 Example {
public static void main(final String[] args) {
new Example();
}
public Example() {
final SimpleUniverse universe = new SimpleUniverse();
final BranchGroup group = 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);
}
// Create rotation to see all sides of the 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);
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 the group of objects to the Universe
universe.addBranchGraph(group);
}
private Node createMonster() {
final Shape3D child = createShape();
final Appearance fillAppNode = new Appearance();
final PolygonAttributes pAtt = new PolygonAttributes();
// avoid trouble with clockwise faces
pAtt.setCullFace(PolygonAttributes.CULL_NONE);
pAtt.setBackFaceNormalFlip(true);
fillAppNode.setPolygonAttributes(pAtt);
child.setAppearance(fillAppNode);
return 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[] {// the 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 really are per vertex reference
//so each vertex can have a different color for each of its 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 so that the surface can be light
final NormalGenerator normalGenerator = new NormalGenerator();
normalGenerator.generateNormals(gi);
final GeometryArray geometryArray = gi.getGeometryArray();
return 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);
return 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);
}
}
}
关于java - java3d中的面部着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26369331/
我正在寻找可以支持人脸检测、识别和聚类的SDK。我们试过了Pittpatt ,而且效果很好,但是被谷歌收购了,因此卡住了新契约(Contract)。所以我们不得不决定使用OpenCV,业务风险低,技术
我正在努力寻找正确的方法来调整大小、裁剪和图像,以及重点区域。在我的例子中,焦点区域是图像中检测到的人脸,我需要确保该区域在裁剪后的版本中可见。 我有例如给出的重点领域。 face_height、fa
我们的应用程序从钥匙串(keychain)中保存和检索项目,以便使用生物识别技术进行身份验证。 在第三次错误尝试时,我被重定向到设备 PIN 码。相反,想提示一条消息,指出 3 次错误尝试。 检索项目
我是一名优秀的程序员,十分优秀!