Skip to content

Commit

Permalink
視差マップのタイリングなどのパラメタが設定できるように
Browse files Browse the repository at this point in the history
  • Loading branch information
S20817 committed Mar 16, 2023
1 parent 1fd1117 commit 61ce32c
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 66 deletions.
6 changes: 6 additions & 0 deletions Assets/Nova/Editor/Core/Scripts/MaterialPropertyNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,15 @@ public static class MaterialPropertyNames
public const string FlowMapTarget = "_FlowMapTarget";

// Parallax Map
public const string ParallaxMapMode = "_ParallaxMapMode";
public const string ParallaxMap = "_ParallaxMap";
public const string ParallaxMap2DArray = "_ParallaxMap2DArray";
public const string ParallaxMap3D = "_ParallaxMap3D";
public const string ParallaxMapProgress = "_ParallaxMapProgress";
public const string ParallaxMapProgressCoord = "_ParallaxMapProgressCoord";
public const string ParallaxMapOffsetXCoord = "_ParallaxMapOffsetXCoord";
public const string ParallaxMapOffsetYCoord = "_ParallaxMapOffsetYCoord";
public const string ParallaxMapSliceCount = "_ParallaxMapSliceCount";
public const string ParallaxMapChannel = "_ParallaxMapChannel";
public const string ParallaxScale = "_ParallaxScale";
public const string ParallaxMapTarget = "_ParallaxMapTarget";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public enum ParallaxMapTarget
TintMap = 1 << 1,
EmissionMap = 1 << 2
}

public enum ParallaxMapMode
{
SingleTexture,
FlipBook,
FlipBookBlending,
}
}
64 changes: 53 additions & 11 deletions Assets/Nova/Editor/Core/Scripts/ParticlesUberCommonGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ private bool IsCustomCoordUsedInFlowMap()
|| IsCustomCoordUsed(_commonMaterialProperties.FlowMapOffsetYCoordProp)
|| IsCustomCoordUsed(_commonMaterialProperties.FlowIntensityCoordProp);
}

private bool IsCustomCoordUsedInParallax()
{
var isCustomCoordUsed = IsCustomCoordUsed(_commonMaterialProperties.ParallaxMapOffsetXCoordProp)
|| IsCustomCoordUsed(_commonMaterialProperties.ParallaxMapOffsetYCoordProp);
isCustomCoordUsed |= (ParallaxMapMode)_commonMaterialProperties.ParallaxMapModeProp.Value.floatValue != ParallaxMapMode.SingleTexture
&& (IsCustomCoordUsed(_commonMaterialProperties.ParallaxMapProgressProp)
|| IsCustomCoordUsed(_commonMaterialProperties.ParallaxMapProgressCoordProp));

return isCustomCoordUsed;
}

private bool IsCustomCoordUsedInAlphaTransition()
{
Expand All @@ -100,6 +111,8 @@ private bool IsCustomCoordUsedInAlphaTransition()
return isCustomCoordUsed;
}



