private void DrawDirLine(IRenderTarget currentRenderBuffer, float linelength, float4 worldpos, float3 dir, float4 color) { float4 sv1; float4 sv2; calDirectionPos(worldpos, dir, linelength, out sv1, out sv2); Rasterizer.Line(currentRenderBuffer, sv1.xy / sv1.w, sv2.xy / sv2.w, color); }
public void drawTriangles(IndexBuffer3D indexBuffer, int firstIndex = 0, int numTriangles = -1) { if (currentVertexBuffer == null) { throw new InvalidOperationException("VertexBuffer无效"); } if (current_program3D == null) { throw new InvalidOperationException("尚未设置着色器"); } if (!current_program3D.Validate()) { throw new InvalidOperationException("着色器校验失败"); } if (numTriangles == -1) { numTriangles = (indexBuffer.data.Count - firstIndex) / 3; } if (rasters == null || rasters.Length < currentRenderTarget.renderTarget.rt_width * currentRenderTarget.renderTarget.rt_height) { rasters = new Rasterizer.Raster[currentRenderTarget.renderTarget.rt_width * currentRenderTarget.renderTarget.rt_height]; for (int i = 0; i < rasters.Length; i++) { rasters[i] = new Rasterizer.Raster(); } } current_program3D.vertexShader.constants = programConstants; current_program3D.fragementShader.constants = programConstants; v2f[] v2fs = new v2f[currentVertexBuffer.vertices.Count]; #region 计算所有顶点 var vs = current_program3D.vertexShader; for (int i = 0; i < v2fs.Length; i++) { vs.appdata = currentVertexBuffer.vertices[i]; v2fs[i] = vs.Execute(); } #endregion for (int i = 0; i < numTriangles; i++) { if (firstIndex + i * 3 > indexBuffer.data.Count - 3) { throw new ArgumentException("index数量不足"); } uint idx1 = indexBuffer.data[firstIndex + i * 3]; uint idx2 = indexBuffer.data[firstIndex + i * 3 + 1]; uint idx3 = indexBuffer.data[firstIndex + i * 3 + 2]; #region 顶点着色器运算 v2f v1; v2f v2; v2f v3; //var vs = current_program3D.vertexShader; { //vs.appdata = currentVertexBuffer.vertices[(int)idx1]; //v1=vs.Execute(); v1 = v2fs[(int)idx1]; } { //vs.appdata = currentVertexBuffer.vertices[(int)idx2]; //v2=vs.Execute(); v2 = v2fs[(int)idx2]; } { //vs.appdata = currentVertexBuffer.vertices[(int)idx3]; //v3=vs.Execute(); v3 = v2fs[(int)idx3]; } #endregion #region clip and cull { float3 xyz1 = v1.SV_POSITION.xyz / v1.SV_POSITION.w; float3 xyz2 = v2.SV_POSITION.xyz / v2.SV_POSITION.w; float3 xyz3 = v3.SV_POSITION.xyz / v3.SV_POSITION.w; #region 视景体外裁剪 { if ((xyz1.z > 1 || xyz1.z < 0 || xyz1.x < -1 || xyz1.x > 1 || xyz1.y < -1 || xyz1.y > 1 || double.IsNaN(xyz1.x) || double.IsNaN(xyz1.y) || double.IsNaN(xyz1.z)) && (xyz2.z > 1 || xyz2.z < 0 || xyz2.x < -1 || xyz2.x > 1 || xyz2.y < -1 || xyz2.y > 1 || double.IsNaN(xyz2.x) || double.IsNaN(xyz2.y) || double.IsNaN(xyz2.z)) && (xyz3.z > 1 || xyz3.z < 0 || xyz3.x < -1 || xyz3.x > 1 || xyz3.y < -1 || xyz3.y > 1 || double.IsNaN(xyz3.x) || double.IsNaN(xyz3.y) || double.IsNaN(xyz3.z)) ) { continue; } } #endregion //背面剔除 { float area = Rasterizer.TriangleDoubleArea(xyz1.xy, xyz2.xy, xyz3.xy); switch (_culling) { case Context3DTriangleFace.BACK: if (area < 0) { continue; } break; case Context3DTriangleFace.FRONT: if (area > 0) { continue; } break; case Context3DTriangleFace.FRONT_AND_BACK: continue; case Context3DTriangleFace.NONE: break; default: continue; } } } #endregion int totalraises; Rasterizer.Triangle(currentRenderTarget.renderTarget, v1, v2, v3 , rasters , out totalraises ); var pixels = currentRenderTarget.renderTarget.getRealPixels(); for (int j = 0; j < totalraises; j++) { var r = rasters[j]; if (r.vsout.SV_POSITION.z < -1.192092896e-07F || r.vsout.SV_POSITION.z > r.vsout.SV_POSITION.w || double.IsNaN(r.vsout.SV_POSITION.z)) { //在远近裁剪面外,去掉 continue; } r.isclippass = true; } var zBuffer = currentRenderTarget.zBuffer; var currentRenderBuffer = currentRenderTarget.renderTarget; for (int j = 0; j < totalraises; j += 4) { var r0 = rasters[j]; var r1 = rasters[j + 1]; var r2 = rasters[j + 2]; var r3 = rasters[j + 3]; quadFragementUnit.setQuadUnit(this, current_program3D.fragementShader, r0.vsout, r1.vsout, r2.vsout, r3.vsout); for (int k = 0; k < 4; k++) { var r = rasters[j + k]; if (r.isclippass && r.rasterize) { var unit = quadFragementUnit[k]; debugger.FrameDebugData debugData = null; if (DebugBuffer != null && current_program3D.fragementShader.HasDebug) { debugData = DebugBuffer.buffer[r.x][r.y]; debugData.inputdata = unit.input; debugData.i = r.x; debugData.j = r.y; debugData.Ready(); } float4 oc = pixels[r.x][r.y]; current_program3D.fragementShader.Run(unit, debugData); float4 color = unit.output; color.a = Mathf.clamp01(color.a); if (unit.isdiscard) { continue; } #region 深度检测处理 { //float depth = r.vsout.SV_POSITION.z / r.vsout.SV_POSITION.w; ushort depth = zBuffer.Convert(r.vsout.SV_POSITION.z / r.vsout.SV_POSITION.w); //读出zBuffer //var buffer = zBuffer.buffer; var di = (int)(r.x * 1.0f * zBuffer.width / currentRenderBuffer.rt_width); var dj = (int)(r.y * 1.0f * zBuffer.height / currentRenderBuffer.rt_height); //float depth_buffer = buffer[di][dj]; ushort depth_buffer = zBuffer[di, dj]; #region 深度检测 switch (_depthtest_passCompareMode) { case Context3DCompareMode.ALWAYS: break; case Context3DCompareMode.EQUAL: if (!(depth == depth_buffer)) { continue; } break; case Context3DCompareMode.GREATER: if (!(depth > depth_buffer)) { continue; } break; case Context3DCompareMode.GREATER_EQUAL: if (!(depth >= depth_buffer)) { continue; } break; case Context3DCompareMode.LESS: if (!(depth < depth_buffer)) { continue; } break; case Context3DCompareMode.LESS_EQUAL: if (!(depth <= depth_buffer)) { continue; } break; case Context3DCompareMode.NEVER: //永远测试不通过 continue; case Context3DCompareMode.NOT_EQUAL: if (!(depth != depth_buffer)) { continue; } break; default: continue; } #endregion //深度测试通过 if (_depthtest_depthMask) //需要写深度 { //buffer[di][dj] = depth; zBuffer.WriteDepth(di, dj, depth); } } #endregion //***Blend*** float4 final = oc * (1 - color.a) + color * color.a; pixels[r.x][r.y] = final; } } } #region 线框 //var linepos1 = v1.SV_POSITION / v1.SV_POSITION.w; //var linepos2 = v2.SV_POSITION / v2.SV_POSITION.w; //var linepos3 = v3.SV_POSITION / v3.SV_POSITION.w; //Rasterizer.Line(currentRenderBuffer, // linepos1.xy, linepos2.xy, new float4(0, 0, 0, 1)); //Rasterizer.Line(currentRenderBuffer, // linepos2.xy, linepos3.xy, new float4(0, 0, 0, 1)); //Rasterizer.Line(currentRenderBuffer, // linepos3.xy, linepos1.xy, new float4(0, 0, 0, 1)); #endregion #region 显示经着色器计算后的法线和切线 { //DrawDirLine(currentRenderBuffer, 0.2f, v1.worldPos, v1.worldNormal, new float4(0, 0, 1, 1)); //DrawDirLine(currentRenderBuffer, 0.2f, v1.worldPos, v1.worldTangent, new float4(1, 1, 0, 1)); } #endregion } }