public MetalKitEssentialsSubmesh(MTKSubmesh mtkSubmesh, MDLSubmesh mdlSubmesh, IMTLDevice device) { materialUniforms = device.CreateBuffer((nuint)Marshal.SizeOf <MaterialUniforms> (), MTLResourceOptions.CpuCacheModeDefault); var uniforms = Marshal.PtrToStructure <MaterialUniforms> (materialUniforms.Contents); submesh = mtkSubmesh; for (nuint i = 0; i < mdlSubmesh.Material.Count; i++) { MDLMaterialProperty property = ObjectAtIndexedSubscript(mdlSubmesh.Material, i); if (property == null) { continue; } if (property.Name == "baseColorMap") { if (property.Type != MDLMaterialPropertyType.String) { continue; } var textureURL = new NSUrl(string.Format("file://{0}", property.StringValue)); var textureLoader = new MTKTextureLoader(device); NSError error; diffuseTexture = textureLoader.FromUrl(textureURL, null, out error); if (diffuseTexture == null) { throw new Exception(string.Format("Diffuse texture load: {0}", error.LocalizedDescription)); } } else if (property.Name == "BlinnSpecularColor") { if (property.Type == MDLMaterialPropertyType.Float4) { uniforms.specularColor = property.Float4Value; } else if (property.Type == MDLMaterialPropertyType.Float3) { uniforms.specularColor = new Vector4(property.Float3Value); } } else if (property.Name == "emission") { if (property.Type == MDLMaterialPropertyType.Float4) { uniforms.emissiveColor = property.Float4Value; } else if (property.Type == MDLMaterialPropertyType.Float3) { uniforms.emissiveColor = new Vector4(property.Float3Value); } } } Marshal.StructureToPtr(uniforms, materialUniforms.Contents, true); }
public void Copy() { TestRuntime.AssertXcodeVersion(8, 0); using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion)) { Assert.IsNotNull(obj.Copy()); } }
public void Setup() { if (!UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) { Assert.Ignore("Requires iOS9+"); } if (Runtime.Arch == Arch.SIMULATOR && IntPtr.Size == 4) { // There's a bug in the i386 version of objc_msgSend where it doesn't preserve SIMD arguments // when resizing the cache of method selectors for a type. So here we call all selectors we can // find, so that the subsequent tests don't end up producing any cache resize (radar #21630410). object dummy; using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion)) { dummy = obj.Color; dummy = obj.Float2Value; dummy = obj.Float3Value; dummy = obj.Float4Value; dummy = obj.FloatValue; dummy = obj.Matrix4x4; dummy = obj.Name; dummy = obj.Semantic; obj.SetProperties(new MDLMaterialProperty("foo", MDLMaterialSemantic.AmbientOcclusion)); dummy = obj.StringValue; dummy = obj.TextureSamplerValue; dummy = obj.Type; dummy = obj.UrlValue; } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, Vector3.Zero)) { } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, new MDLTextureSampler())) { } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, "string value")) { } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, new NSUrl("http://foo.com"))) { } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, Matrix4.Identity)) { } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, Vector4.Zero)) { } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, UIColor.Black.CGColor)) { } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, Vector2.Zero)) { } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, 1.23f)) { } } }
public void Ctors() { Vector2 V2; Vector3 V3; Vector4 V4; Matrix4 M4; MDLTextureSampler tsv; NSUrl url; using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion)) { Assert.AreEqual(MDLMaterialSemantic.AmbientOcclusion, obj.Semantic, "1 Semantic"); Assert.IsNull(obj.Color, "1 Color"); Asserts.AreEqual(Vector2.Zero, obj.Float2Value, "1 Float2Value"); Asserts.AreEqual(Vector3.Zero, obj.Float3Value, "1 Float3Value"); Asserts.AreEqual(Vector4.Zero, obj.Float4Value, "1 Float4Value"); Assert.AreEqual(0.0f, obj.FloatValue, "1 FloatValue"); Asserts.AreEqual(Matrix4.Identity, obj.Matrix4x4, "1 Matrix4x4"); Assert.AreEqual("name", obj.Name, "1 Name"); Assert.IsNull(obj.StringValue, "1 StringValue"); Assert.IsNull(obj.TextureSamplerValue, "1 TextureSamplerValue"); Assert.AreEqual(MDLMaterialPropertyType.Float, obj.Type, "1 Type"); Assert.IsNull(obj.UrlValue, "1 UrlValue"); V2 = new Vector2(1, 2); V3 = new Vector3(3, 4, 5); V4 = new Vector4(6, 7, 8, 9); M4 = new Matrix4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); tsv = new MDLTextureSampler(); url = new NSUrl("http://xamarin.com"); obj.Semantic = MDLMaterialSemantic.Anisotropic; Assert.AreEqual(MDLMaterialSemantic.Anisotropic, obj.Semantic, "2 Semantic"); obj.Color = UIColor.Blue.CGColor; Assert.AreEqual(UIColor.Blue.CGColor.ToString(), obj.Color.ToString(), "2 Color"); obj.Float2Value = V2; Asserts.AreEqual(V2, obj.Float2Value, "2 Float2Value"); obj.Float3Value = V3; Asserts.AreEqual(V3, obj.Float3Value, "2 Float3Value"); obj.Float4Value = V4; Asserts.AreEqual(V4, obj.Float4Value, "2 Float4Value"); obj.FloatValue = 3.14f; Assert.AreEqual(3.14f, obj.FloatValue, "2 FloatValue"); obj.Matrix4x4 = M4; // It looks like the Matrix4 setter is ignored, assigning a matrix // doesn't work in Xcode either. Asserts.AreEqual(Matrix4.Identity, obj.Matrix4x4, "2 Matrix4x4"); obj.Name = "new name"; Assert.AreEqual("new name", obj.Name, "2 Name"); obj.StringValue = "string value"; Assert.AreEqual("string value", obj.StringValue, "2 StringValue"); obj.TextureSamplerValue = tsv; Assert.AreEqual(tsv.Handle, obj.TextureSamplerValue.Handle, "2 TextureSamplerValue"); Assert.AreEqual(MDLMaterialPropertyType.Texture, obj.Type, "2 Type"); // Looks like the URLValue can't change after construction obj.UrlValue = url; Assert.IsNull(obj.UrlValue, "2 UrlValue"); } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, url)) { Assert.AreEqual(url.Handle, obj.UrlValue.Handle, "3 UrlValue"); } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, V3)) { Asserts.AreEqual(V3, obj.Float3Value, "4 Float3Value"); } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, tsv)) { Assert.AreEqual(tsv.Handle, obj.TextureSamplerValue.Handle, "5 TextureSamplerValue"); } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, "string value")) { Assert.AreEqual("string value", obj.StringValue, "6 StringValue"); } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, M4)) { Asserts.AreEqual(M4, obj.Matrix4x4, "7 Matrix4x4"); } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, V4)) { Asserts.AreEqual(V4, obj.Float4Value, "8 Float4Value"); } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, UIColor.Red.CGColor)) { Assert.AreEqual(UIColor.Blue.CGColor.ToString(), obj.Color.ToString(), "9 Color"); } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, V2)) { Asserts.AreEqual(V2, obj.Float2Value, "10 Float2Value"); } using (var obj = new MDLMaterialProperty("name", MDLMaterialSemantic.AmbientOcclusion, 3.1415f)) { Assert.AreEqual(3.1415f, obj.FloatValue, "11 FloatValue"); } }