Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ModelWidget.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
5 public class ModelWidget : Widget {
6 public static LitShader m_shader = new(1, false, false, true, false, false);
7
8 public static LitShader m_shaderAlpha = new(1, false, false, true, false, true);
9
10 public List<Model> Models = new();
11
12 public Dictionary<Model, Matrix?[]> m_boneTransforms = new();
13
14 public Dictionary<Model, Matrix[]> m_absoluteBoneTransforms = new();
15
16 public Dictionary<Model, Texture2D> Textures = new();
17
18 public Vector2 Size { get; set; }
19
20 public Color Color { get; set; }
21
22 public bool UseAlphaThreshold { get; set; }
23
24 public bool IsPerspective { get; set; }
25
26 public Vector3 OrthographicFrustumSize { get; set; }
27
28 public Vector3 ViewPosition { get; set; }
29
30 public Vector3 ViewTarget { get; set; }
31
32 public float ViewFov { get; set; }
33
34 public Matrix ModelMatrix { get; set; } = Matrix.Identity;
35
36 public Vector3 AutoRotationVector { get; set; }
37
38 [Obsolete(
39 "A ModelWidget may contains multiple models, please use Models field instead. This field only represents the first model of Models field."
40 )]
41 public Model Model {
42 get => Models?[0] ?? null;
43 set {
44 if (value != null) {
45 if (Models.Count == 0) {
46 Models.Add(value);
47 }
48 else {
49 Models[0] = value;
50 }
51 m_boneTransforms[value] = new Matrix?[value.Bones.Count];
52 m_absoluteBoneTransforms[value] = new Matrix[value.Bones.Count];
53 }
54 else {
55 Models.RemoveAt(0);
56 }
57 }
58 }
59
60 public Action<ModelWidget, Shader, Model, ModelMesh> OnSetupShaderParameters;
61
66 public TransformedShader CustomShader { get; set; }
67
68 public void AddModel(Model value) {
69 if (value != null) {
70 Models.Add(value);
71 m_boneTransforms[value] = new Matrix?[value.Bones.Count];
72 m_absoluteBoneTransforms[value] = new Matrix[value.Bones.Count];
73 }
74 }
75
76 public bool RemoveModel(Model value) {
77 if (value != null) {
78 Models.Remove(value);
79 m_boneTransforms.Remove(value);
80 m_absoluteBoneTransforms.Remove(value);
81 Textures.Remove(value);
82 return true;
83 }
84 return false;
85 }
86
87 [Obsolete("A ModelWidget may contains multiple models. TextureOverride only represents the texture of the first model.")]
89 get => Textures[Models[0]];
90 set => Textures[Models[0]] = value;
91 }
92
93 public ModelWidget() {
94 Size = new Vector2(float.PositiveInfinity);
95 IsHitTestVisible = false;
96 Color = Color.White;
97 UseAlphaThreshold = false;
98 IsPerspective = true;
99 ViewPosition = new Vector3(0f, 0f, -5f);
100 ViewTarget = new Vector3(0f, 0f, 0f);
101 ViewFov = 1f;
102 OrthographicFrustumSize = new Vector3(0f, 10f, 10f);
103 }
104
105 public Matrix? GetBoneTransform(Model model, int boneIndex) => m_boneTransforms[model][boneIndex];
106
107 public void SetBoneTransform(Model model, int boneIndex, Matrix? transformation) {
108 m_boneTransforms[model][boneIndex] = transformation;
109 }
110
111 public override void Draw(DrawContext dc) {
112 if (Models.Count == 0) {
113 return;
114 }
115 TransformedShader shader;
116 if (CustomShader != null) {
117 shader = CustomShader;
118 }
119 else {
121 LitShader litShader = shader as LitShader;
122 litShader.SamplerState = SamplerState.PointClamp;
123 litShader.MaterialColor = new Vector4(Color * GlobalColorTransform);
124 litShader.AmbientLightColor = new Vector3(0.66f, 0.66f, 0.66f);
125 litShader.DiffuseLightColor1 = new Vector3(1f, 1f, 1f);
126 litShader.LightDirection1 = Vector3.Normalize(new Vector3(1f, 1f, 1f));
127 if (UseAlphaThreshold) {
128 litShader.AlphaThreshold = 0f;
129 }
130 }
131 shader.Transforms.View = Matrix.CreateLookAt(ViewPosition, ViewTarget, Vector3.UnitY);
132 Viewport viewport = Display.Viewport;
133 float num = ActualSize.X / ActualSize.Y;
134 if (IsPerspective) {
135 shader.Transforms.Projection = Matrix.CreatePerspectiveFieldOfView(ViewFov, num, 0.1f, 100f)
136 * MatrixUtils.CreateScaleTranslation(0.5f * ActualSize.X, -0.5f * ActualSize.Y, ActualSize.X / 2f, ActualSize.Y / 2f)
138 * MatrixUtils.CreateScaleTranslation(2f / viewport.Width, -2f / viewport.Height, -1f, 1f);
139 }
140 else {
141 Vector3 orthographicFrustumSize = OrthographicFrustumSize;
142 if (orthographicFrustumSize.X < 0f) {
143 orthographicFrustumSize.X = orthographicFrustumSize.Y / num;
144 }
145 else if (orthographicFrustumSize.Y < 0f) {
146 orthographicFrustumSize.Y = orthographicFrustumSize.X * num;
147 }
148 shader.Transforms.Projection =
149 Matrix.CreateOrthographic(orthographicFrustumSize.X, orthographicFrustumSize.Y, 0f, OrthographicFrustumSize.Z)
150 * MatrixUtils.CreateScaleTranslation(0.5f * ActualSize.X, -0.5f * ActualSize.Y, ActualSize.X / 2f, ActualSize.Y / 2f)
152 * MatrixUtils.CreateScaleTranslation(2f / viewport.Width, -2f / viewport.Height, -1f, 1f);
153 }
154 Display.DepthStencilState = DepthStencilState.Default;
155 Display.BlendState = BlendState.AlphaBlend;
156 Display.RasterizerState = RasterizerState.CullNoneScissor;
157 foreach (Model model in Models) {
159 }
160 float num2 = (float)Time.RealTime + GetHashCode() % 1000 / 100f;
161 Matrix m = AutoRotationVector.LengthSquared() > 0f
164 foreach (Model model in Models) {
165 Texture2D texture = Textures[model];
166 if (texture == null) {
167 continue;
168 }
169 shader.GetParameter("u_texture", true)?.SetValue(texture);
170 foreach (ModelMesh mesh in model.Meshes) {
171 shader.Transforms.World[0] = m_absoluteBoneTransforms[mesh.ParentBone.Model][mesh.ParentBone.Index] * ModelMatrix * m;
172 OnSetupShaderParameters?.Invoke(this, shader, model, mesh);
173 foreach (ModelMeshPart meshPart in mesh.MeshParts) {
174 if (meshPart.IndicesCount > 0) {
176 PrimitiveType.TriangleList,
177 shader,
178 meshPart.VertexBuffer,
179 meshPart.IndexBuffer,
180 meshPart.StartIndex,
181 meshPart.IndicesCount
182 );
183 }
184 }
185 }
186 }
187 }
188
189 public override void MeasureOverride(Vector2 parentAvailableSize) {
190 IsDrawRequired = Models.Count > 0;
192 }
193
194 public void ProcessBoneHierarchy(ModelBone modelBone, Matrix currentTransform) {
195 Matrix[] transforms = m_absoluteBoneTransforms[modelBone.Model];
196 Matrix m = modelBone.Transform;
197 if (m_boneTransforms[modelBone.Model][modelBone.Index].HasValue) {
198 Vector3 translation = m.Translation;
199 m.Translation = Vector3.Zero;
200 m *= m_boneTransforms[modelBone.Model][modelBone.Index].Value;
201 m.Translation += translation;
202 Matrix.MultiplyRestricted(ref m, ref currentTransform, out transforms[modelBone.Index]);
203 }
204 else {
205 Matrix.MultiplyRestricted(ref m, ref currentTransform, out transforms[modelBone.Index]);
206 }
207 foreach (ModelBone childBone in modelBone.ChildBones) {
208 ProcessBoneHierarchy(childBone, transforms[modelBone.Index]);
209 }
210 }
211 }
212}
Engine.Vector3 Vector3
static readonly BlendState AlphaBlend
static readonly DepthStencilState Default
static Viewport Viewport
static void DrawIndexed(PrimitiveType primitiveType, Shader shader, VertexBuffer vertexBuffer, IndexBuffer indexBuffer, int startIndex, int indicesCount)
ReadOnlyList< ModelBone > ChildBones
ReadOnlyList< ModelBone > Bones
ReadOnlyList< ModelMesh > Meshes
ReadOnlyList< ModelMeshPart > MeshParts
static readonly RasterizerState CullNoneScissor
static double RealTime
定义 Time.cs:38
static Matrix CreateScaleTranslation(float sx, float sy, float tx, float ty)
Action< ModelWidget, Shader, Model, ModelMesh > OnSetupShaderParameters
void SetBoneTransform(Model model, int boneIndex, Matrix? transformation)
override void MeasureOverride(Vector2 parentAvailableSize)
bool RemoveModel(Model value)
static LitShader m_shader
void AddModel(Model value)
List< Model > Models
static LitShader m_shaderAlpha
Dictionary< Model, Texture2D > Textures
Matrix? GetBoneTransform(Model model, int boneIndex)
void ProcessBoneHierarchy(ModelBone modelBone, Matrix currentTransform)
Dictionary< Model, Matrix[]> m_absoluteBoneTransforms
TransformedShader CustomShader
�Զ�����ɫ������������ʹ��Ĭ����ɫ��������Ⱦ�� ��Ҫʹ�ã�������ͨ�� OnSetupShaderParameters ���ò��������������Ч
Dictionary< Model, Matrix?[]> m_boneTransforms
Vector3 OrthographicFrustumSize
override void Draw(DrawContext dc)
Color GlobalColorTransform
Vector2 DesiredSize
bool IsDrawRequired
virtual bool IsHitTestVisible
Vector2 ActualSize
Matrix GlobalTransform
static Matrix CreatePerspectiveFieldOfView(float fieldOfViewY, float aspectRatio, float nearPlane, float farPlane)
static void MultiplyRestricted(ref Matrix m1, ref Matrix m2, out Matrix result)
static readonly Matrix Identity
static Matrix CreateLookAt(Vector3 position, Vector3 target, Vector3 up)
static Matrix CreateFromAxisAngle(Vector3 axis, float angle)
Vector3 Translation
static Matrix CreateOrthographic(float width, float height, float nearPlane, float farPlane)
static Vector3 Normalize(Vector3 v)
static readonly Vector3 Zero
static readonly Vector3 UnitY