private void UpdatePreview() { ModelX newModel = ApplyChanges(_model); if (_previewModel != null) { _previewModel.DisposeBuffers(); for (Int32 set = 0; set < _previewModel.MaterialsSetsCount; ++set) { foreach (var material in _previewModel.Materials(set)) { String texName = material.Texture; if (material.Textures != null) { material.Textures.Dispose(); } } } } _previewModel = newModel; LoadTextures(_previewModel, Path.GetDirectoryName(_importFile)); _preview.Model = _previewModel; _preview.MaterialSet = _currentSet; }
private void LoadTextures(ModelX model, String directory) { for (Int32 set = 0; set < model.MaterialsSetsCount; ++set) { foreach (var material in model.Materials(set)) { String texName = material.Texture; if (material.Textures != null) { material.Textures.Dispose(); } if (String.IsNullOrWhiteSpace(texName)) { material.Textures = null; continue; } String fileName = Path.Combine(directory, texName); try { using (Stream stream = new FileStream(fileName, FileMode.Open)) { Texture2D texture = Texture2D.FromStream(_preview.GraphicsDevice, stream); material.Textures = new MaterialTextures(texture); } } catch { fileName = GetFileNameWithoutExtension(texName) + ".png"; fileName = Path.Combine(directory, fileName); try { using (Stream stream = new FileStream(fileName, FileMode.Open)) { Texture2D texture = Texture2D.FromStream(_preview.GraphicsDevice, stream); material.Textures = new MaterialTextures(texture); } } catch { material.Textures = null; } } } } }
public static void Render(ModelX model, IShaderEffect effect, Vector3 ambientColor, int materialsSet) { GraphicsDevice device = model.VertexBuffer.GraphicsDevice; Material[] materials = model.Materials(materialsSet); device.SetVertexBuffer(model.VertexBuffer); for (int steps = 0; steps < 2; ++steps) { for (int idx = 0; idx < model.Subsets.Length; ++idx) { var subset = model.Subsets[idx]; Material material = materials[subset.Material]; if (steps == 0) { if (material.Opacity < 1) { continue; } } if (steps == 1) { if (material.Opacity == 1) { continue; } } device.Indices = subset.IndexBuffer; //// Draw the triangle. effect.DiffuseColor = material.Diffuse; effect.AmbientLightColor = ambientColor * material.Ambient; effect.SpecularColor = material.Specular; effect.EmissiveColor = material.Emissive; effect.Alpha = material.Opacity; MaterialTextures textures = material.Textures; effect.SpecularPower = material.SpecularExponent; Texture2D texture = textures != null ? textures.Diffuse : null; effect.Texture = texture; effect.TextureEnabled = texture != null; effect.Apply(0); int startIndex = 0; while (startIndex < subset.Indices.Length) { int primitiveCount = Math.Min((subset.Indices.Length - startIndex) / 3, 30000); device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, model.Vertices.Length, startIndex, primitiveCount); startIndex += primitiveCount * 3; } } } }
/// <summary> /// Draws the control. /// </summary> protected override void Draw() { GraphicsDevice.Clear(Color.CornflowerBlue); if (_model == null || _effect == null) { return; } Vector3 center = (_model.BoundA + _model.BoundB) / 2; Vector3 camera = center + new Vector3(1, 0, 0) * _model.Size.Length() * 1.5f * _zoom; Vector3 cameraDir = camera - center; cameraDir.Normalize(); //// Set transform matrices. float aspect = GraphicsDevice.Viewport.AspectRatio; _effect.World = ComputeCameraRotation(cameraDir, _horz, _vert); _effect.View = Matrix.CreateLookAt(camera, center, _zisUp ? new Vector3(0, 0, 1): new Vector3(0, 1, 0)); _effect.Projection = Matrix.CreatePerspectiveFieldOfView(1, aspect, (camera - center).Length() / 4, (camera - center).Length() * 3f / _zoom); //// Set renderstates. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; GraphicsDevice.SetVertexBuffer(_model.VertexBuffer); GraphicsDevice.DepthStencilState = DepthStencilState.Default; _effect.VertexColorEnabled = false; _effect.LightingEnabled = true; _effect.EnableDefaultLighting(); _effect.PreferPerPixelLighting = true; GraphicsDevice.BlendState = BlendState.AlphaBlend; Color ambientColor = new Color(32, 24, 48); Material[] materials = _model.Materials(_materialSet); for (Int32 steps = 0; steps < 2; ++steps) { for (Int32 idx = 0; idx < _model.Subsets.Length; ++idx) { var subset = _model.Subsets[idx]; Material material = materials[subset.Material]; if (steps == 0) { if (material.Opacity < 1) { continue; } } if (steps == 1) { if (material.Opacity == 1) { continue; } } GraphicsDevice.Indices = subset.IndexBuffer; //// Draw the triangle. _effect.DiffuseColor = material.Diffuse; _effect.AmbientLightColor = ambientColor.ToVector3() * material.Ambient; _effect.SpecularColor = material.Specular; _effect.EmissiveColor = material.Emissive; _effect.Alpha = material.Opacity; MaterialTextures textures = material.Textures; _effect.SpecularPower = material.SpecularExponent; _effect.Texture = textures != null ? textures.Diffuse : null; _effect.TextureEnabled = _effect.Texture != null; _effect.CurrentTechnique.Passes[0].Apply(); Int32 startIndex = 0; while (startIndex < subset.Indices.Length) { Int32 primitiveCount = Math.Min((subset.Indices.Length - startIndex) / 3, 30000); GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _model.Vertices.Length, startIndex, primitiveCount); startIndex += primitiveCount * 3; } } } _effect.TextureEnabled = false; _effect.VertexColorEnabled = true; _effect.LightingEnabled = false; Single length = _model.Size.Length() * 1.5f; _effect.World = _effect.World * Matrix.CreateScale(length); _effect.CurrentTechnique.Passes[0].Apply(); GraphicsDevice.DrawUserPrimitives <VertexPositionColor>(PrimitiveType.LineList, _axes, 0, 3); }
private ModelX ApplyChanges(ModelX model) { Boolean swapZy = swapZyCheckBox.Checked; Boolean swapYz = swapYzCheckBox.Checked; Vector3 scale = _exportSize / OriginalSize; Vector3 move = Vector3.Zero; if (centerX.Checked) { move.X = -(_model.BoundA.X + _model.BoundB.X) / 2; } if (centerY.Checked) { move.Y = -(_model.BoundA.Y + _model.BoundB.Y) / 2; } if (centerZ.Checked) { move.Z = -(_model.BoundA.Z + _model.BoundB.Z) / 2; } if (layXz.Checked) { move.Y = -_model.BoundA.Y; } if (layXy.Checked) { move.Z = -_model.BoundA.Z; } if (Math.Abs(scale.X - scale.Y) > 0.0000001 || Math.Abs(scale.X - scale.Z) > 0.0000001) { throw new Exception("Niepoprawna wielkość modelu."); } VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[_model.Vertices.Length]; for (Int32 idx = 0; idx < vertices.Length; ++idx) { vertices[idx] = _model.Vertices[idx]; vertices[idx].Position = (vertices[idx].Position + move) * scale.X; if (swapZy || swapYz) { Single mul = swapYz ? -1 : 1; vertices[idx].Position = new Vector3(-vertices[idx].Position.X, vertices[idx].Position.Z, vertices[idx].Position.Y); vertices[idx].Normal = new Vector3(-vertices[idx].Normal.X, vertices[idx].Normal.Z, vertices[idx].Normal.Y); } } String newName; List <Material[]> materials = new List <Material[]>(); for (Int32 set = 0; set < _model.MaterialsSetsCount; ++set) { materials.Add(new Material[_model.Materials(set).Length]); Material[] newSet = materials.Last(); for (Int32 idx = 0; idx < newSet.Length; ++idx) { Material mat = _model.Materials(set)[idx]; newName = mat.Texture; newName = GetFileNameWithoutExtension(newName); if (newName != mat.Texture) { mat = new Material(newName, mat.Diffuse, mat.Ambient, mat.Specular, mat.SpecularExponent, mat.Emissive, mat.Opacity); } newSet[idx] = mat; } } ModelX modelX = new ModelX(materials, _model.Subsets, vertices); modelX.Up = checkZisUp.Checked ? new Vector3(0, 0, 1) : new Vector3(0, 1, 0); return(modelX); }