From 64c923e238fc9545e96d0dad2f8dbea3889fdf98 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 5 Feb 2025 18:54:44 +0900 Subject: [PATCH] BumpMap --- .../VRM10Viewer/Assets/TinyPbr.shadergraph | 542 +++++++++++++++++- .../VRM10Viewer/TinyPbrMaterialContext.cs | 22 + .../TinyPbrMaterialDescriptorGenerator.cs | 14 +- 3 files changed, 575 insertions(+), 3 deletions(-) diff --git a/Assets/VRM10_Samples/VRM10Viewer/Assets/TinyPbr.shadergraph b/Assets/VRM10_Samples/VRM10Viewer/Assets/TinyPbr.shadergraph index 2eb3b1c4d..fd98bb652 100644 --- a/Assets/VRM10_Samples/VRM10Viewer/Assets/TinyPbr.shadergraph +++ b/Assets/VRM10_Samples/VRM10Viewer/Assets/TinyPbr.shadergraph @@ -23,6 +23,12 @@ }, { "m_Id": "fd4b1637b32c44d79eea652e4b5626b2" + }, + { + "m_Id": "f8ac3f450f1a4d849926601ace10a04f" + }, + { + "m_Id": "d77500f24b3f490daf8cc2f5dcd19f60" } ], "m_Keywords": [], @@ -107,6 +113,18 @@ }, { "m_Id": "73efafc771664c7b8a4e4b9e58a6e868" + }, + { + "m_Id": "42b5b42d2e304772a31a535448fded9c" + }, + { + "m_Id": "4f05aa87bb184723a2d2b15721a0c3e9" + }, + { + "m_Id": "b83e5bff780742a79344a87ff879965a" + }, + { + "m_Id": "609f313b8b6f4ee8a871650dc611dc0c" } ], "m_GroupDatas": [], @@ -224,6 +242,20 @@ "m_SlotId": 0 } }, + { + "m_OutputSlot": { + "m_Node": { + "m_Id": "42b5b42d2e304772a31a535448fded9c" + }, + "m_SlotId": 2 + }, + "m_InputSlot": { + "m_Node": { + "m_Id": "6f683c2eb23a4baf8dc028500640b75a" + }, + "m_SlotId": 0 + } + }, { "m_OutputSlot": { "m_Node": { @@ -238,6 +270,34 @@ "m_SlotId": 1 } }, + { + "m_OutputSlot": { + "m_Node": { + "m_Id": "4f05aa87bb184723a2d2b15721a0c3e9" + }, + "m_SlotId": 0 + }, + "m_InputSlot": { + "m_Node": { + "m_Id": "42b5b42d2e304772a31a535448fded9c" + }, + "m_SlotId": 0 + } + }, + { + "m_OutputSlot": { + "m_Node": { + "m_Id": "609f313b8b6f4ee8a871650dc611dc0c" + }, + "m_SlotId": 0 + }, + "m_InputSlot": { + "m_Node": { + "m_Id": "4f05aa87bb184723a2d2b15721a0c3e9" + }, + "m_SlotId": 1 + } + }, { "m_OutputSlot": { "m_Node": { @@ -350,6 +410,20 @@ "m_SlotId": 1 } }, + { + "m_OutputSlot": { + "m_Node": { + "m_Id": "b83e5bff780742a79344a87ff879965a" + }, + "m_SlotId": 0 + }, + "m_InputSlot": { + "m_Node": { + "m_Id": "42b5b42d2e304772a31a535448fded9c" + }, + "m_SlotId": 1 + } + }, { "m_OutputSlot": { "m_Node": { @@ -483,6 +557,19 @@ "m_Labels": [] } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Texture2DMaterialSlot", + "m_ObjectId": "064aae75fadd4a0b942d21348da5dd81", + "m_Id": 0, + "m_DisplayName": "BumpMap", + "m_SlotType": 1, + "m_Hidden": false, + "m_ShaderOutputName": "Out", + "m_StageCapability": 3, + "m_BareResource": false +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.CategoryData", @@ -509,6 +596,12 @@ }, { "m_Id": "c4ff105a2181464aaee7d5b6146a2c9b" + }, + { + "m_Id": "f8ac3f450f1a4d849926601ace10a04f" + }, + { + "m_Id": "d77500f24b3f490daf8cc2f5dcd19f60" } ] } @@ -1100,6 +1193,29 @@ "m_SerializedDescriptor": "VertexDescription.Normal" } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector3MaterialSlot", + "m_ObjectId": "32680074fcd94dcfb904ea7725f3bdf7", + "m_Id": 0, + "m_DisplayName": "In", + "m_SlotType": 0, + "m_Hidden": false, + "m_ShaderOutputName": "In", + "m_StageCapability": 3, + "m_Value": { + "x": 0.0, + "y": 0.0, + "z": 1.0 + }, + "m_DefaultValue": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "m_Labels": [] +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.Vector4MaterialSlot", @@ -1234,6 +1350,47 @@ "m_Labels": [] } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.NormalStrengthNode", + "m_ObjectId": "42b5b42d2e304772a31a535448fded9c", + "m_Group": { + "m_Id": "" + }, + "m_Name": "Normal Strength", + "m_DrawState": { + "m_Expanded": true, + "m_Position": { + "serializedVersion": "2", + "x": -324.6666259765625, + "y": 1138.6668701171875, + "width": 209.3333740234375, + "height": 303.9998779296875 + } + }, + "m_Slots": [ + { + "m_Id": "32680074fcd94dcfb904ea7725f3bdf7" + }, + { + "m_Id": "4e90ec3f541d453c8ae515b9ae1ecacc" + }, + { + "m_Id": "7a258b7ee50f42959e7e5e1bd2c7631b" + } + ], + "synonyms": [ + "intensity" + ], + "m_Precision": 0, + "m_PreviewExpanded": true, + "m_DismissedVersion": 0, + "m_PreviewMode": 0, + "m_CustomColors": { + "m_SerializableColors": [] + } +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.Texture2DMaterialSlot", @@ -1342,6 +1499,81 @@ "m_Channel": 0 } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", + "m_ObjectId": "4e90ec3f541d453c8ae515b9ae1ecacc", + "m_Id": 1, + "m_DisplayName": "Strength", + "m_SlotType": 0, + "m_Hidden": false, + "m_ShaderOutputName": "Strength", + "m_StageCapability": 3, + "m_Value": 1.0, + "m_DefaultValue": 1.0, + "m_Labels": [] +} + +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.SampleTexture2DNode", + "m_ObjectId": "4f05aa87bb184723a2d2b15721a0c3e9", + "m_Group": { + "m_Id": "" + }, + "m_Name": "Sample Texture 2D", + "m_DrawState": { + "m_Expanded": true, + "m_Position": { + "serializedVersion": "2", + "x": -633.9999389648438, + "y": 979.3333129882813, + "width": 184.66665649414063, + "height": 254.66668701171876 + } + }, + "m_Slots": [ + { + "m_Id": "6d10127b9d4e4bcfa16626d3ae7ef55e" + }, + { + "m_Id": "b00b5f49f832499283825f4a2cf12f79" + }, + { + "m_Id": "c35266cc646a444b9bd38a748550b8e4" + }, + { + "m_Id": "aeac528b6a2444518362cffca1be05ba" + }, + { + "m_Id": "63a74a0ddc784303b8449630e548ae2c" + }, + { + "m_Id": "a071058625974c08b0932da5861c7128" + }, + { + "m_Id": "9a3a7785b0b44221afd748ed04b0dde6" + }, + { + "m_Id": "7d670b40f8f2468a9af5840562b1c62d" + } + ], + "synonyms": [ + "tex2d" + ], + "m_Precision": 0, + "m_PreviewExpanded": false, + "m_DismissedVersion": 0, + "m_PreviewMode": 0, + "m_CustomColors": { + "m_SerializableColors": [] + }, + "m_TextureType": 1, + "m_NormalMapSpace": 0, + "m_EnableGlobalMipBias": true, + "m_MipSamplingMode": 0 +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.DynamicValueMaterialSlot", @@ -1625,6 +1857,42 @@ "m_SerializedDescriptor": "SurfaceDescription.Metallic" } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.PropertyNode", + "m_ObjectId": "609f313b8b6f4ee8a871650dc611dc0c", + "m_Group": { + "m_Id": "" + }, + "m_Name": "Property", + "m_DrawState": { + "m_Expanded": true, + "m_Position": { + "serializedVersion": "2", + "x": -833.9999389648438, + "y": 1216.0, + "width": 138.0, + "height": 35.9998779296875 + } + }, + "m_Slots": [ + { + "m_Id": "064aae75fadd4a0b942d21348da5dd81" + } + ], + "synonyms": [], + "m_Precision": 0, + "m_PreviewExpanded": true, + "m_DismissedVersion": 0, + "m_PreviewMode": 0, + "m_CustomColors": { + "m_SerializableColors": [] + }, + "m_Property": { + "m_Id": "d77500f24b3f490daf8cc2f5dcd19f60" + } +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.PositionMaterialSlot", @@ -1649,6 +1917,21 @@ "m_Space": 0 } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", + "m_ObjectId": "63a74a0ddc784303b8449630e548ae2c", + "m_Id": 7, + "m_DisplayName": "A", + "m_SlotType": 1, + "m_Hidden": false, + "m_ShaderOutputName": "A", + "m_StageCapability": 2, + "m_Value": 0.0, + "m_DefaultValue": 0.0, + "m_Labels": [] +} + { "m_SGVersion": 1, "m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty", @@ -1792,7 +2075,32 @@ "isMainTexture": false, "useTilingAndOffset": true, "m_Modifiable": true, - "m_DefaultType": 0 + "m_DefaultType": 4 +} + +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector4MaterialSlot", + "m_ObjectId": "6d10127b9d4e4bcfa16626d3ae7ef55e", + "m_Id": 0, + "m_DisplayName": "RGBA", + "m_SlotType": 1, + "m_Hidden": false, + "m_ShaderOutputName": "RGBA", + "m_StageCapability": 2, + "m_Value": { + "x": 0.0, + "y": 0.0, + "z": 0.0, + "w": 0.0 + }, + "m_DefaultValue": { + "x": 0.0, + "y": 0.0, + "z": 0.0, + "w": 0.0 + }, + "m_Labels": [] } { @@ -1978,6 +2286,29 @@ } } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector3MaterialSlot", + "m_ObjectId": "7a258b7ee50f42959e7e5e1bd2c7631b", + "m_Id": 2, + "m_DisplayName": "Out", + "m_SlotType": 1, + "m_Hidden": false, + "m_ShaderOutputName": "Out", + "m_StageCapability": 3, + "m_Value": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "m_DefaultValue": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "m_Labels": [] +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.Texture2DInputMaterialSlot", @@ -2030,6 +2361,19 @@ "m_SerializedDescriptor": "VertexDescription.Tangent" } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.SamplerStateMaterialSlot", + "m_ObjectId": "7d670b40f8f2468a9af5840562b1c62d", + "m_Id": 3, + "m_DisplayName": "Sampler", + "m_SlotType": 0, + "m_Hidden": false, + "m_ShaderOutputName": "Sampler", + "m_StageCapability": 3, + "m_BareResource": false +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.DynamicValueMaterialSlot", @@ -2440,6 +2784,46 @@ } } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.UVMaterialSlot", + "m_ObjectId": "9a3a7785b0b44221afd748ed04b0dde6", + "m_Id": 2, + "m_DisplayName": "UV", + "m_SlotType": 0, + "m_Hidden": false, + "m_ShaderOutputName": "UV", + "m_StageCapability": 3, + "m_Value": { + "x": 0.0, + "y": 0.0 + }, + "m_DefaultValue": { + "x": 0.0, + "y": 0.0 + }, + "m_Labels": [], + "m_Channel": 0 +} + +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Texture2DInputMaterialSlot", + "m_ObjectId": "a071058625974c08b0932da5861c7128", + "m_Id": 1, + "m_DisplayName": "Texture", + "m_SlotType": 0, + "m_Hidden": false, + "m_ShaderOutputName": "Texture", + "m_StageCapability": 3, + "m_BareResource": false, + "m_Texture": { + "m_SerializedTexture": "", + "m_Guid": "" + }, + "m_DefaultType": 3 +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.PropertyNode", @@ -2569,6 +2953,36 @@ "m_Labels": [] } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", + "m_ObjectId": "aeac528b6a2444518362cffca1be05ba", + "m_Id": 6, + "m_DisplayName": "B", + "m_SlotType": 1, + "m_Hidden": false, + "m_ShaderOutputName": "B", + "m_StageCapability": 2, + "m_Value": 0.0, + "m_DefaultValue": 0.0, + "m_Labels": [] +} + +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", + "m_ObjectId": "b00b5f49f832499283825f4a2cf12f79", + "m_Id": 4, + "m_DisplayName": "R", + "m_SlotType": 1, + "m_Hidden": false, + "m_ShaderOutputName": "R", + "m_StageCapability": 2, + "m_Value": 0.0, + "m_DefaultValue": 0.0, + "m_Labels": [] +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.PropertyNode", @@ -2605,6 +3019,42 @@ } } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.PropertyNode", + "m_ObjectId": "b83e5bff780742a79344a87ff879965a", + "m_Group": { + "m_Id": "" + }, + "m_Name": "Property", + "m_DrawState": { + "m_Expanded": true, + "m_Position": { + "serializedVersion": "2", + "x": -830.6666259765625, + "y": 1260.6666259765625, + "width": 134.66668701171876, + "height": 36.0 + } + }, + "m_Slots": [ + { + "m_Id": "e7d2ed98f9964f4aa647d47a4707f549" + } + ], + "synonyms": [], + "m_Precision": 0, + "m_PreviewExpanded": true, + "m_DismissedVersion": 0, + "m_PreviewMode": 0, + "m_CustomColors": { + "m_SerializableColors": [] + }, + "m_Property": { + "m_Id": "f8ac3f450f1a4d849926601ace10a04f" + } +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.Vector3Node", @@ -2713,6 +3163,21 @@ "m_BareResource": false } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", + "m_ObjectId": "c35266cc646a444b9bd38a748550b8e4", + "m_Id": 5, + "m_DisplayName": "G", + "m_SlotType": 1, + "m_Hidden": false, + "m_ShaderOutputName": "G", + "m_StageCapability": 2, + "m_Value": 0.0, + "m_DefaultValue": 0.0, + "m_Labels": [] +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", @@ -2755,7 +3220,7 @@ "isMainTexture": false, "useTilingAndOffset": true, "m_Modifiable": true, - "m_DefaultType": 0 + "m_DefaultType": 4 } { @@ -2836,6 +3301,36 @@ } } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Internal.Texture2DShaderProperty", + "m_ObjectId": "d77500f24b3f490daf8cc2f5dcd19f60", + "m_Guid": { + "m_GuidSerialized": "96cd3240-b1e2-4ec9-816f-b4be029dbeb6" + }, + "m_Name": "BumpMap", + "m_DefaultRefNameVersion": 1, + "m_RefNameGeneratedByDisplayName": "BumpMap", + "m_DefaultReferenceName": "_BumpMap", + "m_OverrideReferenceName": "", + "m_GeneratePropertyBlock": true, + "m_UseCustomSlotLabel": false, + "m_CustomSlotLabel": "", + "m_DismissedVersion": 0, + "m_Precision": 0, + "overrideHLSLDeclaration": false, + "hlslDeclarationOverride": 0, + "m_Hidden": false, + "m_Value": { + "m_SerializedTexture": "", + "m_Guid": "" + }, + "isMainTexture": false, + "useTilingAndOffset": false, + "m_Modifiable": true, + "m_DefaultType": 3 +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.DynamicValueMaterialSlot", @@ -2976,6 +3471,21 @@ "m_Labels": [] } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", + "m_ObjectId": "e7d2ed98f9964f4aa647d47a4707f549", + "m_Id": 0, + "m_DisplayName": "BumpScale", + "m_SlotType": 1, + "m_Hidden": false, + "m_ShaderOutputName": "Out", + "m_StageCapability": 3, + "m_Value": 0.0, + "m_DefaultValue": 0.0, + "m_Labels": [] +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.DynamicValueMaterialSlot", @@ -3131,6 +3641,34 @@ "m_SerializedDescriptor": "VertexDescription.Position" } +{ + "m_SGVersion": 1, + "m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty", + "m_ObjectId": "f8ac3f450f1a4d849926601ace10a04f", + "m_Guid": { + "m_GuidSerialized": "8bb96dec-3d73-4fb0-8e69-db74dee7110e" + }, + "m_Name": "BumpScale", + "m_DefaultRefNameVersion": 1, + "m_RefNameGeneratedByDisplayName": "BumpScale", + "m_DefaultReferenceName": "_BumpScale", + "m_OverrideReferenceName": "", + "m_GeneratePropertyBlock": true, + "m_UseCustomSlotLabel": false, + "m_CustomSlotLabel": "", + "m_DismissedVersion": 0, + "m_Precision": 0, + "overrideHLSLDeclaration": false, + "hlslDeclarationOverride": 0, + "m_Hidden": false, + "m_Value": 0.0, + "m_FloatType": 0, + "m_RangeValues": { + "x": 0.0, + "y": 1.0 + } +} + { "m_SGVersion": 1, "m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty", diff --git a/Assets/VRM10_Samples/VRM10Viewer/TinyPbrMaterialContext.cs b/Assets/VRM10_Samples/VRM10Viewer/TinyPbrMaterialContext.cs index c9a8282f2..70b07e553 100644 --- a/Assets/VRM10_Samples/VRM10Viewer/TinyPbrMaterialContext.cs +++ b/Assets/VRM10_Samples/VRM10Viewer/TinyPbrMaterialContext.cs @@ -29,6 +29,14 @@ namespace UniVRM10.VRM10Viewer /// Texture2D.Blue = 1.0. The metalness /// private static readonly int MetallicRoughnessMapProp = Shader.PropertyToID("_MetallicRoughnessMap"); + /// + /// float = 1.0 + /// + private static readonly int BumpScaleProp = Shader.PropertyToID("_BumpScale"); + /// + /// Texture2D = [0, 0, 1.0] + /// + private static readonly int BumpMapProp = Shader.PropertyToID("_BumpMap"); public readonly Material Material; public TinyPbrMaterialContext(Material material) @@ -86,5 +94,19 @@ namespace UniVRM10.VRM10Viewer get => Material.GetTexture(MetallicRoughnessMapProp); set => Material.SetTexture(MetallicRoughnessMapProp, value); } + + public float BumpScale + { + get => Material.GetFloat(BumpScaleProp); + set => Material.SetFloat(BumpScaleProp, value); + } + public Texture BumpMap + { + get => Material.GetTexture(BumpMapProp); + set + { + Material.SetTexture(BumpMapProp, value); + } + } } } \ No newline at end of file diff --git a/Assets/VRM10_Samples/VRM10Viewer/TinyPbrMaterialDescriptorGenerator.cs b/Assets/VRM10_Samples/VRM10Viewer/TinyPbrMaterialDescriptorGenerator.cs index 035c2a539..5ba063422 100644 --- a/Assets/VRM10_Samples/VRM10Viewer/TinyPbrMaterialDescriptorGenerator.cs +++ b/Assets/VRM10_Samples/VRM10Viewer/TinyPbrMaterialDescriptorGenerator.cs @@ -72,7 +72,7 @@ namespace UniVRM10.VRM10Viewer await ImportBaseColorAsync(data, src, context, getTextureAsync, awaitCaller); await ImportMetallicRoughnessAsync(data, src, context, getTextureAsync, awaitCaller); await ImportOcclusionAsync(data, src, context, getTextureAsync, awaitCaller); - // await ImportNormalAsync(data, src, context, getTextureAsync, awaitCaller); + await ImportNormalAsync(data, src, context, getTextureAsync, awaitCaller); // await ImportEmissionAsync(data, src, context, getTextureAsync, awaitCaller); // context.Validate(); @@ -130,5 +130,17 @@ namespace UniVRM10.VRM10Viewer } } } + + private static async Task ImportNormalAsync(GltfData data, glTFMaterial src, TinyPbrMaterialContext context, GetTextureAsyncFunc getTextureAsync, IAwaitCaller awaitCaller) + { + if (src.normalTexture is { index: >= 0 }) + { + if (GltfPbrTextureImporter.TryNormalTexture(data, src, out _, out var desc)) + { + context.BumpMap = await getTextureAsync(desc, awaitCaller); + context.BumpScale = src.normalTexture.scale; + } + } + } } } \ No newline at end of file