Ejemplo n.º 1
0
        public void LoadScene(string path)
        {
            NSError error;

            this.selectedMaterial = null;

            // Load the specified scene. First create a dictionary containing the options we want.
            var options = new SCNSceneLoadingOptions
            {
                // Create normals if absent.
                CreateNormalsIfAbsent = true,
                // Optimize the rendering by flattening the scene graph when possible. Note that this would prevent you from animating objects independantly.
                FlattenScene = true,
            };

            var scene = SCNScene.FromUrl(new NSUrl($"file://{path}"), options, out error);

            if (scene != null)
            {
                base.Scene = scene;
            }
            else
            {
                Console.WriteLine($"Problem loading scene from: {path}\n{error.LocalizedDescription}");
            }
        }
Ejemplo n.º 2
0
        public void SCNSceneLoadingOptions_AnimationImportPolicyTest()
        {
            SCNSceneLoadingOptions o = new SCNSceneLoadingOptions();

            RoundTrip(o, SCNAnimationImportPolicy.Play);
            RoundTrip(o, SCNAnimationImportPolicy.PlayRepeatedly);
            RoundTrip(o, SCNAnimationImportPolicy.DoNotPlay);
            RoundTrip(o, SCNAnimationImportPolicy.PlayUsingSceneTimeBase);
        }
Ejemplo n.º 3
0
		public static CAAnimation LoadAnimationNamed (string animationName, string sceneName)
		{
			NSUrl url = NSBundle.MainBundle.GetUrlForResource (sceneName, "dae");
			var options = new SCNSceneLoadingOptions () {
				ConvertToYUp = true
			};

			var sceneSource = new SCNSceneSource (url, options);
			var animation = (CAAnimation)sceneSource.GetEntryWithIdentifier (animationName, new Class (typeof(CAAnimation)));

			return animation;
		}
Ejemplo n.º 4
0
        public static CAAnimation LoadAnimationNamed(string animationName, string sceneName)
        {
            NSUrl url     = NSBundle.MainBundle.GetUrlForResource(sceneName, "dae");
            var   options = new SCNSceneLoadingOptions()
            {
                ConvertToYUp = true
            };

            var sceneSource = new SCNSceneSource(url, options);
            var animation   = (CAAnimation)sceneSource.GetEntryWithIdentifier(animationName, new Class(typeof(CAAnimation)));

            return(animation);
        }
