static public int constructor(IntPtr l) { try { FairyGUI.BlendModeUtils o; o = new FairyGUI.BlendModeUtils(); pushValue(l, true); pushValue(l, o); return(2); } catch (Exception e) { return(error(l, e)); } }
public void UpdateMaterial(UpdateContext context) { NMaterial nm = null; if (_manager != null && !_customMatarial) { nm = _manager.GetMaterial(this, context); _material = nm.material; if ((object)_material != (object)meshRenderer.sharedMaterial && (object)_material.mainTexture != null) { meshRenderer.sharedMaterial = _material; } if (nm.combined) { _material.SetTexture("_AlphaTex", _manager.texture.alphaTexture.nativeTexture); } } if (maskFrameId != 0 && maskFrameId != UpdateContext.frameId) { //曾经是遮罩对象,现在不是了 if (_stencilEraser != null) { _stencilEraser.enabled = false; } } if (_material != null) { if (blendMode != BlendMode.Normal) //GetMateria已经保证了不同的blendMode会返回不同的共享材质,所以这里可以放心设置 { BlendModeUtils.Apply(_material, blendMode); } if (context.clipped) { if (maskFrameId != UpdateContext.frameId && context.rectMaskDepth > 0) //在矩形剪裁下,且不是遮罩对象 { _material.SetVector("_ClipBox", context.clipInfo.clipBox); if (context.clipInfo.soft) { _material.SetVector("_ClipSoftness", context.clipInfo.softness); } } if (context.stencilReferenceValue > 0) { if (maskFrameId == UpdateContext.frameId) //是遮罩 { if (context.stencilReferenceValue == 1) { _material.SetInt("_StencilComp", (int)UnityEngine.Rendering.CompareFunction.Always); _material.SetInt("_Stencil", 1); _material.SetInt("_StencilOp", (int)UnityEngine.Rendering.StencilOp.Replace); _material.SetInt("_StencilReadMask", 255); _material.SetInt("_ColorMask", 0); } else { _material.SetInt("_StencilComp", (int)UnityEngine.Rendering.CompareFunction.Equal); _material.SetInt("_Stencil", context.stencilReferenceValue | (context.stencilReferenceValue - 1)); _material.SetInt("_StencilOp", (int)UnityEngine.Rendering.StencilOp.Replace); _material.SetInt("_StencilReadMask", context.stencilReferenceValue - 1); _material.SetInt("_ColorMask", 0); } //设置擦除stencil的drawcall if (_stencilEraser == null) { _stencilEraser = new StencilEraser(gameObject.transform); _stencilEraser.meshFilter.mesh = mesh; } else { _stencilEraser.enabled = true; } if (nm != null) { NMaterial eraserNm = _manager.GetMaterial(this, context); eraserNm.stencilSet = true; Material eraserMat = eraserNm.material; if ((object)eraserMat != (object)_stencilEraser.meshRenderer.sharedMaterial) { _stencilEraser.meshRenderer.sharedMaterial = eraserMat; } int refValue = context.stencilReferenceValue - 1; eraserMat.SetInt("_StencilComp", (int)UnityEngine.Rendering.CompareFunction.Equal); eraserMat.SetInt("_Stencil", refValue); eraserMat.SetInt("_StencilOp", (int)UnityEngine.Rendering.StencilOp.Replace); eraserMat.SetInt("_StencilReadMask", refValue); eraserMat.SetInt("_ColorMask", 0); } } else { int refValue = context.stencilReferenceValue | (context.stencilReferenceValue - 1); _material.SetInt("_StencilComp", (int)UnityEngine.Rendering.CompareFunction.Equal); _material.SetInt("_Stencil", refValue); _material.SetInt("_StencilOp", (int)UnityEngine.Rendering.StencilOp.Keep); _material.SetInt("_StencilReadMask", refValue); _material.SetInt("_ColorMask", 15); } if (nm != null) { nm.stencilSet = true; } } else if (nm != null && nm.stencilSet) { _material.SetInt("_StencilComp", (int)UnityEngine.Rendering.CompareFunction.Always); _material.SetInt("_Stencil", 0); _material.SetInt("_StencilOp", (int)UnityEngine.Rendering.StencilOp.Keep); _material.SetInt("_StencilReadMask", 255); _material.SetInt("_ColorMask", 15); nm.stencilSet = false; } else { _material.SetInt("_StencilComp", (int)UnityEngine.Rendering.CompareFunction.Always); _material.SetInt("_Stencil", 0); _material.SetInt("_StencilOp", (int)UnityEngine.Rendering.StencilOp.Keep); _material.SetInt("_StencilReadMask", 255); _material.SetInt("_ColorMask", 15); } } } }
/// <summary> /// /// </summary> /// <param name="context"></param> public void UpdateMaterial(UpdateContext context) { Stats.GraphicsCount++; NMaterial nm = null; bool firstInstance = true; if (!_customMatarial) { if (_manager != null) { nm = _manager.GetMaterial(this, context, out firstInstance); _material = nm.material; if ((object)_material != (object)meshRenderer.sharedMaterial) { meshRenderer.sharedMaterial = _material; } } else { _material = null; if ((object)meshRenderer.sharedMaterial != null) { meshRenderer.sharedMaterial = null; } } } if (maskFrameId != 0 && maskFrameId != UpdateContext.frameId) { //曾经是遮罩对象,现在不是了 if (_stencilEraser != null) { _stencilEraser.enabled = false; } } if (firstInstance && (object)_material != null) { if (blendMode != BlendMode.Normal) //GetMateria已经保证了不同的blendMode会返回不同的共享材质,所以这里可以放心设置 { BlendModeUtils.Apply(_material, blendMode); } bool clearStencil = false; if (context.clipped) { if (maskFrameId != UpdateContext.frameId && context.rectMaskDepth > 0) //在矩形剪裁下,且不是遮罩对象 { _material.SetVector(ShaderConfig._properyIDs._ClipBox, context.clipInfo.clipBox); if (context.clipInfo.soft) { _material.SetVector(ShaderConfig._properyIDs._ClipSoftness, context.clipInfo.softness); } } if (context.stencilReferenceValue > 0) { if (maskFrameId == UpdateContext.frameId) //是遮罩 { if (context.stencilReferenceValue == 1) { _material.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.Always); _material.SetInt(ShaderConfig._properyIDs._Stencil, 1); _material.SetInt(ShaderConfig._properyIDs._StencilOp, (int)UnityEngine.Rendering.StencilOp.Replace); _material.SetInt(ShaderConfig._properyIDs._StencilReadMask, 255); _material.SetInt(ShaderConfig._properyIDs._ColorMask, 0); } else { _material.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.Equal); _material.SetInt(ShaderConfig._properyIDs._Stencil, context.stencilReferenceValue | (context.stencilReferenceValue - 1)); _material.SetInt(ShaderConfig._properyIDs._StencilOp, (int)UnityEngine.Rendering.StencilOp.Replace); _material.SetInt(ShaderConfig._properyIDs._StencilReadMask, context.stencilReferenceValue - 1); _material.SetInt(ShaderConfig._properyIDs._ColorMask, 0); } //设置擦除stencil的drawcall if (_stencilEraser == null) { _stencilEraser = new StencilEraser(gameObject.transform); _stencilEraser.meshFilter.mesh = mesh; } else { _stencilEraser.enabled = true; } if (nm != null) { NMaterial eraserNm = _manager.GetMaterial(this, context, out firstInstance); eraserNm.stencilSet = true; Material eraserMat = eraserNm.material; if ((object)eraserMat != (object)_stencilEraser.meshRenderer.sharedMaterial) { _stencilEraser.meshRenderer.sharedMaterial = eraserMat; } int refValue = context.stencilReferenceValue - 1; eraserMat.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.Equal); eraserMat.SetInt(ShaderConfig._properyIDs._Stencil, refValue); eraserMat.SetInt(ShaderConfig._properyIDs._StencilOp, (int)UnityEngine.Rendering.StencilOp.Replace); eraserMat.SetInt(ShaderConfig._properyIDs._StencilReadMask, refValue); eraserMat.SetInt(ShaderConfig._properyIDs._ColorMask, 0); } } else { int refValue = context.stencilReferenceValue | (context.stencilReferenceValue - 1); if (context.clipInfo.reversedMask) { _material.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.NotEqual); } else { _material.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.Equal); } _material.SetInt(ShaderConfig._properyIDs._Stencil, refValue); _material.SetInt(ShaderConfig._properyIDs._StencilOp, (int)UnityEngine.Rendering.StencilOp.Keep); _material.SetInt(ShaderConfig._properyIDs._StencilReadMask, refValue); _material.SetInt(ShaderConfig._properyIDs._ColorMask, 15); } if (nm != null) { nm.stencilSet = true; } } else { clearStencil = nm == null || nm.stencilSet; } } else { clearStencil = nm == null || nm.stencilSet; } if (clearStencil) { _material.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.Always); _material.SetInt(ShaderConfig._properyIDs._Stencil, 0); _material.SetInt(ShaderConfig._properyIDs._StencilOp, (int)UnityEngine.Rendering.StencilOp.Keep); _material.SetInt(ShaderConfig._properyIDs._StencilReadMask, 255); _material.SetInt(ShaderConfig._properyIDs._ColorMask, 15); } } }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="alpha"></param> /// <param name="grayed"></param> public void Update(UpdateContext context, float alpha, bool grayed) { Stats.GraphicsCount++; if (_meshDirty) { _alpha = alpha; UpdateMeshNow(); } else if (_alpha != alpha) { ChangeAlpha(alpha); } uint clipId = context.clipInfo.clipId; int matType = 0; NMaterial nm = null; if (!_customMatarial) { if (_manager != null) { if (context.clipped && !dontClip) { if (_maskFlag == 1) { matType = 6; } else if (context.rectMaskDepth == 0) { matType = grayed ? 1 : 0; } else if (context.clipInfo.soft) { matType = grayed ? 5 : 4; } else { matType = grayed ? 3 : 2; } } else { clipId = 0; matType = grayed ? 1 : 0; } nm = _manager.GetMaterial(matType, blendMode, clipId); _material = nm.material; if ((object)_material != (object)meshRenderer.sharedMaterial) { meshRenderer.sharedMaterial = _material; } } else { _material = null; if ((object)meshRenderer.sharedMaterial != null) { meshRenderer.sharedMaterial = null; } } } if ((nm == null || nm._firstInstance) && (object)_material != null) { if (blendMode != BlendMode.Normal) //GetMateria已经保证了不同的blendMode会返回不同的共享材质,所以这里可以放心设置 { BlendModeUtils.Apply(_material, blendMode); } bool clearStencil = nm == null || nm.stencilSet; if (context.clipped) { if (_maskFlag != 1 && context.rectMaskDepth > 0) //在矩形剪裁下,且不是遮罩对象 { _material.SetVector(ShaderConfig._properyIDs._ClipBox, context.clipInfo.clipBox); if (context.clipInfo.soft) { _material.SetVector(ShaderConfig._properyIDs._ClipSoftness, context.clipInfo.softness); } } if (context.stencilReferenceValue > 0) { if (_maskFlag == 1) //是遮罩 { if (context.stencilReferenceValue == 1) { _material.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.Always); _material.SetInt(ShaderConfig._properyIDs._Stencil, 1); _material.SetInt(ShaderConfig._properyIDs._StencilOp, (int)UnityEngine.Rendering.StencilOp.Replace); _material.SetInt(ShaderConfig._properyIDs._StencilReadMask, 255); _material.SetInt(ShaderConfig._properyIDs._ColorMask, 0); } else { if (context.isStencilReversing) { _material.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.NotEqual); } else { _material.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.Equal); } _material.SetInt(ShaderConfig._properyIDs._Stencil, context.stencilReferenceValue | (context.stencilReferenceValue - 1)); _material.SetInt(ShaderConfig._properyIDs._StencilOp, (int)UnityEngine.Rendering.StencilOp.Replace); _material.SetInt(ShaderConfig._properyIDs._StencilReadMask, context.stencilReferenceValue - 1); _material.SetInt(ShaderConfig._properyIDs._ColorMask, 0); } } else { _material.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.Equal); _material.SetInt(ShaderConfig._properyIDs._Stencil, context.stencilCompareValue); _material.SetInt(ShaderConfig._properyIDs._StencilOp, (int)UnityEngine.Rendering.StencilOp.Keep); _material.SetInt(ShaderConfig._properyIDs._StencilReadMask, context.stencilReferenceValue | (context.stencilReferenceValue - 1)); _material.SetInt(ShaderConfig._properyIDs._ColorMask, 15); } if (nm != null) { nm.stencilSet = true; } clearStencil = false; } } if (clearStencil) { if (nm != null) { nm.stencilSet = false; } _material.SetInt(ShaderConfig._properyIDs._StencilComp, (int)UnityEngine.Rendering.CompareFunction.Always); _material.SetInt(ShaderConfig._properyIDs._Stencil, 0); _material.SetInt(ShaderConfig._properyIDs._StencilOp, (int)UnityEngine.Rendering.StencilOp.Keep); _material.SetInt(ShaderConfig._properyIDs._StencilReadMask, 255); _material.SetInt(ShaderConfig._properyIDs._ColorMask, 15); } } if (_maskFlag != 0) { if (_maskFlag == 1) { _maskFlag = 2; } else { if (_stencilEraser != null) { _stencilEraser.enabled = false; } _maskFlag = 0; } } }
/// <summary> /// /// </summary> /// <param name="flags"></param> /// <param name="blendMode"></param> /// <param name="group"></param> /// <returns></returns> public Material GetMaterial(int flags, BlendMode blendMode, uint group) { if (blendMode != BlendMode.Normal && BlendModeUtils.Factors[(int)blendMode].pma) { flags |= (int)MaterialFlags.ColorFilter; } List <MaterialRef> items; if (!_materials.TryGetValue(flags, out items)) { items = new List <MaterialRef>(); _materials[flags] = items; } int frameId = Time.frameCount; int cnt = items.Count; MaterialRef result = null; for (int i = 0; i < cnt; i++) { MaterialRef item = items[i]; if (item.group == group && item.blendMode == blendMode) { if (item.frame != frameId) { firstMaterialInFrame = true; item.frame = frameId; } else { firstMaterialInFrame = false; } if (_combineTexture) { item.material.SetTexture(ShaderConfig.ID_AlphaTex, _texture.alphaTexture); } return(item.material); } else if (result == null && (item.frame > frameId || item.frame < frameId - 1)) //collect materials if it is unused in last frame { result = item; } } if (result == null) { result = new MaterialRef() { material = CreateMaterial(flags) }; items.Add(result); } else if (_combineTexture) { result.material.SetTexture(ShaderConfig.ID_AlphaTex, _texture.alphaTexture); } if (result.blendMode != blendMode) { BlendModeUtils.Apply(result.material, blendMode); result.blendMode = blendMode; } result.group = group; result.frame = frameId; firstMaterialInFrame = true; return(result.material); }
public void Render(Rect rect, Vector2 scale, float rotation, float alpha, UpdateContext context) { if (_texture == null || !enabled) { return; } if (skipMask && UpdateContext.current.clipped) { UpdateContext.current.SkipMask(true); } if (rotation != 0) { sHelperMatrix = MatrixHelper.RotateZ(rotation); } alpha *= context.alpha; RenderTarget renderTarget = context.renderTarget; if (context.blendMode != blendMode) { context.blendMode = blendMode; BlendModeUtils.Apply(blendMode); } for (int i = 0; i < _quadCount; i++) { Quad quad = _quads[i]; float x, y, w, h; x = quad.drawRect.x * scale.x; y = quad.drawRect.y * scale.y; w = quad.drawRect.Width * scale.x; h = quad.drawRect.Height * scale.y; if (quad.drawRect.Width >= 1 && w < 1) { w = 1; } if (quad.drawRect.Height >= 1 && h < 1) { h = 1; } Vector2 uv0 = quad.uv[0]; Vector2 uv2 = quad.uv[2]; if (rotation != 0) { //底层选装的轴心在中间,要转换一下 Vector3 cePivot = new Vector3(w * 0.5f, h * 0.5f, 0); Vector3 pos = sHelperMatrix.TransformPoint(new Vector3(x, y, 0)) + sHelperMatrix.TransformPoint(cePivot) - cePivot; x = pos.x; y = pos.y; } if (renderTarget == null) { if (pixelSnapping) { x = (float)Math.Floor(rect.x + x) * viewportReverseScale.x; y = (float)Math.Floor(rect.y + y) * viewportReverseScale.y; } else { x = (rect.x + x) * viewportReverseScale.x; y = (rect.y + y) * viewportReverseScale.y; } w *= viewportReverseScale.x; h *= viewportReverseScale.y; #if CE_5_5 IRenderAuxImage.Draw2dImage(x, y, w, h _texture.ID, uv0.x, uv0.y, uv2.x, uv2.y, rotation, quad.color.R, quad.color.G, quad.color.B, quad.color.A * alpha); #else Global.gEnv.pRenderer.Draw2dImage(x, y, w, h, _texture.ID, uv0.x, uv0.y, uv2.x, uv2.y, rotation, quad.color.R, quad.color.G, quad.color.B, quad.color.A * alpha); #endif } else { #if CE_5_5 #else Global.gEnv.pRenderer.PushUITexture(_texture.ID, renderTarget.texture.ID, (rect.x + x - renderTarget.origin.x) / renderTarget.texture.width, (rect.y + y - renderTarget.origin.y) / renderTarget.texture.height, w / renderTarget.texture.width, h / renderTarget.texture.height, uv0.x, uv0.y, uv2.x, uv2.y, quad.color.R, quad.color.G, quad.color.B, quad.color.A * alpha); #endif } } if (skipMask && UpdateContext.current.clipped) { UpdateContext.current.SkipMask(true); } }
public void UpdateMaterial(UpdateContext context) { if (_manager != null && !_customMatarial) { _material = _manager.GetMaterial(this, context); if ((object)_material != (object)meshRenderer.sharedMaterial && (object)_material.mainTexture != null) { meshRenderer.sharedMaterial = _material; } } if (_material != null) { if (blendMode != BlendMode.Normal) //GetMateria已经保证了不同的blendMode会返回不同的共享材质,所以这里可以放心设置 { BlendModeUtils.Apply(_material, blendMode); } if (context.clipped) { if (maskFrameId != UpdateContext.frameId && context.rectMaskDepth > 0) { _material.SetVector("_ClipBox", context.clipInfo.clipBox); if (context.clipInfo.soft) { _material.SetVector("_ClipSoftness", context.clipInfo.softness); } } if (context.stencilReferenceValue > 0) { if (maskFrameId == UpdateContext.frameId) { if (context.stencilReferenceValue == 1) { _material.SetInt("_StencilComp", (int)UnityEngine.Rendering.CompareFunction.Always); _material.SetInt("_Stencil", 1); _material.SetInt("_StencilOp", (int)UnityEngine.Rendering.StencilOp.Replace); _material.SetInt("_StencilReadMask", 255); _material.SetInt("_ColorMask", 0); } else { _material.SetInt("_StencilComp", (int)UnityEngine.Rendering.CompareFunction.Equal); _material.SetInt("_Stencil", context.stencilReferenceValue | (context.stencilReferenceValue - 1)); _material.SetInt("_StencilOp", (int)UnityEngine.Rendering.StencilOp.Replace); _material.SetInt("_StencilReadMask", context.stencilReferenceValue - 1); _material.SetInt("_ColorMask", 0); } } else { _material.SetInt("_StencilComp", (int)UnityEngine.Rendering.CompareFunction.Equal); _material.SetInt("_Stencil", context.stencilReferenceValue | (context.stencilReferenceValue - 1)); _material.SetInt("_StencilOp", (int)UnityEngine.Rendering.StencilOp.Keep); _material.SetInt("_StencilReadMask", context.stencilReferenceValue | (context.stencilReferenceValue - 1)); _material.SetInt("_ColorMask", 15); } if (_material is NMaterial) { ((NMaterial)_material).stencilSet = true; } } else if ((_material is NMaterial) && ((NMaterial)_material).stencilSet) { _material.SetInt("_StencilComp", (int)UnityEngine.Rendering.CompareFunction.Always); _material.SetInt("_Stencil", 0); _material.SetInt("_StencilOp", (int)UnityEngine.Rendering.StencilOp.Keep); _material.SetInt("_StencilReadMask", 255); _material.SetInt("_ColorMask", 15); ((NMaterial)_material).stencilSet = false; } } } }