浏览代码

HDRenderPipeline: Add aspet ratio / angle control for pyramid + add gizmo

/Add-support-for-light-specular-color-tint
Sebastien Lagarde 7 年前
当前提交
bb06e229
共有 8 个文件被更改,包括 320 次插入164 次删除
  1. 3
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/Editor/HDLightEditor.Styles.cs
  2. 6
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/Editor/HDLightEditor.cs
  3. 24
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/HDAdditionalLightData.cs
  4. 30
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/TilePass/TilePass.cs
  5. 261
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/HDLightEditorUtilities.cs
  6. 13
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/HDLightEditorUtilities.cs.meta
  7. 13
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/EditorLightUtilities.cs.meta
  8. 134
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/EditorLightUtilities.cs

3
ScriptableRenderPipeline/HDRenderPipeline/Lighting/Editor/HDLightEditor.Styles.cs


public readonly GUIContent shapeLengthLine = new GUIContent("Length", "Length of the line light");
public readonly GUIContent shapeLengthRect = new GUIContent("Size X", "SizeX of the rectangle light");
public readonly GUIContent shapeWidthRect = new GUIContent("Size Y", "SizeY of the rectangle light");
public readonly GUIContent shapeLengthPyramid = new GUIContent("Size X", "");
public readonly GUIContent shapeWidthPyramid = new GUIContent("Size Y", "");
public readonly GUIContent aspectRatioPyramid = new GUIContent("Aspect ratio", "");
public readonly GUIContent shapeLengthBox = new GUIContent("Size X", "");
public readonly GUIContent shapeWidthBox = new GUIContent("Size Y", "");
public readonly GUIContent applyRangeAttenuation = new GUIContent("Apply Range Attenuation", "Allows disabling range attenuation. This is useful indoor (like a room) to avoid having to setup a large range for a light to get correct inverse square attenuation that may leak out of the indoor");

6
ScriptableRenderPipeline/HDRenderPipeline/Lighting/Editor/HDLightEditor.cs


public SerializedProperty spotLightShape;
public SerializedProperty shapeLength;
public SerializedProperty shapeWidth;
public SerializedProperty aspectRatio;
public SerializedProperty shapeRadius;
public SerializedProperty maxSmoothness;
public SerializedProperty applyRangeAttenuation;

spotLightShape = o.Find(x => x.spotLightShape),
shapeLength = o.Find(x => x.shapeLength),
shapeWidth = o.Find(x => x.shapeWidth),
aspectRatio = o.Find(x => x.aspectRatio),
shapeRadius = o.Find(x => x.shapeRadius),
maxSmoothness = o.Find(x => x.maxSmoothness),
applyRangeAttenuation = o.Find(x => x.applyRangeAttenuation),

// TODO : replace with angle and ratio
else if (spotLightShape == SpotLightShape.Pyramid)
{
EditorGUILayout.Slider(m_AdditionalLightData.shapeLength, 0.01f, 10f, s_Styles.shapeLengthPyramid);
EditorGUILayout.Slider(m_AdditionalLightData.shapeWidth, 0.01f, 10f, s_Styles.shapeWidthPyramid);
EditorGUILayout.Slider(m_BaseData.spotAngle, 0f, 179.9f, s_Styles.spotAngle);
EditorGUILayout.Slider(m_AdditionalLightData.aspectRatio, 0.05f, 20.0f, s_Styles.aspectRatioPyramid);
}
else if (spotLightShape == SpotLightShape.Box)
{

24
ScriptableRenderPipeline/HDRenderPipeline/Lighting/HDAdditionalLightData.cs


#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Experimental.Rendering.HDPipeline;
#endif
using UnityEngine.Serialization;

// Only for Spotlight, should be hide for other light
public SpotLightShape spotLightShape = SpotLightShape.Cone;
// Only for Rectangle/Line/projector lights
// Only for Rectangle/Line/box projector lights
// Only for Rectangle/projector lights
// Only for Rectangle/box projector lights
// Only for pyramid projector
public float aspectRatio = 1.0f;
// Only for Sphere/Disc
public float shapeRadius = 0.0f;

switch (light.type)
{
case LightType.Directional:
EditorLightUtilities.DrawDirectionalLightGizmo(light);
HDLightEditorUtilities.DrawDirectionalLightGizmo(light);
EditorLightUtilities.DrawPointlightGizmo(light, selected);
HDLightEditorUtilities.DrawPointlightGizmo(light, selected);
EditorLightUtilities.DrawSpotlightGizmo(light, selected);
HDLightEditorUtilities.DrawSpotlightGizmo(light, selected);
EditorLightUtilities.DrawFrustumlightGizmo(light);
else if (spotLightShape == SpotLightShape.Box) // TODO
EditorLightUtilities.DrawFrustumlightGizmo(light);
HDLightEditorUtilities.DrawFrustumlightGizmo(light);
else if (spotLightShape == SpotLightShape.Box)
HDLightEditorUtilities.DrawFrustumlightGizmo(light);
break;
}
}

