// ------------------------------------------------------------------------------------------ // // Type-Specific (Mesh, Xform, Camera) Exporters. // ------------------------------------------------------------------------------------------ // void ExportMaterial(Scene scene, Material mat, string usdMaterialPath) { string shaderPath = usdMaterialPath + "/StandardShader"; var material = new USD.NET.Unity.MaterialSample(); material.surface.SetConnectedPath(shaderPath, "outputs:out"); var shader = new StandardShaderSample(); shader.id = new pxr.TfToken("Unity.Standard"); shader.albedo.defaultValue = mat.color; scene.Write(usdMaterialPath, material); scene.Write(shaderPath, shader); }
// ------------------------------------------------------------------------------------------ // // Create a USD scene for testing // ------------------------------------------------------------------------------------------ // static private Scene CreateSceneWithShading() { var scene = Scene.Create(); var cubePath = kCubePath; var materialPath = "/Model/Materials/SimpleMat"; var shaderPath = "/Model/Materials/SimpleMat/StandardShader"; var albedoMapPath = "/Model/Materials/SimpleMat/AlbedoMap"; var normalMapPath = "/Model/Materials/SimpleMat/NormalMap"; var emissionMapPath = "/Model/Materials/SimpleMat/EmissionMap"; var metallicMapPath = "/Model/Materials/SimpleMat/MetallicMap"; var occlusionMapPath = "/Model/Materials/SimpleMat/OcclusionMap"; var parallaxMapPath = "/Model/Materials/SimpleMat/ParallaxMap"; var detailNormalMapPath = "/Model/Materials/SimpleMat/DetailNormalMap"; var detailMaskPath = "/Model/Materials/SimpleMat/DetailMask"; var textureFilePath = @"Assets/Samples/USD/1.0.1-preview.1/ImportMaterials/Textures/"; var cube = new CubeSample(); cube.size = 7; cube.transform = Matrix4x4.identity; // // Setup Material. // var material = new MaterialSample(); material.surface.SetConnectedPath(shaderPath, "outputs:out"); // Various shader keywords are required to enable the standard shader to work as intended, // while these can be encoded as part of the schema, often they require some logic (e.g. is // emission color != black?). material.requiredKeywords = new string[] { "_EMISSION" }; // // Setup Shader. // var shader = new StandardShaderSample(); shader.id = new pxr.TfToken("Unity.Standard"); // Note that USD requires all connections target attributes, hence "outputs:out" appended to // the paths below. Think of this as a shader network graph, where a texture object could // be executed per pixel with a sampler, "outputs:out" would be the sampled color. shader.smoothnessScale.defaultValue = 0.99f; shader.enableSpecularHighlights.defaultValue = true; shader.enableGlossyReflections.defaultValue = true; shader.albedo.defaultValue = Color.white; shader.albedoMap.SetConnectedPath(albedoMapPath, "outputs:out"); shader.normalMapScale.defaultValue = 0.76f; shader.normalMap.SetConnectedPath(normalMapPath, "outputs:out"); shader.emission.defaultValue = Color.white * 0.3f; shader.emissionMap.SetConnectedPath(emissionMapPath, "outputs:out"); shader.metallicMap.SetConnectedPath(metallicMapPath, "outputs:out"); shader.occlusionMapScale.defaultValue = 0.65f; shader.occlusionMap.SetConnectedPath(occlusionMapPath, "outputs:out"); shader.parallaxMapScale.defaultValue = 0.1f; shader.parallaxMap.SetConnectedPath(parallaxMapPath, "outputs:out"); shader.detailMask.SetConnectedPath(detailMaskPath); shader.detailNormalMapScale.defaultValue = .05f; shader.detailNormalMap.SetConnectedPath(detailNormalMapPath, "outputs:out"); // // Setup Textures. // var albedoTexture = new Texture2DSample(); albedoTexture.sourceFile.defaultValue = textureFilePath + "albedoMap.png"; albedoTexture.sRgb = true; var normalTexture = new Texture2DSample(); normalTexture.sourceFile.defaultValue = textureFilePath + "normalMap.png"; normalTexture.sRgb = true; var emissionTexture = new Texture2DSample(); emissionTexture.sourceFile.defaultValue = textureFilePath + "emissionMap.png"; emissionTexture.sRgb = true; var metallicTexture = new Texture2DSample(); metallicTexture.sourceFile.defaultValue = textureFilePath + "metallicMap.png"; metallicTexture.sRgb = true; var occlusionTexture = new Texture2DSample(); occlusionTexture.sourceFile.defaultValue = textureFilePath + "occlusionMap.png"; occlusionTexture.sRgb = true; var parallaxTexture = new Texture2DSample(); parallaxTexture.sourceFile.defaultValue = textureFilePath + "parallaxMap.png"; parallaxTexture.sRgb = true; var detailNormalTexture = new Texture2DSample(); detailNormalTexture.sourceFile.defaultValue = textureFilePath + "detailMap.png"; detailNormalTexture.sRgb = true; var detailMaskTexture = new Texture2DSample(); detailMaskTexture.sourceFile.defaultValue = textureFilePath + "metallicMap.png"; detailMaskTexture.sRgb = true; // // Write Everything. // scene.Write(cubePath, cube); scene.Write(materialPath, material); scene.Write(shaderPath, shader); scene.Write(albedoMapPath, albedoTexture); scene.Write(normalMapPath, normalTexture); scene.Write(emissionMapPath, emissionTexture); scene.Write(metallicMapPath, metallicTexture); scene.Write(occlusionMapPath, occlusionTexture); scene.Write(parallaxMapPath, parallaxTexture); scene.Write(detailNormalMapPath, detailNormalTexture); scene.Write(detailMaskPath, detailMaskTexture); // // Bind Material. // MaterialSample.Bind(scene, cubePath, materialPath); // // Write out the scene as a string, for debugging. // string s; scene.Stage.ExportToString(out s); Debug.Log("Loading:\n" + s); return(scene); }
public static void MaterialBindTest() { // Game plan: // 1. Create a cube // 2. Create a material // 3. Create a shader // 4. Create a texture // 5. Connect the material to the shader's output // 6. Connect the shader's albedo parameter to the texture's output // 7. Connect the texture to the source file on disk // 8. Write all values // 9. Bind the cube to the material var scene = Scene.Create(); var cubePath = "/Model/Geom/Cube"; var materialPath = "/Model/Materials/SimpleMat"; var shaderPath = "/Model/Materials/SimpleMat/StandardShader"; var texturePath = "/Model/Materials/SimpleMat/AlbedoTexture"; var cube = new CubeSample(); cube.size = 1; var material = new MaterialSample(); material.surface.SetConnectedPath(shaderPath, "outputs:out"); var shader = new StandardShaderSample(); shader.albedo.defaultValue = Color.white; shader.albedo.SetConnectedPath(texturePath, "outputs:out"); AssertEqual(shader.albedo.connectedPath, texturePath + ".outputs:out"); var texture = new Texture2DSample(); texture.sourceFile.defaultValue = @"C:\A\Bogus\Texture\Path.png"; texture.sRgb = true; scene.Write("/Model", new XformSample()); scene.Write("/Model/Geom", new XformSample()); scene.Write("/Model/Materials", new XformSample()); scene.Write(cubePath, cube); scene.Write(materialPath, material); scene.Write(shaderPath, shader); scene.Write(texturePath, texture); MaterialSample.Bind(scene, cubePath, materialPath); var material2 = new MaterialSample(); var shader2 = new StandardShaderSample(); var texture2 = new Texture2DSample(); scene.Read(materialPath, material2); scene.Read(shaderPath, shader2); scene.Read(texturePath, texture2); var param = shader2.GetInputParameters().First(); AssertEqual(shader.albedo.connectedPath, param.connectedPath); AssertEqual("albedo", param.usdName); AssertEqual(shader.albedo.defaultValue, param.value); AssertEqual("_Color", param.unityName); var paramTex = shader2.GetInputTextures().First(); AssertEqual(shader.albedoMap.connectedPath, paramTex.connectedPath); AssertEqual("albedoMap", paramTex.usdName); AssertEqual(shader.albedoMap.defaultValue, paramTex.value); AssertEqual("_MainTex", paramTex.unityName); AssertEqual(material.surface.defaultValue, material2.surface.defaultValue); AssertEqual(material.surface.connectedPath, material2.surface.connectedPath); AssertEqual(shader.albedo.defaultValue, shader2.albedo.defaultValue); AssertEqual(shader.albedo.connectedPath, shader2.albedo.connectedPath); AssertEqual(shader.id, shader2.id); AssertEqual(texture.sourceFile.defaultValue, texture2.sourceFile.defaultValue); AssertEqual(texture.sRgb, texture2.sRgb); PrintScene(scene); }