Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentFirstPersonModel.cs
浏览该文件的文档.
1using Engine;
5
6namespace Game {
9
11
13
15
16 public int m_value;
17
19
21
23
24 public float m_swapAnimationTime;
25
26 public float m_pokeAnimationTime;
27
29
31
32 public double m_nextHandLightTime;
33
34 public float m_handLight;
35
36 public int m_itemLight;
37
39
41
42 public static LitShader LitShader = new(
43 ShaderCodeManager.GetFast("Shaders/Lit.vsh"),
44 ShaderCodeManager.GetFast("Shaders/Lit.psh"),
45 2,
46 false,
47 false,
48 true,
49 false,
50 false
51 );
52
53 public static int[] m_drawOrders = [1];
54
55 public Vector3 ItemOffsetOrder { get; set; }
56
57 public Vector3 ItemRotationOrder { get; set; }
58
62 public bool ForceDrawHandOnly { get; set; } = false;
63
64 public int[] DrawOrders => m_drawOrders;
65
66 public UpdateOrder UpdateOrder => UpdateOrder.FirstPersonModels;
67
68 public virtual void Draw(Camera camera, int drawOrder) {
69 if (m_componentPlayer.ComponentHealth.Health > 0f
71 && !m_componentPlayer.ComponentInput.IsControlledByVr) {
72 Viewport viewport = Display.Viewport;
73 Viewport viewport2 = viewport;
74 viewport2.MaxDepth *= 0.1f;
75 Display.Viewport = viewport2;
76 try {
77 Matrix identity = Matrix.Identity;
78 if (m_swapAnimationTime > 0f) {
79 float num = MathF.Pow(MathF.Sin(m_swapAnimationTime * (float)Math.PI), 3f);
80 identity *= Matrix.CreateTranslation(0f, -0.8f * num, 0.2f * num);
81 }
82 if (m_pokeAnimationTime > 0f) {
83 float num2 = MathF.Sin(MathF.Sqrt(m_pokeAnimationTime) * (float)Math.PI);
84 if (m_value != 0 && !ForceDrawHandOnly) {
85 identity *= Matrix.CreateRotationX((0f - MathUtils.DegToRad(90f)) * num2);
86 identity *= Matrix.CreateTranslation(-0.5f * num2, 0.1f * num2, 0f * num2);
87 }
88 else {
89 identity *= Matrix.CreateRotationX((0f - MathUtils.DegToRad(45f)) * num2);
90 identity *= Matrix.CreateTranslation(-0.1f * num2, 0.2f * num2, -0.05f * num2);
91 }
92 }
93 if (m_componentRider.Mount != null) {
94 ComponentCreatureModel componentCreatureModel = m_componentRider.Mount.Entity.FindComponent<ComponentCreatureModel>();
95 if (componentCreatureModel != null) {
96 float num3 = componentCreatureModel.MovementAnimationPhase * (float)Math.PI * 2f + 0.5f;
97 Vector3 position = default;
98 position.Y = 0.02f * MathF.Sin(num3);
99 position.Z = 0.02f * MathF.Sin(num3);
100 identity *= Matrix.CreateRotationX(0.05f * MathF.Sin(num3 * 1f)) * Matrix.CreateTranslation(position);
101 }
102 }
103 else {
104 float num4 = m_componentPlayer.ComponentCreatureModel.MovementAnimationPhase * (float)Math.PI * 2f;
105 Vector3 position2 = default;
106 position2.X = 0.03f * MathF.Sin(num4 * 1f);
107 position2.Y = 0.02f * MathF.Sin(num4 * 2f);
108 position2.Z = 0.02f * MathF.Sin(num4 * 1f);
109 identity *= Matrix.CreateRotationZ(1f * position2.X) * Matrix.CreateTranslation(position2);
110 }
111 Vector3 eyePosition = m_componentPlayer.ComponentCreatureModel.EyePosition;
112 int x = Terrain.ToCell(eyePosition.X);
113 int num5 = Terrain.ToCell(eyePosition.Y);
114 int z = Terrain.ToCell(eyePosition.Z);
115 Matrix m = Matrix.CreateFromQuaternion(m_componentPlayer.ComponentCreatureModel.EyeRotation);
116 m.Translation = m_componentPlayer.ComponentCreatureModel.EyePosition;
117
118 //每隔一段时间重新计算光照。这两段原本分别在对应的绘制前面,为避免被接口跳过所以移到前面来
119 if (m_value != 0 && !ForceDrawHandOnly) {
120 if (num5 >= 0
121 && num5 <= 255) {
122 TerrainChunk chunkAtCell = m_subsystemTerrain.Terrain.GetChunkAtCell(x, z);
123 if (chunkAtCell != null
124 && chunkAtCell.State >= TerrainChunkState.InvalidVertices1) {
125 m_itemLight = m_subsystemTerrain.Terrain.GetCellLightFast(x, num5, z);
126 }
127 }
128 }
129 else {
131 float? num7 = LightingManager.CalculateSmoothLight(m_subsystemTerrain, eyePosition);
132 if (num7.HasValue) {
134 m_handLight = num7.Value;
135 }
136 }
137 }
138 bool skipVanilla = false;
140 "OnFirstPersonModelDrawing",
141 loader => {
142 loader.OnFirstPersonModelDrawing(this, camera, m_value, ref identity, out bool skip);
143 skipVanilla |= skip;
144 return false;
145 }
146 );
147 if (!skipVanilla) {
148 if (m_value != 0 && !ForceDrawHandOnly) { //手持物品时绘制方块图标
149 int num6 = Terrain.ExtractContents(m_value);
150 Block block = BlocksManager.Blocks[num6];
151 Vector3 vector = block.GetFirstPersonRotation(m_value) * ((float)Math.PI / 180f) + m_itemRotation;
153 Matrix matrix = Matrix.CreateFromYawPitchRoll(vector.Y, vector.X, vector.Z)
154 * identity
155 * Matrix.CreateTranslation(position3)
157 * m;
158 Matrix matrix2 = matrix * camera.ViewMatrix;
159 m_drawBlockEnvironmentData.DrawBlockMode = DrawBlockMode.FirstPerson;
160 m_drawBlockEnvironmentData.SubsystemTerrain = m_subsystemTerrain;
161 m_drawBlockEnvironmentData.InWorldMatrix = matrix;
162 m_drawBlockEnvironmentData.Light = m_itemLight;
163 m_drawBlockEnvironmentData.Humidity = m_subsystemTerrain.Terrain.GetSeasonalHumidity(x, z);
164 m_drawBlockEnvironmentData.Temperature = m_subsystemTerrain.Terrain.GetSeasonalTemperature(x, z)
166 m_drawBlockEnvironmentData.EnvironmentTemperature = m_componentPlayer.ComponentVitalStats.EnvironmentTemperature;
167 m_drawBlockEnvironmentData.Owner = m_entity;
168 block.DrawBlock(
170 m_value,
171 Color.White,
173 ref matrix2,
175 );
177 }
178 else { //空手时绘制第一人称手臂模型
179 Vector3 position4 = new(0.25f, -0.3f, -0.05f);
180 Matrix matrix2 = Matrix.CreateScale(0.01f)
181 * Matrix.CreateRotationX(0.8f)
182 * Matrix.CreateRotationY(0.4f)
183 * identity
184 * Matrix.CreateTranslation(position4)
186 * m
187 * camera.ViewMatrix;
188 Display.DepthStencilState = DepthStencilState.Default;
189 Display.RasterizerState = RasterizerState.CullCounterClockwiseScissor;
190 LitShader.Texture = m_componentPlayer.ComponentCreatureModel.TextureOverride;
198 LitShader.Transforms.World[0] = matrix2;
199 LitShader.Transforms.View = Matrix.Identity;
200 LitShader.Transforms.Projection = camera.ProjectionMatrix;
201 foreach (ModelMesh mesh in m_handModel.Meshes) {
202 foreach (ModelMeshPart meshPart in mesh.MeshParts) {
204 PrimitiveType.TriangleList,
205 LitShader,
206 meshPart.VertexBuffer,
207 meshPart.IndexBuffer,
208 meshPart.StartIndex,
209 meshPart.IndicesCount
210 );
211 }
212 }
213 }
214 }
215 }
216 finally {
217 Display.Viewport = viewport;
218 }
219 }
220 }
221
222 public virtual void Update(float dt) {
223 Vector3 vector = m_componentPlayer.ComponentCreatureModel.EyeRotation.ToYawPitchRoll();
224 m_lagAngles *= MathF.Pow(0.2f, dt);
225 if (m_lastYpr.HasValue) {
226 Vector3 vector2 = vector - m_lastYpr.Value;
227 m_lagAngles.X = Math.Clamp(m_lagAngles.X - 0.08f * MathUtils.NormalizeAngle(vector2.X), -0.1f, 0.1f);
228 m_lagAngles.Y = Math.Clamp(m_lagAngles.Y - 0.08f * MathUtils.NormalizeAngle(vector2.Y), -0.1f, 0.1f);
229 }
230 m_lastYpr = vector;
231 int activeBlockValue = m_componentMiner.ActiveBlockValue;
232 if (m_swapAnimationTime == 0f
233 && activeBlockValue != m_value) {
234 if (BlocksManager.Blocks[Terrain.ExtractContents(activeBlockValue)].IsSwapAnimationNeeded(m_value, activeBlockValue)) {
235 m_swapAnimationTime = 0.0001f;
236 }
237 else {
238 m_value = activeBlockValue;
239 }
240 }
241 if (m_swapAnimationTime > 0f) {
242 float swapAnimationTime = m_swapAnimationTime;
243 m_swapAnimationTime += 2f * dt;
244 if (swapAnimationTime < 0.5f
245 && m_swapAnimationTime >= 0.5f) {
246 m_value = activeBlockValue;
247 }
248 if (m_swapAnimationTime > 1f) {
250 }
251 }
257 }
258
259 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
260 m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
261 m_componentPlayer = Entity.FindComponent<ComponentPlayer>(true);
262 m_componentRider = Entity.FindComponent<ComponentRider>(true);
263 m_componentMiner = Entity.FindComponent<ComponentMiner>(true);
264 m_handModel = ContentManager.Get<Model>(valuesDictionary.GetValue<string>("HandModelName"));
265 }
266 }
267}
Engine.Vector3 Vector3
static readonly DepthStencilState Default
static Viewport Viewport
static void DrawIndexed(PrimitiveType primitiveType, Shader shader, VertexBuffer vertexBuffer, IndexBuffer indexBuffer, int startIndex, int indicesCount)
ReadOnlyList< ModelMeshPart > MeshParts
static readonly RasterizerState CullCounterClockwiseScissor
static float NormalizeAngle(float angle)
static float Saturate(float x)
static float DegToRad(float degrees)
static double FrameStartTime
定义 Time.cs:42
virtual float GetFirstPersonScale(int value)
virtual Vector3 GetFirstPersonOffset(int value)
void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
绘制方块_用于绘制方块物品形态
virtual bool IsSwapAnimationNeeded(int oldValue, int newValue)
virtual Vector3 GetFirstPersonRotation(int value)
GameWidget GameWidget
Matrix ViewMatrix
Matrix ProjectionMatrix
bool ForceDrawHandOnly
强制只绘制手部模型(即使有物品也不绘制)
virtual void Draw(Camera camera, int drawOrder)
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
static object Get(Type type, string name)
static readonly Vector3 DirectionToLight2
static readonly float LightAmbient
static ? float CalculateSmoothLight(SubsystemTerrain subsystemTerrain, Vector3 p)
static readonly Vector3 DirectionToLight1
static string GetFast(string fname)
static Func< int, int > GetTemperatureAdjustmentAtHeight
TerrainChunkState State
static int ExtractContents(int value)
static int ToCell(float x)
ValuesDictionary ValuesDictionary
bool IsEntityFirstPersonTarget(Entity entity)
static void HookAction(string HookName, Func< ModLoader, bool > action)
执行Hook
static Color White
static Matrix CreateRotationX(float radians)
static Matrix CreateFromQuaternion(Quaternion quaternion)
static Matrix CreateTranslation(float x, float y, float z)
static Matrix CreateRotationZ(float radians)
static readonly Matrix Identity
static Matrix CreateFromYawPitchRoll(float yaw, float pitch, float roll)
static Matrix CreateRotationY(float radians)
static Matrix CreateScale(float scale)
static Vector3 Lerp(Vector3 v1, Vector3 v2, float f)
static Vector3 TransformNormal(Vector3 v, Matrix m)
static readonly Vector3 Zero
static readonly Vector4 One