public void FixTargetTexture(GameObject go) { var renderer = go.GetComponent <Renderer>(); var material = new UnityEngine.Material(renderer.sharedMaterial); material.mainTexture = TextureProvider.GetTexture("target1-rock"); material.color = UnityEngine.Color.white; renderer.sharedMaterial = material; }
public void FixSpinnerTexture(GameObject go) { var plate = go.transform.Find("Plate").gameObject; var renderer = plate.GetComponent <Renderer>(); var material = new UnityEngine.Material(renderer.sharedMaterial); material.mainTexture = TextureProvider.GetTexture("spinner_gottlieb"); material.color = UnityEngine.Color.white; renderer.sharedMaterial = material; }
private async Task <uint?> LoadTextureAsync(Mesh mesh, TextureType type) { var bitmap = await Task.Run(() => _textureProvider.GetTexture(mesh, type)); if (bitmap == null) { return(null); } uint textureId = Gl.GenTexture(); Gl.BindTexture(TextureTarget.Texture2d, textureId); if (bitmap.PixelFormat.NotIn(System.Drawing.Imaging.PixelFormat.Format32bppArgb, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) { bitmap = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); } // bitmap.Save(@$"E:\tmp\textures\{type}.bmp"); var(internalFormat, pixelFormat) = bitmap.PixelFormat switch { System.Drawing.Imaging.PixelFormat.Format32bppArgb => (InternalFormat.Rgba, PixelFormat.Bgr), System.Drawing.Imaging.PixelFormat.Format24bppRgb => (InternalFormat.Rgb, PixelFormat.Bgr), // 4 => (InternalFormat.Rgba, PixelFormat.Rgba), // 3 => (InternalFormat.Rgb, PixelFormat.Rgb), _ => throw new ArgumentException($"Unsupported pixel format: {bitmap.PixelFormat}", nameof(bitmap)) }; var bits = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat); { Gl.TexImage2D(TextureTarget.Texture2d, 0, internalFormat, bitmap.Width, bitmap.Height, 0, pixelFormat, PixelType.UnsignedByte, bits.Scan0); } var error = Gl.GetError(); if (error != ErrorCode.NoError) { throw new Exception(error.ToString()); } Gl.TexParameteri(TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, Gl.LINEAR); Gl.TexParameteri(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, Gl.LINEAR_MIPMAP_LINEAR); Gl.GenerateMipmap(TextureTarget.Texture2d); return(textureId); } }
public Texture2D GetTexture() { return(TextureProvider.GetTexture(Shape, TextureData)); }
public IntPtr GetTexture(string imageFile) => TextureProvider.GetTexture(imageFile);
public void LoadMesh(Mesh mesh, Matrix4x4 transform) { UnloadMesh(mesh); var textureCoords = mesh.TextureCoordinateChannelCount > 0 ? mesh.TextureCoordinateChannels[0].Select(uv => uv.AsUvPoint()) : null; var triangleIndices = new List<int>(mesh.FaceCount * 4); foreach (var face in mesh.Faces) { triangleIndices.Add(face.Indices[0]); triangleIndices.Add(face.Indices[1]); triangleIndices.Add(face.Indices[2]); if (face.IndexCount == 4) { triangleIndices.Add(face.Indices[0]); triangleIndices.Add(face.Indices[2]); triangleIndices.Add(face.Indices[3]); } if (face.IndexCount > 4) { Debug.WriteLine($"Found {face.IndexCount}gon, only generating quad"); } } var geometry = new MeshGeometry3D { Positions = new Point3DCollection( mesh.Vertices.Select(v => new Point3D(v.X, v.Y, v.Z))), Normals = new Vector3DCollection( mesh.Normals.Select(n => new Vector3D(n.X, n.Y, n.Z))), TriangleIndices = new Int32Collection(triangleIndices), TextureCoordinates = textureCoords != null ? new PointCollection(textureCoords) : null }; var diffuse = _textureProvider.GetTexture(mesh, TextureType.Diffuse); // the ViewPortUnits is very important, or the brush will map MaxU x MaxV to 1 x 1 // see https://books.google.no/books?id=ubgRAAAAQBAJ&pg=PA582&lpg=PA582 // TileMode also seems necessary var brush = diffuse != null ? new ImageBrush(diffuse) { ViewportUnits = BrushMappingMode.Absolute, TileMode = TileMode.Tile } : (Brush) Brushes.Pink; // because reasons? transform = Matrix4x4.Transpose(transform); var geometryModel = new GeometryModel3D { Material = new MaterialGroup { Children = new MaterialCollection { new DiffuseMaterial(brush), } }, Geometry = geometry, }; var group = new Model3DGroup() { Transform = transform.ToTransform3D(), }; group.Children.Add(geometryModel); _meshModelGroup.Children.Add(group); var (wireFrame, wireFrameGeometry) = CreateWireFrame(mesh); wireFrame.Transform = transform.ToTransform3D(); _wireFrameModelGroup.Children.Add(wireFrame); _meshes[mesh] = new MeshEntry(group, wireFrame, geometry, wireFrameGeometry); }