浏览代码

Adding support for 3D textures to mask local homogeneous fog volumes.

/main
Raymond Graham 7 年前
当前提交
667f9ee7
共有 12 个文件被更改,包括 265 次插入21 次删除
  1. 6
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/DensityVolumeManager.cs
  2. 48
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/HomogeneousDensityVolume.cs
  3. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/VolumeVoxelization.compute
  4. 5
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/VolumetricLighting.cs
  5. 10
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/VolumetricLighting.cs.hlsl
  6. 8
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Sky/AtmosphericScattering/VolumetricFog.cs
  7. 8
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Volumetric.meta
  8. 67
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Volumetric/HomogeneousDensityVolumeEditor.cs
  9. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Volumetric/HomogeneousDensityVolumeEditor.cs.meta
  10. 108
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Volumetric/Texture3DCreationEditor.cs
  11. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Volumetric/Texture3DCreationEditor.cs.meta

6
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/DensityVolumeManager.cs


public VolumeTextureAtlas volumeAtlas = null;
private bool atlasNeedsRefresh = false;
//TODO: hardcoded size....:-(
public static int volumeTextureSize = 32;
//TODO: hardcoded size....:-(
volumeAtlas = new VolumeTextureAtlas(TextureFormat.RGBA32, 32);
volumeAtlas = new VolumeTextureAtlas(TextureFormat.RGBA32, volumeTextureSize);
volumeAtlas.OnAtlasUpdated += AtlasUpdated;
}

48
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/HomogeneousDensityVolume.cs


public Texture3D volumeMask;
public int textureIndex;
public Vector3 textureScrollingSpeed;
public Vector3 textureTiling;
private Vector3 volumeScrollingAmount;
public DensityVolumeParameters(Color color, float _meanFreePath, float _asymmetry)
{
albedo = color;
meanFreePath = _meanFreePath;
asymmetry = _asymmetry;
volumeMask = null;
textureIndex = -1;
textureScrollingSpeed = Vector3.zero;
textureTiling = Vector3.one;
volumeScrollingAmount = textureScrollingSpeed;
}
public void Update()
{
//Update scrolling based on deltaTime
if (volumeMask != null)
{
volumeScrollingAmount = volumeScrollingAmount + (textureScrollingSpeed * Time.deltaTime);
}
}
public void Constrain()
{

meanFreePath = Mathf.Clamp(meanFreePath, 1.0f, float.MaxValue);
asymmetry = Mathf.Clamp(asymmetry, -1.0f, 1.0f);
volumeScrollingAmount = Vector3.zero;
}
public DensityVolumeData GetData()

data.extinction = VolumeRenderingUtils.ExtinctionFromMeanFreePath(meanFreePath);
data.scattering = VolumeRenderingUtils.ScatteringFromExtinctionAndAlbedo(data.extinction, (Vector3)(Vector4)albedo);
//The UV coordinates are in -1,1 space so have to convert to 0,1 space before applying texture scrolling and tiling.
// uv = ((uv * 0.5) + 0.5) * tiling + scroll
// uv = (uv * 0.5 * tiling + (0.5 * tiling + scroll))
data.textureScroll = volumeScrollingAmount + new Vector3(0.5f * textureTiling.x, 0.5f * textureTiling.y, 0.5f * textureTiling.z);
data.textureTiling = textureTiling * 0.5f;
return data;
}

[AddComponentMenu("Rendering/Homogeneous Density Volume", 1100)]
public class HomogeneousDensityVolume : MonoBehaviour
{
public DensityVolumeParameters parameters;
public DensityVolumeParameters parameters = new DensityVolumeParameters(Color.grey, 10.0f, 0.0f);
public HomogeneousDensityVolume()
{
parameters.albedo = new Color(0.5f, 0.5f, 0.5f);
parameters.meanFreePath = 10.0f;
parameters.asymmetry = 0.0f;
parameters.volumeMask = null;
parameters.textureIndex = -1;
}
//Gather and Update any parameters that may have changed
public void PrepareParameters()
{

NotifyUpdatedTexure();
previousVolumeMask = parameters.volumeMask;
}
parameters.Update();
}
private void NotifyUpdatedTexure()

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/VolumeVoxelization.compute


