窥探到一点关于HDRP/Lit shader的一些参数,比如mat.SetTexture("_BaseColorMap", DiffuseMap);

此文章介绍了一个从USD格式文件导入材质到Unity HDRP(高动态范围渲染管线)的过程。文章详细解释了如何使用自定义的ShaderImporter类来处理不同类型的纹理映射,包括漫反射、金属感、粗糙度、遮挡、置换、透明涂层和法线贴图,以及如何将这些映射整合到单一的遮罩贴图中。

Skip to content

 

Pull requestsIssuesMarketplaceExplore

 

 

@emanonhero

Unity-Technologies/usd-unity-sdk

 Code Issues 17 Pull requests 3 Projects 0 Wiki Insights

Tree: 0253d7a17d 

usd-unity-sdk/package/com.unity.formats.usd/Runtime/Scripts/IO/Materials/HdrpShaderImporter.cs

Find fileCopy path

@jcowles jcowles Namespace: Rename to Unity.Formats.USD3a70e60 on 18 Feb

1 contributor

154 lines (127 sloc)  5.54 KB

RawBlameHistory

  

 // Copyright 2018 Jeremy Cowles. All rights reserved.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
 // You may obtain a copy of the License at
 //
 // http://www.apache.org/licenses/LICENSE-2.0
 //
 // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
  
 using UnityEngine;
  
 namespace Unity.Formats.USD {
  
 public class HdrpShaderImporter : ShaderImporterBase {
  
 private static Material ChannelCombinerMat;
  
 public HdrpShaderImporter(Material material) : base(material) {
 }
  
 public override void ImportFromUsd() {
 Material mat = Material;
  
 if (DiffuseMap) {
 Debug.Log("here");
 mat.SetTexture("_BaseColorMap", DiffuseMap);
 mat.SetColor("_BaseColor", Color.white);
 } else {
 mat.SetColor("_BaseColor", Diffuse.GetValueOrDefault(mat.color));
 }
  
 // TODO: What about opacity map?
  
 if (IsMetallicWorkflow) {
 // Robustness: It would be ideal if this parameter were provided by HDRP, however that
 // would require this asset package having a dependency on the HDRP package itself,
 // which is (yet) not desirable.
 mat.SetFloat("_MaterialID", /*Standard Metallic*/ 1);
 } else {
 mat.SetFloat("_MaterialID", /*Spec Color*/ 4);
 mat.EnableKeyword("_MATERIAL_FEATURE_SPECULAR_COLOR");
 mat.EnableKeyword("_SPECULARCOLORMAP");
 }
  
 // R=Metallic, G=Occlusion, B=Displacement, A=Roughness(Smoothness)
 var MaskMap = BuildMaskMap(IsMetallicWorkflow ? MetallicMap : null, OcclusionMap, DisplacementMap, RoughnessMap);
 if (MaskMap) {
 mat.SetTexture("_MaskMap", MaskMap);
 mat.EnableKeyword("_MASKMAP");
 }
  
 if (IsMetallicWorkflow) {
 if (!MetallicMap) {
 mat.SetFloat("_Metallic", Metallic.GetValueOrDefault());
 }
 } else {
 if (SpecularMap) {
 mat.SetTexture("_SpecularColorMap", SpecularMap);
 } else {
 mat.SetColor("_SpecularColor", Specular.GetValueOrDefault());
 }
 }
  
 if (!RoughnessMap) {
 var smoothness = 1 - Roughness.GetValueOrDefault();
 mat.SetFloat("_Smoothness", smoothness);
 // HDRP Lit does not seem to respect smoothness, so just clamp to the correct value.
 mat.SetFloat("_SmoothnessRemapMin", smoothness);
 mat.SetFloat("_SmoothnessRemapMax", smoothness);
 }
  
 if (!OcclusionMap) {
 mat.SetFloat("_AORemapMin", Occlusion.GetValueOrDefault());
 mat.SetFloat("_AORemapMax", Occlusion.GetValueOrDefault());
 }
  
 // Single displacement scalar value not supported.
  
 if (ClearcoatMap) {
 mat.SetTexture("_CoatMaskMap", ClearcoatMap);
 mat.EnableKeyword("_MATERIAL_FEATURE_CLEAR_COAT");
 }
 mat.SetFloat("_CoatMask", ClearcoatRoughness.GetValueOrDefault());
  
 if (NormalMap) {
 mat.SetTexture("_NormalMap", NormalMap);
 mat.EnableKeyword("_NORMALMAP");
 }
  
 if (EmissionMap) {
 mat.SetTexture("_EmissionMap", EmissionMap);
 mat.EnableKeyword("_EMISSIVE_COLOR_MAP");
 } else {
 mat.SetColor("_EmissionColor", Emission.GetValueOrDefault());
 }
 }
  
 private static Texture2D BuildMaskMap(Texture2D red, Texture2D green, Texture2D blue, Texture2D alpha) {
 var maxW = Mathf.Max(red ? red.width : 0, green ? green.width : 0);
 maxW = Mathf.Max(maxW, blue ? blue.width : 0);
 maxW = Mathf.Max(maxW, alpha ? alpha.width : 0);
  
 var maxH = Mathf.Max(red ? red.height : 0, green ? green.height : 0);
 maxH = Mathf.Max(maxH, blue ? blue.height : 0);
 maxH = Mathf.Max(maxH, alpha ? alpha.height : 0);
  
 if (maxH == 0 || maxW == 0) { return null; }
  
 var tmp = RenderTexture.GetTemporary(maxW, maxH, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
  
 if (!ChannelCombinerMat) {
 ChannelCombinerMat = new Material(Shader.Find("Hidden/USD/ChannelCombiner"));
 }
  
 var newTex = new Texture2D(maxW, maxH, TextureFormat.ARGB32, true, true);
 ChannelCombinerMat.SetFloat("_InvertAlpha", 1);
 ChannelCombinerMat.SetTexture("_R", red);
 ChannelCombinerMat.SetTexture("_G", green);
 ChannelCombinerMat.SetTexture("_B", blue);
 ChannelCombinerMat.SetTexture("_A", alpha);
 Graphics.Blit(red, tmp, ChannelCombinerMat);
  
 RenderTexture.active = tmp;
 newTex.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
 newTex.Apply();
 RenderTexture.ReleaseTemporary(tmp);
  
 #if UNITY_EDITOR
 var newAssetPath = UnityEditor.AssetDatabase.GenerateUniqueAssetPath("Assets/maskMap.png");
 var bytes = newTex.EncodeToPNG();
 Debug.Log(newAssetPath);
 System.IO.File.WriteAllBytes(newAssetPath, bytes);
 UnityEditor.AssetDatabase.ImportAsset(newAssetPath);
 var texImporter = (UnityEditor.TextureImporter)UnityEditor.AssetImporter.GetAtPath(newAssetPath);
 UnityEditor.EditorUtility.SetDirty(texImporter);
 texImporter.SaveAndReimport();
 #endif
 // To get the correct file ID, the texture must be reloaded from the asset path.
 Texture2D.DestroyImmediate(newTex);
 #if UNITY_EDITOR
 return (Texture2D)UnityEditor.AssetDatabase.LoadAssetAtPath(newAssetPath, typeof(Texture2D));
 #else
 return null;
 #endif
 }
  
 }
 }

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值