private bool IsCustomCoordUsedInEmission()
{
var mode = (EmissionAreaType)_commonMaterialProperties.EmissionAreaTypeProp.Value.floatValue;
Expand Down Expand Up @@ -148,6 +161,7 @@ private bool IsCustomCoordUsed()
|| IsCustomCoordUsedInBaseMap()
|| IsCustomCoordUsedInTintColor()
|| IsCustomCoordUsedInFlowMap()
|| IsCustomCoordUsedInParallax()
|| IsCustomCoordUsedInAlphaTransition()
|| IsCustomCoordUsedInEmission()
|| IsCustomCoordUsedInTransparency();
Expand Down Expand Up @@ -405,30 +419,58 @@ private void InternalDrawParallaxMapsProperties()
{
var props = _commonMaterialProperties;
// The surface maps mode is decided by baseMapMode.
var baseMapMode = (BaseMapMode)props.BaseMapModeProp.Value.floatValue;
var parallaxMapMode = (ParallaxMapMode)props.BaseMapModeProp.Value.floatValue;
MaterialProperty textureProp;
switch (baseMapMode)
switch (parallaxMapMode)
{
case BaseMapMode.SingleTexture:
case ParallaxMapMode.SingleTexture:
textureProp = props.ParallaxMapProp.Value;
break;
case BaseMapMode.FlipBook:
case ParallaxMapMode.FlipBook:
textureProp = props.ParallaxMap2DArrayProp.Value;
break;
case BaseMapMode.FlipBookBlending:
case ParallaxMapMode.FlipBookBlending:
textureProp = props.ParallaxMap3DProp.Value;
break;
default:
throw new ArgumentOutOfRangeException();
}

MaterialEditorUtility.DrawTexture(
_editor,
textureProp,
false,
props.ParallaxMapChannel.Value

using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
{
MaterialEditorUtility.DrawTexture(
_editor,
textureProp,
props.ParallaxMapOffsetXCoordProp.Value,
props.ParallaxMapOffsetYCoordProp.Value,
props.ParallaxMapChannel.Value,
null
);

if (changeCheckScope.changed)
{
if (parallaxMapMode == ParallaxMapMode.FlipBook && props.ParallaxMap2DArrayProp.Value.textureValue != null)
{
var tex2DArray = (Texture2DArray)props.ParallaxMap2DArrayProp.Value.textureValue;
props.ParallaxMapSliceCountProp.Value.floatValue = tex2DArray.depth;
}
if (parallaxMapMode == ParallaxMapMode.FlipBookBlending && props.ParallaxMap3DProp.Value.textureValue != null)
{
var tex3D = (Texture3D)props.ParallaxMap3DProp.Value.textureValue;
props.ParallaxMapSliceCountProp.Value.floatValue = tex3D.depth;
}
}
}

if (parallaxMapMode > ParallaxMapMode.SingleTexture)
{
MaterialEditorUtility.DrawPropertyAndCustomCoord(
_editor,
"Flip-Book Progress",
props.ParallaxMapProgressProp.Value,
props.ParallaxMapProgressCoordProp.Value);
}

MaterialEditorUtility.DrawFloatRangeProperty(
_editor,
"Scale",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ public void Setup(MaterialProperty[] properties)
FlowMapTargetProp.Setup(properties);

// Parallax Map
ParallaxMapModeProp.Setup(properties);
ParallaxMapProp.Setup(properties);
ParallaxMap2DArrayProp.Setup(properties);
ParallaxMap3DProp.Setup(properties);
ParallaxMapProgressProp.Setup(properties);
ParallaxMapProgressCoordProp.Setup(properties);
ParallaxMapOffsetXCoordProp.Setup(properties);
ParallaxMapOffsetYCoordProp.Setup(properties);
ParallaxMapSliceCountProp.Setup(properties);
ParallaxMapChannel.Setup(properties);
ParallaxScaleProp.Setup(properties);
ParallaxMapTargetProp.Setup(properties);
Expand Down Expand Up @@ -317,9 +323,15 @@ public void Setup(MaterialProperty[] properties)

#region Paralax Map Material Properties

public ParticlesGUI.Property ParallaxMapModeProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMapMode);
public ParticlesGUI.Property ParallaxMapProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMap);
public ParticlesGUI.Property ParallaxMap2DArrayProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMap2DArray);
public ParticlesGUI.Property ParallaxMap3DProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMap3D);
public ParticlesGUI.Property ParallaxMapProgressProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMapProgress);
public ParticlesGUI.Property ParallaxMapProgressCoordProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMapProgressCoord);
public ParticlesGUI.Property ParallaxMapOffsetXCoordProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMapOffsetXCoord);
public ParticlesGUI.Property ParallaxMapOffsetYCoordProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMapOffsetYCoord);
public ParticlesGUI.Property ParallaxMapSliceCountProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMapSliceCount);
public ParticlesGUI.Property ParallaxMapChannel { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMapChannel);
public ParticlesGUI.Property ParallaxScaleProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxScale);
public ParticlesGUI.Property ParallaxMapTargetProp { get; } = new ParticlesGUI.Property(PropertyNames.ParallaxMapTarget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static class ParticlesUberUnlitMaterialPostProcessor
private static readonly int FlowIntensityCoordId =
Shader.PropertyToID(MaterialPropertyNames.FlowIntensityCoord);

private static readonly int ParallaxMapModeId = Shader.PropertyToID(MaterialPropertyNames.ParallaxMapMode);
private static readonly int ParallaxMapId = Shader.PropertyToID(MaterialPropertyNames.ParallaxMap);
private static readonly int ParallaxMap2DArrayId = Shader.PropertyToID(MaterialPropertyNames.ParallaxMap2DArray);
private static readonly int ParallaxMap3DId = Shader.PropertyToID(MaterialPropertyNames.ParallaxMap3D);
Expand Down Expand Up @@ -220,28 +221,31 @@ private static void SetupFlowMapMaterialKeywords(Material material)
var emissionEnabled = hasFlowMap && (flowMapTarget & FlowMapTarget.EmissionMap) != 0;
MaterialEditorUtility.SetKeyword(material, ShaderKeywords.FlowMapTargetEmission, emissionEnabled);
}

