浏览代码

Merge pull request #1855 from Unity-Technologies/Move-light-Gizmo-Drawing-to-core

Move light Gizmo Drawing to core
/main
GitHub 6 年前
当前提交
bc502caa
共有 9 个文件被更改,包括 356 次插入360 次删除
  1. 66
      com.unity.render-pipelines.high-definition/HDRP/Editor/Lighting/HDAdditionalLightDataEditor.cs
  2. 76
      com.unity.render-pipelines.high-definition/HDRP/Lighting/Light/HDAdditionalLightData.cs
  3. 8
      com.unity.render-pipelines.core/CoreRP/Editor/Lighting.meta
  4. 34
      com.unity.render-pipelines.high-definition/HDRP/Editor/Lighting/HDLightEditorUtilities.cs
  5. 11
      com.unity.render-pipelines.high-definition/HDRP/Editor/Lighting/HDLightEditorUtilities.cs.meta
  6. 236
      com.unity.render-pipelines.core/CoreRP/Editor/Lighting/CoreLightEditorUtilities.cs
  7. 285
      com.unity.render-pipelines.high-definition/HDRP/Lighting/Light/HDLightEditorUtilities.cs
  8. 0
      /com.unity.render-pipelines.core/CoreRP/Editor/Lighting/CoreLightEditorUtilities.cs.meta

66
com.unity.render-pipelines.high-definition/HDRP/Editor/Lighting/HDAdditionalLightDataEditor.cs


{
}
}
public class HDAdditionalLightDataGizmoDrawer
{
[DrawGizmo(GizmoType.Selected | GizmoType.Active)]
static void DrawGizmoForHDAdditionalLightData(HDAdditionalLightData src, GizmoType gizmoType)
{
bool selected = (gizmoType & GizmoType.Selected) != 0;
var light = src.gameObject.GetComponent<Light>();
var gizmoColor = light.color;
gizmoColor.a = selected ? 1.0f : 0.3f; // Fade for the gizmo
Gizmos.color = Handles.color = gizmoColor;
if (src.lightTypeExtent == LightTypeExtent.Punctual)
{
switch (light.type)
{
case LightType.Directional:
CoreLightEditorUtilities.DrawDirectionalLightGizmo(light);
break;
case LightType.Point:
CoreLightEditorUtilities.DrawPointlightGizmo(light, selected);
break;
case LightType.Spot:
if (src.spotLightShape == SpotLightShape.Cone)
CoreLightEditorUtilities.DrawSpotlightGizmo(light, src.GetInnerSpotPercent01(), selected);
else if (src.spotLightShape == SpotLightShape.Pyramid)
HDLightEditorUtilities.DrawFrustumlightGizmo(light);
else if (src.spotLightShape == SpotLightShape.Box)
HDLightEditorUtilities.DrawFrustumlightGizmo(light);
break;
}
}
else
{
switch (src.lightTypeExtent)
{
case LightTypeExtent.Rectangle:
CoreLightEditorUtilities.DrawArealightGizmo(light);
break;
case LightTypeExtent.Line:
CoreLightEditorUtilities.DrawArealightGizmo(light);
break;
}
}
if (selected)
{
// Trace a ray down to better locate the light location
Ray ray = new Ray(src.gameObject.transform.position, Vector3.down);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Handles.color = Color.green;
Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;
Handles.DrawLine(src.gameObject.transform.position, hit.point);
Handles.DrawWireDisc(hit.point, hit.normal, 0.5f);
Handles.color = Color.red;
Handles.zTest = UnityEngine.Rendering.CompareFunction.Greater;
Handles.DrawLine(src.gameObject.transform.position, hit.point);
Handles.DrawWireDisc(hit.point, hit.normal, 0.5f);
}
}
}
}
}

76
com.unity.render-pipelines.high-definition/HDRP/Lighting/Light/HDAdditionalLightData.cs


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

