Skip to content

Commit d1618f2

Browse files
committed
Merge branch 'master' into skia-text
2 parents 03b31ba + 925ef2d commit d1618f2

File tree

4 files changed

+158
-5
lines changed

4 files changed

+158
-5
lines changed

Assets/Scripts/OpenTS2/Scenes/NeighborhoodDecorations.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ private void RenderRoad(RoadDecoration road)
104104

105105
roadMesh.SetUVs(0, uvs);
106106
// TODO - Make this smoother, maybe generate a low res normal map from the terrain height map or calculate normals ourselves.
107-
roadMesh.RecalculateNormals();
107+
// roadMesh.RecalculateNormals();
108+
roadMesh.SetNormals(new[] { Vector3.up, Vector3.up, Vector3.up, Vector3.up });
108109
roadObject.GetComponent<MeshFilter>().sharedMesh = roadMesh;
109110

110111
if (!_roadMaterialLookup.TryGetValue(roadTextureName, out Material roadMaterial))

Assets/Scripts/OpenTS2/Scenes/ParticleEffects/SwarmDecal.cs

+84-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Text;
1212
using System.Threading.Tasks;
1313
using UnityEngine;
14+
using static UnityEditor.FilePathAttribute;
1415

1516
namespace OpenTS2.Scenes.ParticleEffects
1617
{
@@ -22,6 +23,7 @@ public class SwarmDecal : MonoBehaviour
2223
private Vector3 _position;
2324
private Vector3 _rotation;
2425
private Material _material;
26+
private Mesh _decalMesh;
2527

2628
private void Start()
2729
{
@@ -47,12 +49,11 @@ public void Initialize()
4749
var meshFilter = GetComponent<MeshFilter>();
4850
var meshRenderer = GetComponent<MeshRenderer>();
4951

50-
meshFilter.sharedMesh = terrainMeshFilter.sharedMesh;
52+
BuildDecalMesh(terrainMeshFilter.sharedMesh);
53+
meshFilter.sharedMesh = _decalMesh;
5154

52-
_material = new Material(Shader.Find("OpenTS2/NeighborhoodDecal"));
55+
_material = new Material(Shader.Find("OpenTS2/BakedDecal"));
5356
_material.mainTexture = _textureAsset.GetSelectedImageAsUnityTexture();
54-
_material.SetVector(Location, _position);
55-
_material.SetVector(Rotation, _rotation);
5657

5758
meshRenderer.sharedMaterial = _material;
5859
transform.SetParent(null);
@@ -61,10 +62,89 @@ public void Initialize()
6162
transform.localScale = Vector3.one;
6263
}
6364

65+
private Vector2 RotateUV(Vector2 uv, float angle)
66+
{
67+
var cos = Mathf.Cos(angle);
68+
var sin = Mathf.Sin(angle);
69+
var x = uv.x * cos - uv.y * sin;
70+
var y = uv.x * sin + uv.y * cos;
71+
return new Vector2(x, y);
72+
}
73+
74+
private Vector2 GetUV(Vector3 point)
75+
{
76+
var radius = 80f;
77+
var radiusHalf = radius * 0.5f;
78+
var minPos = _position;
79+
minPos -= new Vector3(radiusHalf, radiusHalf, radiusHalf);
80+
var maxPos = minPos + new Vector3(radius, radius, radius);
81+
82+
point -= minPos;
83+
point /= radius;
84+
85+
var uv = new Vector2(point.x - 0.5f, point.z - 0.5f);
86+
uv = RotateUV(GetUV(uv), _rotation.y * Mathf.Deg2Rad);
87+
uv += new Vector2(0.5f, 0.5f);
88+
return uv;
89+
}
90+
private void BuildDecalMesh(Mesh terrainMesh)
91+
{
92+
_decalMesh = new Mesh();
93+
var vertices = terrainMesh.vertices;
94+
var uvs = new Vector2[terrainMesh.vertices.Length];
95+
96+
var oTriangles = terrainMesh.triangles;
97+
var triangles = new List<int>();
98+
99+
var radius = 80f;
100+
var radiusHalf = radius * 0.5f;
101+
var decalPositionMatrix = Matrix4x4.Translate(new Vector3(-_position.x + radiusHalf, 0f, -_position.z + radiusHalf));
102+
var decalRotationMatrix = Matrix4x4.Rotate(Quaternion.Euler(0f, _rotation.y, 0f));
103+
104+
for(var i = 0; i < vertices.Length; i++)
105+
{
106+
var v = decalPositionMatrix.MultiplyPoint(vertices[i]);
107+
v /= radius;
108+
v.x -= 0.5f;
109+
v.z -= 0.5f;
110+
v = decalRotationMatrix.MultiplyPoint(v);
111+
v.x += 0.5f;
112+
v.z += 0.5f;
113+
114+
uvs[i] = new Vector2(v.x, v.z);
115+
}
116+
117+
for (var i = 0; i < oTriangles.Length; i += 3)
118+
{
119+
var v1i = oTriangles[i];
120+
var v2i = oTriangles[i + 1];
121+
var v3i = oTriangles[i + 2];
122+
123+
var v1 = uvs[v1i];
124+
var v2 = uvs[v2i];
125+
var v3 = uvs[v3i];
126+
127+
if ((v1.x < 1f && v1.y < 1f && v1.x > 0f && v1.y > 0f) ||
128+
(v2.x < 1f && v2.y < 1f && v2.x > 0f && v2.y > 0f) ||
129+
(v3.x < 1f && v3.y < 1f && v3.x > 0f && v3.y > 0f))
130+
{
131+
triangles.Add(v1i);
132+
triangles.Add(v2i);
133+
triangles.Add(v3i);
134+
}
135+
}
136+
137+
_decalMesh.SetVertices(vertices);
138+
_decalMesh.SetUVs(0, uvs);
139+
_decalMesh.SetTriangles(triangles, 0);
140+
}
141+
64142
private void OnDestroy()
65143
{
66144
if (_material != null)
67145
_material.Free();
146+
if (_decalMesh != null)
147+
_decalMesh.Free();
68148
}
69149
}
70150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
2+
3+
Shader "OpenTS2/BakedDecal"
4+
{
5+
Properties
6+
{
7+
_MainTex ("Texture", 2D) = "white" {}
8+
}
9+
SubShader
10+
{
11+
Tags { "RenderType" = "Transparent" "Queue" = "Transparent-2" }
12+
LOD 100
13+
14+
Pass
15+
{
16+
ZWrite Off
17+
Offset -1, -1
18+
Blend SrcAlpha OneMinusSrcAlpha
19+
CGPROGRAM
20+
#pragma vertex vert
21+
#pragma fragment frag
22+
// make fog work
23+
#pragma multi_compile_fog
24+
25+
#include "UnityCG.cginc"
26+
27+
struct appdata
28+
{
29+
float4 vertex : POSITION;
30+
float2 uv : TEXCOORD0;
31+
};
32+
33+
struct v2f
34+
{
35+
float4 vertex : SV_POSITION;
36+
float2 uv : TEXCOORD0;
37+
};
38+
39+
sampler2D _MainTex;
40+
float4 _MainTex_ST;
41+
42+
v2f vert (appdata v)
43+
{
44+
v2f o;
45+
o.uv = v.uv;
46+
o.vertex = UnityObjectToClipPos(v.vertex);
47+
return o;
48+
}
49+
50+
fixed4 frag(v2f i) : SV_Target
51+
{
52+
53+
if (i.uv.x < 0 || i.uv.y < 0 || i.uv.x > 1 || i.uv.y > 1)
54+
clip(-1);
55+
// sample the texture
56+
fixed4 col = tex2D(_MainTex, i.uv);
57+
return col;
58+
}
59+
ENDCG
60+
}
61+
}
62+
}

Assets/Shaders/Neighborhood/BakedDecal.shader.meta

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)