Ejemplo n.º 5
0
		public SCNNode CreateLevel ()
		{
			RootNode = new SCNNode ();

			// load level dae and add all root children to the scene.
			var options = new SCNSceneLoadingOptions { ConvertToYUp = true };
			SCNScene scene = SCNScene.FromFile ("level", GameSimulation.PathForArtResource ("level/"), options);
			foreach (SCNNode node in scene.RootNode.ChildNodes)
				RootNode.AddChildNode (node);

			// retrieve the main camera
			Camera = RootNode.FindChildNode ("camera_game", true);

			// create our path that the player character will follow.
			CalculatePathPositions ();

			// Sun/Moon light
			SunLight = RootNode.FindChildNode ("FDirect001", true);
			SunLight.EulerAngles = new SCNVector3 (7.1f * (nfloat)Math.PI / 4, (nfloat)Math.PI / 4, 0f);
			SunLight.Light.ShadowSampleCount = 1;
			lightOffsetFromCharacter = new SCNVector3 (1500f, 2000f, 1000f);

			//workaround directional light deserialization issue
			SunLight.Light.ZNear = 100f;
			SunLight.Light.ZFar = 5000f;
			SunLight.Light.OrthographicScale = 1000f;

			// Torches
			var torchesPos = new float[] { 0f, -1f, 0.092467f, -1f, -1f, 0.5f, 0.792f, 0.95383f };
			for (int i = 0; i < torchesPos.Length; i++) {
				if (torchesPos [i] != -1) {
					SCNVector3 location = LocationAlongPath (torchesPos [i]);
					location.Y += 50;
					location.Z += 150;

					SCNNode node = CreateTorchNode ();

					node.Position = location;
					RootNode.AddChildNode (node);
				}
			}

			// After load, we add nodes that are dynamic / animated / or otherwise not static.
			CreateLavaAnimation ();
			CreateSwingingTorch ();
			AnimateDynamicNodes ();

			// Create our player character
			SCNNode characterRoot = GameSimulation.LoadNodeWithName (string.Empty, "art.scnassets/characters/explorer/explorer_skinned.dae");
			PlayerCharacter = new PlayerCharacter (characterRoot); 
			TimeAlongPath = 0;

			PlayerCharacter.Position = LocationAlongPath (TimeAlongPath);
			PlayerCharacter.Rotation = GetPlayerDirectionFromCurrentPosition ();
			RootNode.AddChildNode (PlayerCharacter);

			// Optimize lighting and shadows
			// only the charadcter should cast shadows
			foreach (var child in RootNode.ChildNodes)
				child.CastsShadow = false;

			foreach (var child in PlayerCharacter.ChildNodes)
				child.CastsShadow = true;

			// Add some monkeys to the scene.
			AddMonkeyAtPosition (new SCNVector3 (0f, -30f, -400f), 0f);
			AddMonkeyAtPosition (new SCNVector3 (3211f, 146f, -400f), -(nfloat)Math.PI / 4f);
			AddMonkeyAtPosition (new SCNVector3 (5200f, 330f, 600f), 0f);

			// Volcano
			SCNNode oldVolcano = RootNode.FindChildNode ("volcano", true);
			string volcanoDaeName = GameSimulation.PathForArtResource ("level/volcano_effects.dae");
			SCNNode newVolcano = GameSimulation.LoadNodeWithName ("dummy_master", volcanoDaeName);

			oldVolcano.AddChildNode (newVolcano);
			oldVolcano.Geometry = null;
			oldVolcano = newVolcano.FindChildNode ("volcano", true);
			oldVolcano = oldVolcano.ChildNodes [0];

			// Animate our dynamic volcano node.
			string shaderCode = "uniform float speed;\n" +
			                    "_geometry.color = vec4(a_color.r, a_color.r, a_color.r, a_color.r);\n" +
			                    "_geometry.texcoords[0] += (vec2(0.0, 1.0) * 0.05 * u_time);\n";

			string fragmentShaderCode = "#pragma transparent\n";

			// Dim background
			SCNNode back = RootNode.FindChildNode ("dumy_rear", true);
			foreach (var child in back.ChildNodes) {
				child.CastsShadow = false;

				if (child.Geometry == null)
					continue;

				foreach (SCNMaterial material in child.Geometry.Materials) {
					material.LightingModelName = SCNLightingModel.Constant;
					material.Multiply.Contents = AppKit.NSColor.FromDeviceWhite (0.3f, 1f);

					material.Multiply.Intensity = 1;
				}
			}

			SCNNode backMiddle = RootNode.FindChildNode ("dummy_middle", true);
			foreach (var child in backMiddle.ChildNodes) {

				if (child.Geometry == null)
					continue;

				foreach (SCNMaterial material in child.Geometry.Materials)
					material.LightingModelName = SCNLightingModel.Constant;
			}

			foreach (var child in newVolcano.ChildNodes)
				foreach (var volc in child.ChildNodes) {
					if (volc != oldVolcano && volc.Geometry != null) {
						volc.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant;
						volc.Geometry.FirstMaterial.Multiply.Contents = AppKit.NSColor.White;

						volc.Geometry.ShaderModifiers = new SCNShaderModifiers { 
							EntryPointGeometry = shaderCode,
							EntryPointFragment = fragmentShaderCode
						};
					}
				}

			Coconuts = new List<Coconut> ();
			return RootNode;
		}
Ejemplo n.º 6
0
 void RoundTrip(SCNSceneLoadingOptions o, SCNAnimationImportPolicy policy)
 {
     o.AnimationImportPolicy = policy;
     Assert.IsTrue(o.AnimationImportPolicy == policy);
 }