private static bool HasSurfaceMap(Material material, BaseMapMode baseMapMode, int map2DId, int map2DArrayId,
int map3DID)
{
switch (baseMapMode)
{
case BaseMapMode.SingleTexture:
return material.GetTexture(map2DId) != null;
case BaseMapMode.FlipBook:
return material.GetTexture(map2DArrayId) != null;
case BaseMapMode.FlipBookBlending:
return material.GetTexture(map3DID) != null;
}

return false;
}

private static void SetupParallaxMapMaterialKeywords(Material material)
{
var baseMapMode = (BaseMapMode)material.GetFloat(BaseMapModeId);
var hasParallaxMap = HasSurfaceMap(material, baseMapMode, ParallaxMapId, ParallaxMap2DArrayId, ParallaxMap3DId);
var parallaxMapMode = (ParallaxMapMode)material.GetFloat(ParallaxMapModeId);
foreach (ParallaxMapMode value in Enum.GetValues(typeof(ParallaxMapMode)))
{
var isOn = parallaxMapMode == value;
var keyword = value.GetShaderKeyword();
MaterialEditorUtility.SetKeyword(material, keyword, isOn);
}

bool hasParallaxMap = false;
switch (parallaxMapMode)
{
case ParallaxMapMode.SingleTexture:
hasParallaxMap = material.GetTexture(ParallaxMapId);
break;
case ParallaxMapMode.FlipBook:
hasParallaxMap = material.GetTexture(ParallaxMap2DArrayId);
break;
case ParallaxMapMode.FlipBookBlending:
hasParallaxMap = material.GetTexture(ParallaxMap3DId);
break;
}

var parallaxMapTarget = (ParallaxMapTarget)material.GetFloat(ParallaxMapTargetId);
var baseEnabled = hasParallaxMap && (parallaxMapTarget & ParallaxMapTarget.BaseMap) != 0;
MaterialEditorUtility.SetKeyword(material, ShaderKeywords.ParallaxMapTargetBase, baseEnabled);
Expand Down
18 changes: 18 additions & 0 deletions Assets/Nova/Editor/Core/Scripts/ShaderKeywords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public static class ShaderKeywords
public const string ParallaxMapTargetBase = "_PARALLAX_MAP_TARGET_BASE";
public const string ParallaxMapTargetTint = "_PARALLAX_MAP_TARGET_TINT";
public const string ParallaxMapTargetEmission = "_PARALLAX_MAP_TARGET_EMISSION";
public const string ParallaxMapMode2D = "_PARALLAX_MAP_MODE_2D";
public const string ParallaxMapMode2DArray = "_PARALLAX_MAP_MODE_2D_ARRAY";
public const string ParallaxMapMode3D = "_PARALLAX_MAP_MODE_3D";

// Alpha Transition
public const string FadeTransitionEnabled = "_FADE_TRANSITION_ENABLED";
Expand Down Expand Up @@ -200,5 +203,20 @@ public static string GetShaderKeyword(this ColorCorrectionMode colorCorrectionMo
throw new ArgumentOutOfRangeException(nameof(colorCorrectionMode), colorCorrectionMode, null);
}
}

