BlueRoseNote/07-Other/Unity/Unity通用渲染管线(URP)系列(三)——方向光(Direct Illumination).md
2023-06-29 11:55:02 +08:00

1.6 KiB
Raw Blame History

BRDF

案例中使用了迪士尼模型。

混合模式

SrcBlendOneDetBlendOneMinusSrc

Shader GUI

Shader{}内添加CustomEditor“ CustomShaderGUI”。之后添加脚本:

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

之后实现

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函数了。添加按钮逻辑如下:

bool PresetButton (string name) {
    if (GUILayout.Button(name)) {
        editor.RegisterPropertyChangeUndo(name);
        return true;
    }
    return false;
}