return useColorTemperatureProperty.boolValue;
}
}
private void DrawGizmos(bool selected)
{
var light = gameObject.GetComponent<Light>();
var gizmoColor = light.color;
gizmoColor.a = selected ? 1.0f : 0.3f; // Fade for the gizmo
Gizmos.color = Handles.color = gizmoColor;
if (lightTypeExtent == LightTypeExtent.Punctual)
{
switch (light.type)
{
case LightType.Directional:
HDLightEditorUtilities.DrawDirectionalLightGizmo(light);
break;
case LightType.Point:
HDLightEditorUtilities.DrawPointlightGizmo(light, selected);
break;
case LightType.Spot:
if (spotLightShape == SpotLightShape.Cone)
HDLightEditorUtilities.DrawSpotlightGizmo(light, selected);
else if (spotLightShape == SpotLightShape.Pyramid)
HDLightEditorUtilities.DrawFrustumlightGizmo(light);
else if (spotLightShape == SpotLightShape.Box)
HDLightEditorUtilities.DrawFrustumlightGizmo(light);
break;
}
}
else
{
switch (lightTypeExtent)
{
case LightTypeExtent.Rectangle:
HDLightEditorUtilities.DrawArealightGizmo(light);
break;
case LightTypeExtent.Line:
HDLightEditorUtilities.DrawArealightGizmo(light);
break;
}
}
if (selected)
{
DrawVerticalRay();
}
}
// Trace a ray down to better locate the light location
private void DrawVerticalRay()
{
Ray ray = new Ray(transform.position, Vector3.down);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Handles.color = Color.green;
Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;
Handles.DrawLine(transform.position, hit.point);
Handles.DrawWireDisc(hit.point, hit.normal, 0.5f);
Handles.color = Color.red;
Handles.zTest = UnityEngine.Rendering.CompareFunction.Greater;
Handles.DrawLine(transform.position, hit.point);
Handles.DrawWireDisc(hit.point, hit.normal, 0.5f);
}
}
private void OnDrawGizmos()
{
// DrawGizmos(false);
}
private void OnDrawGizmosSelected()
{
DrawGizmos(true);
}
// TODO: There are a lot of old != current checks and assignation in this function, maybe think about using another system ?

8
com.unity.render-pipelines.core/CoreRP/Editor/Lighting.meta


fileFormatVersion: 2
guid: a20277bec4b2cc14dbfbc6e401f1f8f1
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

34
com.unity.render-pipelines.high-definition/HDRP/Editor/Lighting/HDLightEditorUtilities.cs


using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.Experimental.Rendering.HDPipeline;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
public static class HDLightEditorUtilities
{
public static void DrawFrustumlightGizmo(Light frustumlight)
{
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)
{
CoreLightEditorUtilities.DrawLightPyramidFrustum(Vector3.zero, frustumlight.spotAngle, frustumlight.range, 0.0f, additionalLightData.aspectRatio);
}
else // Ortho frustum
{
//DrawLightOrthoFrustum(Vector3.zero, additionalLightData.shapeWidth, additionalLightData.shapeHeight, frustumlight.range, 0.0f);
Vector3 frustumCenter = new Vector3(0.0f, 0.0f, 0.5f * frustumlight.range);
Vector3 frustumsize = new Vector3(additionalLightData.shapeWidth, additionalLightData.shapeHeight, frustumlight.range);
Gizmos.DrawWireCube(frustumCenter, frustumsize);
}
Gizmos.matrix = Matrix4x4.identity;
}
}
}

11
com.unity.render-pipelines.high-definition/HDRP/Editor/Lighting/HDLightEditorUtilities.cs.meta


fileFormatVersion: 2
guid: ef4691ff5d1cc1f4c9d444b14135a45d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

236
com.unity.render-pipelines.core/CoreRP/Editor/Lighting/CoreLightEditorUtilities.cs


