Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentBlockHighlight.cs
浏览该文件的文档.
1using Engine;
5
6namespace Game {
9
11
13
15
17
19
21
22 public int m_value;
23
25
27
28 public static int[] m_drawOrders = [1, 2000];
29
30 public Point3? NearbyEditableCell { get; set; }
31
32 public UpdateOrder UpdateOrder => UpdateOrder.BlockHighlight;
33
34 public int[] DrawOrders => m_drawOrders;
35
36 public virtual void Update(float dt) {
37 Camera activeCamera = m_componentPlayer.GameWidget.ActiveCamera;
38 Ray3 ray = new(activeCamera.ViewPosition, activeCamera.ViewDirection);
39 NearbyEditableCell = null;
40 m_highlightRaycastResult = m_componentPlayer.ComponentMiner.Raycast(ray, RaycastMode.Digging);
41 if (!(m_highlightRaycastResult is TerrainRaycastResult terrainRaycastResult)) {
42 return;
43 }
44 if (terrainRaycastResult.Distance < 3f) {
45 CellFace cellFace = terrainRaycastResult.CellFace;
46 Point3 point = cellFace.Point;
47 int cellValue = m_subsystemTerrain.Terrain.GetCellValue(point.X, point.Y, point.Z);
49 if (obj is CrossBlock) {
50 terrainRaycastResult.Distance = MathUtils.Max(terrainRaycastResult.Distance, 0.1f);
51 m_highlightRaycastResult = terrainRaycastResult;
52 }
53 if (obj.IsEditable_(cellValue)) {
54 NearbyEditableCell = cellFace.Point;
55 }
56 }
57#if DEBUG
58 if (m_componentPlayer.GameWidget.GameWidgetIndex == 0) {
59 CellFace cellFace = terrainRaycastResult.CellFace;
60 int cellValue = m_subsystemTerrain.Terrain.GetCellValue(cellFace.X, cellFace.Y, cellFace.Z);
61 PerformanceManager.AddExtraStat($"Block Value: {cellValue}, CellFace: ({cellFace.X},{cellFace.Y},{cellFace.Z},{cellFace.Face}), Distance: {terrainRaycastResult.Distance:F1}");
62 }
63#endif
64 }
65
66 public virtual void Draw(Camera camera, int drawOrder) {
67 if (camera.GameWidget.PlayerData == m_componentPlayer.PlayerData) {
68 if (drawOrder == m_drawOrders[0]) {
69 DrawFillHighlight(camera);
72 }
73 else {
74 DrawRayHighlight(camera);
75 }
76 }
77 }
78
79 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
80 m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
82 m_subsystemSky = Project.FindSubsystem<SubsystemSky>(true);
83 m_componentPlayer = Entity.FindComponent<ComponentPlayer>(true);
84 m_shader = new Shader(
85 ModsManager.GetInPakOrStorageFile<string>("Shaders/Highlight", "vsh"),
86 ModsManager.GetInPakOrStorageFile<string>("Shaders/Highlight", "psh"),
87 new ShaderMacro("ShadowShader")
88 );
89 }
90
91 public virtual void DrawRayHighlight(Camera camera) {
92 if (!camera.Eye.HasValue) {
93 return;
94 }
95 Ray3 ray;
96 float num;
97 if (m_highlightRaycastResult is TerrainRaycastResult terrainRaycastResult) {
98 ray = terrainRaycastResult.Ray;
99 num = MathUtils.Min(terrainRaycastResult.Distance, 2f);
100 }
101 else if (m_highlightRaycastResult is BodyRaycastResult bodyRaycastResult) {
102 ray = bodyRaycastResult.Ray;
103 num = MathUtils.Min(bodyRaycastResult.Distance, 2f);
104 }
105 else if (m_highlightRaycastResult is MovingBlocksRaycastResult movingBlocksRaycastResult) {
106 ray = movingBlocksRaycastResult.Ray;
107 num = MathUtils.Min(movingBlocksRaycastResult.Distance, 2f);
108 }
109 else {
110 if (!(m_highlightRaycastResult is Ray3 ray3)) {
111 return;
112 }
113 ray = ray3;
114 num = 2f;
115 }
116 Color color = Color.White * 0.5f;
117 Color color2 = Color.Lerp(color, Color.Transparent, MathUtils.Saturate(num / 2f));
118 FlatBatch3D flatBatch3D = m_primitivesRenderer3D.FlatBatch();
119 flatBatch3D.QueueLine(ray.Position, ray.Position + ray.Direction * num, color, color2);
120 flatBatch3D.Flush(camera.ViewProjectionMatrix);
121 }
122
123 public virtual void DrawReticleHighlight(Camera camera) {
124 if (camera.Eye != null) {
126 return;
127 }
128 Vector3 vector = result.HitPoint();
129 Vector3 vector2;
130 if (BlocksManager.Blocks[Terrain.ExtractContents(result.Value)] is CrossBlock) {
131 vector2 = -result.Ray.Direction;
132 }
133 else {
134 vector2 = CellFace.FaceToVector3(result.CellFace.Face);
135 }
136 float num = Vector3.Distance(camera.ViewPosition, vector);
137 float num2 = 0.03f + MathUtils.Min(0.008f * num, 0.04f);
138 float num3 = 0.01f * num;
139 Vector3 vector3 = ((MathUtils.Abs(Vector3.Dot(vector2, Vector3.UnitY)) < 0.5f) ? Vector3.UnitY : Vector3.UnitX);
140 Vector3 vector4 = Vector3.Normalize(Vector3.Cross(vector2, vector3));
141 Vector3 vector5 = Vector3.Normalize(Vector3.Cross(vector2, vector4));
142 Subtexture subtexture = ContentManager.Get<Subtexture>("Textures/Atlas/Reticle");
143 TexturedBatch3D texturedBatch3D = m_primitivesRenderer3D.TexturedBatch(
144 subtexture.Texture,
145 false,
146 0,
148 null,
149 null,
151 );
152 Vector3 vector6 = vector + num2 * (-vector4 + vector5) + num3 * vector2;
153 Vector3 vector7 = vector + num2 * (vector4 + vector5) + num3 * vector2;
154 Vector3 vector8 = vector + num2 * (vector4 - vector5) + num3 * vector2;
155 Vector3 vector9 = vector + num2 * (-vector4 - vector5) + num3 * vector2;
156 Vector2 vector10 = new Vector2(subtexture.TopLeft.X, subtexture.TopLeft.Y);
157 Vector2 vector11 = new Vector2(subtexture.BottomRight.X, subtexture.TopLeft.Y);
158 Vector2 vector12 = new Vector2(subtexture.BottomRight.X, subtexture.BottomRight.Y);
159 Vector2 vector13 = new Vector2(subtexture.TopLeft.X, subtexture.BottomRight.Y);
160 texturedBatch3D.QueueQuad(
161 vector6,
162 vector7,
163 vector8,
164 vector9,
165 vector10,
166 vector11,
167 vector12,
168 vector13,
170 );
171 texturedBatch3D.Flush(camera.ViewProjectionMatrix);
172 }
173 }
174
175 public virtual void DrawFillHighlight(Camera camera) {
176 if (camera.Eye != null
178 CellFace cellFace = result.CellFace;
179 int cellValue = m_subsystemTerrain.Terrain.GetCellValue(cellFace.X, cellFace.Y, cellFace.Z);
180 int num = Terrain.ExtractContents(cellValue);
181 Block block = BlocksManager.Blocks[num];
182 if (m_geometry == null
183 || cellValue != m_value
184 || cellFace != m_cellFace) {
185 Utilities.Dispose(ref m_geometry);
188 m_subsystemTerrain.BlockGeometryGenerator,
190 cellValue,
191 cellFace.X,
192 cellFace.Y,
193 cellFace.Z
194 );
195 m_cellFace = cellFace;
196 m_value = cellValue;
197 }
198 DynamicArray<TerrainVertex> vertices = m_geometry.SubsetOpaque.Vertices;
199 DynamicArray<int> indices = m_geometry.SubsetOpaque.Indices;
200 Vector3 translation = camera.InvertedViewMatrix.Translation;
201 Vector3 vector = new Vector3(MathUtils.Floor(translation.X), 0f, MathUtils.Floor(translation.Z));
202 Matrix matrix = Matrix.CreateTranslation(vector - translation) * camera.ViewMatrix.OrientationMatrix * camera.ProjectionMatrix;
203 Display.BlendState = BlendState.NonPremultiplied;
204 Display.DepthStencilState = DepthStencilState.Default;
205 Display.RasterizerState = RasterizerState.CullCounterClockwiseScissor;
206 m_shader.GetParameter("u_origin").SetValue(vector.XZ);
207 m_shader.GetParameter("u_viewProjectionMatrix").SetValue(matrix);
208 m_shader.GetParameter("u_viewPosition").SetValue(translation);
209 m_shader.GetParameter("u_texture").SetValue(m_subsystemAnimatedTextures.AnimatedBlocksTexture);
210 m_shader.GetParameter("u_samplerState").SetValue(SamplerState.PointWrap);
211 m_shader.GetParameter("u_fogYMultiplier").SetValue(m_subsystemSky.VisibilityRangeYMultiplier);
212 m_shader.GetParameter("u_fogColor").SetValue(new Vector3(m_subsystemSky.ViewFogColor));
213 m_shader.GetParameter("u_fogBottomTopDensity")
214 .SetValue(new Vector3(m_subsystemSky.ViewFogBottom, m_subsystemSky.ViewFogTop, m_subsystemSky.ViewFogDensity));
215 m_shader.GetParameter("u_hazeStartDensity").SetValue(new Vector2(m_subsystemSky.ViewHazeStart, m_subsystemSky.ViewHazeDensity));
216 Display.DrawUserIndexed(
217 PrimitiveType.TriangleList,
218 m_shader,
220 vertices.Array,
221 0,
222 vertices.Count,
223 indices.Array,
224 0,
225 indices.Count
226 );
227 }
228 }
229
230 public virtual void DrawOutlineHighlight(Camera camera) {
231 if (camera.UsesMovementControls
232 || !(m_componentPlayer.ComponentHealth.Health > 0f)
233 || !m_componentPlayer.ComponentGui.ControlsContainerWidget.IsVisible) {
234 return;
235 }
236 if (m_componentPlayer.ComponentMiner.DigCellFace.HasValue) {
237 CellFace value = m_componentPlayer.ComponentMiner.DigCellFace.Value;
238 BoundingBox cellFaceBoundingBox = GetCellFaceBoundingBox(value.Point);
239 float num = m_subsystemSky.CalculateFog(camera.ViewPosition, cellFaceBoundingBox.Center());
240 Color color = Color.MultiplyNotSaturated(Color.Black, 1f - num);
243 value.Face,
244 cellFaceBoundingBox.Min,
245 cellFaceBoundingBox.Max,
246 color
247 );
248 }
249 else {
250 if (!m_componentPlayer.ComponentAimingSights.IsSightsVisible
251 && (SettingsManager.LookControlMode == LookControlMode.SplitTouch || !m_componentPlayer.ComponentInput.IsControlledByTouch)
252 && m_highlightRaycastResult is TerrainRaycastResult terrainRaycastResult) {
253 CellFace cellFace = terrainRaycastResult.CellFace;
254 BoundingBox cellFaceBoundingBox2 = GetCellFaceBoundingBox(cellFace.Point);
255 float num2 = m_subsystemSky.CalculateFog(camera.ViewPosition, cellFaceBoundingBox2.Center());
256 Color color2 = Color.MultiplyNotSaturated(Color.Black, 1f - num2);
259 cellFace.Face,
260 cellFaceBoundingBox2.Min,
261 cellFaceBoundingBox2.Max,
262 color2
263 );
264 }
265 if (NearbyEditableCell.HasValue) {
266 BoundingBox cellFaceBoundingBox3 = GetCellFaceBoundingBox(NearbyEditableCell.Value);
267 float num3 = m_subsystemSky.CalculateFog(camera.ViewPosition, cellFaceBoundingBox3.Center());
268 Color color3 = Color.MultiplyNotSaturated(Color.Black, 1f - num3);
269 m_primitivesRenderer3D.FlatBatch(0, DepthStencilState.None).QueueBoundingBox(cellFaceBoundingBox3, color3);
270 }
271 }
273 }
274
275 public static void DrawBoundingBoxFace(FlatBatch3D batch, int face, Vector3 c1, Vector3 c2, Color color) {
276 switch (face) {
277 case 0:
278 batch.QueueLine(new Vector3(c1.X, c1.Y, c2.Z), new Vector3(c2.X, c1.Y, c2.Z), color);
279 batch.QueueLine(new Vector3(c2.X, c2.Y, c2.Z), new Vector3(c1.X, c2.Y, c2.Z), color);
280 batch.QueueLine(new Vector3(c2.X, c1.Y, c2.Z), new Vector3(c2.X, c2.Y, c2.Z), color);
281 batch.QueueLine(new Vector3(c1.X, c2.Y, c2.Z), new Vector3(c1.X, c1.Y, c2.Z), color);
282 break;
283 case 1:
284 batch.QueueLine(new Vector3(c2.X, c1.Y, c2.Z), new Vector3(c2.X, c2.Y, c2.Z), color);
285 batch.QueueLine(new Vector3(c2.X, c1.Y, c1.Z), new Vector3(c2.X, c2.Y, c1.Z), color);
286 batch.QueueLine(new Vector3(c2.X, c2.Y, c1.Z), new Vector3(c2.X, c2.Y, c2.Z), color);
287 batch.QueueLine(new Vector3(c2.X, c1.Y, c1.Z), new Vector3(c2.X, c1.Y, c2.Z), color);
288 break;
289 case 2:
290 batch.QueueLine(new Vector3(c1.X, c1.Y, c1.Z), new Vector3(c2.X, c1.Y, c1.Z), color);
291 batch.QueueLine(new Vector3(c2.X, c1.Y, c1.Z), new Vector3(c2.X, c2.Y, c1.Z), color);
292 batch.QueueLine(new Vector3(c2.X, c2.Y, c1.Z), new Vector3(c1.X, c2.Y, c1.Z), color);
293 batch.QueueLine(new Vector3(c1.X, c2.Y, c1.Z), new Vector3(c1.X, c1.Y, c1.Z), color);
294 break;
295 case 3:
296 batch.QueueLine(new Vector3(c1.X, c2.Y, c2.Z), new Vector3(c1.X, c1.Y, c2.Z), color);
297 batch.QueueLine(new Vector3(c1.X, c2.Y, c1.Z), new Vector3(c1.X, c1.Y, c1.Z), color);
298 batch.QueueLine(new Vector3(c1.X, c1.Y, c1.Z), new Vector3(c1.X, c1.Y, c2.Z), color);
299 batch.QueueLine(new Vector3(c1.X, c2.Y, c1.Z), new Vector3(c1.X, c2.Y, c2.Z), color);
300 break;
301 case 4:
302 batch.QueueLine(new Vector3(c2.X, c2.Y, c2.Z), new Vector3(c1.X, c2.Y, c2.Z), color);
303 batch.QueueLine(new Vector3(c2.X, c2.Y, c1.Z), new Vector3(c1.X, c2.Y, c1.Z), color);
304 batch.QueueLine(new Vector3(c1.X, c2.Y, c1.Z), new Vector3(c1.X, c2.Y, c2.Z), color);
305 batch.QueueLine(new Vector3(c2.X, c2.Y, c1.Z), new Vector3(c2.X, c2.Y, c2.Z), color);
306 break;
307 case 5:
308 batch.QueueLine(new Vector3(c1.X, c1.Y, c2.Z), new Vector3(c2.X, c1.Y, c2.Z), color);
309 batch.QueueLine(new Vector3(c1.X, c1.Y, c1.Z), new Vector3(c2.X, c1.Y, c1.Z), color);
310 batch.QueueLine(new Vector3(c1.X, c1.Y, c1.Z), new Vector3(c1.X, c1.Y, c2.Z), color);
311 batch.QueueLine(new Vector3(c2.X, c1.Y, c1.Z), new Vector3(c2.X, c1.Y, c2.Z), color);
312 break;
313 }
314 }
315
317 int cellValue = m_subsystemTerrain.Terrain.GetCellValue(point.X, point.Y, point.Z);
318 BoundingBox[] customCollisionBoxes = BlocksManager.Blocks[Terrain.ExtractContents(cellValue)]
320 Vector3 vector = new(point.X, point.Y, point.Z);
321 if (customCollisionBoxes.Length != 0) {
322 BoundingBox? boundingBox = null;
323 for (int i = 0; i < customCollisionBoxes.Length; i++) {
324 if (customCollisionBoxes[i] != default) {
325 boundingBox = boundingBox.HasValue ? BoundingBox.Union(boundingBox.Value, customCollisionBoxes[i]) : customCollisionBoxes[i];
326 }
327 }
328 boundingBox ??= new BoundingBox(Vector3.Zero, Vector3.One);
329 return new BoundingBox(boundingBox.Value.Min + vector, boundingBox.Value.Max + vector);
330 }
331 return new BoundingBox(vector, vector + Vector3.One);
332 }
333
334 public class Geometry : TerrainGeometry {
335 public Geometry(Texture2D texture2D) : base(texture2D) {
336 for (int l = 0; l < Subsets.Length; l++) {
337 Utilities.Dispose(ref Subsets[l]);
338 }
339 TerrainGeometrySubset terrainGeometrySubset = new ();
340 TerrainGeometrySubset[] array = new[] {
341 terrainGeometrySubset,
342 terrainGeometrySubset,
343 terrainGeometrySubset,
344 terrainGeometrySubset,
345 terrainGeometrySubset,
346 terrainGeometrySubset
347 };
348 SubsetOpaque = terrainGeometrySubset;
349 SubsetAlphaTest = terrainGeometrySubset;
350 SubsetTransparent = terrainGeometrySubset;
351 OpaqueSubsetsByFace = array;
354 for (int i = 0; i < 7; i++) {
355 Subsets[i] = terrainGeometrySubset;
356 }
357 }
358 }
359 }
360}
Engine.Vector3 Vector3
void Flush(Matrix matrix, bool clearAfterFlush=true)
static readonly BlendState NonPremultiplied
static readonly DepthStencilState Default
static readonly DepthStencilState None
static readonly DepthStencilState DepthRead
void QueueLine(Vector3 p1, Vector3 p2, Color color)
static readonly RasterizerState CullCounterClockwiseScissor
static int Abs(int x)
static int Min(int x1, int x2)
static float Saturate(float x)
static int Max(int x1, int x2)
static float Floor(float x)
void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
生成地形顶点(用于绘制放置的方块)
virtual bool IsEditable_(int value)
virtual BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value)
Vector3 ViewPosition
GameWidget GameWidget
bool UsesMovementControls
Matrix InvertedViewMatrix
Vector3 ViewDirection
Matrix ProjectionMatrix
Matrix ViewProjectionMatrix
virtual void DrawRayHighlight(Camera camera)
virtual void DrawFillHighlight(Camera camera)
SubsystemAnimatedTextures m_subsystemAnimatedTextures
virtual void DrawReticleHighlight(Camera camera)
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
virtual void DrawOutlineHighlight(Camera camera)
virtual void Draw(Camera camera, int drawOrder)
static void DrawBoundingBoxFace(FlatBatch3D batch, int face, Vector3 c1, Vector3 c2, Color color)
BoundingBox GetCellFaceBoundingBox(Point3 point)
static object Get(Type type, string name)
static void AddExtraStat(string stat)
在性能信息下方添加一行信息,需每帧添加,不支持中文
static LookControlMode LookControlMode
readonly Texture2D Texture
readonly Vector2 BottomRight
readonly Vector2 TopLeft
TerrainGeometrySubset[] Subsets
TerrainGeometrySubset[] TransparentSubsetsByFace
TerrainGeometrySubset SubsetTransparent
TerrainGeometrySubset SubsetAlphaTest
TerrainGeometrySubset SubsetOpaque
TerrainGeometrySubset[] AlphaTestSubsetsByFace
TerrainGeometrySubset[] OpaqueSubsetsByFace
static int ExtractContents(int value)
ValuesDictionary ValuesDictionary
PlayerData PlayerData
static BoundingBox Union(BoundingBox b1, BoundingBox b2)
static Color Lerp(Color c1, Color c2, float f)
static Color Transparent
定义 Color.cs:5
static Color White
static Color MultiplyNotSaturated(Color c, float s)
static Color Black
定义 Color.cs:7
static Matrix CreateTranslation(float x, float y, float z)
Vector3 Translation
Vector3 Position
定义 Ray3.cs:3
Vector3 Direction
定义 Ray3.cs:5
static readonly Vector3 One
static Vector3 Cross(Vector3 v1, Vector3 v2)
static Vector3 Normalize(Vector3 v)
static readonly Vector3 Zero
static float Distance(Vector3 v1, Vector3 v2)
static readonly Vector3 UnitX
static readonly Vector3 UnitY
static float Dot(Vector3 v1, Vector3 v2)
CellFace(int x, int y, int z, int face)
static Vector3 FaceToVector3(int face)
static readonly VertexDeclaration VertexDeclaration