//--------------------------------------------------------------------------------------------------
float4 SampleVolumeMask(DensityVolumeData volumeData, float3 voxelCenterUV)
{
//TODO: transform voxelCenterUV based on volume texture transform
//scale and bias the UVs and then take fractional part, will be in [0,1] range
voxelCenterUV = frac(voxelCenterUV * volumeData.textureTiling + volumeData.textureScroll);
voxelCenterUV.z /= totalTextures;
voxelCenterUV.z += offset;

5
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/VolumetricLighting.cs


public Vector3 scattering; // [0, 1], prefer sRGB
public float extinction; // [0, 1], prefer sRGB
public int textureIndex; //
public Vector3 textureTiling;
public Vector3 textureScroll;
public static DensityVolumeData GetNeutralValues()
{

data.extinction = 0;
data.textureIndex = -1;
data.textureTiling = Vector3.one;
data.textureScroll = Vector3.zero;
return data;
}

10
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/VolumetricLighting.cs.hlsl


float3 scattering;
float extinction;
int textureIndex;
float3 textureTiling;
float3 textureScroll;
};
//

int GetTextureIndex(DensityVolumeData value)
{
return value.textureIndex;
}
float3 GetTextureTiling(DensityVolumeData value)
{
return value.textureTiling;
}
float3 GetTextureScroll(DensityVolumeData value)
{
return value.textureScroll;
}

8
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Sky/AtmosphericScattering/VolumetricFog.cs


