Exemplo n.º 1
0
 public QuadFragementUnit()
 {
     unit0 = new FragementUnit(0, this);
     unit1 = new FragementUnit(1, this);
     unit2 = new FragementUnit(2, this);
     unit3 = new FragementUnit(3, this);
 }
Exemplo n.º 2
0
        private float4 sampleTex(MipLevel level, float3 uvw, FragementUnit unit)
        {
            switch (filter)
            {
            case Context3DTextureFilter.NEAREST:
                //break;
            {
                return(readpixel(uvw, level));
            }

            case Context3DTextureFilter.LINEAR:
            {
                return(readliner(uvw, level));
            }

            case Context3DTextureFilter.ANISOTROPIC:
            {
                var quadunit = unit.quadunit;
                var center   = (quadunit.unit0.input.uv + quadunit.unit1.input.uv + quadunit.unit2.input.uv + quadunit.unit3.input.uv) * 0.25f;
                var t_center = (quadunit.unit0.input.uv + quadunit.unit1.input.uv) * 0.5f;
                var b_center = (quadunit.unit2.input.uv + quadunit.unit3.input.uv) * 0.5f;
                var l_center = (quadunit.unit0.input.uv + quadunit.unit2.input.uv) * 0.5f;
                var r_center = (quadunit.unit1.input.uv + quadunit.unit3.input.uv) * 0.5f;

                float3 p0, p1, p2, p3;
                if (unit.index == 0)
                {
                    p0 = unit.input.uv - (center - unit.input.uv);
                    p1 = t_center - (center - t_center);
                    p2 = l_center - (center - l_center);
                    p3 = center;
                }
                else if (unit.index == 1)
                {
                    p0 = t_center - (center - t_center);
                    p1 = unit.input.uv - (center - unit.input.uv);
                    p2 = center;
                    p3 = r_center - (center - r_center);
                }
                else if (unit.index == 2)
                {
                    p0 = l_center - (center - l_center);
                    p1 = center;
                    p2 = unit.input.uv - (center - unit.input.uv);
                    p3 = b_center - (center - b_center);
                }
                else
                {
                    p0 = center;
                    p1 = r_center - (center - r_center);
                    p2 = b_center - (center - b_center);
                    p3 = unit.input.uv - (center - unit.input.uv);
                }

                //float3 lp0, lp1, lp2, lp3;
                //lp0 = (p0 + p1) * 0.5f;
                //lp1 = (p1 + p2) * 0.5f;
                //lp2 = (p2 + p3) * 0.5f;
                //lp3 = (p3 + p0) * 0.5f;


                return((readliner(p0, level) + readliner(p1, level) + readliner(p2, level) + readliner(p3, level)
                        + readliner(unit.input.uv, level)
                        //+ readliner(lp0, level)
                        //+ readliner(lp1, level)
                        //+ readliner(lp2, level)
                        //+ readliner(lp3, level)


                        ) * 0.2f);


                //float3 dxuv = (unit.dpdx_v2.input.uv - unit.dpdx_v1.input.uv);
                //float3 dyuv = (unit.dpdy_v2.input.uv - unit.dpdy_v1.input.uv);

                //float l1 =  dxuv.x * dxuv.x + dxuv.y * dxuv.y;
                //float l2 =  dyuv.x * dyuv.x + dyuv.y * dyuv.y;

                //float3 duv = (dxuv * l1 + dyuv * l2) / (l1 + l2);


                //float4 c = new float4();

                //float3 st = uvw - new float3(-duv.x, -duv.y, uvw.z) * 0.5;
                //float3 ed = uvw + new float3(duv.x, duv.y, uvw.z) * 0.5;

                //float stepx = (ed.x - st.x);
                //float stepy = (ed.y - st.y);

                //for (int i = 0; i < 2; i++)
                //{
                //	for (int j = 0; j < 2; j++)
                //	{
                //		float3 uvpos = uvw + new float3(i * stepx, j * stepy, 0);
                //		c += readliner(uvpos, level);
                //	}
                //}

                //return c * 1f / 4;
            }
                throw new NotImplementedException();

            default:

                throw new NotImplementedException();
            }
        }
Exemplo n.º 3
0
        public float4 getData(float3 uvw, FragementUnit unit, float lodbias = 0)
        {
            var miplevels = texture.getMipLevels();

            if (mipFilter == Context3DMipFilter.MIPNONE)
            {
                return(sampleTex(miplevels[0], uvw, unit));
            }
            else if (mipFilter == Context3DMipFilter.MIPNEAREST)
            {
                float3 dxuv = (unit.dpdx_v2.input.uv - unit.dpdx_v1.input.uv) * new float3(miplevels[0].width, miplevels[0].height, 1);
                float3 dyuv = (unit.dpdy_v2.input.uv - unit.dpdy_v1.input.uv) * new float3(miplevels[0].width, miplevels[0].height, 1);

                float len1 = dxuv.x * dxuv.x + dxuv.y * dxuv.y;
                float len2 = dyuv.x * dyuv.x + dyuv.y * dyuv.y;

                float l = Mathf.max(len1, len2);
                float p = Mathf.sqrt(l);
                float d = (float)Math.Log(p, 2) + lodbias;

                int level = (int)Mathf.floor(Mathf.clamp(d + 0.5f, 0, texture.maxMipLevel));


                //return new float4((level+1.0f)/(texture.maxMipLevel+1), (level + 1.0f) / (texture.maxMipLevel + 1), (level + 1.0f) / (texture.maxMipLevel + 1), 1);

                return(sampleTex(miplevels[level], uvw, unit));
            }
            else
            {
                float3 dxuv = (unit.dpdx_v2.input.uv - unit.dpdx_v1.input.uv) * new float3(miplevels[0].width, miplevels[0].height, 1);
                float3 dyuv = (unit.dpdy_v2.input.uv - unit.dpdy_v1.input.uv) * new float3(miplevels[0].width, miplevels[0].height, 1);

                float len1 = dxuv.x * dxuv.x + dxuv.y * dxuv.y;
                float len2 = dyuv.x * dyuv.x + dyuv.y * dyuv.y;

                float l = Mathf.max(len1, len2);
                float p = Mathf.sqrt(l);
                float d = (float)Math.Log(p, 2) + lodbias;


                //if (filter == Context3DTextureFilter.ANISOTROPIC)
                //	d -= 1;

                float level = Mathf.clamp(d, 0, texture.maxMipLevel);



                //return new float4( level * 1.0f / texture.maxMipLevel ,level * 1.0f / texture.maxMipLevel, level * 1.0f / texture.maxMipLevel, 1 );

                int l1 = (int)Mathf.floor(level);
                int l2 = l1 + 1;
                if (l2 > texture.maxMipLevel)
                {
                    l2 = texture.maxMipLevel;
                }

                float a = Shader.frac(level);

                float4 c1 = sampleTex(miplevels[l1], uvw, unit);
                float4 c2 = sampleTex(miplevels[l2], uvw, unit);

                //c1.r = dxuv.x;
                //c1.g = dyuv.x;
                //c2.r = dxuv.x;
                //c2.g = dyuv.x;

                return(c1 * (1 - a) + c2 * (a));
            }
        }
Exemplo n.º 4
0
 internal void Run(FragementUnit unit, debugger.FrameDebugData debugData)
 {
     this.unit   = unit; this.debugData = debugData;
     unit.output = Execute(unit.input);
     this.unit   = null; this.debugData = null;
 }