Ejemplo n.º 7
0
        public SCNNode CreateLevel()
        {
            RootNode = new SCNNode();

            // load level dae and add all root children to the scene.
            var options = new SCNSceneLoadingOptions {
                ConvertToYUp = true
            };
            SCNScene scene = SCNScene.FromFile("level", GameSimulation.PathForArtResource("level/"), options);

            foreach (SCNNode node in scene.RootNode.ChildNodes)
            {
                RootNode.AddChildNode(node);
            }

            // retrieve the main camera
            Camera = RootNode.FindChildNode("camera_game", true);

            // create our path that the player character will follow.
            CalculatePathPositions();

            // Sun/Moon light
            SunLight                         = RootNode.FindChildNode("FDirect001", true);
            SunLight.EulerAngles             = new SCNVector3(7.1f * (nfloat)Math.PI / 4, (nfloat)Math.PI / 4, 0f);
            SunLight.Light.ShadowSampleCount = 1;
            lightOffsetFromCharacter         = new SCNVector3(1500f, 2000f, 1000f);

            //workaround directional light deserialization issue
            SunLight.Light.ZNear             = 100f;
            SunLight.Light.ZFar              = 5000f;
            SunLight.Light.OrthographicScale = 1000f;

            // Torches
            var torchesPos = new float[] { 0f, -1f, 0.092467f, -1f, -1f, 0.5f, 0.792f, 0.95383f };

            for (int i = 0; i < torchesPos.Length; i++)
            {
                if (torchesPos [i] != -1)
                {
                    SCNVector3 location = LocationAlongPath(torchesPos [i]);
                    location.Y += 50;
                    location.Z += 150;

                    SCNNode node = CreateTorchNode();

                    node.Position = location;
                    RootNode.AddChildNode(node);
                }
            }

            // After load, we add nodes that are dynamic / animated / or otherwise not static.
            CreateLavaAnimation();
            CreateSwingingTorch();
            AnimateDynamicNodes();

            // Create our player character
            SCNNode characterRoot = GameSimulation.LoadNodeWithName(string.Empty, "art.scnassets/characters/explorer/explorer_skinned.dae");

            PlayerCharacter = new PlayerCharacter(characterRoot);
            TimeAlongPath   = 0;

            PlayerCharacter.Position = LocationAlongPath(TimeAlongPath);
            PlayerCharacter.Rotation = GetPlayerDirectionFromCurrentPosition();
            RootNode.AddChildNode(PlayerCharacter);

            // Optimize lighting and shadows
            // only the charadcter should cast shadows
            foreach (var child in RootNode.ChildNodes)
            {
                child.CastsShadow = false;
            }

            foreach (var child in PlayerCharacter.ChildNodes)
            {
                child.CastsShadow = true;
            }

            // Add some monkeys to the scene.
            AddMonkeyAtPosition(new SCNVector3(0f, -30f, -400f), 0f);
            AddMonkeyAtPosition(new SCNVector3(3211f, 146f, -400f), -(nfloat)Math.PI / 4f);
            AddMonkeyAtPosition(new SCNVector3(5200f, 330f, 600f), 0f);

            // Volcano
            SCNNode oldVolcano     = RootNode.FindChildNode("volcano", true);
            string  volcanoDaeName = GameSimulation.PathForArtResource("level/volcano_effects.dae");
            SCNNode newVolcano     = GameSimulation.LoadNodeWithName("dummy_master", volcanoDaeName);

            oldVolcano.AddChildNode(newVolcano);
            oldVolcano.Geometry = null;
            oldVolcano          = newVolcano.FindChildNode("volcano", true);
            oldVolcano          = oldVolcano.ChildNodes [0];

            // Animate our dynamic volcano node.
            string shaderCode = "uniform float speed;\n" +
                                "_geometry.color = vec4(a_color.r, a_color.r, a_color.r, a_color.r);\n" +
                                "_geometry.texcoords[0] += (vec2(0.0, 1.0) * 0.05 * u_time);\n";

            string fragmentShaderCode = "#pragma transparent\n";

            // Dim background
            SCNNode back = RootNode.FindChildNode("dumy_rear", true);

            foreach (var child in back.ChildNodes)
            {
                child.CastsShadow = false;

                if (child.Geometry == null)
                {
                    continue;
                }

                foreach (SCNMaterial material in child.Geometry.Materials)
                {
                    material.LightingModelName = SCNLightingModel.Constant;
                    material.Multiply.Contents = AppKit.NSColor.FromDeviceWhite(0.3f, 1f);

                    material.Multiply.Intensity = 1;
                }
            }

            SCNNode backMiddle = RootNode.FindChildNode("dummy_middle", true);

            foreach (var child in backMiddle.ChildNodes)
            {
                if (child.Geometry == null)
                {
                    continue;
                }

                foreach (SCNMaterial material in child.Geometry.Materials)
                {
                    material.LightingModelName = SCNLightingModel.Constant;
                }
            }

            foreach (var child in newVolcano.ChildNodes)
            {
                foreach (var volc in child.ChildNodes)
                {
                    if (volc != oldVolcano && volc.Geometry != null)
                    {
                        volc.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant;
                        volc.Geometry.FirstMaterial.Multiply.Contents = AppKit.NSColor.White;

                        volc.Geometry.ShaderModifiers = new SCNShaderModifiers {
                            EntryPointGeometry = shaderCode,
                            EntryPointFragment = fragmentShaderCode
                        };
                    }
                }
            }

            Coconuts = new List <Coconut> ();
            return(RootNode);
        }