浏览代码

fptl: cookie texture settings and conversion, RGBA cookies

/main
vlad-andreev 8 年前
当前提交
54dac420
共有 7 个文件被更改,包括 145 次插入13 次删除
  1. 88
      Assets/ScriptableRenderLoop/common/TextureCache.cs
  2. 13
      Assets/ScriptableRenderLoop/fptl/FptlLighting.cs
  3. 16
      Assets/ScriptableRenderLoop/fptl/Internal-DeferredShading.shader
  4. 10
      Assets/ScriptableRenderLoop/fptl/renderloopfptl.asset
  5. 22
      Assets/ScriptableRenderLoop/common/TextureSettings.cs
  6. 9
      Assets/TestScenes/FPTL/FPTL.meta

88
Assets/ScriptableRenderLoop/common/TextureCache.cs


public override void TransferToSlice(int sliceIndex, Texture texture)
{
for (int m = 0; m < m_numMipLevels; m++)
Graphics.CopyTexture(texture, 0, m, cache, sliceIndex, m);
bool mismatch = (cache.width != texture.width) || (cache.height != texture.height);
if (!mismatch)
{
if (texture is Texture2D)
{
mismatch = cache.format != (texture as Texture2D).format;
}
}
if (!mismatch)
{
Graphics.CopyTexture(texture, 0, cache, sliceIndex);
}
else
{
// TODO: move to C++
Texture2D src = texture as Texture2D;
if (src == null)
{
Debug.LogError("TransferToSlice() requires size conversion, but the input texture is not a Texture2D.");
return;
}
Color[] data = cache.GetPixels(sliceIndex);
float sx = 1.0f / (float)cache.width;
float sy = 1.0f / (float)cache.height;
for (int i = 0; i < data.Length; i++)
{
int x = i % cache.width;
int y = i / cache.width;
data[i] = src.GetPixelBilinear(sx * (float)x, sy * (float)y);
}
cache.SetPixels(data, sliceIndex);
cache.Apply();
}
}
public override Texture GetTexCache()

public override void TransferToSlice(int sliceIndex, Texture texture)
{
for (int f = 0; f < 6; f++)
for (int m = 0; m < m_numMipLevels; m++)
Graphics.CopyTexture(texture, f, m, cache, 6 * sliceIndex + f, m);
bool mismatch = (cache.width != texture.width) || (cache.height != texture.height);
if (!mismatch)
{
if (texture is Cubemap)
{
mismatch = cache.format != (texture as Cubemap).format;
}
}
if (!mismatch)
{
for (int f = 0; f < 6; f++)
Graphics.CopyTexture(texture, f, cache, 6 * sliceIndex + f);
}
else
{
// TODO: move to C++
Cubemap src = texture as Cubemap;
if (src == null)
{
Debug.LogError("TransferToSlice() requires size conversion, but the input texture is not a Texture2D.");
return;
}
for (int f = 0; f < 6; f++)
{
Color[] rpixels = cache.GetPixels((CubemapFace)f, sliceIndex);
float sx = 1.0f / (float)cache.width;
float sy = 1.0f / (float)cache.height;
for (int i = 0; i < rpixels.Length; i++)
{
int x = i % cache.width;
int y = i / cache.width;
rpixels[i] = src.GetPixelBilinear((CubemapFace)f, sx * (float)x, sy * (float)y);
}
cache.SetPixels(rpixels, (CubemapFace)f, sliceIndex);
cache.Apply();
}
}
}
public override Texture GetTexCache()

13
Assets/ScriptableRenderLoop/fptl/FptlLighting.cs


ShadowSettings m_ShadowSettings = ShadowSettings.Default;
ShadowRenderPass m_ShadowPass;
[SerializeField]
TextureSettings m_TextureSettings = TextureSettings.Default;
public Shader m_DeferredShader;
public Shader m_DeferredReflectionShader;

void OnValidate()
{
if (!Mathf.IsPowerOfTwo((int)m_TextureSettings.pointCookieSize) ||
!Mathf.IsPowerOfTwo((int)m_TextureSettings.spotCookieSize))
{
// let the user type in peace..
return;
}
Rebuild();
}

m_cookieTexArray = new TextureCache2D();
m_cubeCookieTexArray = new TextureCacheCubemap();
m_cubeReflTexArray = new TextureCacheCubemap();
m_cookieTexArray.AllocTextureArray(8, 128, 128, TextureFormat.Alpha8, true);
m_cubeCookieTexArray.AllocTextureArray(4, 512, TextureFormat.Alpha8, true);
m_cookieTexArray.AllocTextureArray(8, (int)m_TextureSettings.spotCookieSize, (int)m_TextureSettings.spotCookieSize, TextureFormat.RGBA32, true);
m_cubeCookieTexArray.AllocTextureArray(4, (int)m_TextureSettings.pointCookieSize, TextureFormat.RGBA32, true);
m_cubeReflTexArray.AllocTextureArray(64, 128, TextureFormat.BC6H, true);