public static string GetShaderKeyword(this ParallaxMapMode parallaxMapMode)
{
switch (parallaxMapMode)
{
case ParallaxMapMode.SingleTexture:
return ParallaxMapMode2D;
case ParallaxMapMode.FlipBook:
return ParallaxMapMode2DArray;
case ParallaxMapMode.FlipBookBlending:
return ParallaxMapMode3D;
default:
throw new ArgumentOutOfRangeException(nameof(ParallaxMapMode), parallaxMapMode, null);
}
}
}
}
48 changes: 38 additions & 10 deletions Assets/Nova/Runtime/Core/Shaders/ParticlesUber.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ TEXTURE3D(_ParallaxMap3D);
SAMPLER(sampler_ParallaxMap3D);
half _ParallaxMapChannel;
half _ParallaxScale;
float4 _ParallaxMap_ST;
float4 _ParallaxMap2DArray_ST;
float4 _ParallaxMap3D_ST;
float _ParallaxMapProgress;
DECLARE_CUSTOM_COORD(_ParallaxMapProgressCoord);
float _ParallaxMapSliceCount;
DECLARE_CUSTOM_COORD(_ParallaxMapOffsetXCoord);
DECLARE_CUSTOM_COORD(_ParallaxMapOffsetYCoord);

// Specular Map
TEXTURE2D(_SpecularMap);
Expand Down Expand Up @@ -203,17 +211,13 @@ SamplerState GetNormalMapSamplerState()

SamplerState GetParallaxMapSamplerState()
{
#ifdef BASE_SAMPLER_STATE_OVERRIDE_ENABLED
return BASE_SAMPLER_STATE_NAME;
#else
#ifdef _BASE_MAP_MODE_2D
#ifdef _PARALLAX_MAP_MODE_2D
return sampler_ParallaxMap;
#elif _BASE_MAP_MODE_2D_ARRAY
#elif _PARALLAX_MAP_MODE_2D_ARRAY
return sampler_ParallaxMap2DArray;
#elif _BASE_MAP_MODE_3D
#elif _PARALLAX_MAP_MODE_3D
return sampler_ParallaxMap3D;
#endif
#endif
}

SamplerState GetMetallicMapSamplerState()
Expand Down Expand Up @@ -338,12 +342,15 @@ SamplerState GetEmissionMapSamplerState()
#endif

// Sample the parallax map.
#ifdef _BASE_MAP_MODE_2D
#ifdef _PARALLAX_MAP_MODE_2D
#define SAMPLE_PARALLAX_MAP(uv, progress) SAMPLE_TEXTURE2D(_ParallaxMap, GetParallaxMapSamplerState(), uv);
#elif _BASE_MAP_MODE_2D_ARRAY
#define TRANSFORM_PARALLAX_MAP(texcoord) TRANSFORM_TEX(texcoord, _ParallaxMap);
#elif _PARALLAX_MAP_MODE_2D_ARRAY
#define SAMPLE_PARALLAX_MAP(uv, progress) SAMPLE_TEXTURE2D_ARRAY(_ParallaxMap2DArray, GetParallaxMapSamplerState(), uv, progress);
#elif _BASE_MAP_MODE_3D
#define TRANSFORM_PARALLAX_MAP(texcoord) TRANSFORM_TEX(texcoord, _ParallaxMap2DArray);
#elif _PARALLAX_MAP_MODE_3D
#define SAMPLE_PARALLAX_MAP(uv, progress) SAMPLE_TEXTURE3D_LOD(_ParallaxMap3D, GetParallaxMapSamplerState(), float3(uv, progress), 0);
#define TRANSFORM_PARALLAX_MAP(texcoord) TRANSFORM_TEX(texcoord, _ParallaxMap3D);
#endif

