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 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); }