浏览代码

Added attenuation procedural texture generation code and attenuation texture in shader.

/RenderPassXR_Sandbox
Felipe Lira 7 年前
当前提交
c28f1752
共有 13 个文件被更改,包括 232 次插入6 次删除
  1. 5
      Assets/ScriptableRenderPipeline/LightweightPipeline/Editor/LightweightAssetInspector.cs
  2. 6
      Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs
  3. 2
      Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipelineAsset.asset
  4. 7
      Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipelineAsset.cs
  5. 1
      Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipeline.shader
  6. 2
      Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineInput.cginc
  7. 14
      Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineLighting.cginc
  8. 58
      Assets/ScriptableRenderPipeline/LightweightPipeline/Editor/GenerateTextures.cs
  9. 13
      Assets/ScriptableRenderPipeline/LightweightPipeline/Editor/GenerateTextures.cs.meta
  10. 10
      Assets/ScriptableRenderPipeline/LightweightPipeline/Textures.meta
  11. 3
      Assets/ScriptableRenderPipeline/LightweightPipeline/Textures/LightweightLightAttenuation.png
  12. 117
      Assets/ScriptableRenderPipeline/LightweightPipeline/Textures/LightweightLightAttenuation.png.meta

5
Assets/ScriptableRenderPipeline/LightweightPipeline/Editor/LightweightAssetInspector.cs


"Shader to use when creating materials");
public static GUIContent msaaContent = new GUIContent("Anti Aliasing", "Controls the global anti aliasing quality. When set to disabled, MSAA will not be performed even if the camera allows it.");
public static GUIContent attenuationTextureLabel = new GUIContent("Attenuation Texture", "Light attenuation falloff texture");
}
private SerializedProperty m_LinearRenderingProperty;

private SerializedProperty m_DefaultUIMaterial;
private SerializedProperty m_DefaultShader;
private SerializedProperty m_MSAA;
private SerializedProperty m_AttenuationTexture;
void OnEnable()
{

m_DefaultUIMaterial = serializedObject.FindProperty("m_DefaultUIMaterial");
m_DefaultShader = serializedObject.FindProperty("m_DefaultShader");
m_MSAA = serializedObject.FindProperty("m_MSAA");
m_AttenuationTexture = serializedObject.FindProperty("m_AttenuationTexture");
}
public override void OnInspectorGUI()

EditorGUILayout.PropertyField(m_EnableLightmapsProp, Styles.enableLightmap);
EditorGUILayout.PropertyField(m_EnableAmbientProbeProp, Styles.enableAmbientProbe);
EditorGUILayout.PropertyField(m_MSAA, Styles.msaaContent);
EditorGUILayout.PropertyField(m_AttenuationTexture, Styles.attenuationTextureLabel);
EditorGUI.indentLevel--;
EditorGUILayout.Space();
EditorGUILayout.Space();

6
Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs


cmd.SetGlobalVectorArray ("globalLightColor", m_LightColors);
cmd.SetGlobalVectorArray ("globalLightAtten", m_LightAttenuations);
cmd.SetGlobalVectorArray ("globalLightSpotDir", m_LightSpotDirections);
if (m_Asset.AttenuationTexture != null) cmd.SetGlobalTexture("_AttenuationTexture", m_Asset.AttenuationTexture);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

cmd.EnableShaderKeyword("_SINGLE_DIRECTIONAL_LIGHT");
else
cmd.DisableShaderKeyword("_SINGLE_DIRECTIONAL_LIGHT");
if (m_Asset.AttenuationTexture != null)
cmd.EnableShaderKeyword("_ATTENUATION_TEXTURE");
else
cmd.DisableShaderKeyword("_ATTENUATION_TEXTURE");
string[] shadowKeywords = new string[] { "_HARD_SHADOWS", "_SOFT_SHADOWS", "_HARD_SHADOWS_CASCADES", "_SOFT_SHADOWS_CASCADES" };
for (int i = 0; i < shadowKeywords.Length; ++i)

2
Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipelineAsset.asset


m_Cascade2Split: 0.25
m_Cascade4Split: {x: 0.067, y: 0.2, z: 0.467}
m_LinearRendering: 1
m_AttenuationTexture: {fileID: 2800000, guid: ee420ad43ff580149a07f10ad66c49ff,
type: 3}
m_DefaultDiffuseMaterial: {fileID: 2100000, guid: 6a1143ee683302f4aa628c052723efc1,
type: 2}
m_DefaultParticleMaterial: {fileID: 2100000, guid: e823cd5b5d27c0f4b8256e7c12ee3e6d,

7
Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipelineAsset.cs


[SerializeField] private float m_Cascade2Split = 0.25f;
[SerializeField] private Vector3 m_Cascade4Split = new Vector3(0.067f, 0.2f, 0.467f);
[SerializeField] private bool m_LinearRendering = true;
[SerializeField] private Texture2D m_AttenuationTexture;
[SerializeField] private Material m_DefaultDiffuseMaterial;
[SerializeField] private Material m_DefaultParticleMaterial;

{
get { return m_LinearRendering; }
set { m_LinearRendering = value; }
}
public Texture2D AttenuationTexture
{
get { return m_AttenuationTexture; }
set { m_AttenuationTexture = value; }
}
#endregion

1
Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipeline.shader


#pragma multi_compile _ _LIGHT_PROBES_ON
#pragma multi_compile _ _HARD_SHADOWS _SOFT_SHADOWS _HARD_SHADOWS_CASCADES _SOFT_SHADOWS_CASCADES
#pragma multi_compile _ _VERTEX_LIGHTS
#pragma multi_compile _ _ATTENUATION_TEXTURE
#pragma multi_compile_fog
#pragma multi_compile_instancing

2
Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineInput.cginc


half4 spotDir;
};
sampler2D _AttenuationTexture;
// Per object light list data
#ifndef _SINGLE_DIRECTIONAL_LIGHT
half4 unity_LightIndicesOffsetAndCount;