// Sample the metallic map.
Expand Down Expand Up @@ -588,3 +595,24 @@ float3 GetNormalWS(float3 normalTS, float3 tangentWS, float3 binormalWS, float3
#endif
#endif

// Parallax Map
#ifdef USE_PARALLAX_MAP
inline half2 ParallaxOffset(in half height, in half scale, in half3 viewDirTS)
{
// 参考: URP公式視差メソッド ParallaxOffset1Step(height, scale, viewDirTS)
// todo-zyb: まだ改善余地がある
half scaledHeight = -(1 - height) * scale;
half3 view = normalize(viewDirTS);
view.z += 0.42;
half2 offset = view.xy / view.z * scaledHeight;
return offset;
}

inline half2 GetParallaxMappingUVOffset(in half2 uv, in half progress, in half channel, in half scale, in half3 viewDirTS)
{
half4 map = SAMPLE_PARALLAX_MAP(uv, progress);
half height = map[(int)channel];
half2 offset = ParallaxOffset(height, scale, viewDirTS);
return offset;
}
#endif
10 changes: 8 additions & 2 deletions Assets/Nova/Runtime/Core/Shaders/ParticlesUberLit.shader
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,16 @@ Shader "Nova/Particles/UberLit"
_FlowMapTarget("Flow Map Target", Float) = 1.0

// Parallax Map
_ParallaxMapMode("Emission Map Mode", Float) = 0.0
_ParallaxMap("Parallax Map", 2D) = "" {}
_ParallaxMap2DArray("Parallax Map 2D Array", 2DArray) = "" {}
_ParallaxMap3D("Parallax Map 3D", 3D) = "" {}
_ParallaxMapChannel("Parallax Map Channel", Float) = 1.0
_ParallaxMapProgress("Parallax Map Progress", Range(0.0, 1.0)) = 0.0
_ParallaxMapProgressCoord("Parallax Map Progress Coord", Float) = 0.0
_ParallaxMapOffsetXCoord("Parallax Map Offset X Coord", Float) = 0.0
_ParallaxMapOffsetYCoord("Parallax Map Offset Y Coord", Float) = 0.0
_ParallaxMapSliceCount("Parallax Map Slice Count", Float) = 4.0
_ParallaxMapChannel("Parallax Map Channel", Float) = 0.0
_ParallaxScale("Parallax Scale", Range(0.0, 0.08)) = 0.03
_ParallaxMapTarget("Parallax Map Target", Float) = 1.0

Expand Down Expand Up @@ -237,7 +243,7 @@ Shader "Nova/Particles/UberLit"
#pragma shader_feature_local _PARALLAX_MAP_TARGET_BASE
#pragma shader_feature_local _PARALLAX_MAP_TARGET_TINT
#pragma shader_feature_local _PARALLAX_MAP_TARGET_EMISSION
#pragma shader_feature_local _PARALLAX_MAP_TARGET_ALPHA_TRANSITION
#pragma shader_feature_local _PARALLAX_MAP_MODE_2D _PARALLAX_MAP_MODE_2D_ARRAY _PARALLAX_MAP_MODE_3D

// Color Correction
#pragma shader_feature_local_fragment _ _GREYSCALE_ENABLED _GRADIENT_MAP_ENABLED
Expand Down
Loading

0 comments on commit 61ce32c

Please sign in to comment.