浏览代码

Cleaned up MaterialGraphPreviewGenerator

/main
Mads Ronnow 7 年前
当前提交
3ecf6537
共有 3 个文件被更改,包括 50 次插入111 次删除
  1. 144
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphPreviewGenerator.cs
  2. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/NodePreviewPresenter.cs
  3. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Shaders/Checkerboard.shader

144
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphPreviewGenerator.cs


private Light Light0 { get; set; }
private Light Light1 { get; set; }
//private SavedRenderTargetState m_SavedState;
public Light[] Lights
{
get
{
return new[] {Light0, Light1};
}
}
private static readonly Mesh[] s_Meshes = {null, null, null, null, null};
private static Mesh s_PlaneMesh;

Light1.color = new Color(.4f, .4f, .45f, 0f) * .7f;
m_CheckerboardMaterial = new Material(Shader.Find("Hidden/Checkerboard"));
m_CheckerboardMaterial.SetFloat("_X", 8);
m_CheckerboardMaterial.SetFloat("_Y", 8);
if (s_Meshes[0] == null)
{

}
}
public float GetScaleFactor(float width, float height)
{
float scaleFacX = Mathf.Max(Mathf.Min(width * 2, 1024), width) / width;
float scaleFacY = Mathf.Max(Mathf.Min(height * 2, 1024), height) / height;
float result = Mathf.Min(scaleFacX, scaleFacY) * EditorGUIUtility.pixelsPerPoint;
//if (m_PixelPerfect) //m_PixelPerfect = false; in PreviewRenderUtility default constructor.
// result = Mathf.Max(Mathf.Round(result), 1f);
return result;
}
//utility.BeginPreview(size, GUIStyle.none);
float scaleFac = GetScaleFactor(size.width, size.height);
int rtWidth = (int)(size.width * scaleFac);
int rtHeight = (int)(size.height * scaleFac);
int rtWidth = (int)(size.width);
int rtHeight = (int)(size.height);
if (!m_RenderTexture || m_RenderTexture.width != rtWidth || m_RenderTexture.height != rtHeight)
{

m_RenderTexture.hideFlags = HideFlags.HideAndDontSave;
m_Camera.targetTexture = m_RenderTexture;
foreach (var light in Lights) //redundant?
light.enabled = true;
Graphics.Blit(null, m_RenderTexture, m_CheckerboardMaterial);
//m_SavedState = new SavedRenderTargetState();
//EditorGUIUtility.SetRenderTextureNoViewport(m_RenderTexture);
GL.LoadOrtho();
//GL.LoadPixelMatrix(0, m_RenderTexture.width, m_RenderTexture.height, 0);
//ShaderUtil.rawViewportRect = new Rect(0, 0, m_RenderTexture.width, m_RenderTexture.height);
//ShaderUtil.rawScissorRect = new Rect(0, 0, m_RenderTexture.width, m_RenderTexture.height);
//GL.Clear(true, true, m_Camera.backgroundColor);
foreach (var light in Lights)
light.enabled = true;
//Debug.Log(string.Format("RT: {0}, Material: {1}", m_RenderTexture, m_CheckerboardMaterial));
m_CheckerboardMaterial.SetFloat("_X", 32);
m_CheckerboardMaterial.SetFloat("_Y", 32);
Graphics.Blit(Texture2D.whiteTexture, m_RenderTexture, m_CheckerboardMaterial);
if (mode == PreviewMode.Preview3D)
{

}
EditorUtility.SetCameraAnimateMaterialsTime(m_Camera, time);
Lights[0].intensity = 1.0f;
Lights[0].transform.rotation = Quaternion.Euler(50f, 50f, 0);
Lights[1].intensity = 1.0f;
Light0.enabled = true;
Light0.intensity = 1.0f;
Light0.transform.rotation = Quaternion.Euler(50f, 50f, 0);
Light1.enabled = true;
Light1.intensity = 1.0f;
m_Camera.clearFlags = CameraClearFlags.Depth;
DrawMesh(

mat,
0);
Render(true, false);
m_Camera.Render();
return EndPreview();
Light0.enabled = false;
Light1.enabled = false;
return m_RenderTexture;
//EditorSceneManager.ClosePreviewScene(m_Scene); //Removing this line makes it work in the shader graph viewer (checkerboard disappears after windows loses and regains focus)
/*foreach (var go in m_GameObjects)
Object.DestroyImmediate(go);
m_GameObjects.Clear();*/
}
public void Reset() {
if (m_RenderTexture)
if (m_RenderTexture == null)
/*if (m_InvisibleMaterial != null)
if (Light0 == null)
UnityEngine.Object.DestroyImmediate(m_InvisibleMaterial);
m_InvisibleMaterial = null;
}*/
UnityEngine.Object.DestroyImmediate(Light0.gameObject);
Light0 = null;
}
Dispose();
}
if (Light1 == null)
{
UnityEngine.Object.DestroyImmediate(Light1.gameObject);
Light1 = null;
}
public Texture EndPreview()
{
//Unsupported.RestoreOverrideRenderSettings();
if (m_Camera == null)
{
UnityEngine.Object.DestroyImmediate(m_Camera.gameObject);
m_Camera = null;
}
//m_SavedState.Restore();
FinishFrame();
return m_RenderTexture;
}
if (m_CheckerboardMaterial == null)
{
UnityEngine.Object.DestroyImmediate(m_CheckerboardMaterial);
m_CheckerboardMaterial = null;
}
private void FinishFrame()
{
foreach (var light in Lights)
light.enabled = false;
EditorSceneManager.ClosePreviewScene(m_Scene);
public void DrawMesh(Mesh mesh, Vector3 pos, Quaternion rot, Material mat, int subMeshIndex)
private void DrawMesh(Mesh mesh, Vector3 pos, Quaternion rot, Material mat, int subMeshIndex)
public void DrawMesh(Mesh mesh, Vector3 pos, Quaternion rot, Material mat, int subMeshIndex, MaterialPropertyBlock customProperties, Transform probeAnchor, bool useLightProbe)
private void DrawMesh(Mesh mesh, Vector3 pos, Quaternion rot, Material mat, int subMeshIndex, MaterialPropertyBlock customProperties, Transform probeAnchor, bool useLightProbe)
}
public void Render(bool allowScriptableRenderPipeline = false, bool updatefov = true)
{
foreach (var light in Lights)
light.enabled = true;
//var oldAllowPipes = Unsupported.useScriptableRenderPipeline;
//Unsupported.useScriptableRenderPipeline = allowScriptableRenderPipeline;
float saveFieldOfView = m_Camera.fieldOfView;
if (updatefov)
{
// Calculate a view multiplier to avoid clipping when the preview width is smaller than the height.
float viewMultiplier = (m_RenderTexture.width <= 0 ? 1.0f : Mathf.Max(1.0f, (float)m_RenderTexture.height / m_RenderTexture.width));
// Multiply the viewing area by the viewMultiplier - it requires some conversions since the camera view is expressed as an angle.
m_Camera.fieldOfView = Mathf.Atan(viewMultiplier * Mathf.Tan(m_Camera.fieldOfView * 0.5f * Mathf.Deg2Rad)) * Mathf.Rad2Deg * 2.0f;
}
m_Camera.Render();
m_Camera.fieldOfView = saveFieldOfView;
//Unsupported.useScriptableRenderPipeline = oldAllowPipes;
}
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/NodePreviewPresenter.cs


bool status = false;
if (m_modificationScope >= ModificationScope.Graph)
{
previewGenerator.Reset();
// TODO: Handle shader regeneration error
status = UpdatePreviewShader();
}

return m_texture;
}
void OnDisable()
{
if (m_PreviewGenerator != null)
m_PreviewGenerator.Dispose();
}
protected virtual string GetPreviewShaderString()
{

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Shaders/Checkerboard.shader


uniform float _X;
uniform float _Y;
static const float4 col1 = float4(0.25, 0.25, 0.25, 1.0);
static const float4 col2 = float4(0.75, 0.75, 0.75, 1.0);
//float2 size = float2(16, 16);//textureSize2D(Texture0,0);
float4 col1 = float4(0.0, 0.0, 0.0, 1.0);
float4 col2 = float4(1.0, 1.0, 1.0, 1.0);
fixed4 col = (isEven) ? col1 : col2;
return col;//fixed4(1.0, 0.0, 0.0, 1.0);
return isEven ? col1 : col2;
}
ENDCG
}
正在加载...
取消
保存