iOS Guides | Samples

SceneKit.SCNGeometry.Create Method

Creates a new geometry object from the specified arrays of sources and elements.

Syntax

[Foundation.Export("geometryWithSources:elements:")]
[ObjCRuntime.BindingImpl(ObjCRuntime.BindingImplOptions.GeneratedCode | ObjCRuntime.BindingImplOptions.Optimizable)]
public static SCNGeometry Create (SCNGeometrySource[] sources, SCNGeometryElement[] elements)

Parameters

sources
An array of SCNGeometrySource objects.
elements
An array of SCNGeometryElement objects.

This parameter can be null.

Remarks

Used to create custom geometry. Depending on the needs of the developer, the number and types of the sources and elements arrays can be highly variable. The following example shows the use of vertices, defining triangles, on which a single texture is mapped. Note how specific index values within locs are used to define indices and how the ordering within locs affects normals and txCoords. Also, notice how the triplets in indices are linked to SCNGeometryPrimitiveType.Triangles.

C# Example

//Lower-left
var a = new SCNVector3(-1, -1, 0);
//Upper-right
var b = new SCNVector3(1, 1, 0);

var halfX = (c.X + a.X) / 2;
var halfY = (c.Y + a.Y) / 2;
var halfZ = (c.Z + a.Z) / 2;

var b = new SCNVector3(a.X, c.Y, halfZ);
var d = new SCNVector3(c.X, a.Y, halfZ);
//Elevate the midpoint so that it's clearly a pyramid
var midPoint = new SCNVector3(halfX, halfY, halfZ + 1.0);

//The vertices of the geometry
var locs = new [] {
	a, b, c, d, midPoint
};
var locSource = SCNGeometrySource.FromVertices(locs);

//Note that this relies on the ordering of locs above
//and it defines triangles (could be triangle strips, etc.)
var indices = new [] {
	//Triangles are defined counter-clockwise!
	4, 1, 0,
	1, 4, 2,
	2, 4, 3,
	3, 4, 0
};


var idxArray = new byte[indices.Length][];
for(int i = 0; i < idxArray.Length; i++)
{
	idxArray[i] = BitConverter.GetBytes(indices[i]);
}
var idxData = NSData.FromArray(idxArray.SelectMany(id => id).ToArray());

//Note that this relies on indices defining triangles
var element = SCNGeometryElement.FromData(idxData, SCNGeometryPrimitiveType.Triangles, indices.Length / 3, sizeof(int));

//Normals are relative to geometry
var normals = new [] {
	new SCNVector3(0, 0, 1),
	new SCNVector3(0, 0, 1),
	new SCNVector3(0, 0, 1),
	new SCNVector3(0, 0, 1),
	new SCNVector3(0, 0, 1),
};;
var normSource = SCNGeometrySource.FromNormals(normals);

var txCoords = new [] {
	new CGPoint(0, 0),
	new CGPoint(0, 1),
	new CGPoint(1, 1),
	new CGPoint(1, 0),
  new CGPoint(0.5, 0.5)
};

var txCoordsSource = SCNGeometrySource.FromTextureCoordinates(txCoords);

var geometry = SCNGeometry.Create(new [] { locSource, normSource, txCoordsSource }, new [] { element });

Requirements

Namespace: SceneKit
Assembly: Xamarin.iOS (in Xamarin.iOS.dll)
Assembly Versions: 0.0.0.0