16
Assets/ScriptableRenderLoop/fptl/Internal-DeferredShading.shader


float2 cookCoord = (-lgtDat.cotan)*float2( dot(vL, lgtDat.vLaxisX.xyz), dot(vL, lgtDat.vLaxisY.xyz) ) / fProjVec;
const bool bHasCookie = (lgtDat.flags&IS_CIRCULAR_SPOT_SHAPE)==0; // all square spots have cookies
float d0=0.65, angularAtt = smoothstep(0.0, 1.0-d0, 1.0-length(cookCoord));
float d0 = 0.65;
float4 angularAtt = float4(1,1,1,smoothstep(0.0, 1.0-d0, 1.0-length(cookCoord)));
angularAtt = UNITY_SAMPLE_TEX2DARRAY_LOD(_spotCookieTextures, float3(cookCoord, lgtDat.iSliceIndex), 0.0).w;
angularAtt = UNITY_SAMPLE_TEX2DARRAY_LOD(_spotCookieTextures, float3(cookCoord, lgtDat.iSliceIndex), 0.0);
atten *= angularAtt*(fProjVec>0.0); // finally apply this to the dist att.
atten *= angularAtt.w*(fProjVec>0.0); // finally apply this to the dist att.
const bool bHasShadow = (lgtDat.flags&HAS_SHADOW)!=0;
[branch]if(bHasShadow)

}
UnityLight light;
light.color.xyz = lgtDat.vCol.xyz*atten;
light.color.xyz = lgtDat.vCol.xyz*atten*angularAtt.xyz;
light.dir.xyz = mul((float3x3) g_mViewToWorld, vL).xyz; //unity_CameraToWorld
ints += UNITY_BRDF_PBS (data.diffuseColor, data.specularColor, oneMinusReflectivity, data.smoothness, data.normalWorld, vWSpaceVDir, light, ind);

float attLookUp = dist*lgtDat.fRecipRange; attLookUp *= attLookUp;
float atten = tex2Dlod(_LightTextureB0, float4(attLookUp.rr, 0.0, 0.0)).UNITY_ATTEN_CHANNEL;
float4 cookieColor = float4(1,1,1,1);
atten *= UNITY_SAMPLE_TEXCUBEARRAY_LOD(_pointCookieTextures, float4(-vLw, lgtDat.iSliceIndex), 0.0).w;
cookieColor = UNITY_SAMPLE_TEXCUBEARRAY_LOD(_pointCookieTextures, float4(-vLw, lgtDat.iSliceIndex), 0.0);
atten *= cookieColor.w;
}
const bool bHasShadow = (lgtDat.flags&HAS_SHADOW)!=0;

}
UnityLight light;
light.color.xyz = lgtDat.vCol.xyz*atten;
light.color.xyz = lgtDat.vCol.xyz*atten*cookieColor.xyz;
light.dir.xyz = vLw;
ints += UNITY_BRDF_PBS (data.diffuseColor, data.specularColor, oneMinusReflectivity, data.smoothness, data.normalWorld, vWSpaceVDir, light, ind);

10
Assets/ScriptableRenderLoop/fptl/renderloopfptl.asset


m_Script: {fileID: 11500000, guid: ad5bf4f8e45bdd1429eadc3445df2c89, type: 3}
m_Name: renderloopfptl
m_EditorClassIdentifier:
m_ShadowSettings:
enabled: 1
shadowAtlasWidth: 4096
shadowAtlasHeight: 4096
maxShadowDistance: 1000
directionalLightCascadeCount: 4
directionalLightCascades: {x: 0.05, y: 0.2, z: 0.3}
m_TextureSettings:
spotCookieSize: 512
pointCookieSize: 256
m_DeferredShader: {fileID: 4800000, guid: 1c102a89f3460254a8c413dbdcd63a2a, type: 3}
m_DeferredReflectionShader: {fileID: 4800000, guid: 3899e06d641c2cb4cbff794df0da536b,
type: 3}

22
Assets/ScriptableRenderLoop/common/TextureSettings.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
[System.Serializable]
public struct TextureSettings
{
public uint spotCookieSize;
public uint pointCookieSize;
static public TextureSettings Default
{
get
{
TextureSettings settings;
settings.spotCookieSize = 128;
settings.pointCookieSize = 512;
return settings;
}
}
}

9
Assets/TestScenes/FPTL/FPTL.meta


fileFormatVersion: 2
guid: 9d1c12c0e8e265a47b31bdd5728a079b
folderAsset: yes
timeCreated: 1473161971
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存