在立方体上设置法线或UV,并向 3D 实体添加材质
Contents
[
Hide
]
Aspose.3D for .NET 提供管理几何图形上的法线和UV。网格存储每个顶点的关键属性是其在空间中的位置及其法线,垂直于原始曲面的向量。正常对于阴影计数是主要的。法线应该是单位向量。大多数网格格式还支持某种形式的UV坐标,它们是网格 “展开” 的单独2d表示,以显示二维纹理贴图的哪个部分应用于网格的不同多边形。
创建正常向量
为了在照明上有一个很好的视觉外观,我们需要为每个顶点指定法线信息,为了有更好的细节,我们还可以使用法线和漫反射贴图 (当然你可以使用阴影/镜面贴图) 来执行每像素法线/颜色。VertexElement实现了每个顶点的信息,如法线或顶点颜色。在 Aspose.3D 中,我们可以将额外的信息映射到控制点/多边形顶点/多边形/边,这是一个为顶点定义法线的示例:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-3d/Aspose.3D-for-.NET | |
// Raw normal data | |
Vector4[] normals = new Vector4[] | |
{ | |
new Vector4(-0.577350258,-0.577350258, 0.577350258, 1.0), | |
new Vector4( 0.577350258,-0.577350258, 0.577350258, 1.0), | |
new Vector4( 0.577350258, 0.577350258, 0.577350258, 1.0), | |
new Vector4(-0.577350258, 0.577350258, 0.577350258, 1.0), | |
new Vector4(-0.577350258,-0.577350258,-0.577350258, 1.0), | |
new Vector4( 0.577350258,-0.577350258,-0.577350258, 1.0), | |
new Vector4( 0.577350258, 0.577350258,-0.577350258, 1.0), | |
new Vector4(-0.577350258, 0.577350258,-0.577350258, 1.0) | |
}; | |
// Call Common class create mesh using polygon builder method to set mesh instance | |
Mesh mesh = Common.CreateMeshUsingPolygonBuilder(); | |
VertexElementNormal elementNormal = mesh.CreateElement(VertexElementType.Normal, MappingMode.ControlPoint, ReferenceMode.Direct) as VertexElementNormal; | |
// Copy the data to the vertex element | |
elementNormal.Data.AddRange(normals); |
8个法线向量直接映射到8个控制点,在接下来的例子中,我们将演示一个更复杂的场景。
创建UV坐标
在这里,我们只定义了4个UV坐标,但是通过使用索引将它们应用于24个多边形顶点 (每个多边形6面 * 4个顶点)。 Aspose.3D 提供5种映射模式:
ControlPoint
-每个数据都映射到几何图形的控制点。PolygonVertex
-数据映射到多边形的顶点。Polygon
-数据映射到多边形。Edge
-数据映射到边缘。AllSame
-映射到整个几何图形的一个数据。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-3d/Aspose.3D-for-.NET | |
// UVs | |
Vector4[] uvs = new Vector4[] | |
{ | |
new Vector4( 0.0, 1.0,0.0, 1.0), | |
new Vector4( 1.0, 0.0,0.0, 1.0), | |
new Vector4( 0.0, 0.0,0.0, 1.0), | |
new Vector4( 1.0, 1.0,0.0, 1.0) | |
}; | |
// Indices of the uvs per each polygon | |
int[] uvsId = new int[] | |
{ | |
0,1,3,2,2,3,5,4,4,5,7,6,6,7,9,8,1,10,11,3,12,0,2,13 | |
}; | |
// Call Common class create mesh using polygon builder method to set mesh instance | |
Mesh mesh = Common.CreateMeshUsingPolygonBuilder(); | |
// Create UVset | |
VertexElementUV elementUV = mesh.CreateElementUV(TextureMapping.Diffuse, MappingMode.PolygonVertex, ReferenceMode.IndexToDirect); | |
// Copy the data to the UV vertex element | |
elementUV.Data.AddRange(uvs); | |
elementUV.Indices.AddRange(uvsId); |
将材质添加到 3D 个对象
Aspose.3D for .NET 允许开发人员使用着色算法实现精确的着色和高光。Phong有几个地图输入,我们可以用它来屏蔽节点的效果。基于物理的渲染 (PBR) 考虑了对象的一些物理属性,这种方法提供了真实世界中材质的外观。
立方体纹理Phong材料
当UV坐标准备使用时,我们可以通过使用材料在网格表面上施加纹理。只有顶点颜色不能描述表面的细节,这就是材料的用途。以下是将Phong材质附加到多维数据集节点的示例:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-3d/Aspose.3D-for-.NET | |
// Initialize scene object | |
Scene scene = new Scene(); | |
// Initialize cube node object | |
Node cubeNode = new Node("cube"); | |
// Call Common class create mesh using polygon builder method to set mesh instance | |
Mesh mesh = Common.CreateMeshUsingPolygonBuilder(); | |
// Point node to the mesh | |
cubeNode.Entity = mesh; | |
// Add cube to the scene | |
scene.RootNode.ChildNodes.Add(cubeNode); | |
// Initiallize PhongMaterial object | |
PhongMaterial mat = new PhongMaterial(); | |
// Initiallize Texture object | |
Texture diffuse = new Texture(); | |
// The path to the documents directory. | |
// Set local file path | |
diffuse.FileName = RunExamples.GetOutputFilePath("surface.dds"); | |
// Set Texture of the material | |
mat.SetTexture("DiffuseColor", diffuse); | |
// Embed raw content data to FBX (only for FBX and optional) | |
// Set file name | |
diffuse.FileName = "embedded-texture.png"; | |
// Set binary content | |
diffuse.Content = File.ReadAllBytes(RunExamples.GetDataFilePath("aspose-logo.jpg")); | |
// Set color | |
mat.SpecularColor = new Vector3(Color.Red); | |
// Set brightness | |
mat.Shininess = 100; | |
// Set material property of the cube object | |
cubeNode.Material = mat; | |
// Save 3D scene in the supported file formats | |
scene.Save("MaterialToCube.fbx"); |
我们指定了漫射纹理映射和带有光泽参数的镜面颜色。
将基于物理的渲染 (PBR) 材料应用于盒子
PBR在游戏引擎视觉效果中起着关键作用,它通过衰减亮度和反射光的散射来有效且逼真地渲染光和表面之间的相互作用。开发人员可以使用 Aspose.3D API 将PBR材质应用于场景中的 3D 对象。此代码示例演示如何创建Box对象,然后应用PBR材质。
.NET, C#
// initialize a scene
Scene scene = new Scene();
// initialize PBR material object
PbrMaterial mat = new PbrMaterial();
// an almost metal material
mat.MetallicFactor = 0.9;
// material surface is very rough
mat.RoughnessFactor = 0.9;
// create a box to which the material will be applied
var boxNode = scene.RootNode.CreateChildNode("box", new Box());
boxNode.Material = mat;
// save 3d scene into STL format
scene.Save(@"C:\3D\PBR_Material_Box_Out.stl", FileFormat.STLASCII);