浏览代码

fast shadow fix some artifacts

/main
xingwei.zhu 5 年前
当前提交
f00edb6c
共有 7 个文件被更改,包括 65 次插入14 次删除
  1. 21
      Runtime/Resources/UIWidgets_canvas_shadowRBox.shader
  2. 2
      Runtime/ui/painting/shadow_utils.cs
  3. 3
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs
  4. 23
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shader.cs
  5. 8
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shadow_utils.cs
  6. 11
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shadow_utils.cs.meta
  7. 11
      Runtime/ui/renderer/common/geometry/path/path_extension.cs.meta

21
Runtime/Resources/UIWidgets_canvas_shadowRBox.shader


{
_SrcBlend("_SrcBlend", Int) = 1 // One
_DstBlend("_DstBlend", Int) = 10 // OneMinusSrcAlpha
_StencilComp("_StencilComp", Float) = 8 // - Equal, 8 - Always
ZTest Always
ZWrite Off
Stencil {
Ref 128
Comp [_StencilComp]
}
float2 window;
float4 _viewport;
float _mat[9];
struct appdata
{

v2f o;
float padding = 3.0 * sigma;
o.coord = lerp(box.xy - padding, box.zw + padding, v.vertex.xy);
o.vertex = float4(o.coord.x * 2.0 /window.x - 1.0, o.coord.y * 2.0/window.y - 1.0, 0, 1);
float3x3 mat = float3x3(_mat[0], _mat[1], _mat[2], _mat[3], _mat[4], _mat[5], 0, 0, 1);
float2 p = mul(mat, float3(o.coord.xy, 1.0)).xy - _viewport.xy;
#if UNITY_UV_STARTS_AT_TOP
o.vertex = float4(2.0 * p.x / _viewport.z - 1.0, 2.0 * p.y / _viewport.w - 1.0, 0, 1);
#else
o.vertex = float4(2.0 * p.x / _viewport.z - 1.0, 1.0 - 2.0 * p.y / _viewport.w, 0, 1);
#endif
return o;
}

2
Runtime/ui/painting/shadow_utils.cs


const float kMaxAmbientRadius = 300 * kAmbientHeightFactor * kAmbientGeomFactor;
const bool debugShadow = true;
const bool debugShadow = false;
static float divideAndPin(float numer, float denom, float min, float max) {
return (numer / denom).clamp(min, max);

3
Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs


break;
case CmdDraw cmd:
if (cmd.material != CanvasShader.shadowBox) {
}
this._setRenderTarget(cmdBuf, layer.rtID, ref toClear);
if (cmd.layerId != null) {

23
Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shader.cs


using System;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Rendering;

_shadowRBox = new Material(shadowRBoxShader) {hideFlags = HideFlags.HideAndDontSave};
}
public static Material shadowBox => _shadowBox;
static readonly int _viewportId = Shader.PropertyToID("_viewport");
static readonly int _alphaId = Shader.PropertyToID("_alpha");
static readonly int _colorId = Shader.PropertyToID("_color");

static readonly int _shadowBoxId = Shader.PropertyToID("box");
static readonly int _shadowSigma = Shader.PropertyToID("sigma");
static readonly int _shadowColor = Shader.PropertyToID("color");
static readonly int _shadowSigmaId = Shader.PropertyToID("sigma");
static readonly int _shadowColorId = Shader.PropertyToID("color");
static readonly int _shadowCornerId = Shader.PropertyToID("corner");
static Vector4 _colorToVector4(uiColor c) {
return new Vector4(

}
public static PictureFlusher.CmdDraw fastShadow(PictureFlusher.RenderLayer layer, uiMeshMesh mesh,
bool isRect, Vector4 bound) {
bool isRect, Vector4 bound, uiColor color) {
//if (isRect) {
// mat = _shadowBox;
//}
if (!isRect) {
mat = _shadowRBox;
}
props.SetFloat(_shadowSigma, 3f);
props.SetFloat(_shadowSigmaId, 3f);
props.SetVector(_shadowColor, new Vector4(0, 0, 0, 1));
props.SetVector(_shadowColorId, _colorToVector4(color));
if (!isRect) {
props.SetFloat(_shadowCornerId, 30f);
}
return PictureFlusher.CmdDraw.create(
mesh: mesh,

8
Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shadow_utils.cs


void _drawRRectShadow(uiPath path, uiPaint paint) {
D.assert(path.isRRect, () => "Cannot draw Shadow for non-RRect shapes");
D.assert(paint.style == PaintingStyle.fill, () => "Cannot draw Shadow for stroke lines");
var bound = path.getBounds();
if (!this._applyClip(bound)) {
return;
}
var layer = this._currentLayer;
var state = layer.currentState;

_triangles.Add(3);
var mesh = uiMeshMesh.create(state.matrix, vertices, _triangles);
var bound = path.getBounds();
Debug.Log("Draw shadow>>> " + bound.top);
layer.draws.Add(CanvasShader.fastShadow(layer, mesh, path.isRect, new Vector4(bound.left, bound.top, bound.right, bound.bottom)));
layer.draws.Add(CanvasShader.fastShadow(layer, mesh, path.isRect, new Vector4(bound.left, bound.top, bound.right, bound.bottom), paint.color));
}
}

11
Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shadow_utils.cs.meta


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

11
Runtime/ui/renderer/common/geometry/path/path_extension.cs.meta


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