{
case LightTypeExtent.Rectangle:
EditorLightUtilities.DrawArealightGizmo(light);
HDLightEditorUtilities.DrawArealightGizmo(light);
EditorLightUtilities.DrawArealightGizmo(light);
HDLightEditorUtilities.DrawArealightGizmo(light);
break;
}
}

30
ScriptableRenderPipeline/HDRenderPipeline/Lighting/TilePass/TilePass.cs


lightData.size = new Vector2(additionalLightData.shapeLength, additionalLightData.shapeWidth);
if (lightData.lightType == GPULightType.ProjectorBox || lightData.lightType == GPULightType.ProjectorPyramid)
if (lightData.lightType == GPULightType.ProjectorBox)
lightData.right *= 2 / additionalLightData.shapeLength;
lightData.up *= 2 / additionalLightData.shapeWidth;
lightData.right *= 2.0f / additionalLightData.shapeLength;
lightData.up *= 2.0f / additionalLightData.shapeWidth;
}
else if (lightData.lightType == GPULightType.ProjectorPyramid)
{
// Get width and height for the current frustum
var spotAngle = light.spotAngle;
float frustumHeight;
float frustumWidth;
if (additionalLightData.aspectRatio >= 1.0f)
{
frustumHeight = 2.0f * Mathf.Tan(spotAngle * 0.5f * Mathf.Deg2Rad);
frustumWidth = frustumHeight * additionalLightData.aspectRatio;
}
else
{
frustumWidth = 2.0f * Mathf.Tan(spotAngle * 0.5f * Mathf.Deg2Rad);
frustumHeight = frustumWidth / additionalLightData.aspectRatio;
}
lightData.size = new Vector2(frustumWidth, frustumHeight);
// Rescale for cookies and windowing.
lightData.right *= 2.0f / frustumWidth;
lightData.up *= 2.0f / frustumHeight;
}
if (lightData.lightType == GPULightType.Spot)

261
ScriptableRenderPipeline/HDRenderPipeline/Lighting/HDLightEditorUtilities.cs


