浏览代码

Merge pull request #500 from Unity-Technologies/feature/HDEditorCLI

Feature/hd editor cli
/Yibing-Project-2
GitHub 7 年前
当前提交
dda4fde5
共有 6 个文件被更改,包括 278 次插入31 次删除
  1. 33
      ScriptableRenderPipeline/HDRenderPipeline/Editor/HDEditorUtils.cs
  2. 58
      ScriptableRenderPipeline/HDRenderPipeline/Editor/HDRenderPipelineMenuItems.cs
  3. 77
      ScriptableRenderPipeline/HDRenderPipeline/Editor/HDEditorCLI.cs
  4. 13
      ScriptableRenderPipeline/HDRenderPipeline/Editor/HDEditorCLI.cs.meta
  5. 119
      ScriptableRenderPipeline/HDRenderPipeline/Editor/cli.ps1
  6. 9
      ScriptableRenderPipeline/HDRenderPipeline/Editor/cli.ps1.meta

33
ScriptableRenderPipeline/HDRenderPipeline/Editor/HDEditorUtils.cs


using System.IO;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
delegate void MaterialResetter(Material material);
static Dictionary<string, MaterialResetter> k_MaterialResetters = new Dictionary<string, MaterialResetter>()
{
{ "HDRenderPipeline/LayeredLit", LayeredLitGUI.SetupMaterialKeywordsAndPass },
{ "HDRenderPipeline/LayeredLitTessellation", LayeredLitGUI.SetupMaterialKeywordsAndPass },
{ "HDRenderPipeline/Lit", LitGUI.SetupMaterialKeywordsAndPass },
{ "HDRenderPipeline/LitTessellation", LitGUI.SetupMaterialKeywordsAndPass },
{ "HDRenderPipeline/Unlit", UnlitGUI.SetupMaterialKeywordsAndPass }
};
public static string GetHDRenderPipelinePath()
{
// User can create their own directory for SRP, so we need to find the current path that they use.

var fullPath = Path.GetFullPath(hdrpPath + "../Core");
var relativePath = fullPath.Substring(fullPath.IndexOf("Assets"));
return relativePath.Replace("\\", "/") + "/";
}
public static bool ResetMaterialKeywords(Material material)
{
MaterialResetter resetter;
if (k_MaterialResetters.TryGetValue(material.shader.name, out resetter))
{
RemoveMaterialKeywords(material);
resetter(material);
EditorUtility.SetDirty(material);
return true;
}
return false;
}
public static void RemoveMaterialKeywords(Material material)
{
foreach (var keyword in material.shaderKeywords)
material.DisableKeyword(keyword);
}
}
}

58
ScriptableRenderPipeline/HDRenderPipeline/Editor/HDRenderPipelineMenuItems.cs


}
}
static void RemoveMaterialKeywords(Material material)
{
foreach (var keyword in material.shaderKeywords)
material.DisableKeyword(keyword);
}
// The goal of this script is to help maintenance of data that have already been produced but need to update to the latest shader code change.
// In case the shader code have change and the inspector have been update with new kind of keywords we need to regenerate the set of keywords use by the material.
// This script will remove all keyword of a material and trigger the inspector that will re-setup all the used keywords.

for (int i = 0, length = materials.Length; i < length; i++)
{
var mat = materials[i];
EditorUtility.DisplayProgressBar(
"Setup materials Keywords...",
string.Format("{0} / {1} materials cleaned.", i, length),
i / (float)(length - 1));
HDEditorUtils.ResetMaterialKeywords(materials[i]);
}
}
finally
{
EditorUtility.ClearProgressBar();
}
}
[MenuItem("HDRenderPipeline/Test/Reset all materials keywords in project")]
static void ResetAllMaterialKeywordsInProject()
{
try
{
var matIds = AssetDatabase.FindAssets("t:Material");
for (int i = 0, length = matIds.Length; i < length; i++)
{
var path = AssetDatabase.GUIDToAssetPath(matIds[i]);
var mat = AssetDatabase.LoadAssetAtPath<Material>(path);
EditorUtility.DisplayProgressBar(
"Setup materials Keywords...",

if (mat.shader.name == "HDRenderPipeline/LayeredLit" || mat.shader.name == "HDRenderPipeline/LayeredLitTessellation")
{
// We remove all keyword already present
RemoveMaterialKeywords(mat);
LayeredLitGUI.SetupMaterialKeywordsAndPass(mat);
EditorUtility.SetDirty(mat);
}
else if (mat.shader.name == "HDRenderPipeline/Lit" || mat.shader.name == "HDRenderPipeline/LitTessellation")
{
// We remove all keyword already present
RemoveMaterialKeywords(mat);
LitGUI.SetupMaterialKeywordsAndPass(mat);
EditorUtility.SetDirty(mat);
}
else if (mat.shader.name == "HDRenderPipeline/Unlit")
{
// We remove all keyword already present
RemoveMaterialKeywords(mat);
UnlitGUI.SetupMaterialKeywordsAndPass(mat);
EditorUtility.SetDirty(mat);
}
HDEditorUtils.ResetMaterialKeywords(mat);
}
}
finally

{
mat.shader = litShader;
// We remove all keyword already present
RemoveMaterialKeywords(mat);
HDEditorUtils.RemoveMaterialKeywords(mat);
LitGUI.SetupMaterialKeywordsAndPass(mat);
EditorUtility.SetDirty(mat);
}

// We remove all keyword already present
RemoveMaterialKeywords(mat);
HDEditorUtils.RemoveMaterialKeywords(mat);
LayeredLitGUI.SetupMaterialKeywordsAndPass(mat);
EditorUtility.SetDirty(mat);
}

77
ScriptableRenderPipeline/HDRenderPipeline/Editor/HDEditorCLI.cs


using System;
using UnityEngine;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
public class HDEditorCLI
{
enum CommandLineOperation
{
None,
ResetMaterialKeywords
}
struct CommandLineAction
{
internal CommandLineOperation operation;
}
const string k_SwitchOperation = "-operation";
public static void Run()
{
var args = System.Environment.GetCommandLineArgs();
var action = ParseCommandLine(args);
Execute(action);
}
static void Execute(CommandLineAction action)
{
switch (action.operation)
{
case CommandLineOperation.ResetMaterialKeywords:
{
Console.WriteLine("[HDEditorCLI][ResetMaterialKeywords] Starting material reset");
var matIds = AssetDatabase.FindAssets("t:Material");
for (int i = 0, length = matIds.Length; i < length; i++)
{
var path = AssetDatabase.GUIDToAssetPath(matIds[i]);
var mat = AssetDatabase.LoadAssetAtPath<Material>(path);
if (HDEditorUtils.ResetMaterialKeywords(mat))
Console.WriteLine("[HDEditorCLI][ResetMaterialKeywords] " + path);
}
break;
}
}
}
static CommandLineAction ParseCommandLine(string[] args)
{
CommandLineAction action = new CommandLineAction();
for (int i = 0, length = args.Length; i < length; ++i)
{
switch (args[i])
{
case k_SwitchOperation:
{
if (i + 1 < length)
{
++i;
try
{
action.operation = (CommandLineOperation)Enum.Parse(typeof(CommandLineOperation), args[i]);
}
catch (Exception e) { }
}
break;
}
}
}
return action;
}
}
}