public override void PushShaderParameters(CommandBuffer cmd, FrameSettings frameSettings)
{
DensityVolumeParameters param;
param.albedo = albedo;
param.meanFreePath = meanFreePath;
param.asymmetry = asymmetry;
param.textureIndex = -1;
param.volumeMask = null;
DensityVolumeParameters param = new DensityVolumeParameters(albedo, meanFreePath, asymmetry);
DensityVolumeData data = param.GetData();

8
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Volumetric.meta


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

67
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Volumetric/HomogeneousDensityVolumeEditor.cs


using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Experimental.Rendering.HDPipeline;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
[CanEditMultipleObjects]
[CustomEditor(typeof(HomogeneousDensityVolume))]
class HomogeneousDensityVolumeEditor : Editor
{
private static GUIContent albedoLabel = new GUIContent("Scattering Color");
private static GUIContent meanFreePathLabel = new GUIContent("Mean Free Path");
private static GUIContent asymmetryLabel = new GUIContent("Asymmetry");
private static GUIContent volumeTextureLabel = new GUIContent("Volume Texture Mask");
private static GUIContent textureScrollLabel = new GUIContent("Texture Scroll Speed");
private static GUIContent textureTileLabel = new GUIContent("Texture Tiling Amount");
private static GUIContent textureSettingsTitle = new GUIContent("Volume Texture Settings");
private bool showTextureParams = false;
SerializedProperty densityParams;
SerializedProperty albedo;
SerializedProperty meanFreePath;
SerializedProperty asymmetry;
SerializedProperty volumeTexture;
SerializedProperty textureScroll;
SerializedProperty textureTile;
void OnEnable()
{
densityParams = serializedObject.FindProperty("parameters");
albedo = densityParams.FindPropertyRelative("albedo");
meanFreePath = densityParams.FindPropertyRelative("meanFreePath");
asymmetry = densityParams.FindPropertyRelative("asymmetry");
volumeTexture = densityParams.FindPropertyRelative("volumeMask");
textureScroll = densityParams.FindPropertyRelative("textureScrollingSpeed");
textureTile = densityParams.FindPropertyRelative("textureTiling");
if (volumeTexture != null && volumeTexture.objectReferenceValue != null)
{
showTextureParams = true;
}
}
public override void OnInspectorGUI()
{
albedo.colorValue = EditorGUILayout.ColorField(albedoLabel, albedo.colorValue, true, false, false);
EditorGUILayout.PropertyField(meanFreePath, meanFreePathLabel);
EditorGUILayout.PropertyField(asymmetry, asymmetryLabel);
EditorGUILayout.Space();
showTextureParams = EditorGUILayout.Foldout(showTextureParams, textureSettingsTitle, true);
if (showTextureParams)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(volumeTexture, volumeTextureLabel);
EditorGUILayout.PropertyField(textureScroll, textureScrollLabel);
EditorGUILayout.PropertyField(textureTile, textureTileLabel);
EditorGUI.indentLevel--;
}
serializedObject.ApplyModifiedProperties ();
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Volumetric/HomogeneousDensityVolumeEditor.cs.meta


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

108
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Volumetric/Texture3DCreationEditor.cs


using System;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Experimental.Rendering.HDPipeline;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
//This Editor window is a quick way to generate 3D Textures for the Volumetric system.
//It will take a sourceTexture and slice it up into tiles that will fill a 3D Texture
//The volumetric system has a hardcoded size of 32x32x32 volume texture atlas so this tool makes sure it fits that size.
public class Texture3DCreationEditor : EditorWindow
{
private Texture2D sourceTexture = null;
private string sourcePath = null;
private int tileSize = DensityVolumeManager.volumeTextureSize;
private int numXTiles
{
get { return sourceTexture != null ? sourceTexture.width / tileSize : 0;}
set {}
}
private int numYTiles
{
get{ return sourceTexture != null ? sourceTexture.height / tileSize : 0; }
set {}
}
private bool validData
{
get { return numXTiles * numYTiles >= tileSize; }
set{}
}
[MenuItem("Window/Render Pipeline/Create 3D Texture")]
private static void Init()
{
Texture3DCreationEditor window = (Texture3DCreationEditor)EditorWindow.GetWindow(typeof(Texture3DCreationEditor));
window.titleContent.text = "Create Texture3D Asset";
window.Show();
}
private void OnGUI()
{
EditorGUILayout.BeginVertical(EditorStyles.miniButton);
GUILayout.Button(new GUIContent(" Create Texture3D Asset", ""), EditorStyles.centeredGreyMiniLabel);
EditorGUILayout.Separator();
EditorGUILayout.LabelField("Source Texture");
sourceTexture = (Texture2D)EditorGUILayout.ObjectField(sourceTexture, typeof(Texture2D), false);
EditorGUILayout.HelpBox(String.Format("Volumetric system requires textures of size {0}x{0}x{0} so please ensure the source texture is at least this many pixels.", tileSize), MessageType.Info);
EditorGUILayout.Separator();
if(sourceTexture != null)
{
sourcePath = AssetDatabase.GetAssetPath(sourceTexture);
if (validData)
{
if (GUILayout.Button(new GUIContent("Generate 3D Texture", ""), EditorStyles.toolbarButton))
{
Generate3DTexture();
}
}
else
{
EditorGUILayout.HelpBox("Invalid Source Texture: Source texture size is not enough to create " + tileSize + " depthSlices.", MessageType.Error);
}
}
EditorGUILayout.EndVertical();
}
private void Generate3DTexture()
{
Texture3D texture = new Texture3D(tileSize, tileSize, tileSize, sourceTexture.format, false);
texture.wrapMode = sourceTexture.wrapMode;
texture.wrapModeU = sourceTexture.wrapModeU;
texture.wrapModeV = sourceTexture.wrapModeV;
texture.wrapModeW = sourceTexture.wrapModeW;
texture.filterMode = sourceTexture.filterMode;
texture.mipMapBias = 0;
texture.anisoLevel = 0;
Color[] colorArray = new Color[0];
for(int i = 0; i < numXTiles; ++i)
{
for(int j = 0; j < numYTiles; ++j)
{
Color[] texColor = sourceTexture.GetPixels(i*tileSize, j*tileSize, tileSize, tileSize);
Array.Resize(ref colorArray, texColor.Length + colorArray.Length);
Array.Copy(texColor, 0, colorArray, colorArray.Length - texColor.Length, texColor.Length);
}
}
texture.SetPixels(colorArray);
texture.Apply();
AssetDatabase.CreateAsset(texture, System.IO.Directory.GetParent(sourcePath) + "\\" + sourceTexture.name + "_Texture3D.asset");
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Volumetric/Texture3DCreationEditor.cs.meta


fileFormatVersion: 2
guid: 9723a65f374db464a8a4f39c94ca7f54
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存