Skip to content

Commit

Permalink
Merge pull request #8066 from Unity-Technologies/internal/2023.2/staging
Browse files Browse the repository at this point in the history
Internal/2023.2/staging
  • Loading branch information
UnityAljosha authored Apr 29, 2024
2 parents 99062be + a0dbcb4 commit eb81481
Show file tree
Hide file tree
Showing 42 changed files with 1,174 additions and 415 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
* [Custom Material Inspector](custom-material-inspector.md)
* [Adding properties in the menu](adding-properties.md)
* [Synchronizing shader code and C#](generating-shader-includes.md)
* [Shaders](shaders.md)
* [Use shader methods from the SRP Core shader library](built-in-shader-methods.md)
* [Synchronizing shader code and C#](generating-shader-includes.md)
* [Look Dev](Look-Dev.md)
* [Environment Library](Look-Dev-Environment-Library.md)
* [Rendering Debugger](Rendering-Debugger.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Use shader methods from the SRP Core shader library

SRP Core has a library of High-Level Shader Language (HLSL) shader files that contain helper methods. You can import these files into your custom shader files and use the helper methods.

To use the following methods, add `#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"` inside the `HLSLPROGRAM` in your shader file.

### Get matrices

| **Method** | **Syntax** | **Description** |
|-|-|-|
| `CreateTangentToWorld` | `real3x3 CreateTangentToWorld(real3 normal, real3 tangent, real flipSign)` | Returns the matrix that converts tangents to world space. |
| `GetObjectToWorldMatrix()` | `float4x4 GetObjectToWorldMatrix()` | Returns the matrix that converts positions in object space to world space. |
| `GetViewToHClipMatrix()` | `float4x4 GetViewToHClipMatrix()` | Returns the matrix that converts positions in view space to clip space. |
| `GetViewToWorldMatrix()` | `float4x4 GetViewToWorldMatrix()` | Returns the matrix that converts positions in view space to world space. |
| `GetWorldToHClipMatrix()` | `float4x4 GetWorldToHClipMatrix()` | Returns the matrix that converts positions in world space to clip space. |
| `GetWorldToObjectMatrix()` | `float4x4 GetWorldToObjectMatrix()` | Returns the matrix that converts positions in world space to object space. |
| `GetWorldToViewMatrix()` | `float4x4 GetWorldToViewMatrix()` | Returns the matrix that converts positions in world space to view space. |

### Transform positions

| **Method** | **Syntax** | **Description** |
|-|-|-|
| `TransformObjectToHClip` | `float4 TransformObjectToHClip(float3 positionInObjectSpace)` | Converts a position in object space to clip space. |
| `TransformObjectToWorld` | `float3 TransformObjectToWorld(float3 positionInObjectSpace)` | Converts a position in object space to world space. |
| `TransformViewToWorld` | `float3 TransformViewToWorld(float3 positionInViewSpace)` | Converts a position in view space to world space. |
| `TransformWorldToHClip` | `float4 TransformWorldToHClip(float3 positionInWorldSpace)` | Converts a position in world space to clip space. |
| `TransformWorldToObject` | `float3 TransformWorldToObject(float3 positionInWorldSpace)` | Converts a position in world space to object space. |
| `TransformWorldToView` | `float3 TransformWorldToView(float3 positionInWorldSpace)` | Converts a position in world space to view space. |
| `TransformWViewToHClip` | `float4 TransformWViewToHClip(float3 positionInViewSpace)` | Converts a position in view space to clip space. |

### Transform directions

| **Method** | **Syntax** | **Description** |
|-|-|-|
| `TransformObjectToTangent` | `real3 TransformObjectToTangent(real3 directionInObjectSpace, real3x3 tangentToWorldMatrix)` | Converts a direction in object space to tangent space, using a tangent-to-world matrix. |
| `TransformObjectToWorldDir` | `float3 TransformObjectToWorldDir(float3 directionInObjectSpace, bool normalize = true)` | Converts a direction in object space to world space. |
| `TransformTangentToObject` | `real3 TransformTangentToObject(real3 dirTS, real3x3 tangentToWorldMatrix)` | Converts a direction in tangent space to object space, using a tangent-to-world matrix. |
| `TransformTangentToWorldDir` | `real3 TransformTangentToWorldDir(real3 directionInWorldSpace, real3x3 tangentToWorldMatrix, bool normalize = false)` | Converts a direction in tangent space to world space, using a tangent-to-world matrix. |
| `TransformViewToWorldDir` | `real3 TransformViewToWorldDir(real3 directionInViewSpace, bool normalize = false)` | Converts a direction in view space to world space. |
| `TransformWorldToHClipDir` | `real3 TransformWorldToHClipDir(real3 directionInWorldSpace, bool normalize = false)` | Converts a direction in world space to clip space. |
| `TransformWorldToObjectDir` | `float3 TransformWorldToObjectDir(float3 directionInWorldSpace, bool normalize = true)` | Converts a direction in world space to object space. |
| `TransformWorldToTangentDir` | `real3 TransformWorldToTangentDir(real3 directionInWorldSpace, real3x3 tangentToWorldMatrix, bool normalize = false)` | Converts a direction in world space to tangent space, using a tangent-to-world matrix. |
| `TransformWorldToViewDir` | `real3 TransformWorldToViewDir(real3 directionInWorldSpace, bool normalize = false)` | Converts a direction in world space to view space. |

### Transform surface normals

| **Method** | **Syntax** | **Description** |
|-|-|-|
| `TransformObjectToWorldNormal` | `float3 TransformObjectToWorldNormal(float3 normalInObjctSpace, bool normalize = true)` | Converts a normal in object space to world space. |
| `TransformTangentToWorld` | `float3 TransformTangentToWorld(float3 normalInTangentSpace, real3x3 tangentToWorldMatrix, bool normalize = false)` | Converts a normal in tangent space to world space, using a tangent-to-world matrix. |
| `TransformViewToWorldNormal` | `real3 TransformViewToWorldNormal(real3 normalInViewSpace, bool normalize = false)` | Converts a normal in view space to world space. |
| `TransformWorldToObjectNormal` | `float3 TransformWorldToObjectNormal(float3 normalInWorldSpace, bool normalize = true)` | Converts a normal in world space to object space. |
| `TransformWorldToTangent` | `float3 TransformWorldToTangent(float3 normalInWorldSpace, real3x3 tangentToWorldMatrix, bool normalize = true)` | Converts a normal in world space to tangent space using a tangent-to-world matrix. |
| `TransformWorldToViewNormal` | `real3 TransformWorldToViewNormal(real3 normalInWorldSpace, bool normalize = false)` | Converts a normal in world space to view space. |

## Additional resources

- [HLSL in Unity](https://docs.unity3d.com/Manual/SL-ShaderPrograms.html)


12 changes: 12 additions & 0 deletions Packages/com.unity.render-pipelines.core/Documentation~/shaders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Shaders

Work with shader code in the Scriptable Render Pipeline (SRP).

|**Page**|**Description**|
|-|-|
|[Use shader methods from the SRP Core shader library](built-in-shader-methods.md)|SRP Core has a library of High-Level Shader Language (HLSL) shader files that contain helper methods. You can import these files into your custom shader files and use the helper methods.|
|[Synchronizing shader code and C#](generating-shader-includes.md)|Generate HLSL code based on C# structs to synchronize data and constants between shaders and C#.|

## Additional resources

- [HLSL in Unity](https://docs.unity3d.com/Manual/SL-ShaderPrograms.html)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEditor;
#if UNITY_2020_2_OR_NEWER
using UnityEditor.AssetImporters;
Expand Down Expand Up @@ -118,7 +119,7 @@ public string GetPhotometricType()
int width = 2 * textureSize;
int height = 2 * textureSize;

NativeArray<Color32> colorBuffer;
NativeArray<Color> colorBuffer;

switch (m_iesReader.PhotometricType)
{
Expand Down Expand Up @@ -148,7 +149,7 @@ public string GetPhotometricType()
/// <returns>A Generated 2D texture doing the projection of the IES using the Gnomonic projection of the bottom half hemisphere with the given 'cone angle'</returns>
public (string, Texture) Generate2DCookie(TextureImporterCompression compression, float coneAngle, int textureSize, bool applyLightAttenuation)
{
NativeArray<Color32> colorBuffer;
NativeArray<Color> colorBuffer;

switch (m_iesReader.PhotometricType)
{
Expand All @@ -171,7 +172,7 @@ public string GetPhotometricType()
int width = 2 * textureSize;
int height = textureSize;

NativeArray<Color32> colorBuffer;
NativeArray<Color> colorBuffer;

switch (m_iesReader.PhotometricType)
{
Expand All @@ -189,7 +190,7 @@ public string GetPhotometricType()
return GenerateTexture(TextureImporterType.Default, TextureImporterShape.Texture2D, compression, width, height, colorBuffer);
}

(string, Texture) GenerateTexture(TextureImporterType type, TextureImporterShape shape, TextureImporterCompression compression, int width, int height, NativeArray<Color32> colorBuffer)
(string, Texture) GenerateTexture(TextureImporterType type, TextureImporterShape shape, TextureImporterCompression compression, int width, int height, NativeArray<Color> colorBuffer)
{
// Default values set by the TextureGenerationSettings constructor can be found in this file on GitHub:
// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/AssetPipeline/TextureGenerator.bindings.cs
Expand Down Expand Up @@ -218,6 +219,7 @@ public string GetPhotometricType()
platformSettings.maxTextureSize = 2048;
platformSettings.resizeAlgorithm = TextureResizeAlgorithm.Bilinear;
platformSettings.textureCompression = compression;
platformSettings.format = TextureImporterFormat.RGB9E5;

TextureGenerationOutput output = TextureGenerator.GenerateTexture(settings, colorBuffer);

Expand All @@ -229,21 +231,22 @@ public string GetPhotometricType()
return (output.importInspectorWarnings, output.output);
}

private static byte PackIESValue(float value)
Color ComputePixelColor(float horizontalAnglePosition, float verticalAnglePosition, float attenuation = 1.0f)
{
return (byte)Math.Clamp(value * 255, 0, 255);
float value = m_iesReader.InterpolateBilinear(horizontalAnglePosition, verticalAnglePosition) / (m_iesReader.MaxCandelas * attenuation);
return new Color(value, value, value, value);
}

NativeArray<Color32> BuildTypeACylindricalTexture(int width, int height)
NativeArray<Color> BuildTypeACylindricalTexture(int width, int height)
{
float stepU = 360f / (width - 1);
float stepV = 180f / (height - 1);

var textureBuffer = new NativeArray<Color32>(width * height, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
var textureBuffer = new NativeArray<Color>(width * height, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

for (int y = 0; y < height; y++)
{
var slice = new NativeSlice<Color32>(textureBuffer, y * width, width);
var slice = new NativeSlice<Color>(textureBuffer, y * width, width);

float latitude = y * stepV - 90f; // in range [-90..+90] degrees

Expand All @@ -255,24 +258,23 @@ NativeArray<Color32> BuildTypeACylindricalTexture(int width, int height)

float horizontalAnglePosition = m_iesReader.ComputeTypeAorBHorizontalAnglePosition(longitude);

byte value = PackIESValue(m_iesReader.InterpolateBilinear(horizontalAnglePosition, verticalAnglePosition) / m_iesReader.MaxCandelas);
slice[x] = new Color32(value, value, value, value);
slice[x] = ComputePixelColor(horizontalAnglePosition, verticalAnglePosition);
}
}

return textureBuffer;
}

NativeArray<Color32> BuildTypeBCylindricalTexture(int width, int height)
NativeArray<Color> BuildTypeBCylindricalTexture(int width, int height)
{
float stepU = k_TwoPi / (width - 1);
float stepV = Mathf.PI / (height - 1);

var textureBuffer = new NativeArray<Color32>(width * height, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
var textureBuffer = new NativeArray<Color>(width * height, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

for (int y = 0; y < height; y++)
{
var slice = new NativeSlice<Color32>(textureBuffer, y * width, width);
var slice = new NativeSlice<Color>(textureBuffer, y * width, width);

float v = y * stepV - k_HalfPi; // in range [-90..+90] degrees

Expand All @@ -293,24 +295,23 @@ NativeArray<Color32> BuildTypeBCylindricalTexture(int width, int height)
float horizontalAnglePosition = m_iesReader.ComputeTypeAorBHorizontalAnglePosition(longitude);
float verticalAnglePosition = m_iesReader.ComputeVerticalAnglePosition(latitude);

byte value = PackIESValue(m_iesReader.InterpolateBilinear(horizontalAnglePosition, verticalAnglePosition) / m_iesReader.MaxCandelas);
slice[x] = new Color32(value, value, value, value);
slice[x] = ComputePixelColor(horizontalAnglePosition, verticalAnglePosition);
}
}

return textureBuffer;
}

NativeArray<Color32> BuildTypeCCylindricalTexture(int width, int height)
NativeArray<Color> BuildTypeCCylindricalTexture(int width, int height)
{
float stepU = k_TwoPi / (width - 1);
float stepV = Mathf.PI / (height - 1);

var textureBuffer = new NativeArray<Color32>(width * height, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
var textureBuffer = new NativeArray<Color>(width * height, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

for (int y = 0; y < height; y++)
{
var slice = new NativeSlice<Color32>(textureBuffer, y * width, width);
var slice = new NativeSlice<Color>(textureBuffer, y * width, width);

float v = y * stepV - k_HalfPi; // in range [-90..+90] degrees

Expand All @@ -331,25 +332,24 @@ NativeArray<Color32> BuildTypeCCylindricalTexture(int width, int height)
float horizontalAnglePosition = m_iesReader.ComputeTypeCHorizontalAnglePosition(longitude);
float verticalAnglePosition = m_iesReader.ComputeVerticalAnglePosition(latitude);

byte value = PackIESValue(m_iesReader.InterpolateBilinear(horizontalAnglePosition, verticalAnglePosition) / m_iesReader.MaxCandelas);
slice[x] = new Color32(value, value, value, value);
slice[x] = ComputePixelColor(horizontalAnglePosition, verticalAnglePosition);
}
}

return textureBuffer;
}

NativeArray<Color32> BuildTypeAGnomonicTexture(float coneAngle, int size, bool applyLightAttenuation)
NativeArray<Color> BuildTypeAGnomonicTexture(float coneAngle, int size, bool applyLightAttenuation)
{
float limitUV = Mathf.Tan(0.5f * coneAngle * Mathf.Deg2Rad);
float stepUV = (2 * limitUV) / (size - 3);

var textureBuffer = new NativeArray<Color32>(size * size, Allocator.Temp, NativeArrayOptions.ClearMemory);
var textureBuffer = new NativeArray<Color>(size * size, Allocator.Temp, NativeArrayOptions.ClearMemory);

// Leave a one-pixel black border around the texture to avoid cookie spilling.
for (int y = 1; y < size - 1; y++)
{
var slice = new NativeSlice<Color32>(textureBuffer, y * size, size);
var slice = new NativeSlice<Color>(textureBuffer, y * size, size);

float v = (y - 1) * stepUV - limitUV;

Expand All @@ -368,25 +368,24 @@ NativeArray<Color32> BuildTypeAGnomonicTexture(float coneAngle, int size, bool a
// Factor in the light attenuation further from the texture center.
float lightAttenuation = applyLightAttenuation ? rayLengthSquared : 1f;

byte value = PackIESValue(m_iesReader.InterpolateBilinear(horizontalAnglePosition, verticalAnglePosition) / (m_iesReader.MaxCandelas * lightAttenuation));
slice[x] = new Color32(value, value, value, value);
slice[x] = ComputePixelColor(horizontalAnglePosition, verticalAnglePosition, lightAttenuation);
}
}

return textureBuffer;
}

NativeArray<Color32> BuildTypeBGnomonicTexture(float coneAngle, int size, bool applyLightAttenuation)
NativeArray<Color> BuildTypeBGnomonicTexture(float coneAngle, int size, bool applyLightAttenuation)
{
float limitUV = Mathf.Tan(0.5f * coneAngle * Mathf.Deg2Rad);
float stepUV = (2 * limitUV) / (size - 3);

var textureBuffer = new NativeArray<Color32>(size * size, Allocator.Temp, NativeArrayOptions.ClearMemory);
var textureBuffer = new NativeArray<Color>(size * size, Allocator.Temp, NativeArrayOptions.ClearMemory);

// Leave a one-pixel black border around the texture to avoid cookie spilling.
for (int y = 1; y < size - 1; y++)
{
var slice = new NativeSlice<Color32>(textureBuffer, y * size, size);
var slice = new NativeSlice<Color>(textureBuffer, y * size, size);

float v = (y - 1) * stepUV - limitUV;

Expand All @@ -406,25 +405,24 @@ NativeArray<Color32> BuildTypeBGnomonicTexture(float coneAngle, int size, bool a
// Factor in the light attenuation further from the texture center.
float lightAttenuation = applyLightAttenuation ? rayLengthSquared : 1f;

byte value = PackIESValue(m_iesReader.InterpolateBilinear(horizontalAnglePosition, verticalAnglePosition) / (m_iesReader.MaxCandelas * lightAttenuation));
slice[x] = new Color32(value, value, value, value);
slice[x] = ComputePixelColor(horizontalAnglePosition, verticalAnglePosition, lightAttenuation);
}
}

return textureBuffer;
}

NativeArray<Color32> BuildTypeCGnomonicTexture(float coneAngle, int size, bool applyLightAttenuation)
NativeArray<Color> BuildTypeCGnomonicTexture(float coneAngle, int size, bool applyLightAttenuation)
{
float limitUV = Mathf.Tan(0.5f * coneAngle * Mathf.Deg2Rad);
float stepUV = (2 * limitUV) / (size - 3);

var textureBuffer = new NativeArray<Color32>(size * size, Allocator.Temp, NativeArrayOptions.ClearMemory);
var textureBuffer = new NativeArray<Color>(size * size, Allocator.Temp, NativeArrayOptions.ClearMemory);

// Leave a one-pixel black border around the texture to avoid cookie spilling.
for (int y = 1; y < size - 1; y++)
{
var slice = new NativeSlice<Color32>(textureBuffer, y * size, size);
var slice = new NativeSlice<Color>(textureBuffer, y * size, size);

float v = (y - 1) * stepUV - limitUV;

Expand All @@ -443,8 +441,7 @@ NativeArray<Color32> BuildTypeCGnomonicTexture(float coneAngle, int size, bool a
// Factor in the light attenuation further from the texture center.
float lightAttenuation = applyLightAttenuation ? (uvLength * uvLength + 1) : 1f;

byte value = PackIESValue(m_iesReader.InterpolateBilinear(horizontalAnglePosition, verticalAnglePosition) / (m_iesReader.MaxCandelas * lightAttenuation));
slice[x] = new Color32(value, value, value, value);
slice[x] = ComputePixelColor(horizontalAnglePosition, verticalAnglePosition, lightAttenuation);
}
}

Expand Down
Loading

0 comments on commit eb81481

Please sign in to comment.