public Straight(v2f _origin, v2f _dirrection) { Origin = _origin; Dirrection = _dirrection; CalculateABC(); }
//简单的顶点着色器 v2f vert(a2v v) { v2f output = new v2f(); output.pos = Transform.MulMVPMat(v.pos); return(output); }
protected override float4 Execute(v2f i) { float4 tex = tex2D(0, i.uv); //从法线贴图中取出法线||此处*4,是因为机器人的法线需要平铺,其他地方不需要*4!! float3 normal = tex2D(2, i.uv * 4).xyz * 2 - 1; //将法线从切线空间旋转到世界空间 Rotate normals from tangent space to world space float3 worldNorm = float3(1, 1, 1); worldNorm.x = dot(i.tSpace0, normal); worldNorm.y = dot(i.tSpace1, normal); worldNorm.z = dot(i.tSpace2, normal); worldNorm = mul((float3x3)MATRIX_V, worldNorm); float4 mc = tex2D(1, worldNorm * 0.5+0.5) * tex * 2; //float4 mc = (tex + (tex2D(1, IN.worldNormal) * 2.0) - 1.0); //return (tex + (mc * 2.0) - 1.0); return mc; }
public override v2f Execute() { v2f v2f = __init_v2f(); v2f.SV_POSITION = mul(MATRIX_MVP, appdata.vertex); v2f.worldPos = mul(_ObjectToWorld, appdata.vertex).xyz; v2f.objPos = appdata.vertex.xyz; v2f.color = appdata.color; v2f.uv = appdata.uv; v2f.worldNormal = ObjectToWorldNormal(normalize(appdata.normal)); //normalize(_WorldToObject[0].xyz * appdata.normal.x + _WorldToObject[1].xyz * appdata.normal.y + _WorldToObject[2].xyz * appdata.normal.z); v2f.worldTangent = mul((float3x3)_ObjectToWorld, appdata.tangent.xyz); // Declares 3x3 matrix 'rotation', filled with tangent space basis float3 worldNormal = ObjectToWorldNormal(normalize(appdata.normal)); float3 worldTangent = ObjectToWorldDir(appdata.tangent.xyz); float3 worldBinormal = cross(v2f.worldNormal, worldTangent) * appdata.tangent.w; #region 切线空间旋转 //创建切线空间 v2f.tSpace0 = float3(worldTangent.x, worldBinormal.x, worldNormal.x); v2f.tSpace1 = float3(worldTangent.y, worldBinormal.y, worldNormal.y); v2f.tSpace2 = float3(worldTangent.z, worldBinormal.z, worldNormal.z); #endregion return(v2f); }
protected override void getRoughnessAndMetalic(v2f i, out float roughness, out float metallic) { var tex = tex2D(3, i.uv); metallic = tex.r; roughness = 1 - (tex.g) * Roughness; }
public override PointPlace GetPointPlace(v2f _point) { float mathEqualRes = Mathf.Pow((_point.x - Center.x), 2) + Mathf.Pow((_point.y - Center.y), 2); float rr = Mathf.Pow(Radius, 2); return ((mathEqualRes.Less(rr)) ? PointPlace.INSET : (mathEqualRes.More(rr)) ? PointPlace.OUTSET : PointPlace.BORDER); }
public override v2f Execute() { v2f v2f = __init_v2f(); v2f.SV_POSITION = mul(MATRIX_VP, mul(MATRIX_M, appdata.vertex)); v2f.color = appdata.color; v2f.uv = appdata.uv; return(v2f); }
// так как размеры элемента могут измениться из-за ресайза родителя, надо тречить, // проверки одних текущих свойств недостаточно void CheckUpdate() { if (WidthHeight != oldWH) { OnResize?.Invoke(oldWH); DoResize(oldWH); } oldWH = WidthHeight; }
public RectField(v2f a, v2f b, v2f c, v2f d) { // тут нужны проверки, одной формулы со скалярным произведением недостаточно, ибо получается арккосинус, // надо чекать синус наверное this.a = a; this.b = b; this.c = c; this.d = d; }
protected override float4 Execute(v2f IN) { float4 tex= tex2D(0, IN.uv); float4 mc = tex2D(1, IN.worldNormal) * tex * 2; //float4 mc = (tex + (tex2D(1, IN.worldNormal) * 2.0) - 1.0); //return (tex + (mc * 2.0) - 1.0); return mc; }
Color FragShader_Human_T(v2f v) { Vector3 viewDir = camera.transform.position - v.postion; float rim = 1 - Mathf.Max(0, Vector3.Dot(Vector3.Normalize(v.normal), Vector3.Normalize(viewDir))); float rimPower = Mathf.Pow(rim, 1 / 0.55f); Color texture = MainTexture[v.uv.x, v.uv.y]; float rimr = 1f * rimPower + texture.r * 0.2f; float rimg = 0.77f * rimPower + texture.g * 0.2f; float rimb = 0.77f * rimPower + texture.b * 0.2f; return(new Color(rimr, rimg, rimb, 0.77f)); }
public static float InvLerp(v2f a, v2f b, v2f c) { // подразумевается, что точки лежат на одной прямой, c лежит между b и a, отрезок, если он направленный, начинается в a и заканчивается в b if (Mathf.Abs(a.x - b.x) > Mathf.Abs(a.y - b.y)) { return((c.x - a.x) / (b.x - a.x)); } else { return((c.y - a.y) / (b.y - a.y)); } }
public override v2f Execute() { v2f v2f = __init_v2f(); v2f.SV_POSITION = mul(MATRIX_MVP, appdata.vertex); v2f.uv = appdata.uv; float3 worldNorm = ObjectToWorldDir(appdata.normal); worldNorm = mul((float3x3)MATRIX_V, worldNorm); v2f.worldNormal = worldNorm * 0.5 + 0.5; v2f.worldNormal.z = 0; return v2f; }
Color FragShader_Human(v2f v) { float r = 0f, g = 0f, b = 0f; float atten = 0f; Vector3 lightDir; Light light = lights[0]; Color texture = MainTexture[v.uv.x, v.uv.y]; lightDir = -light.transform.forward; atten = Vector3.Dot(Vector3.Normalize(lightDir), Vector3.Normalize(v.normal)); atten = Mathf.Max(0, atten); r = light.color.r * atten * texture.r; g = light.color.g * atten * texture.g; b = light.color.b * atten * texture.b; return(new Color(r, g, b)); }
public virtual Vertex VertShader(a2v i) { v2f v = new v2f(); v.postion = L2WMat.MultiplyPoint3x4(i.position); v.normal = L2WMat.MultiplyVector(i.normal); v.uv = i.uv; Vertex vert = new Vertex(); Vector4 svp = i.position; svp.w = 1f; svp = MVPMat * svp; vert.data = v; vert.x = (svp.x / svp.w / 2 + 0.5f) * SoftRender.width; vert.y = (svp.y / svp.w / 2 + 0.5f) * SoftRender.height; vert.z = svp.z / svp.w / 2 + 0.5f; return(vert); }
Vertex VertShader(a2v a) { v2f v = new v2f(); v.postion = L2WMat.MultiplyPoint3x4(a.postion); v.normal = L2WMat.MultiplyVector(a.normal); v.uv = a.uv; Vertex vert = new Vertex(); Vector4 svp = a.postion; svp.w = 1f; svp = MVPMat * svp; vert.data = v; vert.x = (svp.x / svp.w / 2 + 0.5f) * width; vert.y = (svp.y / svp.w / 2 + 0.5f) * height; vert.z = svp.z / svp.w / 2 + 0.5f; return(vert); }
public override v2f Execute() { v2f v2f = __init_v2f(); v2f.SV_POSITION = mul(MATRIX_MVP, appdata.vertex); v2f.worldPos = mul(_ObjectToWorld, appdata.vertex).xyz; v2f.objPos = appdata.vertex.xyz; v2f.color = appdata.color; v2f.uv = appdata.uv; v2f.worldNormal = normalize(_WorldToObject[0].xyz * appdata.normal.x + _WorldToObject[1].xyz * appdata.normal.y + _WorldToObject[2].xyz * appdata.normal.z); v2f.worldTangent = mul((float3x3)_ObjectToWorld, appdata.tangent.xyz); //float3 lightDir = normalize(float3(0, 4, -4)); //float3 normalDir = v2f.worldNormal; ////计算高光 ////计算反射光夹角 ////fixed3 reflectDir = normalize(reflect(-lightDir,normalDir)); //float3 viewDir = normalize(WorldSpaceViewDir(mul(_ObjectToWorld, appdata.vertex).xyz)); ////平分线 //float3 halfDir = normalize(viewDir + lightDir); //float _Gloss = 50; //float3 _Specular = float3(1, 1, 1); //float4 _LightColor0 = float4(1, 1, 1, 1); ////控制高光颜色 //float3 specular = _Specular * _LightColor0.rgb * pow(max(dot(normalDir, halfDir), 0), _Gloss); //float diff = max(0, dot(normalDir, lightDir)); ////fixed nh = max(0, dot(s.Normal, halfDir)); ////fixed spec = pow(nh, s.Specular * 128) * s.Gloss; //float4 c = float4(0, 0, 0, 1); //c.rgb = _LightColor0.rgb * diff + specular; //v2f.color = c; return(v2f); }
void DrawScanline(HScanline scanline) { HScreenDevice ScreenDevice = HScreenDevice.GetInstance(); List <byte> framebuffer = ScreenDevice.FrameBuff; List <float> zbuffer = ScreenDevice.DepthBuff; int x = (int)scanline.x; int y = (int)scanline.y; int scanlineWidth = (int)scanline.width; int ScreenWidth = ScreenDevice.ScreenWidth; int ScreenHeight = ScreenDevice.ScreenHeight; for (; scanlineWidth > 0; x++, scanlineWidth--) { if (x >= 0 && x < ScreenWidth) { float rhw = scanline.v.rhw; if (rhw >= zbuffer[x + y * ScreenWidth]) { float w = 1.0f / rhw; zbuffer[x + y * ScreenWidth] = rhw; float u = scanline.v.uv.u * w; float v = scanline.v.uv.v * w; v2f fragIn = new v2f(); fragIn.uv.u = u; fragIn.uv.v = v; int color = frag(fragIn); //片元着色器 byte r = (byte)0x0, g = (byte)0x0, b = (byte)0x0, a = (byte)0x0; MathHelper.GetRGBAFromInt(color, ref r, ref g, ref b, ref a); framebuffer[(x + y * ScreenWidth) * 4] = r; framebuffer[(x + y * ScreenWidth) * 4 + 1] = g; framebuffer[(x + y * ScreenWidth) * 4 + 2] = b; framebuffer[(x + y * ScreenWidth) * 4 + 3] = a; } } scanline.v = scanline.v.Add(scanline.step); if (x >= ScreenWidth) { break; } } }
public override v2f Execute() { v2f v2f = __init_v2f(); v2f.SV_POSITION = mul(MATRIX_MVP, appdata.vertex); v2f.uv = appdata.uv; // Declares 3x3 matrix 'rotation', filled with tangent space basis float3 worldNormal = ObjectToWorldNormal(normalize(appdata.normal)); float3 worldTangent = ObjectToWorldDir(appdata.tangent.xyz); float3 worldBinormal = cross(v2f.worldNormal, worldTangent) * appdata.tangent.w; //创建切线空间 v2f.tSpace0 = float3(worldTangent.x, worldBinormal.x, worldNormal.x); v2f.tSpace1 = float3(worldTangent.y, worldBinormal.y, worldNormal.y); v2f.tSpace2 = float3(worldTangent.z, worldBinormal.z, worldNormal.z); return v2f; }
protected override float4 Execute(v2f IN) { return(GetNormalByGray(IN.uv.xy)); //float3 lightdir = normalize(float3(-5, 5, 8)); ////从法线贴图中取出法线 //float3 normal = GetNormalByGray(IN.uv.xy) * 2-1; ////将法线从切线空间旋转到世界空间 Rotate normals from tangent space to world space //float3 worldNorm = float3(1,1,1); //worldNorm.x = dot(IN.tSpace0, normal); //worldNorm.y = dot(IN.tSpace1, normal); //worldNorm.z = dot(IN.tSpace2, normal); ////AddDebugInfo(worldNorm, "normal", MiniRender.debugger.DebugInfoType.Vector, float3(0, 0, 1)); ////AddDebugInfo(lightdir, "lightdir", MiniRender.debugger.DebugInfoType.Vector, float3(1, 1, 0)); ////AddDebugInfo(IN.worldNormal, "normal", MiniRender.debugger.DebugInfoType.Vector, float3(0, 0, 1)); ////float4 tex = tex2D(0, IN.uv); //float4 texColor = float4(1, 1, 1, 1); //float diffuse = clamp(dot(worldNorm, lightdir ), 0, 1); //float4 color = texColor; //color.rgb = texColor.rgb * diffuse; //return color; //return color; //return tex; }
Color FragShader(v2f v) { float r = 0f, g = 0f, b = 0f; float dis = 0f; float atten = 0f; Vector3 lightDir; foreach (var light in lights) { switch (light.type) { case LightType.Directional: lightDir = -light.transform.forward; atten = Vector3.Dot(Vector3.Normalize(lightDir), Vector3.Normalize(v.normal)); atten = Mathf.Max(0, atten); r += light.color.r * atten; g += light.color.g * atten; b += light.color.b * atten; break; case LightType.Point: dis = Vector3.Distance(light.transform.position, v.postion); if (dis > light.range) { continue; } atten = dotLightAtten[(int)(dis / light.range), 2].r; lightDir = light.transform.position - v.postion; atten *= Vector3.Dot(Vector3.Normalize(lightDir), Vector3.Normalize(v.normal)); atten *= light.intensity; r += light.color.r * atten; g += light.color.g * atten; b += light.color.b * atten; break; } } return(new Color(r, g, b)); }
public override PointPlace GetPointPlace(v2f _point) { // небольшой хак: если сумма всех 4 углов, образованных 2 соседними вершинами прямоугольника и точкой _point // равна 360 градусов - точка находится внутри прямоугольника, иначе - снаружи // ВНИМАНИЕ - хак работает ТОЛЬКО при нахождении углов через скалярное произведение, // если искать иным путём, будет находиться развёрнутый угол, а не его противоположность // если один из углов равен 180 градусам - точка лежит на линии прямоугольника Vector2 pa = (Vector2)(a - _point), pb = (Vector2)(b - _point), pc = (Vector2)(c - _point), pd = (Vector2)(d - _point); float anglePA = Vector2.Angle(pa, pb), anglePB = Vector2.Angle(pb, pc), anglePC = Vector2.Angle(pc, pd), anglePD = Vector2.Angle(pd, pa); if ( anglePA.Equal(180) || anglePB.Equal(180) || anglePC.Equal(180) || anglePD.Equal(180) ) { return(PointPlace.BORDER); } float angls = Vector2.Angle(pa, pb) + Vector2.Angle(pb, pc) + Vector2.Angle(pc, pd) + Vector2.Angle(pd, pa); return((angls.Equal(180)) ? PointPlace.INSET : PointPlace.OUTSET); }
public override PointPlace GetPointPlace(v2f _point) { List <v2f> pointsPath = Path.Points; List <SegmentLine> segmentsPath = new List <SegmentLine>(pointsPath.Count); for (int i = 1; i < pointsPath.Count; i++) { segmentsPath[i - 1] = SegmentLine.From2Points(pointsPath[i], pointsPath[i - 1]); } segmentsPath[pointsPath.Count - 1] = SegmentLine.From2Points(pointsPath[0], pointsPath.Last()); // надо замутить контрольный тест когда луч из точки проходит через точку многоугольника, тогда по идее эта точка должна принадлежать двум его отрезкам // луч берём параллельный оси x в сторону +бесконечности LightWay dirrection = new LightWay(_point, new v2f(1, 0)); int countCollisions = 0; foreach (var segmentPath in segmentsPath) { v2f collision = dirrection.GetCollision(segmentPath); if (collision == null) { continue; } countCollisions++; if (segmentPath.PointBelong(collision)) { return(PointPlace.BORDER); } } return((countCollisions % 2 > 0) ? PointPlace.INSET : PointPlace.OUTSET); }
protected override float4 Execute(v2f i) { //Blinn-Phong float3 lightDir = normalize(float3(0, 9, -4)); float3 normalDir = normalize(i.worldNormal); //计算高光 //计算反射光夹角 //fixed3 reflectDir = normalize(reflect(-lightDir,normalDir)); float3 viewDir = normalize(WorldSpaceViewDir(mul(_ObjectToWorld, i.objPos).xyz)); //平分线 float3 halfDir = normalize(viewDir + lightDir); //AddDebugInfo(lightDir, "lightDir", MiniRender.debugger.DebugInfoType.Vector, float3(1, 0, 0)); //AddDebugInfo(viewDir, "viewDir", MiniRender.debugger.DebugInfoType.Vector, float3(0, 1, 0)); //AddDebugInfo(halfDir, "halfDir", MiniRender.debugger.DebugInfoType.Vector, float3(1, 1, 0)); AddDebugInfo(normalDir, "normalDir", MiniRender.debugger.DebugInfoType.Vector, float3(0, 0, 1)); AddDebugInfo(i.worldTangent, "worldTangent", MiniRender.debugger.DebugInfoType.Vector, float3(1, 1, 0)); float _Gloss = 10; float3 _Specular = float3(1, 1, 1); float4 _LightColor0 = float4(1, 1, 1, 1); //控制高光颜色 float3 specular = _Specular * _LightColor0.rgb * pow(max(dot(normalDir, halfDir), 0), _Gloss); float diff = max(0, dot(normalDir, lightDir)); //fixed nh = max(0, dot(s.Normal, halfDir)); //fixed spec = pow(nh, s.Specular * 128) * s.Gloss; float4 c = float4(0.7, 0.7, 0.7, 0.9); c.rgb = c.rgb * _LightColor0.rgb * diff + specular; return(c); //float3 lightDir = normalize(float3(0, 4, -4)); //float3 normalDir = normalize( i.worldNormal); //float3 viewDir = normalize(WorldSpaceViewDir(mul(_ObjectToWorld, i.objPos).xyz)); //float3 halfDir = normalize(viewDir + lightDir); //float _Gloss = 20; //float _Specular = 3.0f;//float3(1, 1, 1); //float4 _LightColor0 = float4(1, 1, 1, 1); //float diff = max(0, dot(normalDir, lightDir)); //float nh = max(0, dot(normalDir, halfDir)); //float spec = pow(nh, _Specular * 128) * _Gloss; //float4 c=float4(1,1,1,1); //c.rgb = ( _LightColor0.rgb * diff + _LightColor0.rgb * spec) * 1; //return c; //return i.color; //float3 ni = normalize(i.normal); //float3 bi = normalize( cross(i.normal, i.tangent)); //float3 ti = normalize(cross(bi, i.normal)); //float3x3 tbn = float3x3(ti, bi, ni); //float3 n2 = mul(tbn, i.tangent); //return n2 * 0.5 + 0.5; //return i.normal * 0.5+0.5; }
public static float FloatRange(v2f _ab) { return(Random.Range(_ab.x, _ab.y)); }
public abstract PointPlace GetPointPlace(v2f _point);
public static v2f Lerp(v2f a, v2f b, float t) { return((v2f)Vector2.Lerp((Vector2)a, (Vector2)b, t)); }
void InitCheckUpdate() { oldWH = WidthHeight; }
protected virtual void DoResize(v2f _oldSize) { }
public float RandomeFloatRange(v2f _ab) => a_Root.RandomeFloatRange(_ab);