提交 3815b57d 编写于 作者: H happyfire

+ FPS Display

- Optimize for Test in triangle
上级 4b4b101f
......@@ -37,6 +37,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Assets\URasterizer\Codes\HandmakeMesh.cs" />
<Compile Include="Assets\URasterizer\Codes\ProfileManager.cs" />
<Compile Include="Assets\URasterizer\Codes\FragmentShader.cs" />
<Compile Include="Assets\URasterizer\Codes\RenderingObject.cs" />
<Compile Include="Assets\URasterizer\Codes\VertexColors.cs" />
......@@ -46,7 +47,7 @@
<Compile Include="Assets\URasterizer\Codes\Triangle.cs" />
<Compile Include="Assets\URasterizer\Codes\Rasterizer.cs" />
<Compile Include="Assets\URasterizer\Codes\TransformTool.cs" />
<Compile Include="Assets\URasterizer\Codes\ProfileManager.cs" />
<Compile Include="Assets\URasterizer\Codes\FPSDisplay.cs" />
<None Include="Assets\URasterizer\DefaultShader.shader" />
<None Include="Assets\URasterizer\URComputeShader.compute" />
<Reference Include="UnityEngine">
......
......@@ -324,6 +324,7 @@ GameObject:
- component: {fileID: 264331599}
- component: {fileID: 264331601}
- component: {fileID: 264331598}
- component: {fileID: 264331602}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
......@@ -417,6 +418,21 @@ MonoBehaviour:
rawImg: {fileID: 129304943}
_mainLight: {fileID: 1933767905}
_config: {fileID: 11400000, guid: 18e833324d51d904aa0c708c8c56856c, type: 2}
--- !u!114 &264331602
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 264331597}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 64bca05a011e9854ca73cf4ae054dad6, type: 3}
m_Name:
m_EditorClassIdentifier:
SampleTime: 0.5
FontSize: 20
TextColor: {r: 0, g: 1, b: 0.33849406, a: 1}
--- !u!1 &666679034
GameObject:
m_ObjectHideFlags: 0
......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FPSDisplay : MonoBehaviour
{
public float SampleTime = 1f;
public int FontSize = 20;
public Color TextColor = Color.white;
GUIStyle style;
int frameCount;
float timeTotal;
string textDisplay;
void Awake(){
style = new GUIStyle();
style.fontSize = FontSize;
style.normal.textColor = TextColor;
}
void Start()
{
frameCount = 0;
timeTotal = 0;
textDisplay = "";
}
void Update(){
++frameCount;
timeTotal += Time.unscaledDeltaTime;
if(timeTotal >= SampleTime){
float fps = frameCount / timeTotal;
textDisplay = $"FPS:{fps.ToString("F2")}";
frameCount = 0;
timeTotal = 0;
}
}
void OnGUI(){
GUI.Label(new Rect(10, 10, 200, 100), textDisplay, style);
}
}
fileFormatVersion: 2
guid: 64bca05a011e9854ca73cf4ae054dad6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -251,9 +251,9 @@ namespace URasterizer
outBuffer.GetData(vsOutput);
vertexBuffer.Dispose();
normalBuffer.Dispose();
outBuffer.Dispose();
vertexBuffer.Release();
normalBuffer.Release();
outBuffer.Release();
ProfileManager.EndSample();
}
......@@ -647,13 +647,16 @@ namespace URasterizer
{
for(int x = minPX; x < maxPX; ++x)
{
if(IsInsideTriangle(x, y, t))
//if(IsInsideTriangle(x, y, t)) //-->检测是否在三角形内比使用重心坐标检测要慢,因此先计算重心坐标,再检查3个坐标是否有小于0
{
//计算重心坐标
var c = ComputeBarycentric2D(x, y, t);
float alpha = c.x;
float beta = c.y;
float gamma = c.z;
if(alpha < 0 || beta < 0 || gamma < 0){
continue;
}
//透视校正插值,z为透视校正插值后的view space z值
float z = 1.0f / (alpha / v[0].w + beta / v[1].w + gamma / v[2].w);
//zp为透视校正插值后的screen space z值
......@@ -795,7 +798,7 @@ namespace URasterizer
float c1 = (x * (v[1].y - v[2].y) + (v[2].x - v[1].x) * y + v[1].x * v[2].y - v[2].x * v[1].y) / (v[0].x * (v[1].y - v[2].y) + (v[2].x - v[1].x) * v[0].y + v[1].x * v[2].y - v[2].x * v[1].y);
float c2 = (x * (v[2].y - v[0].y) + (v[0].x - v[2].x) * y + v[2].x * v[0].y - v[0].x * v[2].y) / (v[1].x * (v[2].y - v[0].y) + (v[0].x - v[2].x) * v[1].y + v[2].x * v[0].y - v[0].x * v[2].y);
float c3 = (x * (v[0].y - v[1].y) + (v[1].x - v[0].x) * y + v[0].x * v[1].y - v[1].x * v[0].y) / (v[2].x * (v[0].y - v[1].y) + (v[1].x - v[0].x) * v[2].y + v[0].x * v[1].y - v[1].x * v[0].y);
ProfileManager.EndSample();
return new Vector3(c1, c2, c3);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册