Skip to content

Commit

Permalink
Fix back ground draw with sprite origin
Browse files Browse the repository at this point in the history
  • Loading branch information
h8man committed May 19, 2021
1 parent 54313e7 commit cbff0ad
Show file tree
Hide file tree
Showing 16 changed files with 1,070 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/Temp
/bin
/obj
/Assets/Plugins/TextMesh Pro
30 changes: 17 additions & 13 deletions Assets/Codebehind/HQ/HqRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class HqRenderer : MonoBehaviour
public SpriteRenderer BG;
public SpriteRenderer Plane;
public SpriteRenderer FG;
public Sprite BGSprite;
public int PPU;

public TrackObject track;
Expand All @@ -29,6 +30,7 @@ public class HqRenderer : MonoBehaviour
public int DravingDistance = 300; //segments
public int cameraHeight = 1500; //pixels?
public float centrifugal = 0.1f;
public float paralaxSpeed = 0.1f;
public bool drawRoad;
public bool drawSprites;
public int rumbleWidth;
Expand Down Expand Up @@ -65,6 +67,8 @@ public class HqRenderer : MonoBehaviour
private RenderTexture _renderTexture;
[NonSerialized]
private int speed;
[NonSerialized]
private Vector2 bgOffset;

private void OnEnable()
{
Expand Down Expand Up @@ -94,6 +98,11 @@ void Awake()
tex2.filterMode = FilterMode.Point;
Plane.sprite = Sprite.Create(tex2, new Rect(0, 0, screenWidthRef, screenHeightRef), new Vector2(0.5f, 0.5f), PPU);
Plane.sprite.name = "runtimePlane";

Texture2D tex3 = new Texture2D(BGSprite.texture.width, BGSprite.texture.height, TextureFormat.RGBA32, false);
tex2.filterMode = FilterMode.Point;
BG.sprite = Sprite.Create(tex3, BGSprite.rect, new Vector2(0.5f, 0.5f), PPU);
BG.sprite.name = "runtimeBG";

quad = new Quad[] { new Quad(), new Quad(), new Quad(), new Quad(), new Quad(), new Quad(), new Quad() };
combined = new Mesh[] { new Mesh(), new Mesh(),new Mesh(),new Mesh(),new Mesh(),new Mesh(),new Mesh(),new Mesh(),new Mesh()};
Expand Down Expand Up @@ -134,9 +143,9 @@ public void drawSprite(ref Line line)
Rect source = new Rect(Vector2Int.zero, new Vector2(1, 1 - clipH / destH));
Renderer.draw(source, s, target);
}
private void addQuad(Material c, float x1, float y1, float w1, float x2, float y2, float w2)
private void addQuad(Material c, float x1, float y1, float w1, float x2, float y2, float w2, float z)
{
dictionary[c].SetQuad(x1 / PPU, y1 / PPU, w1 / PPU, x2 / PPU, y2 / PPU, w2 / PPU);
dictionary[c].SetQuad(x1 / PPU, y1 / PPU, w1 / PPU, x2 / PPU, y2 / PPU, w2 / PPU, z);
}

private void DrawObjects()
Expand Down Expand Up @@ -171,12 +180,6 @@ private void DrawRoad()
Graphics.SetRenderTarget(_renderTexture);
GL.Clear(false, true, new Color(0.0f, 0.0f, 0, 0));
GL.PushMatrix();
//GL.LoadOrtho();
//var proj = Camera.main.projectionMatrix;
////// If Camera.current is set, multiply our matrix by the inverse of its view matrix
//if (Camera.current != null) proj = proj * Camera.current.worldToCameraMatrix.inverse;
////// Use instead of LoadOrtho
//GL.LoadProjectionMatrix(proj);
float refH = targetCamera.orthographicSize * PPU * 2;
float refHScale = refH / screenHeightRef;
float HScale = ((float)screenHeightRef) / targetCamera.pixelHeight;
Expand All @@ -198,7 +201,7 @@ private void DrawRoad()