#if UNITY_EDITOR
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.Experimental.Rendering.HDPipeline;
#endif
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
#if UNITY_EDITOR
public static class HDLightEditorUtilities
{
public static void DrawSpotlightGizmo(Light spotlight, bool selected)
{
var flatRadiusAtRange = spotlight.range * Mathf.Tan(spotlight.spotAngle * Mathf.Deg2Rad * 0.5f);
var vectorLineUp = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.up * flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineDown = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.up * -flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineRight = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.right * flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineLeft = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.right * -flatRadiusAtRange - spotlight.gameObject.transform.position);
var rangeDiscDistance = Mathf.Cos(Mathf.Deg2Rad * spotlight.spotAngle / 2) * spotlight.range;
var rangeDiscRadius = spotlight.range * Mathf.Sin(spotlight.spotAngle * Mathf.Deg2Rad * 0.5f);
var nearDiscDistance = Mathf.Cos(Mathf.Deg2Rad * spotlight.spotAngle / 2) * spotlight.shadowNearPlane;
var nearDiscRadius = spotlight.shadowNearPlane * Mathf.Sin(spotlight.spotAngle * Mathf.Deg2Rad * 0.5f);
//Draw Range disc
Handles.Disc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * rangeDiscDistance, spotlight.gameObject.transform.forward, rangeDiscRadius, false, 1);
//Draw Lines
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineUp * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineDown * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineRight * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineLeft * spotlight.range);
if (selected)
{
//Draw Range Arcs
Handles.DrawWireArc(spotlight.gameObject.transform.position, spotlight.gameObject.transform.right, vectorLineUp, spotlight.spotAngle, spotlight.range);
Handles.DrawWireArc(spotlight.gameObject.transform.position, spotlight.gameObject.transform.up, vectorLineLeft, spotlight.spotAngle, spotlight.range);
//Draw Near Plane Disc
if (spotlight.shadows != LightShadows.None) Handles.Disc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * nearDiscDistance, spotlight.gameObject.transform.forward, nearDiscRadius, false, 1);
//Inner Cone
var additionalLightData = spotlight.GetComponent<HDAdditionalLightData>();
DrawInnerCone(spotlight,additionalLightData);
}
}
public static void DrawInnerCone(Light spotlight, HDAdditionalLightData additionalLightData)
{
if (additionalLightData == null) return;
var flatRadiusAtRange = spotlight.range * Mathf.Tan(spotlight.spotAngle * additionalLightData.m_InnerSpotPercent * 0.01f * Mathf.Deg2Rad * 0.5f);
var vectorLineUp = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.up * flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineDown = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.up * -flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineRight = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.right * flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineLeft = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.right * -flatRadiusAtRange - spotlight.gameObject.transform.position);
//Draw Lines
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineUp * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineDown * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineRight * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineLeft * spotlight.range);
var innerAngle = spotlight.spotAngle * additionalLightData.GetInnerSpotPercent01();
if (innerAngle > 0)
{
var innerDiscDistance = Mathf.Cos(Mathf.Deg2Rad * innerAngle * 0.5f) * spotlight.range;
var innerDiscRadius = spotlight.range * Mathf.Sin(innerAngle * Mathf.Deg2Rad * 0.5f);
//Draw Range disc
Handles.Disc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * innerDiscDistance, spotlight.gameObject.transform.forward, innerDiscRadius, false, 1);
}
}
public static void DrawArealightGizmo(Light arealight)
{
var RectangleSize = new Vector3(arealight.areaSize.x, arealight.areaSize.y, 0);
Gizmos.matrix = arealight.transform.localToWorldMatrix;
Gizmos.DrawWireCube(Vector3.zero, RectangleSize);
Gizmos.matrix = Matrix4x4.identity;
Gizmos.DrawWireSphere(arealight.transform.position, arealight.range);
}
public static void DrawPointlightGizmo(Light pointlight, bool selected)
{
if (pointlight.shadows != LightShadows.None && selected) Gizmos.DrawWireSphere(pointlight.transform.position, pointlight.shadowNearPlane);
Gizmos.DrawWireSphere(pointlight.transform.position, pointlight.range);
}
public static void DrawSpherelightGizmo(Light spherelight)
{
var additionalLightData = spherelight.GetComponent<HDAdditionalLightData>();
if (additionalLightData == null) return;
Gizmos.DrawSphere(spherelight.transform.position, additionalLightData.shapeLength);
if (spherelight.shadows != LightShadows.None) Gizmos.DrawWireSphere(spherelight.transform.position, spherelight.shadowNearPlane);
Gizmos.DrawWireSphere(spherelight.transform.position, spherelight.range);
}
// Same as Gizmo.DrawFrustum except that when aspect is below one, fov represent fovX instead of fovY
// Use to match our light frustum pyramid behavior
public static void DrawLightPyramidFrustum(Vector3 center, float fov, float maxRange, float minRange, float aspect)
{
fov = Mathf.Deg2Rad * fov * 0.5f;
float tanfov = Mathf.Tan(fov);
Vector3 farEnd = new Vector3(0, 0, maxRange);
Vector3 endSizeX;
Vector3 endSizeY;
if (aspect >= 1.0f)
{
endSizeX = new Vector3(maxRange * tanfov * aspect, 0, 0);
endSizeY = new Vector3(0, maxRange * tanfov, 0);
}
else
{
endSizeX = new Vector3(maxRange * tanfov, 0, 0);
endSizeY = new Vector3(0, maxRange * tanfov / aspect, 0);
}
Vector3 s1, s2, s3, s4;
Vector3 e1 = farEnd + endSizeX + endSizeY;
Vector3 e2 = farEnd - endSizeX + endSizeY;
Vector3 e3 = farEnd - endSizeX - endSizeY;
Vector3 e4 = farEnd + endSizeX - endSizeY;
if (minRange <= 0.0f)
{
s1 = s2 = s3 = s4 = center;
}
else
{
Vector3 startSizeX;
Vector3 startSizeY;
if (aspect >= 1.0f)
{
startSizeX = new Vector3(minRange * tanfov * aspect, 0, 0);
startSizeY = new Vector3(0, minRange * tanfov, 0);
}
else
{
startSizeY = new Vector3(minRange * tanfov / aspect, 0, 0);
startSizeX = new Vector3(0, minRange * tanfov, 0);
}
Vector3 startPoint = center;
s1 = startPoint + startSizeX + startSizeY;
s2 = startPoint - startSizeX + startSizeY;
s3 = startPoint - startSizeX - startSizeY;
s4 = startPoint + startSizeX - startSizeY;
Gizmos.DrawLine(s1, s2);
Gizmos.DrawLine(s2, s3);
Gizmos.DrawLine(s3, s4);
Gizmos.DrawLine(s4, s1);
}
Gizmos.DrawLine(e1, e2);
Gizmos.DrawLine(e2, e3);
Gizmos.DrawLine(e3, e4);
Gizmos.DrawLine(e4, e1);
Gizmos.DrawLine(s1, e1);
Gizmos.DrawLine(s2, e2);
Gizmos.DrawLine(s3, e3);
Gizmos.DrawLine(s4, e4);
}
public static void DrawLightOrthoFrustum(Vector3 center, float width, float height, float maxRange, float minRange)
{
Vector3 farEnd = new Vector3(0, 0, maxRange);
Vector3 endSizeX = new Vector3(width, 0, 0);
Vector3 endSizeY = new Vector3(0, height, 0);
Vector3 s1, s2, s3, s4;
Vector3 e1 = farEnd + endSizeX + endSizeY;
Vector3 e2 = farEnd - endSizeX + endSizeY;
Vector3 e3 = farEnd - endSizeX - endSizeY;
Vector3 e4 = farEnd + endSizeX - endSizeY;
if (minRange <= 0.0f)
{
s1 = s2 = s3 = s4 = center;
}
else
{
Vector3 startSizeX = new Vector3(width, 0, 0);
Vector3 startSizeY = new Vector3(0, height, 0);
Vector3 startPoint = center;
s1 = startPoint + startSizeX + startSizeY;
s2 = startPoint - startSizeX + startSizeY;
s3 = startPoint - startSizeX - startSizeY;
s4 = startPoint + startSizeX - startSizeY;
Gizmos.DrawLine(s1, s2);
Gizmos.DrawLine(s2, s3);
Gizmos.DrawLine(s3, s4);
Gizmos.DrawLine(s4, s1);
}
Gizmos.DrawLine(e1, e2);
Gizmos.DrawLine(e2, e3);
Gizmos.DrawLine(e3, e4);
Gizmos.DrawLine(e4, e1);
Gizmos.DrawLine(s1, e1);
Gizmos.DrawLine(s2, e2);
Gizmos.DrawLine(s3, e3);
Gizmos.DrawLine(s4, e4);
}
public static void DrawFrustumlightGizmo(Light frustumlight)
{
DisableLightGizmos();
var additionalLightData = frustumlight.GetComponent<HDAdditionalLightData>();
if (additionalLightData == null) return;
Matrix4x4 matrix = new Matrix4x4(frustumlight.transform.right, frustumlight.transform.up, frustumlight.transform.forward, frustumlight.transform.position);
Gizmos.matrix = matrix;
if (additionalLightData.spotLightShape == SpotLightShape.Pyramid)
{
DrawLightPyramidFrustum(Vector3.zero, frustumlight.spotAngle, frustumlight.range, 0.0f, additionalLightData.aspectRatio);
}
else // Ortho frustum
{
//DrawLightOrthoFrustum(Vector3.zero, additionalLightData.shapeLength, additionalLightData.shapeWidth, frustumlight.range, 0.0f);
Vector3 frustumCenter = new Vector3(0.0f, 0.0f, 0.5f * frustumlight.range);
Vector3 frustumsize = new Vector3(additionalLightData.shapeLength, additionalLightData.shapeWidth, frustumlight.range);
Gizmos.DrawWireCube(frustumCenter, frustumsize);
}
Gizmos.matrix = Matrix4x4.identity;
}
public static void DrawDirectionalLightGizmo(Light directionalLight)
{
var gizmoSize = 0.2f;
Handles.Disc(directionalLight.transform.rotation, directionalLight.transform.position, directionalLight.gameObject.transform.forward, gizmoSize, false, 1);
Gizmos.DrawLine(directionalLight.transform.position, directionalLight.transform.position + directionalLight.transform.forward);
Gizmos.DrawLine(directionalLight.transform.position + directionalLight.transform.up * gizmoSize, directionalLight.transform.position + directionalLight.transform.up * gizmoSize + directionalLight.transform.forward);
Gizmos.DrawLine(directionalLight.transform.position + directionalLight.transform.up * -gizmoSize, directionalLight.transform.position + directionalLight.transform.up * -gizmoSize + directionalLight.transform.forward);
Gizmos.DrawLine(directionalLight.transform.position + directionalLight.transform.right * gizmoSize, directionalLight.transform.position + directionalLight.transform.right * gizmoSize + directionalLight.transform.forward);
Gizmos.DrawLine(directionalLight.transform.position + directionalLight.transform.right * -gizmoSize, directionalLight.transform.position + directionalLight.transform.right * -gizmoSize + directionalLight.transform.forward);
}
public static void DrawCross(Transform m_transform)
{
var gizmoSize = 0.25f;
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.forward * gizmoSize / m_transform.localScale.z));
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.forward * -gizmoSize / m_transform.localScale.z));
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.up * gizmoSize / m_transform.localScale.y));
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.up * -gizmoSize / m_transform.localScale.y));
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.right * gizmoSize / m_transform.localScale.x));
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.right * -gizmoSize / m_transform.localScale.x));
}
}
#endif
}

