static public int GetMaterial(IntPtr l) { try { FairyGUI.MaterialManager self = (FairyGUI.MaterialManager)checkSelf(l); FairyGUI.NGraphics a1; checkType(l, 2, out a1); FairyGUI.UpdateContext a2; checkType(l, 3, out a2); var ret = self.GetMaterial(a1, a2); pushValue(l, true); pushValue(l, ret); 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; } } }
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; } } } }