private void DrawBackground()
{
if (speed != 0 && track.lines[playerPos].curve != 0)
//if (speed != 0 && track.lines[playerPos].curve != 0)
{
//doesnt work with sprite renderer, while still posted as sollution
//BG.material.mainTextureOffset += new Vector2(Mathf.Sign(speed) * track.lines[startPos].curve * Time.deltaTime * paralaxSpeed, 0);
Expand All @@ -207,15 +210,16 @@ private void DrawBackground()
//BG.transform.localPosition += new Vector3(Mathf.Sign(speed) * track.lines[startPos].curve / PPU, 0);

//Good enough
_renderTexture = RenderTexture.GetTemporary(BG.sprite.texture.width, BG.sprite.texture.height);
_renderTexture = RenderTexture.GetTemporary(BG.sprite.texture.width, BG.sprite.texture.height, 0, BG.sprite.texture.graphicsFormat);
RenderTexture currentActiveRT = RenderTexture.active;
RenderTexture.active = _renderTexture;
//Work in the pixel matrix of the texture resolution.
GL.PushMatrix();
GL.LoadPixelMatrix(0, screenWidthRef, screenHeightRef, 0);

Vector2 offset = new Vector2(0.1f * Time.deltaTime * Mathf.Sign(speed) * track.lines[playerPos].curve, 0);
Graphics.Blit(BG.sprite.texture, _renderTexture, Vector2.one, offset, 0, 0);
bgOffset += new Vector2(paralaxSpeed/PPU * speed * Time.deltaTime * track.lines[playerPos].curve, 0);

Graphics.Blit(BGSprite.texture, _renderTexture, Vector2.one, bgOffset, 0, 0);

Graphics.CopyTexture(_renderTexture, BG.sprite.texture);

Expand Down Expand Up @@ -321,7 +325,7 @@ void CalculateProjection()

if ((n / 3) % 2 == 0)
{
addQuad(dashline, p.X, p.Y * 1.1f, p.W * 0.05f, l.X, l.Y * 1.1f, l.W * 0.05f);
addQuad(dashline, p.X, p.Y * 1.1f, p.W * 0.05f, l.X, l.Y * 1.1f, l.W * 0.05f, z);
}

counter++;
Expand Down
6 changes: 5 additions & 1 deletion Assets/Materials/HQ/Grass1.mat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Material:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Grass1
m_Shader: {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down Expand Up @@ -69,6 +69,7 @@ Material:
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _LineWidth: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
Expand All @@ -82,4 +83,7 @@ Material:
- _Color: {r: 0.1482734, g: 0.26415092, b: 0.15406726, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _Flip: {r: 1, g: 1, b: 1, a: 1}
- _GridColor: {r: 0, g: 0, b: 0, a: 1}
- _LineColor: {r: 1, g: 0, b: 1, a: 1}
- _RendererColor: {r: 1, g: 1, b: 1, a: 1}
- _Tint: {r: 0.024919916, g: 0.754717, b: 0.05640279, a: 1}
6 changes: 5 additions & 1 deletion Assets/Materials/HQ/Grass2.mat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Material:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Grass2
m_Shader: {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down Expand Up @@ -69,6 +69,7 @@ Material:
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _LineWidth: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
Expand All @@ -82,4 +83,7 @@ Material:
- _Color: {r: 0.08090067, g: 0.16981131, b: 0.08528051, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _Flip: {r: 1, g: 1, b: 1, a: 1}
- _GridColor: {r: 0, g: 0, b: 0, a: 1}
- _LineColor: {r: 1, g: 0, b: 1, a: 1}
- _RendererColor: {r: 1, g: 1, b: 1, a: 1}
- _Tint: {r: 0.009344969, g: 0.2830189, b: 0.02051226, a: 1}
6 changes: 5 additions & 1 deletion Assets/Materials/HQ/Road1.mat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Material:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Road1
m_Shader: {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down Expand Up @@ -69,6 +69,7 @@ Material:
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _LineWidth: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
Expand All @@ -82,4 +83,7 @@ Material:
- _Color: {r: 0.18867922, g: 0.18867922, b: 0.18867922, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _Flip: {r: 1, g: 1, b: 1, a: 1}
- _GridColor: {r: 0, g: 0, b: 0, a: 1}
- _LineColor: {r: 1, g: 0, b: 1, a: 1}
- _RendererColor: {r: 1, g: 1, b: 1, a: 1}
- _Tint: {r: 0.3301887, g: 0.32240123, b: 0.32240123, a: 1}
6 changes: 5 additions & 1 deletion Assets/Materials/HQ/Road2.mat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Material:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Road2
m_Shader: {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down Expand Up @@ -69,6 +69,7 @@ Material:
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _LineWidth: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
Expand All @@ -82,4 +83,7 @@ Material:
- _Color: {r: 0.3773585, g: 0.3773585, b: 0.3773585, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _Flip: {r: 1, g: 1, b: 1, a: 1}
- _GridColor: {r: 0, g: 0, b: 0, a: 1}
- _LineColor: {r: 1, g: 0, b: 1, a: 1}
- _RendererColor: {r: 1, g: 1, b: 1, a: 1}
- _Tint: {r: 0.3679245, g: 0.35230508, b: 0.35230508, a: 1}
6 changes: 5 additions & 1 deletion Assets/Materials/HQ/Rumble1.mat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Material:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Rumble1
m_Shader: {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down Expand Up @@ -69,6 +69,7 @@ Material:
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _LineWidth: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
Expand All @@ -82,4 +83,7 @@ Material:
- _Color: {r: 0.8962264, g: 0.8962264, b: 0.8962264, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _Flip: {r: 1, g: 1, b: 1, a: 1}
- _GridColor: {r: 0, g: 0, b: 0, a: 1}
- _LineColor: {r: 1, g: 0, b: 1, a: 1}
- _RendererColor: {r: 1, g: 1, b: 1, a: 1}
- _Tint: {r: 1, g: 1, b: 1, a: 1}
6 changes: 5 additions & 1 deletion Assets/Materials/HQ/Rumble2.mat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Material:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Rumble2
m_Shader: {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down Expand Up @@ -69,6 +69,7 @@ Material:
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _LineWidth: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
Expand All @@ -82,4 +83,7 @@ Material:
- _Color: {r: 0.7924528, g: 0, b: 0, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _Flip: {r: 1, g: 1, b: 1, a: 1}
- _GridColor: {r: 0, g: 0, b: 0, a: 1}
- _LineColor: {r: 1, g: 0, b: 1, a: 1}
- _RendererColor: {r: 1, g: 1, b: 1, a: 1}
- _Tint: {r: 0.7830189, g: 0, b: 0, a: 1}
6 changes: 5 additions & 1 deletion Assets/Materials/HQ/Rumble3.mat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Material:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Rumble3
m_Shader: {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down Expand Up @@ -69,6 +69,7 @@ Material:
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _LineWidth: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
Expand All @@ -82,4 +83,7 @@ Material:
- _Color: {r: 0.8962264, g: 0.8962264, b: 0.8962264, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _Flip: {r: 1, g: 1, b: 1, a: 1}
- _GridColor: {r: 0, g: 0, b: 0, a: 1}
- _LineColor: {r: 1, g: 0, b: 1, a: 1}
- _RendererColor: {r: 1, g: 1, b: 1, a: 1}
- _Tint: {r: 1, g: 1, b: 1, a: 1}
26 changes: 0 additions & 26 deletions Assets/Prefab/New Track.asset

This file was deleted.

879 changes: 819 additions & 60 deletions Assets/Scenes/HQ.unity

Large diffs are not rendered by default.

Binary file added Assets/Sprites/h8man2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cbff0ad

Please sign in to comment.