13
ScriptableRenderPipeline/HDRenderPipeline/Lighting/HDLightEditorUtilities.cs.meta


fileFormatVersion: 2
guid: 8f89186f308429942b61ec29b405e2dd
timeCreated: 1508806968
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

13
ScriptableRenderPipeline/HDRenderPipeline/Lighting/EditorLightUtilities.cs.meta


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

134
ScriptableRenderPipeline/HDRenderPipeline/Lighting/EditorLightUtilities.cs


#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
#if UNITY_EDITOR
public static class EditorLightUtilities
{
public static void DrawSpotlightGizmo(Light spotlight, bool selected)
{
var flatRadiusAtRange = spotlight.range * Mathf.Tan(spotlight.spotAngle * Mathf.Deg2Rad * 0.5f);
var vectorLineUp = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.up * flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineDown = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.up * -flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineRight = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.right * flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineLeft = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.right * -flatRadiusAtRange - spotlight.gameObject.transform.position);
var rangeDiscDistance = Mathf.Cos(Mathf.Deg2Rad * spotlight.spotAngle / 2) * spotlight.range;
var rangeDiscRadius = spotlight.range * Mathf.Sin(spotlight.spotAngle * Mathf.Deg2Rad * 0.5f);
var nearDiscDistance = Mathf.Cos(Mathf.Deg2Rad * spotlight.spotAngle / 2) * spotlight.shadowNearPlane;
var nearDiscRadius = spotlight.shadowNearPlane * Mathf.Sin(spotlight.spotAngle * Mathf.Deg2Rad * 0.5f);
//Draw Range disc
Handles.Disc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * rangeDiscDistance, spotlight.gameObject.transform.forward, rangeDiscRadius, false, 1);
//Draw Lines
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineUp * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineDown * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineRight * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineLeft * spotlight.range);
if (selected)
{
//Draw Range Arcs
Handles.DrawWireArc(spotlight.gameObject.transform.position, spotlight.gameObject.transform.right, vectorLineUp, spotlight.spotAngle, spotlight.range);
Handles.DrawWireArc(spotlight.gameObject.transform.position, spotlight.gameObject.transform.up, vectorLineLeft, spotlight.spotAngle, spotlight.range);
//Draw Near Plane Disc
if (spotlight.shadows != LightShadows.None) Handles.Disc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * nearDiscDistance, spotlight.gameObject.transform.forward, nearDiscRadius, false, 1);
//Inner Cone
var additionalLightData = spotlight.GetComponent<HDAdditionalLightData>();
DrawInnerCone(spotlight,additionalLightData);
}
}
public static void DrawInnerCone(Light spotlight, HDAdditionalLightData additionalLightData)
{
if (additionalLightData == null) return;
var flatRadiusAtRange = spotlight.range * Mathf.Tan(spotlight.spotAngle * additionalLightData.m_InnerSpotPercent * 0.01f * Mathf.Deg2Rad * 0.5f);
var vectorLineUp = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.up * flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineDown = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.up * -flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineRight = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.right * flatRadiusAtRange - spotlight.gameObject.transform.position);
var vectorLineLeft = Vector3.Normalize(spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * spotlight.range + spotlight.gameObject.transform.right * -flatRadiusAtRange - spotlight.gameObject.transform.position);
//Draw Lines
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineUp * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineDown * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineRight * spotlight.range);
Gizmos.DrawLine(spotlight.gameObject.transform.position, spotlight.gameObject.transform.position + vectorLineLeft * spotlight.range);
var innerAngle = spotlight.spotAngle * additionalLightData.GetInnerSpotPercent01();
if (innerAngle > 0)
{
var innerDiscDistance = Mathf.Cos(Mathf.Deg2Rad * innerAngle * 0.5f) * spotlight.range;
var innerDiscRadius = spotlight.range * Mathf.Sin(innerAngle * Mathf.Deg2Rad * 0.5f);
//Draw Range disc
Handles.Disc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * innerDiscDistance, spotlight.gameObject.transform.forward, innerDiscRadius, false, 1);
}
}
public static void DrawArealightGizmo(Light arealight)
{
var RectangleSize = new Vector3(arealight.areaSize.x, arealight.areaSize.y, 0);
Gizmos.matrix = arealight.transform.localToWorldMatrix;
Gizmos.DrawWireCube(Vector3.zero, RectangleSize);
Gizmos.matrix = Matrix4x4.identity;
Gizmos.DrawWireSphere(arealight.transform.position, arealight.range);
}
public static void DrawPointlightGizmo(Light pointlight, bool selected)
{
if (pointlight.shadows != LightShadows.None && selected) Gizmos.DrawWireSphere(pointlight.transform.position, pointlight.shadowNearPlane);
Gizmos.DrawWireSphere(pointlight.transform.position, pointlight.range);
}
public static void DrawSpherelightGizmo(Light spherelight)
{
var additionalLightData = spherelight.GetComponent<HDAdditionalLightData>();
if (additionalLightData == null) return;
Gizmos.DrawSphere(spherelight.transform.position, additionalLightData.shapeLength);
if (spherelight.shadows != LightShadows.None) Gizmos.DrawWireSphere(spherelight.transform.position, spherelight.shadowNearPlane);
Gizmos.DrawWireSphere(spherelight.transform.position, spherelight.range);
}
public static void DrawFrustumlightGizmo(Light frustumlight)
{
var additionalLightData = frustumlight.GetComponent<HDAdditionalLightData>();
if (additionalLightData == null) return;
var frustumSize = new Vector3(additionalLightData.shapeLength / frustumlight.gameObject.transform.localScale.x, additionalLightData.shapeWidth / frustumlight.gameObject.transform.localScale.y, frustumlight.range - frustumlight.shadowNearPlane / frustumlight.gameObject.transform.localScale.z);
Gizmos.matrix = frustumlight.transform.localToWorldMatrix;
Gizmos.DrawWireCube(Vector3.forward * (frustumSize.z * 0.5f + frustumlight.shadowNearPlane), frustumSize);
Gizmos.matrix = Matrix4x4.identity;
}
public static void DrawDirectionalLightGizmo(Light directionalLight)
{
var gizmoSize = 0.2f;
Handles.Disc(directionalLight.transform.rotation, directionalLight.transform.position, directionalLight.gameObject.transform.forward, gizmoSize, false, 1);
Gizmos.DrawLine(directionalLight.transform.position, directionalLight.transform.position + directionalLight.transform.forward);
Gizmos.DrawLine(directionalLight.transform.position + directionalLight.transform.up * gizmoSize, directionalLight.transform.position + directionalLight.transform.up * gizmoSize + directionalLight.transform.forward);
Gizmos.DrawLine(directionalLight.transform.position + directionalLight.transform.up * -gizmoSize, directionalLight.transform.position + directionalLight.transform.up * -gizmoSize + directionalLight.transform.forward);
Gizmos.DrawLine(directionalLight.transform.position + directionalLight.transform.right * gizmoSize, directionalLight.transform.position + directionalLight.transform.right * gizmoSize + directionalLight.transform.forward);
Gizmos.DrawLine(directionalLight.transform.position + directionalLight.transform.right * -gizmoSize, directionalLight.transform.position + directionalLight.transform.right * -gizmoSize + directionalLight.transform.forward);
}
public static void DrawCross(Transform m_transform)
{
var gizmoSize = 0.25f;
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.forward * gizmoSize / m_transform.localScale.z));
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.forward * -gizmoSize / m_transform.localScale.z));
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.up * gizmoSize / m_transform.localScale.y));
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.up * -gizmoSize / m_transform.localScale.y));
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.right * gizmoSize / m_transform.localScale.x));
Gizmos.DrawLine(m_transform.position, m_transform.position + m_transform.TransformVector(m_transform.root.right * -gizmoSize / m_transform.localScale.x));
}
}
#endif
}
正在加载...
取消
保存