示例#1
0
        /// <summary>
        ///
        /// </summary>
        public MaterialManager GetMaterialManager(string shaderName, string[] keywords)
        {
            if (_root != this)
            {
                return(_root.GetMaterialManager(shaderName, keywords));
            }

            if (_materialManagers == null)
            {
                _materialManagers = new Dictionary <string, MaterialManager>();
            }

            string key = shaderName;

            if (keywords != null)
            {
                //对于带指定关键字的,目前的设计是不参加共享材质了,因为逻辑会变得更复杂
                key = shaderName + "_" + _gCounter++;
            }

            MaterialManager mm;

            if (!_materialManagers.TryGetValue(key, out mm))
            {
                mm             = new MaterialManager(this, ShaderConfig.GetShader(shaderName), keywords);
                mm._managerKey = key;
                _materialManagers.Add(key, mm);
            }

            return(mm);
        }
示例#2
0
        public static MaterialManager GetInstance(NTexture texture, string shaderName)
        {
            NTexture rootTexture = texture.root;

            if (rootTexture.materialManagers == null)
            {
                rootTexture.materialManagers = new Dictionary <string, MaterialManager>();
            }

            MaterialManager mm;

            if (!rootTexture.materialManagers.TryGetValue(shaderName, out mm))
            {
                mm = new MaterialManager(rootTexture);
                rootTexture.materialManagers.Add(shaderName, mm);
            }

            if (mm.sharedMaterial == null)
            {
                Shader shader = ShaderConfig.Get(shaderName);
                if (shader == null)
                {
                    Debug.LogWarning("FairyGUI: shader not found: " + shaderName);
                    shader = Shader.Find("Transparent/Diffuse");
                }
                mm.sharedMaterial             = new Material(shader);
                mm.sharedMaterial.mainTexture = rootTexture.nativeTexture;
                if (rootTexture.alphaTexture != null)
                {
                    mm.sharedMaterial.SetTexture("_AlphaTex", rootTexture.alphaTexture);
                }
            }

            return(mm);
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        public MaterialManager GetMaterialManager(string shaderName)
        {
            if (_root != this)
            {
                if (_root == null)
                {
                    return(null);
                }
                else
                {
                    return(_root.GetMaterialManager(shaderName));
                }
            }

            if (_materialManagers == null)
            {
                _materialManagers = new Dictionary <string, MaterialManager>();
            }

            MaterialManager mm;

            if (!_materialManagers.TryGetValue(shaderName, out mm))
            {
                mm = new MaterialManager(this, ShaderConfig.GetShader(shaderName));
                _materialManagers.Add(shaderName, mm);
            }

            return(mm);
        }
示例#4
0
        virtual protected void UpdateTexture()
        {
            if (_texture != null)
            {
                contentRect.width  = _texture.width;
                contentRect.height = _texture.height;

                if (_texture.root.alphaTexture != null)
                {
                    quadBatch.shader = ShaderConfig.GetGrayedVersion(ShaderConfig.combinedImageShader,
                                                                     ShaderConfig.IsGrayedVersion(quadBatch.shader));
                }
                else
                {
                    quadBatch.shader = ShaderConfig.GetGrayedVersion(ShaderConfig.imageShader,
                                                                     ShaderConfig.IsGrayedVersion(quadBatch.shader));
                }
            }
            else
            {
                contentRect.width  = 0;
                contentRect.height = 0;
            }
            _needRebuild      = true;
            quadBatch.texture = _texture;
            InvalidateBatchingState();
        }
示例#5
0
        /// <summary>
        /// 进入绘画模式,整个对象将画到一张RenderTexture上,然后这种贴图将代替原有的显示内容。
        /// 可以在onPaint回调里对这张纹理进行进一步操作,实现特殊效果。
        /// 可能有多个地方要求进入绘画模式,这里用requestorId加以区别,取值是1、2、4、8、16以此类推。1024内内部保留。用户自定义的id从1024开始。
        /// </summary>
        /// <param name="requestId">请求者id</param>
        /// <param name="margin">纹理四周的留空。如果特殊处理后的内容大于原内容,那么这里的设置可以使纹理扩大。</param>
        public void EnterPaintingMode(int requestorId, Margin?margin)
        {
            bool first = _paintingMode == 0;

            _paintingMode |= requestorId;
            if (first)
            {
                if (paintingGraphics == null)
                {
                    if (graphics == null)
                    {
                        paintingGraphics = new NGraphics(this.gameObject);
                    }
                    else
                    {
                        GameObject go = new GameObject(this.gameObject.name + " (Painter)");
                        go.layer = this.gameObject.layer;
                        ToolSet.SetParent(go.transform, cachedTransform);
                        go.hideFlags     = DisplayOptions.hideFlags;
                        paintingGraphics = new NGraphics(go);
                    }
                }
                else
                {
                    paintingGraphics.enabled = true;
                }
                paintingGraphics.vertexMatrix = null;

                if (_paintingMaterial == null)
                {
                    _paintingMaterial           = new Material(ShaderConfig.GetShader(ShaderConfig.imageShader));
                    _paintingMaterial.hideFlags = DisplayOptions.hideFlags;
                }
                paintingGraphics.material = _paintingMaterial;

                if (this is Container)
                {
                    ((Container)this).SetChildrenLayer(CaptureCamera.hiddenLayer);
                    ((Container)this).UpdateBatchingFlags();
                }
                else
                {
                    this.InvalidateBatchingState();
                }

                if (graphics != null)
                {
                    this.gameObject.layer = CaptureCamera.hiddenLayer;
                }

                _paintingMargin = new Margin();
            }
            if (margin != null)
            {
                _paintingMargin = (Margin)margin;
            }
            _paintingFlag = 1;
        }
        public void SetGrayed(bool value)
        {
            if (quadBatch == null)
            {
                return;
            }

            string shader = quadBatch.shader;

            quadBatch.shader = ShaderConfig.GetGrayedVersion(shader, value);
        }
示例#7
0
 static public int constructor(IntPtr l)
 {
     try {
         FairyGUI.ShaderConfig o;
         o = new FairyGUI.ShaderConfig();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
示例#8
0
        public NMaterial CreateMaterial()
        {
            NMaterial mat = new NMaterial(ShaderConfig.GetShader(shaderName));

            mat.mainTexture = texture.nativeTexture;
            if (texture.alphaTexture != null)
            {
                mat.EnableKeyword("COMBINED");
                mat.SetTexture("_AlphaTex", texture.alphaTexture);
            }
            if (_keywords != null)
            {
                foreach (string v in _keywords)
                {
                    mat.EnableKeyword(v);
                }
            }
            mat.hideFlags = DisplayOptions.hideFlags;

            return(mat);
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public NMaterial CreateMaterial()
        {
            NMaterial nm = new NMaterial(ShaderConfig.GetShader(shaderName));

            nm.material.mainTexture = texture.nativeTexture;
            if (texture.alphaTexture != null)
            {
                nm.combined = true;
                nm.material.EnableKeyword("COMBINED");
                nm.material.SetTexture("_AlphaTex", texture.alphaTexture.nativeTexture);
            }
            if (_keywords != null)
            {
                foreach (string v in _keywords)
                {
                    nm.material.EnableKeyword(v);
                }
            }
            nm.material.hideFlags = DisplayOptions.hideFlags;

            return(nm);
        }
示例#10
0
        public NMaterial CreateMaterial()
        {
            Shader shader = ShaderConfig.Get(shaderName);

            if (shader == null)
            {
                Debug.LogWarning("FairyGUI: shader not found: " + shaderName);
                shader = Shader.Find("UI/Default");
            }
            NMaterial mat = new NMaterial(shader);

            mat.mainTexture = texture.nativeTexture;
            if (texture.alphaTexture != null)
            {
                mat.EnableKeyword("COMBINED");
                mat.SetTexture("_AlphaTex", texture.alphaTexture);
            }

            shader.hideFlags = DisplayOptions.hideFlags;
            mat.hideFlags    = DisplayOptions.hideFlags;

            return(mat);
        }
示例#11
0
        virtual public void Update(UpdateContext context)
        {
            if (graphics != null)
            {
                graphics.alpha  = context.alpha * _alpha;
                graphics.grayed = context.grayed | _grayed;
                graphics.UpdateMaterial(context);
            }

            if (_paintingMode != 0)
            {
                NTexture paintingTexture = paintingGraphics.texture;
                if (paintingTexture != null && paintingTexture.disposed)                 //Texture可能已被Stage.MonitorTexture销毁
                {
                    paintingTexture = null;
                    _paintingFlag   = 1;
                }
                if (_paintingFlag == 1)
                {
                    _paintingFlag = 0;

                    //从优化考虑,决定使用绘画模式的容器都需要明确指定大小,而不是自动计算包围。这在UI使用上并没有问题,因为组件总是有固定大小的
                    int textureWidth  = Mathf.RoundToInt(_contentRect.width + _paintingMargin.left + _paintingMargin.right);
                    int textureHeight = Mathf.RoundToInt(_contentRect.height + _paintingMargin.top + _paintingMargin.bottom);
                    if (paintingTexture == null || paintingTexture.width != textureWidth || paintingTexture.height != textureHeight)
                    {
                        if (paintingTexture != null)
                        {
                            paintingTexture.Dispose(true);
                        }
                        if (textureWidth > 0 && textureHeight > 0)
                        {
                            paintingTexture = new NTexture(CaptureCamera.CreateRenderTexture(textureWidth, textureHeight, false));
                            Stage.inst.MonitorTexture(paintingTexture);
                        }
                        else
                        {
                            paintingTexture = null;
                        }
                        paintingGraphics.texture = paintingTexture;
                    }

                    if (paintingGraphics.material == null)
                    {
                        paintingGraphics.material           = new Material(ShaderConfig.GetShader(ShaderConfig.imageShader));
                        paintingGraphics.material.hideFlags = DisplayOptions.hideFlags;
                    }

                    if (paintingTexture != null)
                    {
                        paintingGraphics.SetOneQuadMesh(
                            new Rect(-_paintingMargin.left, -_paintingMargin.top, paintingTexture.width, paintingTexture.height),
                            new Rect(0, 0, 1, 1), Color.white);
                    }
                    else
                    {
                        paintingGraphics.ClearMesh();
                    }
                }

                if (paintingTexture != null)
                {
                    paintingTexture.lastActive = Time.time;

                    if (!(this is Container) &&                  //如果是容器,这句移到Container.Update的最后执行,因为容器中可能也有需要Capture的内容,要等他们完成后再进行容器的Capture。
                        (_paintingFlag != 2 || !_cacheAsBitmap))
                    {
                        UpdateContext.OnEnd += _captureDelegate;
                    }
                }

                paintingGraphics.UpdateMaterial(context);
            }

            if (_filter != null)
            {
                _filter.Update();
            }

            Stats.ObjectCount++;
        }
示例#12
0
        public Material GetContextMaterial(UpdateContext context)
        {
            if (!context.clipped)
            {
                return(sharedMaterial);
            }

            Material mat;

            if (context.clipInfo.soft)
            {
                if (context.workCount != _softUpdateSeq)
                {
                    _softUpdateSeq = context.workCount;
                    _softClipId    = context.clipInfo.clipId;
                    _softNextIndex = 0;
                }
                else if (_softClipId != context.clipInfo.clipId)
                {
                    _softNextIndex++;
                    _softClipId = context.clipInfo.clipId;
                }

                if (_softClippedMaterials == null)
                {
                    _softClippedMaterials = new List <Material>();
                }

                if (_softNextIndex < _softClippedMaterials.Count)
                {
                    mat = _softClippedMaterials[_softNextIndex];
                }
                else
                {
                    Shader shader = ShaderConfig.Get(sharedMaterial.shader.name + ShaderConfig.softClipShaderSuffix);
                    if (shader == null)
                    {
                        Debug.LogWarning("FairyGUI: " + sharedMaterial.shader.name + " doesn't have a soft clipped shader version for clipping");
                        shader = sharedMaterial.shader;
                    }

                    mat             = new Material(shader);
                    mat.mainTexture = sharedMaterial.mainTexture;
                    if (_owner.alphaTexture != null)
                    {
                        mat.SetTexture("_AlphaTex", _owner.alphaTexture);
                    }
                    _softClippedMaterials.Add(mat);
                }

                mat.mainTextureOffset = context.clipInfo.offset;
                mat.mainTextureScale  = context.clipInfo.scale;
                mat.SetVector("_ClipSharpness", context.clipInfo.softness);
            }
            else
            {
                if (context.workCount != _updateSeq)
                {
                    _updateSeq = context.workCount;
                    _clipId    = context.clipInfo.clipId;
                    _nextIndex = 0;
                }
                else if (_clipId != context.clipInfo.clipId)
                {
                    _nextIndex++;
                    _clipId = context.clipInfo.clipId;
                }

                if (_clippedMaterials == null)
                {
                    _clippedMaterials = new List <Material>();
                }

                if (_nextIndex < _clippedMaterials.Count)
                {
                    mat = _clippedMaterials[_nextIndex];
                }
                else
                {
                    Shader shader = ShaderConfig.Get(sharedMaterial.shader.name + ShaderConfig.alphaClipShaderSuffix);
                    if (shader == null)
                    {
                        Debug.LogWarning("FairyGUI: " + sharedMaterial.shader.name + " doesn't have a clipped shader version for clipping");
                        shader = sharedMaterial.shader;
                    }

                    mat             = new Material(shader);
                    mat.mainTexture = sharedMaterial.mainTexture;
                    if (_owner.alphaTexture != null)
                    {
                        mat.SetTexture("_AlphaTex", _owner.alphaTexture);
                    }
                    _clippedMaterials.Add(mat);
                }

                mat.mainTextureOffset = context.clipInfo.offset;
                mat.mainTextureScale  = context.clipInfo.scale;
            }

            return(mat);
        }