private void drawItem() { bool RColor = true; foreach (var i in items) { var w = (nfloat)i.PackDimX; var h = (nfloat)i.PackDimY; var d = (nfloat)i.PackDimZ; var item = SCNBox.Create(w, h, d, 0); var itemNode = SCNNode.FromGeometry(item); if (RColor) { item.FirstMaterial.Diffuse.Contents = randomColor(RColor); RColor = false; } else { item.FirstMaterial.Diffuse.Contents = randomColor(RColor); RColor = true; } itemNode.Position = calculateRelativePosition(i); itemsNodeMap.Add(i, itemNode); scene.RootNode.AddChildNode(itemNode); } }
private void PresentPrimitives(PresentationViewController presentationViewController) { var count = 100; var spread = 0.0f; // create a cube with a sphere shape for (int i = 0; i < count; ++i) { var model = SCNNode.Create(); model.Position = GroundNode.ConvertPositionToNode(new SCNVector3(RandFloat(-1, 1), RandFloat(30, 50), RandFloat(-1, 1)), null); model.EulerAngles = new SCNVector3(RandFloat(0, NMath.PI * 2), RandFloat(0, NMath.PI * 2), RandFloat(0, NMath.PI * 2)); var size = new SCNVector3(RandFloat(1.0, 1.5), RandFloat(1.0, 1.5), RandFloat(1.0, 1.5)); var random = new Random((int)DateTime.Now.Ticks); int geometryIndex = random.Next(0, 7); switch (geometryIndex) { case 0: // Box model.Geometry = SCNBox.Create(size.X, size.Y, size.Z, 0); break; case 1: // Pyramid model.Geometry = SCNPyramid.Create(size.X, size.Y, size.Z); break; case 2: // Sphere model.Geometry = SCNSphere.Create(size.X); break; case 3: // Cylinder model.Geometry = SCNCylinder.Create(size.X, size.Y); break; case 4: // Tube model.Geometry = SCNTube.Create(size.X, size.X + size.Z, size.Y); break; case 5: // Capsule model.Geometry = SCNCapsule.Create(size.X, size.Y + 2 * size.X); break; case 6: // Torus model.Geometry = SCNTorus.Create(size.X, NMath.Min(size.X, size.Y) / 2); break; default: break; } model.Geometry.FirstMaterial.Multiply.Contents = new NSImage(NSBundle.MainBundle.PathForResource("SharedTextures/texture", "png")); model.PhysicsBody = SCNPhysicsBody.CreateDynamicBody(); model.PhysicsBody.Velocity = new SCNVector3(RandFloat(-spread, spread), -10, RandFloat(-spread, spread)); model.PhysicsBody.AngularVelocity = new SCNVector4(RandFloat(-1, 1), RandFloat(-1, 1), RandFloat(-1, 1), RandFloat(-3, 3)); Shapes.Add(model); ((SCNView)presentationViewController.View).Scene.RootNode.AddChildNode(model); } }
public override void SetupSlide(PresentationViewController presentationViewController) { // Set the slide's title and subtitle and add some text TextManager.SetTitle("Materials"); TextManager.SetSubtitle("Property contents"); TextManager.AddBulletAtLevel("Color", 0); TextManager.AddBulletAtLevel("CGColorRef / NSColor / UIColor", 0); var node = SCNNode.Create(); node.Name = "material-cube"; node.Geometry = SCNBox.Create(W, W, W, W * 0.02f); Material = node.Geometry.FirstMaterial; Material.Diffuse.Contents = NSColor.Red; Object = node; node.Position = new SCNVector3(8, 11, 0); ContentNode.AddChildNode(node); node.RunAction(SCNAction.RepeatActionForever(SCNAction.RotateBy((float)Math.PI * 2, new SCNVector3(0.4f, 1, 0), 4))); MaterialLayerSlideReference = this; }
public void Set(float x, float y, float width, float height, float radius, ref Style style) { if (this.x != x || this.y != y || this.width != width || this.height != height || this.radius != radius) { //Console.WriteLine ($"({this.sx} == {sx} && {this.sy} == {sy} && {this.ex} == {ex} && {this.ey == ey})"); this.x = x; this.y = y; this.width = width; this.height = height; this.radius = radius; //Console.WriteLine (style.Transform); var g = SCNBox.Create(this.width, this.height, radius * 2, radius); g.FirstMaterial = GetNativeMaterial(style.Color); Geometry = g; base.Transform = SCNMatrix4.CreateTranslation(this.x + this.width / 2, this.y + this.height / 2, 0) * style.Transform ; } if (ColorChanged(ref style)) { this.style.Color = style.Color; if (Geometry is SCNGeometry g) { g.FirstMaterial = GetNativeMaterial(style.Color); } } }
public override void ViewDidLoad() { base.ViewDidLoad(); // Perform any additional setup after loading the view, typically from a nib. //let's create a geometric object var box = SCNBox.Create(0.1f, 0.1f, 0.1f, 0); box.FirstMaterial.Diffuse.ContentColor = UIColor.Green; //let's add the box to a SCNNode var newNode = new SCNNode(); newNode.Geometry = box; newNode.Position = new SCNVector3(0, 0, -1); //Let's create a scene where this node will live in var scene = new SCNScene(); scene.RootNode.Add(newNode); //set the scene in the sceneview to display it. SceneView.Scene = scene; SceneView.DebugOptions = ARKit.ARSCNDebugOptions.ShowFeaturePoints | ARKit.ARSCNDebugOptions.ShowWorldOrigin; SceneView.Session.Run(new ARKit.ARWorldTrackingConfiguration() { PlaneDetection = ARKit.ARPlaneDetection.Horizontal, LightEstimationEnabled = true, AutoFocusEnabled = true, WorldAlignment = ARKit.ARWorldAlignment.Gravity }, ARKit.ARSessionRunOptions.ResetTracking); }
void Initialize() { BackgroundColor = NSColor.Black; var root = scene.RootNode; root.AddChildNode(camNode); root.AddChildNode(viewsNode); var backgroundGeom = SCNBox.Create(10000, 10000, 10, 0); backgroundGeom.FirstMaterial = Materials.Plastic(Xyzw(0.8, 1, 0.8, 1), roughness: 0.1).SCNMaterial; var backgroundNode = SCNNode.FromGeometry(backgroundGeom); backgroundNode.Position = Xyz(0, 0, -5.01).ToSCNVector3(); viewsNode.AddChildNode(backgroundNode); AllowsCameraControl = true; scene.Background.ContentImage = new NSImage( NSBundle.MainBundle.PathForResource("environment", "jpg")); scene.Background.ContentsTransform = SCNMatrix4.CreateRotationX((float)(Math.PI / 2)); scene.LightingEnvironment.ContentImage = scene.Background.ContentImage; scene.LightingEnvironment.ContentsTransform = SCNMatrix4.CreateRotationX((float)(Math.PI / 2)); Scene = scene; UpdateCamera(); }
public void CreateUser() { userShape = SCNBox.Create(8.0f, 7.0f, 15.0f, 0.005f); user = SCNNode.FromGeometry(userShape); // Creating a new node, user, that inherits the shape of the SCNBox, userShape user.Position = new SCNVector3(10, 4, -50); user.PhysicsBody = SCNPhysicsBody. // Trying to set a physics body for the user in order for us to give controls to it gameScene.RootNode.AddChildNode(user); }
private static SCNGeometry CreateGeometry(float size, UIColor color) { var geometry = SCNBox.Create(size, size, size, 0); var material = new SCNMaterial(); material.Diffuse.Contents = color; geometry.Materials = new[] { material }; return(geometry); }
private SCNGeometry CreateBlockMesh(SCNVector3 size) { var diceMesh = SCNBox.Create(size.X, size.Y, size.Z, 0.05f * size.X); diceMesh.FirstMaterial.Diffuse.Contents = new NSImage(NSBundle.MainBundle.PathForResource("SharedTextures/texture", "png")); diceMesh.FirstMaterial.Diffuse.MipFilter = SCNFilterMode.Linear; diceMesh.FirstMaterial.Diffuse.WrapS = SCNWrapMode.Repeat; diceMesh.FirstMaterial.Diffuse.WrapT = SCNWrapMode.Repeat; return(diceMesh); }
//TODO 2.1 Añadiendo elementos private void AddBox() { SCNNode boxNode = new SCNNode(); boxNode.Geometry = SCNBox.Create(0.05f, 0.05f, 0.05f, 0); boxNode.Geometry.FirstMaterial.Diffuse.Contents = UIColor.Red; //Color del objeto boxNode.Geometry.FirstMaterial.Specular.Contents = UIColor.White; //Color del reflejo boxNode.Position = new SCNVector3(0.1f, 0, 0); sceneView.Scene.RootNode.AddChildNode(boxNode); }
SCNNode MakeBox(ARAnchor anchor) { Console.WriteLine($"MakeBox ({anchor})"); var geometry = SCNBox.Create(0.1f, 0.1f, 0.1f, 0); geometry.FirstMaterial.Diffuse.ContentColor = UIColor.Yellow; var gnode = SCNNode.FromGeometry(geometry); return(gnode); }
private static SCNBox CreateGeometry(ARPlaneAnchor planeAnchor) { var geometry = SCNBox.Create(planeAnchor.Extent.X, PlaneHeight, planeAnchor.Extent.Z, 0); var topMaterial = new SCNMaterial(); var bottomMaterial = new SCNMaterial(); topMaterial.Diffuse.Contents = UIColor.FromRGBA(255, 255, 255, 20); bottomMaterial.Diffuse.Contents = UIColor.Black; geometry.Materials = new[] { topMaterial, bottomMaterial, bottomMaterial, bottomMaterial }; return(geometry); }
public CubeNode(float size, UIColor color) { SCNMaterial material = new SCNMaterial(); material.Diffuse.Contents = color; SCNBox geometry = SCNBox.Create(size, size, size, 0); geometry.Materials = new[] { material }; SCNNode node = new SCNNode { Geometry = geometry }; AddChildNode(node); }
private void AddRelativeNodes() { SCNNode boxNode = new SCNNode(); boxNode.Geometry = SCNBox.Create(0.05f, 0.05f, 0.05f, 0); boxNode.Geometry.FirstMaterial.Diffuse.Contents = UIColor.Red; //Color del objeto boxNode.Geometry.FirstMaterial.Specular.Contents = UIColor.White; //Color del reflejo boxNode.Position = new SCNVector3(0.1f, 0, 0); SCNNode sphereNode = new SCNNode(); sphereNode.Geometry = SCNSphere.Create(0.05f / 2); sphereNode.Geometry.FirstMaterial.Diffuse.Contents = UIColor.Blue; //Color del objeto sphereNode.Geometry.FirstMaterial.Specular.Contents = UIColor.White; //Color del reflejo sphereNode.Position = new SCNVector3(0.1f, 0, 0); sceneView.Scene.RootNode.AddChildNode(boxNode); //Se coloca boxNode en una posición absoluta respecto a rootNode boxNode.AddChildNode(sphereNode); //Se coloca sphereNode en una posición relativa a boxNode }
private void PresentHinge(PresentationViewController presentationViewController) { var count = 10.0f; var material = SCNMaterial.Create(); material.Diffuse.Contents = NSColor.White; material.Specular.Contents = NSColor.White; material.LocksAmbientWithDiffuse = true; var cubeWidth = 10.0f / count; var cubeHeight = 0.2f; var cubeLength = 5.0f; var offset = 0; var height = 5 + count * cubeWidth; SCNNode oldModel = null; for (int i = 0; i < count; ++i) { var model = SCNNode.Create(); var worldtr = GroundNode.ConvertTransformToNode(SCNMatrix4.CreateTranslation(-offset + cubeWidth * i, height, 5), null); model.Transform = worldtr; model.Geometry = SCNBox.Create(cubeWidth, cubeHeight, cubeLength, 0); model.Geometry.FirstMaterial = material; var body = SCNPhysicsBody.CreateDynamicBody(); body.Restitution = 0.6f; model.PhysicsBody = body; ((SCNView)presentationViewController.View).Scene.RootNode.AddChildNode(model); var joint = SCNPhysicsHingeJoint.Create(model.PhysicsBody, new SCNVector3(0, 0, 1), new SCNVector3(-cubeWidth * 0.5f, 0, 0), (oldModel != null ? oldModel.PhysicsBody : null), new SCNVector3(0, 0, 1), new SCNVector3(cubeWidth * 0.5f, 0, 0)); ((SCNView)presentationViewController.View).Scene.PhysicsWorld.AddBehavior(joint); Hinges.Add(model); oldModel = model; } }
SCNNode CreateTorchNode() { SCNGeometry geometry = SCNBox.Create(20f, 100f, 20f, 0f); geometry.FirstMaterial.Diffuse.Contents = AppKit.NSColor.Brown; var template = new SCNNode { Geometry = geometry }; var particleEmitter = new SCNNode { Position = new SCNVector3(0f, 50f, 0f) }; SCNParticleSystem fire = GameSimulation.LoadParticleSystemWithName("torch", "spark"); particleEmitter.AddParticleSystem(fire); particleEmitter.Light = TorchLight; template.AddChildNode(particleEmitter); return(template); }
private void drawBox() { var border = 0.05f; //var w = (nfloat)(Math.Round(Box.Width, 0, MidpointRounding.AwayFromZero) + border); //var h = (nfloat)(Math.Round(Box.Height, 0, MidpointRounding.AwayFromZero) + border); //var d = (nfloat)(Math.Round(Box.Depth, 0, MidpointRounding.AwayFromZero) + border); var w = (nfloat)Box.Width + border; var h = (nfloat)Box.Height + border; var d = (nfloat)Box.Depth + border; var boxGeometry = SCNBox.Create(d, h, w, 0); boxNode = SCNNode.FromGeometry(boxGeometry); boxGeometry.FirstMaterial.Diffuse.Contents = UIColor.Black; boxGeometry.FirstMaterial.FillMode = SCNFillMode.Lines; boxGeometry.FirstMaterial.DoubleSided = true; boxNode.Position = new SCNVector3(0, 0, 0); scene.RootNode.AddChildNode(boxNode); }
public static SCNNode CreateAxesNode(float quiverLength, float quiverThickness) { quiverThickness = (quiverLength / 50f) * quiverThickness; var chamferRadius = quiverThickness / 2f; var xQuiverBox = SCNBox.Create(quiverLength, quiverThickness, quiverThickness, chamferRadius); xQuiverBox.InsertMaterial(SCNMaterialExtensions.CreateMaterial(UIColor.Red, false), 0); var xQuiverNode = SCNNode.FromGeometry(xQuiverBox); xQuiverNode.Position = new SCNVector3((quiverLength / 2f), 0, 0); var yQuiverBox = SCNBox.Create(quiverThickness, quiverLength, quiverThickness, chamferRadius); yQuiverBox.InsertMaterial(SCNMaterialExtensions.CreateMaterial(UIColor.Green, false), 0); var yQuiverNode = SCNNode.FromGeometry(yQuiverBox); yQuiverNode.Position = new SCNVector3(0, (quiverLength / 2f), 0); var zQuiverBox = SCNBox.Create(quiverThickness, quiverThickness, quiverLength, chamferRadius); zQuiverBox.InsertMaterial(SCNMaterialExtensions.CreateMaterial(UIColor.Blue, false), 0); var zQuiverNode = SCNNode.FromGeometry(zQuiverBox); zQuiverNode.Position = new SCNVector3(0, 0, (quiverLength / 2f)); // Assemble node var quiverNode = new SCNNode() { Name = "Axes" }; quiverNode.AddChildNode(xQuiverNode); quiverNode.AddChildNode(yQuiverNode); quiverNode.AddChildNode(zQuiverNode); // Return results return(quiverNode); }
private void AddHouse() { var pyramidNode = new SCNNode(); pyramidNode.Geometry = SCNPyramid.Create(0.1f, 0.1f, 0.1f); pyramidNode.Geometry.FirstMaterial.Diffuse.Contents = UIColor.Red; pyramidNode.Position = new SCNVector3(0, 0, -0.2f); sceneView.Scene.RootNode.AddChildNode(pyramidNode); var boxNode = new SCNNode(); boxNode.Geometry = SCNBox.Create(0.1f, 0.1f, 0.1f, 0); boxNode.Geometry.FirstMaterial.Diffuse.Contents = UIColor.Blue; boxNode.Position = new SCNVector3(0, -0.05f, 0); pyramidNode.AddChildNode(boxNode); var door = new SCNNode(); door.Geometry = SCNPlane.Create(0.03f, 0.06f); door.Geometry.FirstMaterial.Diffuse.Contents = UIColor.Brown; door.Position = new SCNVector3(0, -0.02f, 0.051f); boxNode.AddChildNode(door); var window1 = new SCNNode(); window1.Geometry = SCNPlane.Create(0.02f, 0.02f); window1.Geometry.FirstMaterial.Diffuse.Contents = UIColor.White; window1.Position = new SCNVector3(0.03f, 0.025f, 0.051f); boxNode.AddChildNode(window1); var window2 = new SCNNode(); window2.Geometry = SCNPlane.Create(0.02f, 0.02f); window2.Geometry.FirstMaterial.Diffuse.Contents = UIColor.White; window2.Position = new SCNVector3(-0.03f, 0.025f, 0.051f); boxNode.AddChildNode(window2); }
private void VisualizeBones(bool show, SCNNode node, nfloat scale) { // We propagate an inherited scale so that the boxes // representing the bones will be of the same size scale *= node.Scale.X; if (show) { if (node.Geometry == null) { node.Geometry = SCNBox.Create(6.0f / scale, 6.0f / scale, 6.0f / scale, 0.5f); } } else { if (node.Geometry.GetType() == typeof(SCNBox)) { node.Geometry = null; } } //foreach (SCNNode child in node.ChildNodes) //VisualizeBones (show, child, scale); }
public override void PresentStep(int index, PresentationViewController presentationViewController) { // Animate by default SCNTransaction.Begin(); SCNTransaction.AnimationDuration = 0; switch (index) { case 0: TextManager.FlipInText(SlideTextManager.TextType.Bullet); break; case 1: TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddEmptyLine(); TextManager.AddCode("#// Rotate forever\n" + "[aNode #runAction:#\n" + " [SCNAction repeatActionForever:\n" + " [SCNAction rotateByX:0 y:M_PI*2 z:0 duration:5.0]]];#"); TextManager.FlipInText(SlideTextManager.TextType.Code); break; case 2: TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.FlipOutText(SlideTextManager.TextType.Code); TextManager.AddBulletAtLevel("Move", 0); TextManager.AddBulletAtLevel("Rotate", 0); TextManager.AddBulletAtLevel("Scale", 0); TextManager.AddBulletAtLevel("Opacity", 0); TextManager.AddBulletAtLevel("Remove", 0); TextManager.AddBulletAtLevel("Wait", 0); TextManager.AddBulletAtLevel("Custom block", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); break; case 3: TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddEmptyLine(); TextManager.AddBulletAtLevel("Directly targets the render tree", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); break; case 4: TextManager.AddBulletAtLevel("node.position / node.presentationNode.position", 0); //labels var label1 = TextManager.AddTextAtLevel("Action", 0); label1.Position = new SCNVector3(-15, 3, 0); var label2 = TextManager.AddTextAtLevel("Animation", 0); label2.Position = new SCNVector3(-15, -2, 0); //animation var animNode = SCNNode.Create(); var cubeSize = 4.0f; animNode.Position = new SCNVector3(-5, cubeSize / 2, 0); var cube = SCNBox.Create(cubeSize, cubeSize, cubeSize, 0.05f * cubeSize); cube.FirstMaterial.Diffuse.Contents = new NSImage(NSBundle.MainBundle.PathForResource("SharedTextures/texture", "png")); cube.FirstMaterial.Diffuse.MipFilter = SCNFilterMode.Linear; cube.FirstMaterial.Diffuse.WrapS = SCNWrapMode.Repeat; cube.FirstMaterial.Diffuse.WrapT = SCNWrapMode.Repeat; animNode.Geometry = cube; ContentNode.AddChildNode(animNode); SCNTransaction.Begin(); SCNNode animPosIndicator = null; SCNAnimationEvent startEvt = SCNAnimationEvent.Create(0, (CAAnimation animation, NSObject animatedObject, bool playingBackward) => { SCNTransaction.Begin(); SCNTransaction.AnimationDuration = 0; animPosIndicator.Position = new SCNVector3(10, animPosIndicator.Position.Y, animPosIndicator.Position.Z); SCNTransaction.Commit(); }); SCNAnimationEvent endEvt = SCNAnimationEvent.Create(1, (CAAnimation animation, NSObject animatedObject, bool playingBackward) => { SCNTransaction.Begin(); SCNTransaction.AnimationDuration = 0; animPosIndicator.Position = new SCNVector3(-5, animPosIndicator.Position.Y, animPosIndicator.Position.Z); SCNTransaction.Commit(); }); var anim = CABasicAnimation.FromKeyPath("position.x"); anim.Duration = 3; anim.From = new NSNumber(0.0); anim.To = new NSNumber(15.0); anim.Additive = true; anim.AutoReverses = true; anim.AnimationEvents = new SCNAnimationEvent[] { startEvt, endEvt }; anim.RepeatCount = float.MaxValue; animNode.AddAnimation(anim, new NSString("cubeAnimation")); //action var actionNode = SCNNode.Create(); actionNode.Position = new SCNVector3(-5, cubeSize * 1.5f + 1, 0); actionNode.Geometry = cube; ContentNode.AddChildNode(actionNode); var mv = SCNAction.MoveBy(15, 0, 0, 3); actionNode.RunAction(SCNAction.RepeatActionForever(SCNAction.Sequence(new SCNAction[] { mv, mv.ReversedAction() }))); //position indicator var positionIndicator = SCNNode.Create(); positionIndicator.Geometry = SCNCylinder.Create(0.5f, 0.01f); positionIndicator.Geometry.FirstMaterial.Diffuse.Contents = NSColor.Red; positionIndicator.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant; positionIndicator.EulerAngles = new SCNVector3(NMath.PI / 2, 0, 0); positionIndicator.Position = new SCNVector3(0, 0, cubeSize * 0.5f); actionNode.AddChildNode(positionIndicator); //anim pos indicator animPosIndicator = positionIndicator.Clone(); animPosIndicator.Position = new SCNVector3(5, cubeSize / 2, cubeSize * 0.5f); ContentNode.AddChildNode(animPosIndicator); SCNTransaction.Commit(); break; } SCNTransaction.Commit(); }
internal ObjectOriginAxis(Axis axis, float length, float thickness, float radius, float handleSize) { this.axis = axis; base.Init(); UIColor color = default(UIColor); UIImage texture = default(UIImage); SCNVector3 dimensions = default(SCNVector3); var position = new SCNVector3((float)(axis.Normal().X *(length / 2.0)), (float)(axis.Normal().Y *(length / 2.0)), (float)(axis.Normal().Z *(length / 2.0))); var axisHandlePosition = new SCNVector3((float)(axis.Normal().X *length), (float)(axis.Normal().Y *length), (float)(axis.Normal().Z *length)); switch (axis) { case Axis.X: color = UIColor.Red; texture = UIImage.FromFile("handle_red"); dimensions = new SCNVector3(length, thickness, thickness); break; case Axis.Y: color = UIColor.Green; texture = UIImage.FromFile("handle_green"); dimensions = new SCNVector3(thickness, length, thickness); break; case Axis.Z: color = UIColor.Blue; texture = UIImage.FromFile("handle_blue"); dimensions = new SCNVector3(thickness, thickness, length); break; } var axisGeo = SCNBox.Create(dimensions.X, dimensions.Y, dimensions.Z, radius); axisGeo.Materials = new[] { Utilities.Material(color) }; var axisNode = SCNNode.FromGeometry(axisGeo); var axisHandleGeo = SCNPlane.Create(handleSize, handleSize); axisHandleGeo.Materials = new[] { Utilities.Material(texture, false) }; var axisHandle = SCNNode.FromGeometry(axisHandleGeo); axisHandle.Constraints = new[] { new SCNBillboardConstraint() }; axisNode.Position = position; axisHandle.Position = axisHandlePosition; // Increase the axis handle geometry's bounding box that is used for hit testing to make it easier to hit SCNVector3 min = default(SCNVector3); SCNVector3 max = default(SCNVector3); axisHandle.GetBoundingBox(ref min, ref max); var padding = handleSize * 0.8F; var newMin = new SCNVector3(min.X - padding, min.Y - padding, min.Z - padding); var newMax = new SCNVector3(max.X + padding, max.Y + padding, max.Z + padding); axisHandle.SetBoundingBox(ref newMin, ref newMax); AddChildNode(axisNode); AddChildNode(axisHandle); }
public override void PresentStep(int index, PresentationViewController presentationViewController) { Step = index; switch (index) { case 0: break; case 1: TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.SetSubtitle("SCNPhysicsBody"); TextManager.FlipInText(SlideTextManager.TextType.Subtitle); TextManager.AddBulletAtLevel("Dynamic Bodies", 0); // Add some code TextManager.AddCode("#// Make a node dynamic\n" + "aNode.#physicsBody# = [SCNPhysicsBody #dynamicBody#];#"); TextManager.FlipInText(SlideTextManager.TextType.Bullet); TextManager.FlipInText(SlideTextManager.TextType.Code); break; case 2: //add a cube var worldPos = GroundNode.ConvertPositionToNode(new SCNVector3(0, 12, 2), null); var dice = CreateBlock(worldPos, new SCNVector3(1.5f, 1.5f, 1.5f)); dice.PhysicsBody = null; //wait! dice.Rotation = new SCNVector4(0, 0, 1, (float)(Math.PI / 4) * 0.5f); dice.Scale = new SCNVector3(0.001f, 0.001f, 0.001f); ((SCNView)presentationViewController.View).Scene.RootNode.AddChildNode(dice); SCNTransaction.Begin(); SCNTransaction.AnimationDuration = 0.75f; dice.Scale = new SCNVector3(2, 2, 2); SCNTransaction.Commit(); Dices.Add(dice); break; case 3: foreach (var node in Dices) { node.PhysicsBody = SCNPhysicsBody.CreateDynamicBody(); } break; case 4: PresentDices(presentationViewController); break; case 5: TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.FlipOutText(SlideTextManager.TextType.Code); TextManager.AddBulletAtLevel("Manipulate with forces", 0); // Add some code TextManager.AddCode("#// Apply an impulse\n" + "[aNode.physicsBody #applyForce:#aVector3 #atPosition:#aVector3 #impulse:#YES];#"); TextManager.FlipInText(SlideTextManager.TextType.Bullet); TextManager.FlipInText(SlideTextManager.TextType.Code); break; case 6: // remove dices var center = new SCNVector3(0, -5, 20); center = GroundNode.ConvertPositionToNode(center, null); Explosion(center, Dices); var popTime = new DispatchTime(DispatchTime.Now, (long)(1 * Utils.NSEC_PER_SEC)); DispatchQueue.MainQueue.DispatchAfter(popTime, () => { TextManager.FlipOutText(SlideTextManager.TextType.Code); TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddBulletAtLevel("Static Bodies", 0); TextManager.AddCode("#// Make a node static\n" + "aNode.#physicsBody# = [SCNPhysicsBody #staticBody#];#"); TextManager.FlipInText(SlideTextManager.TextType.Bullet); TextManager.FlipInText(SlideTextManager.TextType.Code); }); break; case 7: PresentWalls(presentationViewController); break; case 8: PresentBalls(presentationViewController); break; case 9: //remove walls var walls = new List <SCNNode> (); GroundNode.EnumerateChildNodes(delegate(SCNNode node, out bool stop) { stop = false; if (node.Name == "container-wall") { node.RunAction(SCNAction.Sequence(new SCNAction [] { SCNAction.MoveBy(new SCNVector3(0, -2, 0), 0.5), SCNAction.RemoveFromParentNode() })); walls.Add(node); } return(stop); }); break; case 10: // remove balls center = new SCNVector3(0, -5, 5); center = GroundNode.ConvertPositionToNode(center, null); Explosion(center, Balls); popTime = new DispatchTime(DispatchTime.Now, (long)(0.5 * Utils.NSEC_PER_SEC)); DispatchQueue.MainQueue.DispatchAfter(popTime, () => { TextManager.FlipOutText(SlideTextManager.TextType.Code); TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddBulletAtLevel("Kinematic Bodies", 0); TextManager.AddCode("#// Make a node kinematic\n" + "aNode.#physicsBody# = [SCNPhysicsBody #kinematicBody#];#"); TextManager.FlipInText(SlideTextManager.TextType.Bullet); TextManager.FlipInText(SlideTextManager.TextType.Code); }); break; case 11: var boxNode = SCNNode.Create(); boxNode.Geometry = SCNBox.Create(10, 0.2f, 10, 0); boxNode.Position = new SCNVector3(0, 5, MIDDLE_Z); boxNode.Geometry.FirstMaterial.Emission.Contents = NSColor.DarkGray; boxNode.PhysicsBody = SCNPhysicsBody.CreateKinematicBody(); boxNode.RunAction(SCNAction.RepeatActionForever(SCNAction.RotateBy(0, 0, NMath.PI * 2, 2.0))); GroundNode.AddChildNode(boxNode); KinematicItems.Add(boxNode); var invisibleWall = SCNNode.Create(); invisibleWall.Geometry = SCNBox.Create(4, 40, 10, 0); invisibleWall.Position = new SCNVector3(-7, 0, MIDDLE_Z); invisibleWall.Geometry.FirstMaterial.Transparency = 0; invisibleWall.PhysicsBody = SCNPhysicsBody.CreateStaticBody(); GroundNode.AddChildNode(invisibleWall); KinematicItems.Add(invisibleWall); invisibleWall = (SCNNode)invisibleWall.Copy(); invisibleWall.Position = new SCNVector3(7, 0, MIDDLE_Z); GroundNode.AddChildNode(invisibleWall); KinematicItems.Add(invisibleWall); invisibleWall = (SCNNode)invisibleWall.Copy(); invisibleWall.Geometry = SCNBox.Create(10, 40, 4, 0); invisibleWall.Geometry.FirstMaterial.Transparency = 0; invisibleWall.Position = new SCNVector3(0, 0, MIDDLE_Z - 7); invisibleWall.PhysicsBody = SCNPhysicsBody.CreateStaticBody(); GroundNode.AddChildNode(invisibleWall); KinematicItems.Add(invisibleWall); invisibleWall = (SCNNode)invisibleWall.Copy(); invisibleWall.Position = new SCNVector3(0, 0, MIDDLE_Z + 7); GroundNode.AddChildNode(invisibleWall); KinematicItems.Add(invisibleWall); for (int i = 0; i < 100; i++) { var ball = SCNNode.Create(); worldPos = boxNode.ConvertPositionToNode(new SCNVector3(RandFloat(-4, 4), RandFloat(10, 30), RandFloat(-1, 4)), null); ball.Position = worldPos; ball.Geometry = SCNSphere.Create(0.5f); ball.Geometry.FirstMaterial.Diffuse.Contents = NSColor.Cyan; ball.PhysicsBody = SCNPhysicsBody.CreateDynamicBody(); ((SCNView)presentationViewController.View).Scene.RootNode.AddChildNode(ball); KinematicItems.Add(ball); } break; case 12: TextManager.FlipOutText(SlideTextManager.TextType.Code); TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.FlipOutText(SlideTextManager.TextType.Subtitle); TextManager.SetSubtitle("SCNPhysicsShape"); TextManager.AddCode("#// Configure the physics shape\n\n" + "aNode.physicsBody.#physicsShape# = \n\t[#SCNPhysicsShape# shapeWithGeometry:aGeometry options:options];#"); TextManager.FlipInText(SlideTextManager.TextType.Bullet); TextManager.FlipInText(SlideTextManager.TextType.Code); KinematicItems[0].RunAction(SCNAction.Sequence(new SCNAction[] { SCNAction.FadeOut(0.5), SCNAction.RemoveFromParentNode() })); for (int i = 1; i < 5; i++) { KinematicItems[i].RemoveFromParentNode(); } KinematicItems = null; break; case 13: //add meshes PresentMeshes(presentationViewController); break; case 14: // remove meshes center = new SCNVector3(0, -5, 20); center = GroundNode.ConvertPositionToNode(center, null); Explosion(center, Meshes); break; case 15: // add shapes PresentPrimitives(presentationViewController); break; case 16: // remove shapes center = new SCNVector3(0, -5, 20); center = GroundNode.ConvertPositionToNode(center, null); Explosion(center, Shapes); popTime = new DispatchTime(DispatchTime.Now, (long)(0.5 * Utils.NSEC_PER_SEC)); DispatchQueue.MainQueue.DispatchAfter(popTime, () => { TextManager.FlipOutText(SlideTextManager.TextType.Code); TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.FlipOutText(SlideTextManager.TextType.Subtitle); TextManager.SetSubtitle("SCNPhysicsBehavior"); TextManager.AddCode("#// setup a physics behavior\n\n" + "#SCNPhysicsHingeJoint# *joint = [SCNPhysicsHingeJoint\n\n" + "jointWithBodyA:#nodeA.physicsBody# axisA:[...] anchorA:[...]\n\n" + "bodyB:#nodeB.physicsBody# axisB:[...] anchorB:[...]];\n\n\n" + "[scene.#physicsWorld# addBehavior:joint];#"); TextManager.FlipInText(SlideTextManager.TextType.Bullet); TextManager.FlipInText(SlideTextManager.TextType.Subtitle); TextManager.FlipInText(SlideTextManager.TextType.Code); }); break; case 17: //add meshes PresentHinge(presentationViewController); break; case 18: //remove constraints ((SCNView)presentationViewController.View).Scene.PhysicsWorld.RemoveAllBehaviors(); foreach (var node in Hinges) { node.RunAction(SCNAction.Sequence(new SCNAction[] { SCNAction.Wait(3.0), SCNAction.FadeOut(0.5), SCNAction.RemoveFromParentNode() })); } break; case 19: TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.FlipOutText(SlideTextManager.TextType.Subtitle); TextManager.FlipOutText(SlideTextManager.TextType.Code); TextManager.SetSubtitle("More..."); TextManager.FlipInText(SlideTextManager.TextType.Subtitle); TextManager.AddBulletAtLevel("SCNPhysicsField", 0); TextManager.AddBulletAtLevel("SCNPhysicsVehicle", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); TextManager.FlipInText(SlideTextManager.TextType.Code); break; } }
public void GetBoundingBox() { using (var obj = MDLObject.FromNode(SCNNode.FromGeometry(SCNBox.Create(1, 1, 1, 0)))) { var bb = obj.GetBoundingBox(0); } }
public override void PresentStep(int index, PresentationViewController presentationViewController) { switch (index) { case 0: // Set the slide's title and subtitle TextManager.SetTitle("Scene Graph"); TextManager.SetSubtitle("Summary"); break; case 1: // A node that will help visualize the position of the stars WireframeBoxNode = SCNNode.Create(); WireframeBoxNode.Rotation = new SCNVector4(0, 1, 0, (float)(Math.PI / 4)); WireframeBoxNode.Geometry = SCNBox.Create(1, 1, 1, 0); WireframeBoxNode.Geometry.FirstMaterial.Diffuse.Contents = new NSImage(NSBundle.MainBundle.PathForResource("SharedTextures/box_wireframe", "png")); WireframeBoxNode.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant; // no lighting WireframeBoxNode.Geometry.FirstMaterial.DoubleSided = true; // double sided // Sun SunNode = SCNNode.Create(); SunNode.Position = new SCNVector3(0, 30, 0); ContentNode.AddChildNode(SunNode); SunNode.AddChildNode((SCNNode)WireframeBoxNode.Copy()); // Earth-rotation (center of rotation of the Earth around the Sun) var earthRotationNode = SCNNode.Create(); SunNode.AddChildNode(earthRotationNode); // Earth-group (will contain the Earth, and the Moon) EarthGroupNode = SCNNode.Create(); EarthGroupNode.Position = new SCNVector3(15, 0, 0); earthRotationNode.AddChildNode(EarthGroupNode); // Earth EarthNode = (SCNNode)WireframeBoxNode.Copy(); EarthNode.Position = new SCNVector3(0, 0, 0); EarthGroupNode.AddChildNode(EarthNode); // Rotate the Earth around the Sun var animation = CABasicAnimation.FromKeyPath("rotation"); animation.Duration = 10.0f; animation.To = NSValue.FromVector(new SCNVector4(0, 1, 0, (float)(Math.PI * 2))); animation.RepeatCount = float.MaxValue; earthRotationNode.AddAnimation(animation, new NSString("earth rotation around sun")); // Rotate the Earth animation = CABasicAnimation.FromKeyPath("rotation"); animation.Duration = 1.0f; animation.From = NSValue.FromVector(new SCNVector4(0, 1, 0, 0)); animation.To = NSValue.FromVector(new SCNVector4(0, 1, 0, (float)(Math.PI * 2))); animation.RepeatCount = float.MaxValue; EarthNode.AddAnimation(animation, new NSString("earth rotation")); break; case 2: // Moon-rotation (center of rotation of the Moon around the Earth) var moonRotationNode = SCNNode.Create(); EarthGroupNode.AddChildNode(moonRotationNode); // Moon MoonNode = (SCNNode)WireframeBoxNode.Copy(); MoonNode.Position = new SCNVector3(5, 0, 0); moonRotationNode.AddChildNode(MoonNode); // Rotate the moon around the Earth animation = CABasicAnimation.FromKeyPath("rotation"); animation.Duration = 1.5f; animation.To = NSValue.FromVector(new SCNVector4(0, 1, 0, (float)(Math.PI * 2))); animation.RepeatCount = float.MaxValue; moonRotationNode.AddAnimation(animation, new NSString("moon rotation around earth")); // Rotate the moon animation = CABasicAnimation.FromKeyPath("rotation"); animation.Duration = 1.5f; animation.To = NSValue.FromVector(new SCNVector4(0, 1, 0, (float)(Math.PI * 2))); animation.RepeatCount = float.MaxValue; MoonNode.AddAnimation(animation, new NSString("moon rotation")); break; case 3: // Add geometries (spheres) to represent the stars SunNode.Geometry = SCNSphere.Create(2.5f); EarthNode.Geometry = SCNSphere.Create(1.5f); MoonNode.Geometry = SCNSphere.Create(0.75f); // Add a textured plane to represent Earth's orbit var earthOrbit = SCNNode.Create(); earthOrbit.Opacity = 0.4f; earthOrbit.Geometry = SCNPlane.Create(31, 31); earthOrbit.Geometry.FirstMaterial.Diffuse.Contents = new NSImage(NSBundle.MainBundle.PathForResource("Scenes/earth/orbit", "png")); earthOrbit.Geometry.FirstMaterial.Diffuse.MipFilter = SCNFilterMode.Linear; earthOrbit.Rotation = new SCNVector4(1, 0, 0, -(float)(Math.PI / 2)); earthOrbit.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant; // no lighting SunNode.AddChildNode(earthOrbit); break; case 4: // Add a halo to the Sun (a simple textured plane that does not write to depth) SunHaloNode = SCNNode.Create(); SunHaloNode.Geometry = SCNPlane.Create(30, 30); SunHaloNode.Rotation = new SCNVector4(1, 0, 0, Pitch * (float)(Math.PI / 180.0f)); SunHaloNode.Geometry.FirstMaterial.Diffuse.Contents = new NSImage(NSBundle.MainBundle.PathForResource("Scenes/earth/sun-halo", "png")); SunHaloNode.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant; // no lighting SunHaloNode.Geometry.FirstMaterial.WritesToDepthBuffer = false; // do not write to depth SunHaloNode.Opacity = 0.2f; SunNode.AddChildNode(SunHaloNode); // Add materials to the stars EarthNode.Geometry.FirstMaterial.Diffuse.Contents = new NSImage(NSBundle.MainBundle.PathForResource("Scenes/earth/earth-diffuse-mini", "jpg")); EarthNode.Geometry.FirstMaterial.Emission.Contents = new NSImage(NSBundle.MainBundle.PathForResource("Scenes/earth/earth-emissive-mini", "jpg")); EarthNode.Geometry.FirstMaterial.Specular.Contents = new NSImage(NSBundle.MainBundle.PathForResource("Scenes/earth/earth-specular-mini", "jpg")); MoonNode.Geometry.FirstMaterial.Diffuse.Contents = new NSImage(NSBundle.MainBundle.PathForResource("Scenes/earth/moon", "jpg")); SunNode.Geometry.FirstMaterial.Multiply.Contents = new NSImage(NSBundle.MainBundle.PathForResource("Scenes/earth/sun", "jpg")); SunNode.Geometry.FirstMaterial.Diffuse.Contents = new NSImage(NSBundle.MainBundle.PathForResource("Scenes/earth/sun", "jpg")); SunNode.Geometry.FirstMaterial.Multiply.Intensity = 0.5f; SunNode.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant; SunNode.Geometry.FirstMaterial.Multiply.WrapS = SunNode.Geometry.FirstMaterial.Diffuse.WrapS = SunNode.Geometry.FirstMaterial.Multiply.WrapT = SunNode.Geometry.FirstMaterial.Diffuse.WrapT = SCNWrapMode.Repeat; EarthNode.Geometry.FirstMaterial.LocksAmbientWithDiffuse = MoonNode.Geometry.FirstMaterial.LocksAmbientWithDiffuse = SunNode.Geometry.FirstMaterial.LocksAmbientWithDiffuse = true; EarthNode.Geometry.FirstMaterial.Shininess = 0.1f; EarthNode.Geometry.FirstMaterial.Specular.Intensity = 0.5f; MoonNode.Geometry.FirstMaterial.Specular.Contents = NSColor.Gray; // Achieve a lava effect by animating textures animation = CABasicAnimation.FromKeyPath("contentsTransform"); animation.Duration = 10.0f; var animationTransform1 = CATransform3D.MakeTranslation(0, 0, 0); animationTransform1 = animationTransform1.Concat(CATransform3D.MakeScale(3, 3, 3)); var animationTransform2 = CATransform3D.MakeTranslation(1, 0, 0); animationTransform2 = animationTransform1.Concat(CATransform3D.MakeScale(3, 3, 3)); animation.From = NSValue.FromCATransform3D(animationTransform1); animation.To = NSValue.FromCATransform3D(animationTransform2); animation.RepeatCount = float.MaxValue; SunNode.Geometry.FirstMaterial.Diffuse.AddAnimation(animation, new NSString("sun-texture")); animation = CABasicAnimation.FromKeyPath("contentsTransform"); animation.Duration = 30.0f; animationTransform1 = CATransform3D.MakeTranslation(0, 0, 0); animationTransform1 = animationTransform1.Concat(CATransform3D.MakeScale(5, 5, 5)); animationTransform2 = CATransform3D.MakeTranslation(1, 0, 0); animationTransform2 = animationTransform1.Concat(CATransform3D.MakeScale(5, 5, 5)); animation.From = NSValue.FromCATransform3D(animationTransform1); animation.To = NSValue.FromCATransform3D(animationTransform2); animation.RepeatCount = float.MaxValue; SunNode.Geometry.FirstMaterial.Multiply.AddAnimation(animation, new NSString("sun-texture2")); break; case 5: // We will turn off all the lights in the scene and add a new light // to give the impression that the Sun lights the scene var lightNode = SCNNode.Create(); lightNode.Light = SCNLight.Create(); lightNode.Light.Color = NSColor.Black; // initially switched off lightNode.Light.LightType = SCNLightType.Omni; SunNode.AddChildNode(lightNode); // Configure attenuation distances because we don't want to light the floor lightNode.Light.SetAttribute(new NSNumber(20), SCNLightAttribute.AttenuationEndKey); lightNode.Light.SetAttribute(new NSNumber(19.5), SCNLightAttribute.AttenuationStartKey); // Animation SCNTransaction.Begin(); SCNTransaction.AnimationDuration = 1; lightNode.Light.Color = NSColor.White; // switch on presentationViewController.UpdateLightingWithIntensities(new float[] { 0.0f }); //switch off all the other lights SunHaloNode.Opacity = 0.5f; // make the halo stronger SCNTransaction.Commit(); break; } }
public override void PresentStep(int index, PresentationViewController presentationViewController) { switch (index) { case (int)ParticleSteps.Fire: TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddEmptyLine(); TextManager.AddBulletAtLevel("Particle Image", 0); TextManager.AddBulletAtLevel("Color over life duration", 0); TextManager.AddBulletAtLevel("Size over life duration", 0); TextManager.AddBulletAtLevel("Several blend modes", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); var hole = SCNNode.Create(); hole.Geometry = SCNTube.Create(1.7f, 1.9f, 1.5f); hole.Position = new SCNVector3(0, 0, HOLE_Z); hole.Scale = new SCNVector3(1, 0, 1); GroundNode.AddChildNode(hole); SCNTransaction.Begin(); SCNTransaction.AnimationDuration = 0.5f; hole.Scale = new SCNVector3(1, 1, 1); SCNTransaction.Commit(); var ps = SCNParticleSystem.Create("fire", "Particles"); hole.AddParticleSystem(ps); Hole = hole; break; case (int)ParticleSteps.FireScreen: ps = Hole.ParticleSystems [0]; ps.BlendMode = SCNParticleBlendMode.Screen; break; case (int)ParticleSteps.Local: TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddBulletAtLevel("Local vs Global", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); Hole.RemoveAllParticleSystems(); Hole2 = Hole.Clone(); Hole2.Geometry = (SCNGeometry)Hole.Geometry.Copy(); Hole2.Position = new SCNVector3(0, -2, HOLE_Z - 4); GroundNode.AddChildNode(Hole2); SCNTransaction.Begin(); SCNTransaction.AnimationDuration = 0.5; Hole2.Position = new SCNVector3(0, 0, HOLE_Z - 4); SCNTransaction.Commit(); ps = SCNParticleSystem.Create("reactor", "Particles"); ps.ParticleColorVariation = new SCNVector4(0, 0, 0.5f, 0); Hole.AddParticleSystem(ps); var localPs = (SCNParticleSystem)ps.Copy(); localPs.ParticleImage = ps.ParticleImage; localPs.Local = true; Hole2.AddParticleSystem(localPs); var animation = CABasicAnimation.FromKeyPath("position"); animation.From = NSValue.FromVector(new SCNVector3(7, 0, HOLE_Z)); animation.To = NSValue.FromVector(new SCNVector3(-7, 0, HOLE_Z)); animation.BeginTime = CAAnimation.CurrentMediaTime() + 0.75; animation.Duration = 8; animation.AutoReverses = true; animation.RepeatCount = float.MaxValue; animation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseInEaseOut); animation.TimeOffset = animation.Duration / 2; Hole.AddAnimation(animation, new NSString("animateHole")); animation = CABasicAnimation.FromKeyPath("position"); animation.From = NSValue.FromVector(new SCNVector3(-7, 0, HOLE_Z - 4)); animation.To = NSValue.FromVector(new SCNVector3(7, 0, HOLE_Z - 4)); animation.BeginTime = CAAnimation.CurrentMediaTime() + 0.75; animation.Duration = 8; animation.AutoReverses = true; animation.RepeatCount = float.MaxValue; animation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseInEaseOut); animation.TimeOffset = animation.Duration / 2; Hole2.AddAnimation(animation, new NSString("animateHole")); break; case (int)ParticleSteps.Gravity: TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddBulletAtLevel("Affected by gravity", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); Hole2.RemoveAllParticleSystems(); Hole2.RunAction(SCNAction.Sequence(new SCNAction[] { SCNAction.ScaleTo(0, 0.5), SCNAction.RemoveFromParentNode() })); Hole.RemoveAllParticleSystems(); Hole.RemoveAnimation(new NSString("animateHole"), 0.5f); SCNTransaction.Begin(); SCNTransaction.AnimationDuration = 0.5; var tube = (SCNTube)Hole.Geometry; tube.InnerRadius = 0.3f; tube.OuterRadius = 0.4f; tube.Height = 1.0f; SCNTransaction.Commit(); ps = SCNParticleSystem.Create("sparks", "Particles"); Hole.RemoveAllParticleSystems(); Hole.AddParticleSystem(ps); foreach (var child in ((SCNView)presentationViewController.View).Scene.RootNode.ChildNodes) { if (child.Geometry != null) { if (child.Geometry.GetType() == typeof(SCNFloor)) { FloorNode = child; } } } /*FloorNode = ((SCNView)presentationViewController.View).Scene.RootNode.FindNodes ((SCNNode child, out bool stop) => { * stop = false; * if (child.Geometry != null) * stop = (child.Geometry.GetType () == typeof(SCNFloor)); * return stop; * });*/ /*FloorNode = [presentationViewController.view.scene.rootNode childNodesPassingTest:^BOOL(SCNNode *child, BOOL *stop) { * return [child.geometry isKindOfClass:[SCNFloor class]]; * }][0];*/ ps.ColliderNodes = new SCNNode[] { FloorNode }; break; case (int)ParticleSteps.Collider: TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddBulletAtLevel("Affected by colliders", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); var boxNode = SCNNode.Create(); boxNode.Geometry = SCNBox.Create(5, 0.2f, 5, 0); boxNode.Position = new SCNVector3(0, 7, HOLE_Z); boxNode.Geometry.FirstMaterial.Emission.Contents = NSColor.DarkGray; GroundNode.AddChildNode(boxNode); ps = Hole.ParticleSystems [0]; ps.ColliderNodes = new SCNNode[] { FloorNode, boxNode }; animation = CABasicAnimation.FromKeyPath("eulerAngles"); animation.From = NSValue.FromVector(new SCNVector3(0, 0, NMath.PI / 4 * 1.7f)); animation.To = NSValue.FromVector(new SCNVector3(0, 0, -NMath.PI / 4 * 1.7f)); animation.BeginTime = CAAnimation.CurrentMediaTime() + 0.5; animation.Duration = 2; animation.AutoReverses = true; animation.RepeatCount = float.MaxValue; animation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseInEaseOut); animation.TimeOffset = animation.Duration / 2; boxNode.AddAnimation(animation, new NSString("animateHole")); BoxNode = boxNode; break; case (int)ParticleSteps.Fields: Hole.RemoveAllParticleSystems(); Hole.RunAction(SCNAction.Sequence(new SCNAction[] { SCNAction.ScaleTo(0, 0.75), SCNAction.RemoveFromParentNode() })); BoxNode.RunAction(SCNAction.Sequence(new SCNAction[] { SCNAction.MoveBy(0, 15, 0, 1.0), SCNAction.RemoveFromParentNode() })); var particleHolder = SCNNode.Create(); particleHolder.Position = new SCNVector3(0, 20, HOLE_Z); GroundNode.AddChildNode(particleHolder); ParticleHolder = particleHolder; TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddBulletAtLevel("Affected by physics fields", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); ps = SCNParticleSystem.Create("snow", "Particles"); ps.AffectedByPhysicsFields = true; ParticleHolder.AddParticleSystem(ps); Snow = ps; //physics field var field = SCNPhysicsField.CreateTurbulenceField(50, 1); field.HalfExtent = new SCNVector3(20, 20, 20); field.Strength = 4.0f; var fieldOwner = SCNNode.Create(); fieldOwner.Position = new SCNVector3(0, 5, HOLE_Z); GroundNode.AddChildNode(fieldOwner); fieldOwner.PhysicsField = field; FieldOwner = fieldOwner; ps.ColliderNodes = new SCNNode[] { FloorNode }; break; case (int)ParticleSteps.FieldsVortex: VortexFieldOwner = SCNNode.Create(); VortexFieldOwner.Position = new SCNVector3(0, 5, HOLE_Z); GroundNode.AddChildNode(VortexFieldOwner); //tornado var worldOrigin = new SCNVector3(FieldOwner.WorldTransform.M41, FieldOwner.WorldTransform.M42, FieldOwner.WorldTransform.M43); var worldAxis = new SCNVector3(0, 1, 0); var vortex = SCNPhysicsField.CustomField((SCNVector3 position, SCNVector3 velocity, float mass, float charge, double timeInSeconds) => { var l = new SCNVector3(); l.X = worldOrigin.X - position.X; l.Z = worldOrigin.Z - position.Z; SCNVector3 t = Cross(worldAxis, l); var d2 = (l.X * l.X + l.Z * l.Z); var vs = (nfloat)(VS / Math.Sqrt(d2)); var fy = (nfloat)(1.0 - (Math.Min(1.0, (position.Y / 15.0)))); return(new SCNVector3(t.X * vs + l.X * (nfloat)VW * fy, 0, t.Z * vs + l.Z * (nfloat)VW * fy)); }); vortex.HalfExtent = new SCNVector3(100, 100, 100); VortexFieldOwner.PhysicsField = vortex; break; case (int)ParticleSteps.SubSystems: FieldOwner.RemoveFromParentNode(); ParticleHolder.RemoveAllParticleSystems(); Snow.DampingFactor = -1; TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddBulletAtLevel("Sub-particle system on collision", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); ps = SCNParticleSystem.Create("rain", "Particles"); var pss = SCNParticleSystem.Create("plok", "Particles"); pss.IdleDuration = 0; pss.Loops = false; ps.SystemSpawnedOnCollision = pss; ParticleHolder.AddParticleSystem(ps); ps.ColliderNodes = new SCNNode[] { FloorNode }; break; case (int)ParticleSteps.Confetti: ParticleHolder.RemoveAllParticleSystems(); TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddBulletAtLevel("Custom blocks", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); ps = SCNParticleSystem.Create(); ps.EmitterShape = SCNBox.Create(20, 9, 5, 0); ps.BirthRate = 100; ps.ParticleLifeSpan = 10; ps.ParticleLifeSpanVariation = 0; ps.SpreadingAngle = 20; ps.ParticleSize = 0.25f; ps.ParticleVelocity = 10; ps.ParticleVelocityVariation = 19; ps.BirthDirection = SCNParticleBirthDirection.Constant; ps.EmittingDirection = new SCNVector3(0, -1, 0); ps.BirthLocation = SCNParticleBirthLocation.Volume; ps.ParticleImage = new NSImage(NSBundle.MainBundle.PathForResource("Particles/confetti", "png")); ps.LightingEnabled = true; ps.OrientationMode = SCNParticleOrientationMode.Free; ps.SortingMode = SCNParticleSortingMode.Distance; ps.ParticleAngleVariation = 180; ps.ParticleAngularVelocity = 200; ps.ParticleAngularVelocityVariation = 400; ps.ParticleColor = NSColor.Green; ps.ParticleColorVariation = new SCNVector4(0.2f, 0.1f, 0.1f, 0); ps.ParticleBounce = 0; ps.ParticleFriction = 0.6f; ps.ColliderNodes = new SCNNode[] { FloorNode }; ps.BlendMode = SCNParticleBlendMode.Alpha; var floatAnimation = CAKeyFrameAnimation.FromKeyPath(""); floatAnimation.Values = new NSNumber[] { 1, 1, 0 }; floatAnimation.KeyTimes = new NSNumber[] { 0, 0.9f, 1 }; floatAnimation.Duration = 1.0f; floatAnimation.Additive = false; //ps.PropertyControllers = @{ SCNParticlePropertyOpacity: [SCNParticlePropertyController controllerWithAnimation:floatAnimation] }; //ps.HandleEvent (SCNParticleEvent.Birth, /*[ps handleEvent:SCNParticleEventBirth forProperties:@[SCNParticlePropertyColor] withBlock:^(void **data, size_t *dataStride, uint32_t *indices , NSInteger count) { * * for (int i = 0; i < count; ++i) { * var col = (float *)((char *)data[0] + dataStride[0] * i); * if (rand() & 0x1) { // swith green for red * col[0] = col[1]; * col[1] = 0; * } * * } * }];*/ /*[ps handleEvent:SCNParticleEventCollision forProperties:@[SCNParticlePropertyAngle, SCNParticlePropertyRotationAxis, SCNParticlePropertyAngularVelocity, SCNParticlePropertyVelocity, SCNParticlePropertyContactNormal] withBlock:^(void **data, size_t *dataStride, uint32_t *indices , NSInteger count) { * * for (NSInteger i = 0; i < count; ++i) { * // fix orientation * float *angle = (float *)((char *)data[0] + dataStride[0] * indices[i]); * float *axis = (float *)((char *)data[1] + dataStride[1] * indices[i]); * * float *colNrm = (float *)((char *)data[4] + dataStride[4] * indices[i]); * SCNVector3 collisionNormal = {colNrm[0], colNrm[1], colNrm[2]}; * SCNVector3 cp = SCNVector3CrossProduct(collisionNormal, SCNVector3Make(0, 0, 1)); * CGFloat cpLen = SCNVector3Length(cp); * angle[0] = asin(cpLen); * * axis[0] = cp.x / cpLen; * axis[1] = cp.y / cpLen; * axis[2] = cp.z / cpLen; * * // kill angular rotation * float *angVel = (float *)((char *)data[2] + dataStride[2] * indices[i]); * angVel[0] = 0; * * if (colNrm[1] > 0.4) { * float *vel = (float *)((char *)data[3] + dataStride[3] * indices[i]); * vel[0] = 0; * vel[1] = 0; * vel[2] = 0; * } * } * }];*/ ParticleHolder.AddParticleSystem(ps); break; case (int)ParticleSteps.EmitterCube: ParticleHolder.RemoveAllParticleSystems(); TextManager.FlipOutText(SlideTextManager.TextType.Bullet); TextManager.AddBulletAtLevel("Emitter shape", 0); TextManager.FlipInText(SlideTextManager.TextType.Bullet); ParticleHolder.RemoveFromParentNode(); ps = SCNParticleSystem.Create("emitters", "Particles"); ps.Local = true; ParticleHolder.AddParticleSystem(ps); var node = SCNNode.Create(); node.Position = new SCNVector3(3, 6, HOLE_Z); node.RunAction(SCNAction.RepeatActionForever(SCNAction.RotateBy(NMath.PI * 2, new SCNVector3(0.3f, 1, 0), 8))); GroundNode.AddChildNode(node); Bokeh = ps; node.AddParticleSystem(ps); break; case (int)ParticleSteps.EmitterSphere: Bokeh.EmitterShape = SCNSphere.Create(5); break; case (int)ParticleSteps.EmitterTorus: Bokeh.EmitterShape = SCNTorus.Create(5, 1); break; } }
// Create a carousel of 3D primitives private void PresentPrimitives() { // Create the carousel node. It will host all the primitives as child nodes. CarouselNode = SCNNode.Create(); CarouselNode.Position = new SCNVector3(0, 0.1f, -5); CarouselNode.Scale = new SCNVector3(0, 0, 0); // start infinitely small ContentNode.AddChildNode(CarouselNode); // Animate the scale to achieve a "grow" effect SCNTransaction.Begin(); SCNTransaction.AnimationDuration = 1; CarouselNode.Scale = new SCNVector3(1, 1, 1); SCNTransaction.Commit(); // Rotate the carousel forever var rotationAnimation = CABasicAnimation.FromKeyPath("rotation"); rotationAnimation.Duration = 40.0f; rotationAnimation.RepeatCount = float.MaxValue; rotationAnimation.To = NSValue.FromVector(new SCNVector4(0, 1, 0, (float)Math.PI * 2)); CarouselNode.AddAnimation(rotationAnimation, new NSString("rotationAnimation")); // A material shared by all the primitives var sharedMaterial = SCNMaterial.Create(); sharedMaterial.Reflective.Contents = new NSImage(NSBundle.MainBundle.PathForResource("SharedTextures/envmap", "jpg")); sharedMaterial.Reflective.Intensity = 0.2f; sharedMaterial.DoubleSided = true; PrimitiveIndex = 0; // SCNBox var box = SCNBox.Create(5.0f, 5.0f, 5.0f, 5.0f * 0.05f); box.WidthSegmentCount = 4; box.HeightSegmentCount = 4; box.LengthSegmentCount = 4; box.ChamferSegmentCount = 4; AddPrimitive(box, 5.0f / 2, rotationAnimation, sharedMaterial); // SCNPyramid var pyramid = SCNPyramid.Create(5.0f * 0.8f, 5.0f, 5.0f * 0.8f); pyramid.WidthSegmentCount = 4; pyramid.HeightSegmentCount = 10; pyramid.LengthSegmentCount = 4; AddPrimitive(pyramid, 0, rotationAnimation, sharedMaterial); // SCNCone var cone = SCNCone.Create(0, 5.0f / 2, 5.0f); cone.RadialSegmentCount = 20; cone.HeightSegmentCount = 4; AddPrimitive(cone, 5.0f / 2, rotationAnimation, sharedMaterial); // SCNTube var tube = SCNTube.Create(5.0f * 0.25f, 5.0f * 0.5f, 5.0f); tube.HeightSegmentCount = 5; tube.RadialSegmentCount = 40; AddPrimitive(tube, 5.0f / 2, rotationAnimation, sharedMaterial); // SCNCapsule var capsule = SCNCapsule.Create(5.0f * 0.4f, 5.0f * 1.4f); capsule.HeightSegmentCount = 5; capsule.RadialSegmentCount = 20; AddPrimitive(capsule, 5.0f * 0.7f, rotationAnimation, sharedMaterial); // SCNCylinder var cylinder = SCNCylinder.Create(5.0f * 0.5f, 5.0f); cylinder.HeightSegmentCount = 5; cylinder.RadialSegmentCount = 40; AddPrimitive(cylinder, 5.0f / 2, rotationAnimation, sharedMaterial); // SCNSphere var sphere = SCNSphere.Create(5.0f * 0.5f); sphere.SegmentCount = 20; AddPrimitive(sphere, 5.0f / 2, rotationAnimation, sharedMaterial); // SCNTorus var torus = SCNTorus.Create(5.0f * 0.5f, 5.0f * 0.25f); torus.RingSegmentCount = 40; torus.PipeSegmentCount = 20; AddPrimitive(torus, 5.0f / 4, rotationAnimation, sharedMaterial); // SCNPlane var plane = SCNPlane.Create(5.0f, 5.0f); plane.WidthSegmentCount = 5; plane.HeightSegmentCount = 5; plane.CornerRadius = 5.0f * 0.1f; AddPrimitive(plane, 5.0f / 2, rotationAnimation, sharedMaterial); }
public override void DidAddNode(ISCNSceneRenderer renderer, SCNNode node, ARAnchor anchor) { if (anchor != null && anchor is ARImageAnchor) { var imageAnchor = anchor as ARImageAnchor; var referenceImage = imageAnchor.ReferenceImage; var imageWidth = referenceImage.PhysicalSize.Width; var imageHeight = referenceImage.PhysicalSize.Height; var verticesArray = new List <SCNVector3>() { new SCNVector3((float)-imageWidth / 2, 0, (float)-imageHeight / 2), new SCNVector3((float)-imageWidth / 2, 0, (float)imageHeight / 2), new SCNVector3((float)imageWidth / 2, 0, (float)-imageHeight / 2), new SCNVector3((float)imageWidth / 2, 0, (float)imageHeight / 2), }; foreach (var v in verticesArray) { var sphere = new SCNNode(); sphere.Geometry = SCNSphere.Create(new nfloat(0.0025)); sphere.Geometry.FirstMaterial.Diffuse.Contents = UIColor.Red; sphere.Geometry.FirstMaterial.Emission.Contents = UIColor.Blue; sphere.Position = v; node.Add(sphere); // Create a plane to visualize the initial position of the detected image. var plane = SCNPlane.Create(imageWidth, imageHeight); var planeNode = SCNNode.Create(); planeNode.Name = "Unicorn"; planeNode.Geometry = plane; planeNode.Geometry.FirstMaterial.Diffuse.Contents = UIColor.Blue; planeNode.Opacity = new nfloat(0); /* * `SCNPlane` is vertically oriented in its local coordinate space, but * `ARImageAnchor` assumes the image is horizontal in its local space, so * rotate the plane to match. */ var EulerY = planeNode.EulerAngles.Y; var EulerZ = planeNode.EulerAngles.Z; planeNode.EulerAngles = new SCNVector3((float)-3.1416 / 2, EulerY, EulerZ); node.Add(planeNode); // Create a box to visualize the initial position of the detected image. var box = SCNBox.Create(imageWidth, 0.005f, imageHeight, 0); var opaqueMat = new SCNMaterial(); opaqueMat.Diffuse.ContentColor = UIColor.Red; opaqueMat.DoubleSided = true; var transMat = new SCNMaterial(); opaqueMat.Diffuse.ContentColor = UIColor.Purple; transMat.Transparency = 0; var boxNode = new SCNNode { Position = new SCNVector3(0, 0, 0), Geometry = box }; boxNode.Geometry.Materials = new SCNMaterial[] { opaqueMat, opaqueMat, opaqueMat, opaqueMat, transMat, transMat }; EulerY = boxNode.EulerAngles.Y; EulerZ = boxNode.EulerAngles.Z; //boxNode.EulerAngles = new SCNVector3(-3.1416f / 2, EulerY, EulerZ); node.Add(boxNode); } } }