3D 对象的属性

server/2025/2/3 17:28:51/

JavaFX - Cull Face 属性

JavaFX 还为 3D 对象提供了各种属性。这些属性的范围包括确定形状的材料:内部和外部、渲染 3D 对象几何图形和剔除 3D 形状的面。

提供所有这些属性是为了即兴创作 3D 对象的外观和感觉;并检查适合应用程序的内容并应用它们。

Cull Face 属性

通常,剔除是删除形状方向不正确的部分(在视图区域中不可见)。

Cull Face 属性的类型为 CullFace,它表示 3D 形状的 Cull Face。 您可以使用 setCullFace() 方法设置形状的 Cull Face,如下所示

java">box.setCullFace(CullFace.NONE);

形状的描边类型可以是

默认情况下,3 维形状的剔除面为 Back。

 例1

以下程序是一个示例,演示了球体的各种剔除面。将此代码保存在名为 SphereCullFace.java 的文件中

java">import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.CullFace; 
import javafx.stage.Stage; 
import javafx.scene.shape.Sphere; public class SphereCullFace extends Application { @Override public void start(Stage stage) { //Drawing Sphere1 Sphere sphere1 = new Sphere();//Setting the radius of the Sphere sphere1.setRadius(50.0);   //Setting the position of the sphere sphere1.setTranslateX(100); sphere1.setTranslateY(150); //setting the cull face of the sphere to front sphere1.setCullFace(CullFace.FRONT); //Drawing Sphere2 Sphere sphere2 = new Sphere(); //Setting the radius of the Sphere sphere2.setRadius(50.0);   //Setting the position of the sphere sphere2.setTranslateX(300);  sphere2.setTranslateY(150); //Setting the cull face of the sphere to back sphere2.setCullFace(CullFace.BACK);          //Creating a Group object  Group root = new Group(sphere1, sphere2); //Creating a scene object Scene scene = new Scene(root, 600, 300);  //Setting title to the Stagestage.setTitle("Drawing a Sphere"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } 
}

例2 

以下程序是一个示例,它演示了何时不使用 NONE 属性对框应用剔除面属性。将此代码保存在名为 BoxCullFace.java 的文件中

java">import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.CullFace; 
import javafx.stage.Stage; 
import javafx.scene.shape.Box; public class BoxCullFace extends Application { @Override public void start(Stage stage) { //Drawing Box1 Box box1 = new Box();//Setting the dimensions of the Box box1.setWidth(100.0); box1.setHeight(100.0);   box1.setDepth(100.0);  //Setting the position of the box box1.setTranslateX(100); box1.setTranslateY(150);box1.setTranslateZ(0);//setting the cull face of the box to NONE box1.setCullFace(CullFace.NONE); //Creating a Group object  Group root = new Group(box1); //Creating a scene object Scene scene = new Scene(root, 600, 300);  //Setting title to the Stagestage.setTitle("Drawing a Box"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } 
}

JavaFX -“绘制模式”属性

可以使用 Shape3D 类在 JavaFX 应用程序中绘制 3D 形状。此类允许您构建三种类型的 3D 形状:长方体、圆柱体、球体作为其直接子类。

但是,根据尝试创建的 JavaFX 应用程序的性质,还可以使用 Shape3D 类提供的属性来增强 3D 形状的外观。有三个这样的属性可以应用于 JavaFX 应用程序的 3D 形状。它们列出如下 

Drawing Modes 属性

Drawing Modes 是属于 DrawMode 枚举类型的属性,它表示用于绘制当前 3D 形状的绘制模式的类型。在 JavaFX 中,您可以选择两种绘制模式来绘制 3D 形状,它们是

默认情况下,3Dimensional 形状的绘制模式为 fill。但您仍然可以使用 setDrawMode() 方法选择绘制模式来绘制 3D 形状,如下所示

java">box.setDrawMode(DrawMode.FILL); 

例1

以下程序是演示 3D 框上的 LINE 绘制模式的示例。将此代码保存在名为 BoxDrawModeLine.java 的文件中

java">import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene;  
import javafx.scene.shape.Box; 
import javafx.scene.shape.DrawMode; 
import javafx.stage.Stage; public class BoxDrawModeLine extends Application { @Override public void start(Stage stage) { //Drawing a Box Box box1 = new Box(); //Setting the properties of the Box box1.setWidth(100.0); box1.setHeight(100.0);   box1.setDepth(100.0); //Setting the position of the box box1.setTranslateX(200); box1.setTranslateY(150); box1.setTranslateZ(0);//Setting the drawing mode of the box box1.setDrawMode(DrawMode.LINE);     //Creating a Group object   Group root = new Group(box1); //Creating a scene object Scene scene = new Scene(root, 300, 300);  //Setting title to the Stage stage.setTitle("Drawing a Box"); //Adding scene to the stage stage.setScene(scene);//Displaying the contents of the stage stage.show(); }      public static void main(String args[]){ launch(args); } 
}

例2 

 现在让我们看看另一个示例,该示例显示了 FILL 绘制模式在 3D 形状上的用法。为了正确显示这些模式的差异,我们将再次在 3D 框上应用此模式。将此代码保存在名为 BoxDrawModeFill.java 的文件中

java">import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.PerspectiveCamera; 
import javafx.scene.Scene;  
import javafx.scene.shape.Box; 
import javafx.scene.shape.DrawMode; 
import javafx.stage.Stage; public class BoxDrawModeFill extends Application { @Override public void start(Stage stage) { //Drawing a Box Box box1 = new Box(); //Setting the properties of the Box box1.setWidth(100.0); box1.setHeight(100.0);   box1.setDepth(100.0); //Setting the position of the box box1.setTranslateX(200); box1.setTranslateY(150); box1.setTranslateZ(0);//Setting the drawing mode of the box box1.setDrawMode(DrawMode.FILL); //Creating a Group object   Group root = new Group(box1); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting camera PerspectiveCamera camera = new PerspectiveCamera(false); camera.setTranslateX(100); camera.setTranslateY(50); camera.setTranslateZ(0); scene.setCamera(camera);  //Setting title to the Stage stage.setTitle("Drawing a Box"); //Adding scene to the stage stage.setScene(scene);//Displaying the contents of the stage stage.show(); }      public static void main(String args[]){ launch(args); } 
}

JavaFX - Material 属性

到目前为止,当我们解决 JavaFX 形状对象的属性时,它始终只包括形状填充的颜色和形状描边的类型。但是,JavaFX 还允许您为 3D 形状分配材质类型。

3D 对象的材质只不过是用于创建 3D 对象的纹理类型。例如,现实世界中的盒子可以由不同的材料制成;就像纸板、金属、木头或任何其他固体一样。所有这些都形成相同的形状,但使用不同的材料。同样,JavaFX 允许选择用于创建 3D 对象的材质类型。

 材料属性

Material 属性的类型为 Material,用于选择 3D 形状的材料表面。您可以使用 setMaterial() 方法设置 3D 形状的材料,如下所示

java">cylinder.setMaterial(material);

如上所述,对于此方法,需要传递 Material 类型的对象。包 javafx.scene.paint 的 PhongMaterial 类是此类的子类,它提供 7 个表示 Phong 着色材质的属性。可以使用这些属性的 setter 方法将所有这些类型的材料应用于 3D 形状的表面。 

以下是 JavaFX 中可用的材料类型

默认情况下,3-Dimensional 形状的材质是具有浅灰色漫反射色的 PhongMaterial。

下面是一个在圆柱体上显示各种材质的示例。将此代码保存在名为 CylinderMaterials.java 的文件中

java">import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.PerspectiveCamera; 
import javafx.scene.Scene; 
import javafx.scene.image.Image; 
import javafx.scene.paint.Color; 
import javafx.scene.paint.PhongMaterial; 
import javafx.scene.shape.Cylinder; 
import javafx.stage.Stage;public class CylinderMaterials extends Application {  @Override public void start(Stage stage) { //Drawing Cylinder1 Cylinder cylinder1 = new Cylinder();         //Setting the properties of the Cylinder cylinder1.setHeight(130.0f); cylinder1.setRadius(30.0f);   //Setting the position of the Cylinder cylinder1.setTranslateX(100); cylinder1.setTranslateY(75); //Preparing the phong material of type bump map  PhongMaterial material1 = new PhongMaterial();  material1.setBumpMap(new Image("http://www.tutorialspoint.com/images/tplogo.gif"));   //Setting the bump map material to Cylinder1 cylinder1.setMaterial(material1);    //Drawing Cylinder2 Cylinder cylinder2 = new Cylinder();         //Setting the properties of the Cylinder cylinder2.setHeight(130.0f); cylinder2.setRadius(30.0f);   //Setting the position of the Cylinder cylinder2.setTranslateX(200); cylinder2.setTranslateY(75); //Preparing the phong material of type diffuse map PhongMaterial material2 = new PhongMaterial();material2.setDiffuseMap(new Image("http://www.tutorialspoint.com/images/tp-logo.gif")); //Setting the diffuse map material to Cylinder2 cylinder2.setMaterial(material2);         //Drawing Cylinder3 Cylinder cylinder3 = new Cylinder();         //Setting the properties of the Cylinder cylinder3.setHeight(130.0f); cylinder3.setRadius(30.0f);   //Setting the position of the Cylinder cylinder3.setTranslateX(300); cylinder3.setTranslateY(75); //Preparing the phong material of type Self Illumination Map PhongMaterial material3 = new PhongMaterial();  material3.setSelfIlluminationMap(new Image("http://www.tutorialspoint.com/images/tp-logo.gif"));  //Setting the Self Illumination Map material to Cylinder3 cylinder3.setMaterial(material3);  //Drawing Cylinder4 Cylinder cylinder4 = new Cylinder();         //Setting the properties of the Cylinder cylinder4.setHeight(130.0f); cylinder4.setRadius(30.0f);   //Setting the position of the Cylinder cylinder4.setTranslateX(400); cylinder4.setTranslateY(75); //Preparing the phong material of type Specular Map  PhongMaterial material4 = new PhongMaterial();  material4.setSpecularMap(new Image("http://www.tutorialspoint.com/images/tp-logo.gif")); //Setting the Specular Map material to Cylinder4 cylinder4.setMaterial(material4);  //Drawing Cylinder5 Cylinder cylinder5 = new Cylinder();         //Setting the properties of the Cylinder cylinder5.setHeight(130.0f); cylinder5.setRadius(30.0f);   //Setting the position of the Cylinder cylinder5.setTranslateX(100); cylinder5.setTranslateY(300); //Preparing the phong material of type diffuse color PhongMaterial material5 = new PhongMaterial();  material5.setDiffuseColor(Color.BLANCHEDALMOND); //Setting the diffuse color material to Cylinder5 cylinder5.setMaterial(material5);   //Drawing Cylinder6  Cylinder cylinder6 = new Cylinder();         //Setting the properties of the Cylinder cylinder6.setHeight(130.0f); cylinder6.setRadius(30.0f);   //Setting the position of the Cylinder cylinder6.setTranslateX(200); cylinder6.setTranslateY(300); //Preparing the phong material of type specular color PhongMaterial material6 = new PhongMaterial();  //setting the specular color map to the material material6.setSpecularColor(Color.BLANCHEDALMOND); //Setting the specular color material to Cylinder6 cylinder6.setMaterial(material6);    //Drawing Cylinder7 Cylinder cylinder7 = new Cylinder();//Setting the properties of the Cylinder cylinder7.setHeight(130.0f); cylinder7.setRadius(30.0f);   //Setting the position of the Cylinder cylinder7.setTranslateX(300); cylinder7.setTranslateY(300); //Preparing the phong material of type Specular Power PhongMaterial material7 = new PhongMaterial();  material7.setSpecularPower(0.1); //Setting the Specular Power material to the Cylinder cylinder7.setMaterial(material7);         //Creating a Group object  Group root = new Group(cylinder1 ,cylinder2, cylinder3, cylinder4, cylinder5, cylinder6, cylinder7); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting camera PerspectiveCamera camera = new PerspectiveCamera(false); camera.setTranslateX(0); camera.setTranslateY(0); camera.setTranslateZ(-10); scene.setCamera(camera); //Setting title to the Stage stage.setTitle("Drawing a cylinder"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); }      public static void main(String args[]){ launch(args); } 
}


http://www.ppmy.cn/server/164666.html

相关文章

从0到1:C++ 开启游戏开发奇幻之旅(二)

目录 游戏开发核心组件设计 游戏循环 游戏对象管理 碰撞检测 人工智能(AI) 与物理引擎 人工智能 物理引擎 性能优化技巧 内存管理优化 多线程处理 实战案例:开发一个简单的 2D 射击游戏 项目结构设计 代码实现 总结与展望 游戏…

Visual Basic语言的云计算

以Visual Basic语言的云计算 引言 随着信息技术的快速发展,云计算已逐渐成为现代企业和个人用户的重要选择。它提供了高效、灵活和可扩展的计算资源,彻底改变了传统计算的模式。与此同时,Visual Basic(VB)作为一种易…

unity学习26:用Input接口去监测: 鼠标,键盘,虚拟轴,虚拟按键

目录 1 用Input接口去监测:鼠标,键盘,虚拟轴,虚拟按键 2 鼠标 MouseButton 事件 2.1 鼠标的基本操作 2.2 测试代码 2.3 测试情况 3 键盘Key事件 3.1 键盘的枚举方式 3.2 测试代码同上 3.3 测试代码同上 3.4 测试结果 4…

基于语义-拓扑-度量表征引导的大语言模型推理的空中视觉语言导航

1. 摘要翻译及主要贡献点 摘要: 空中视觉语言导航(VLN)是一项新兴任务,它使无人机能够通过自然语言指令和视觉线索在户外环境中导航。由于户外空中场景中复杂的空间关系,这项任务仍然具有挑战性。本文提出了一种端到…

车载软件架构 --- 基于AUTOSAR软件架构的ECU开发流程小白篇

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 简单,单纯,喜欢独处,独来独往,不易合同频过着接地气的生活…

LVM 逻辑卷管理

LVM介绍 管理磁盘、使用磁盘的一种方式的称呼 优势: 1、在不影响数据的情况下,随意扩容、缩容 2、支持快照功能,方便数据备份 LVM工作流程 磁盘/分区 ....> pv(物理卷)....> vg(卷组)...…

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_strerror_init()函数

目录 ngx_strerror_init()函数声明 ngx_int_t 类型声明定义 intptr_t 类型 ngx_strerror_init()函数实现 NGX_HAVE_STRERRORDESC_NP ngx_strerror_init()函数声明 在 nginx.c 的开头引入了: #include <ngx_core.h> 在 ngx_core.h 中引入了 #include <ngx_er…

Windsurf cursor vscode+cline 与Python快速开发指南

Windsurf简介 Windsurf是由Codeium推出的全球首个基于AI Flow范式的智能IDE&#xff0c;它通过强大的AI助手功能&#xff0c;显著提升开发效率。Windsurf集成了先进的代码补全、智能重构、代码生成等功能&#xff0c;特别适合Python开发者使用。 Python环境配置 1. Conda安装…