13
ScriptableRenderPipeline/HDRenderPipeline/Editor/HDEditorCLI.cs.meta


fileFormatVersion: 2
guid: 4baa489311168cc4ebdf3cd85ceb544a
timeCreated: 1508231097
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

119
ScriptableRenderPipeline/HDRenderPipeline/Editor/cli.ps1


# Execute the HDEditorCLI from a powsershell scripts
# UNITY_PATH environment variable must be set to the folder containing Unity.exe
Param
(
[parameter(Mandatory=$true)]
[ValidateSet("ResetMaterialKeywords")]
[String]
$Operation
)
function FindUnityProjectRootPath
{
Param
(
[parameter(Mandatory=$true)]
[string]
$Path
)
$folderName = [System.IO.Path]::GetFileNameWithoutExtension("$Path");
$currentPath = $Path;
while ($folderName -ne "Assets" -and -not [string]::IsNullOrEmpty("$currentPath"))
{
$currentPath = Split-Path -Path $currentPath -Parent;
$folderName = [System.IO.Path]::GetFileNameWithoutExtension("$currentPath");
}
return Split-Path -Path $currentPath -Parent;
}
function FindUnityBin
{
return [System.IO.Path]::Combine("$env:UNITY_PATH", "Unity.exe");
}
function GetLogFilePath
{
Param
(
[parameter(Mandatory=$true)]
[string]
$ProjectPath
)
$libPath = [System.IO.Path]::Combine("$ProjectPath", "Library");
if (-not [System.IO.Directory]::Exists("$libPath"))
{
[System.IO.Directory]::CreateDirectory("$libPath");
}
return [System.IO.Path]::Combine("$libPath", "hdcli.log");
}
$projectPath = FindUnityProjectRootPath $PSScriptRoot;
if ([string]::IsNullOrEmpty("$projectPath"))
{
throw "Unity Project Path was not found"
}
$unityPath = FindUnityBin
if (-not [System.IO.File]::Exists("$unityPath"))
{
throw "Unity binary was not found at $unityPath";
}
$logFilePath = GetLogFilePath $projectPath
$cmd = $unityPath;
$params = '-projectPath', "$projectPath", '-quit', '-batchmode', '-logFile', "$logFilePath", '-executeMethod', 'UnityEditor.Experimental.Rendering.HDPipeline.HDEditorCLI.Run', '-operation', "$Operation"
if (Test-Path $logFilePath)
{
Remove-Item $logFilePath
}
Start-Process $cmd $params
while ((Test-Path $logFilePath) -eq $false)
{
Start-Sleep -Milliseconds 100
}
$readJob = Start-Job -Name Reader -Arg $logFilePath -ScriptBlock {
param($file)
Get-Content $file -Tail 1 -Wait
}
Write-Host "Starting Task"
while ($true)
{
$lines = Receive-Job -Name Reader
$theEnd = $false;
foreach ($line in $lines)
{
if ($line.Contains("[HDEditorCLI]"))
{
Write-Host $line;
}
if ($line.Contains("Exiting batchmode"))
{
$theEnd = $true;
break;
}
}
if ($theEnd)
{ break; }
Start-Sleep -Milliseconds 500
}
Stop-Job $readJob
Write-Host "Task Complete"

9
ScriptableRenderPipeline/HDRenderPipeline/Editor/cli.ps1.meta


fileFormatVersion: 2
guid: a07b31caef0cfb741b72f94fd0139c89
timeCreated: 1508233704
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存