using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace UnityEditor.Experimental.Rendering
{
public static class CoreLightEditorUtilities
{
// Don't use Handles.Disc as it break the highlight of the gizmo axis, use our own draw disc function instead for gizmo
public static void DrawWireDisc(Quaternion q, Vector3 position, Vector3 axis, float radius)
{
Matrix4x4 rotation = Matrix4x4.TRS(Vector3.zero, q, Vector3.one);
Gizmos.color = Color.white;
float theta = 0.0f;
float x = radius * Mathf.Cos(theta);
float y = radius * Mathf.Sin(theta);
Vector3 pos = rotation * new Vector3(x, y, 0);
pos += position;
Vector3 newPos = pos;
Vector3 lastPos = pos;
for (theta = 0.1f; theta < 2.0f * Mathf.PI; theta += 0.1f)
{
x = radius * Mathf.Cos(theta);
y = radius * Mathf.Sin(theta);
newPos = rotation * new Vector3(x, y, 0);
newPos += position;
Gizmos.DrawLine(pos, newPos);
pos = newPos;
}
Gizmos.DrawLine(pos, lastPos);
}
public static void DrawSpotlightGizmo(Light spotlight, float innerSpotPercent, 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
DrawWireDisc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * rangeDiscDistance, spotlight.gameObject.transform.forward, rangeDiscRadius);
//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)
DrawWireDisc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * nearDiscDistance, spotlight.gameObject.transform.forward, nearDiscRadius);
//Inner Cone
DrawInnerCone(spotlight, innerSpotPercent);
}
}
// innerSpotPercent - 0 to 1 value (percentage 0 - 100%)
public static void DrawInnerCone(Light spotlight, float innerSpotPercent)
{
var flatRadiusAtRange = spotlight.range * Mathf.Tan(spotlight.spotAngle * innerSpotPercent * 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 * innerSpotPercent;
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
DrawWireDisc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * innerDiscDistance, spotlight.gameObject.transform.forward, innerDiscRadius);
}
}
public static void DrawArealightGizmo(Light arealight)
{
var RectangleSize = new Vector3(arealight.areaSize.x, arealight.areaSize.y, 0);
// Remove scale for light, not take into account
var localToWorldMatrix = Matrix4x4.TRS(arealight.transform.position, arealight.transform.rotation, Vector3.one);
Gizmos.matrix = 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);
}
// 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 DrawDirectionalLightGizmo(Light directionalLight)
{
var gizmoSize = 0.2f;
DrawWireDisc(directionalLight.transform.rotation, directionalLight.transform.position, directionalLight.gameObject.transform.forward, gizmoSize);
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);
}
}
}

285
com.unity.render-pipelines.high-definition/HDRP/Lighting/Light/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
{
// Don't use Handles.Disc as it break the highlight of the gizmo axis, use our own draw disc function instead for gizmo
public static void DrawWireDisc(Quaternion q, Vector3 position, Vector3 axis, float radius)
{
Matrix4x4 rotation = Matrix4x4.TRS(Vector3.zero, q, Vector3.one);
Gizmos.color = Color.white;
float theta = 0.0f;
float x = radius * Mathf.Cos(theta);
float y = radius * Mathf.Sin(theta);
Vector3 pos = rotation * new Vector3(x, y, 0);
pos += position;
Vector3 newPos = pos;
Vector3 lastPos = pos;
for (theta = 0.1f; theta < 2.0f * Mathf.PI; theta += 0.1f)
{
x = radius * Mathf.Cos(theta);
y = radius * Mathf.Sin(theta);
newPos = rotation * new Vector3(x, y, 0);
newPos += position;
Gizmos.DrawLine(pos, newPos);
pos = newPos;
}
Gizmos.DrawLine(pos, lastPos);
}
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
DrawWireDisc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * rangeDiscDistance, spotlight.gameObject.transform.forward, rangeDiscRadius);
//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)
DrawWireDisc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * nearDiscDistance, spotlight.gameObject.transform.forward, nearDiscRadius);
//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
DrawWireDisc(spotlight.gameObject.transform.rotation, spotlight.gameObject.transform.position + spotlight.gameObject.transform.forward * innerDiscDistance, spotlight.gameObject.transform.forward, innerDiscRadius);
}
}
public static void DrawArealightGizmo(Light arealight)
{
var RectangleSize = new Vector3(arealight.areaSize.x, arealight.areaSize.y, 0);
// Remove scale for light, not take into account
var localToWorldMatrix = Matrix4x4.TRS(arealight.transform.position, arealight.transform.rotation, Vector3.one);
Gizmos.matrix = 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.shapeWidth);
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)
{
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.shapeWidth, additionalLightData.shapeHeight, frustumlight.range, 0.0f);
Vector3 frustumCenter = new Vector3(0.0f, 0.0f, 0.5f * frustumlight.range);
Vector3 frustumsize = new Vector3(additionalLightData.shapeWidth, additionalLightData.shapeHeight, frustumlight.range);
Gizmos.DrawWireCube(frustumCenter, frustumsize);
}
Gizmos.matrix = Matrix4x4.identity;
}
public static void DrawDirectionalLightGizmo(Light directionalLight)
{
var gizmoSize = 0.2f;
DrawWireDisc(directionalLight.transform.rotation, directionalLight.transform.position, directionalLight.gameObject.transform.forward, gizmoSize);
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
}

/com.unity.render-pipelines.high-definition/HDRP/Lighting/Light/HDLightEditorUtilities.cs.meta → /com.unity.render-pipelines.core/CoreRP/Editor/Lighting/CoreLightEditorUtilities.cs.meta

正在加载...
取消
保存