72 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
		
		
			
		
	
	
			72 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| 
								 | 
							
								## BRDF
							 | 
						|||
| 
								 | 
							
								案例中使用了迪士尼模型。
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								## 混合模式
							 | 
						|||
| 
								 | 
							
								`SrcBlend`为`One`,`DetBlend`为`OneMinusSrc`
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								## Shader GUI
							 | 
						|||
| 
								 | 
							
								在`Shader`{}内添加`CustomEditor“ CustomShaderGUI”`。之后添加脚本:
							 | 
						|||
| 
								 | 
							
								```c#
							 | 
						|||
| 
								 | 
							
								using UnityEditor;
							 | 
						|||
| 
								 | 
							
								using UnityEngine;
							 | 
						|||
| 
								 | 
							
								using UnityEngine.Rendering;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								public class CustomShaderGUI : ShaderGUI {
							 | 
						|||
| 
								 | 
							
									MaterialEditor editor;
							 | 
						|||
| 
								 | 
							
									Object[] materials;
							 | 
						|||
| 
								 | 
							
									MaterialProperty[] properties;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									public override void OnGUI (
							 | 
						|||
| 
								 | 
							
										MaterialEditor materialEditor, MaterialProperty[] properties
							 | 
						|||
| 
								 | 
							
									) {
							 | 
						|||
| 
								 | 
							
										base.OnGUI(materialEditor, properties);
							 | 
						|||
| 
								 | 
							
										editor = materialEditor;
							 | 
						|||
| 
								 | 
							
										materials = materialEditor.targets;
							 | 
						|||
| 
								 | 
							
										this.properties = properties;
							 | 
						|||
| 
								 | 
							
									}
							 | 
						|||
| 
								 | 
							
								}
							 | 
						|||
| 
								 | 
							
								```
							 | 
						|||
| 
								 | 
							
								之后实现
							 | 
						|||
| 
								 | 
							
								```c#
							 | 
						|||
| 
								 | 
							
								bool HasProperty (string name) =>
							 | 
						|||
| 
								 | 
							
								    FindProperty(name, properties, false) != null;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								void SetProperty (string name, string keyword, bool value) {
							 | 
						|||
| 
								 | 
							
								    if (SetProperty(name, value ? 1f : 0f)) {
							 | 
						|||
| 
								 | 
							
								        SetKeyword(keyword, value);
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								bool SetProperty (string name, float value) {
							 | 
						|||
| 
								 | 
							
								    MaterialProperty property = FindProperty(name, properties, false);
							 | 
						|||
| 
								 | 
							
								    if (property != null) {
							 | 
						|||
| 
								 | 
							
								        property.floatValue = value;
							 | 
						|||
| 
								 | 
							
								        return true;
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								    return false;
							 | 
						|||
| 
								 | 
							
								}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								void SetKeyword (string keyword, bool enabled) {
							 | 
						|||
| 
								 | 
							
								    if (enabled) {
							 | 
						|||
| 
								 | 
							
								        foreach (Material m in materials) {
							 | 
						|||
| 
								 | 
							
								            m.EnableKeyword(keyword);
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								    else {
							 | 
						|||
| 
								 | 
							
								        foreach (Material m in materials) {
							 | 
						|||
| 
								 | 
							
								            m.DisableKeyword(keyword);
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 | 
						|||
| 
								 | 
							
								```
							 | 
						|||
| 
								 | 
							
								就可以添加若干自定义Shader属性的`Set`函数了。添加按钮逻辑如下:
							 | 
						|||
| 
								 | 
							
								```c#
							 | 
						|||
| 
								 | 
							
								bool PresetButton (string name) {
							 | 
						|||
| 
								 | 
							
								    if (GUILayout.Button(name)) {
							 | 
						|||
| 
								 | 
							
								        editor.RegisterPropertyChangeUndo(name);
							 | 
						|||
| 
								 | 
							
								        return true;
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								    return false;
							 | 
						|||
| 
								 | 
							
								}
							 | 
						|||
| 
								 | 
							
								```
							 |