14
Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineLighting.cginc


float distanceSqr = max(dot(posToLight, posToLight), 0.001);
// TODO: compute light attenuation from texture to save ALU. Builtin _LightTexture0 not set from SRP but we can create one.
// light attenuation with falloff.
// lightInput.atten.z = kQuadFallOff = (25.0) / (lightRange * lightRange)
// lightInput.atten.w = lightRange * lightRange
// TODO: we can precompute 1.0 / (lightInput.atten.w * 0.64 - lightInput.atten.w)
// falloff is computed from 80% light range squared
#ifdef _ATTENUATION_TEXTURE
half lightAtten = tex2D(_AttenuationTexture, float2(distanceSqr / lightInput.atten.w, 0.0)).a;
#else
//// lightInput.atten.z = kQuadFallOff = (25.0) / (lightRange * lightRange)
//// lightInput.atten.w = lightRange * lightRange
//// TODO: we can precompute 1.0 / (lightInput.atten.w * 0.64 - lightInput.atten.w)
//// falloff is computed from 80% light range squared
#endif
float3 lightDir = posToLight * rsqrt(distanceSqr);
half SdotL = saturate(dot(lightInput.spotDir.xyz, lightDir));

58
Assets/ScriptableRenderPipeline/LightweightPipeline/Editor/GenerateTextures.cs


using System.IO;
using UnityEngine;
using UnityEditor;
public class GenerateTextures : MonoBehaviour
{
private static readonly int kTextureSize = 1024;
private static readonly float kQuadraticFac = 25.0f;
private static readonly float kToZeroFadeStart = 0.8f * 0.8f;
private static readonly string kSavePath = "Assets/ScriptableRenderPipeline/LightweightPipeline/Textures/LightweightLightAttenuation.png";
[MenuItem("RenderPipeline/LightweightPipeline/GenerateLightFalloffTexture")]
public static void GenerateLightFalloffTexture()
{
Texture2D tex = new Texture2D(kTextureSize, 4, TextureFormat.Alpha8, false, true);
tex.wrapMode = TextureWrapMode.Clamp;
byte[] bytes = new byte[kTextureSize * 4];
for (int x = 0; x < kTextureSize; ++x)
{
float sqrRange = (float) x/(float) kTextureSize;
byte atten = LightAttenuationNormalized(sqrRange);
bytes[x] = atten;
bytes[x + kTextureSize] = atten;
bytes[x + kTextureSize * 2] = atten;
bytes[x + kTextureSize * 3] = atten;
}
tex.LoadRawTextureData(bytes);
tex.Apply(false);
SaveTexture(tex);
}
public static void SaveTexture(Texture2D tex)
{
byte[] bytes = tex.EncodeToPNG();
File.WriteAllBytes(kSavePath, bytes);
}
public static byte LightAttenuationNormalized(float distSqr)
{
// 1 / 1.0 + quadAtten * distSqr attenuation function
float atten = 1.0f / (1.0f + CalculateLightQuadFac(1.0f) * distSqr);
// however the above does not falloff to zero at light range.
// Start fading from ktoZeroFadeStart to light range
float fadeMultiplier = Mathf.Clamp01((distSqr - 1.0f) / (kToZeroFadeStart - 1.0f));
atten *= fadeMultiplier;
//return atten;
return (byte)Mathf.RoundToInt(atten * (float)byte.MaxValue);
}
public static float CalculateLightQuadFac(float range)
{
return kQuadraticFac / (range * range);
}
}

13
Assets/ScriptableRenderPipeline/LightweightPipeline/Editor/GenerateTextures.cs.meta


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

10
Assets/ScriptableRenderPipeline/LightweightPipeline/Textures.meta


fileFormatVersion: 2
guid: 3c94d46d9288ec04c8b97aa427dd2a3d
folderAsset: yes
timeCreated: 1503916220
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

3
Assets/ScriptableRenderPipeline/LightweightPipeline/Textures/LightweightLightAttenuation.png

之前 之后
宽度: 1024  |  高度: 4  |  大小: 398 B

117
Assets/ScriptableRenderPipeline/LightweightPipeline/Textures/LightweightLightAttenuation.png.meta


fileFormatVersion: 2
guid: ee420ad43ff580149a07f10ad66c49ff
timeCreated: 1503935793
licenseType: Pro
TextureImporter:
fileIDToRecycleName: {}
externalObjects: {}
serializedVersion: 4
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 0
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -1
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 10
textureShape: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: 1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 1
androidETC2FallbackOverride: 0
- buildTarget: iPhone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- buildTarget: tvOS
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- buildTarget: WiiU
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存