public static Texture2D LoadTexture(string imagePath, bool doMipMaps) { Initialize(); Texture2D resultTexture = null; if (isExistExternalLoader) { TextureInfo infoResult = externalLoader.GetInfo(imagePath); if (infoResult.exist) { IntPtr texture = externalLoader.LoadTexture(imagePath, doMipMaps); if (texture != IntPtr.Zero) { resultTexture = Texture2D.CreateExternalTexture(infoResult.width, infoResult.height, TextureFormat.RGBA32, doMipMaps, false, texture); } } } else { #if !UNITY_EDITOR resultTexture = LoadTextureInternal(imagePath, doMipMaps); #else resultTexture = LoadTextureEditor(imagePath, doMipMaps); #endif } resultTexture = SetupTexture(resultTexture, doMipMaps); return(resultTexture); }
protected void LoadTexture() { IsReady = false; lastLoadPath = textures[_selectedTexture][_selectedColor]; if (!textureCache.ContainsKey(lastLoadPath)) { textureLoader.LoadTexture(new string[] { lastLoadPath }, loadCallback); } else { IsReady = true; } //Loading near textures for smoother work var requiredPaths = new List <string>() { textures[_selectedTexture][_selectedColor], textures[_selectedTexture][GetColorNumber(_selectedColor + 1)], textures[_selectedTexture][GetColorNumber(_selectedColor - 1)], textures[GetTextureNumber(_selectedTexture + 1)][_selectedColor], textures[GetTextureNumber(_selectedTexture - 1)][_selectedColor], }; var neededPaths = new List <string>(); for (int i = 0; i < requiredPaths.Count; i++) { if (!textureCache.ContainsKey(requiredPaths[i])) { neededPaths.Add(requiredPaths[i]); } } if (neededPaths.Count > 0) { lastLoadPath = neededPaths[0]; textureLoader.LoadTexture(neededPaths.ToArray(), loadCallback); } }
public CursorRenderer(IClientSettings settings, IContext context, ITextureLoader textureLoader) { var header = settings.IsEs ? EsHeader : DesktopHeader; var vertexShader = context.Create.VertexShader(header + VertexShaderText); var fragmentShader = context.Create.FragmentShader(header + FragmentShaderText); program = context.Create.Program(new ShaderProgramDescription { VertexShaders = new[] { vertexShader }, FragmentShaders = new[] { fragmentShader }, VertexAttributeNames = new[] { "in_position", "in_tex_coord" }, SamplerNames = new[] { "DiffTexture" } }); var vertexBuffer = context.Create.Buffer(BufferTarget.ArrayBuffer, 4 * Vertex.Size, BufferUsageHint.StaticDraw, new[] { new Vertex(-1f, 1f, 0f, 0f), new Vertex(1f, 1f, 1f, 0f), new Vertex(1f, -1f, 1f, 1f), new Vertex(-1f, -1f, 0f, 1f) }); var indexBuffer = context.Create.Buffer(BufferTarget.ElementArrayBuffer, 6 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[] { 0, 1, 2, 0, 2, 3 }); vertexArray = context.Create.VertexArray(); vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.Size, 0); vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.Size, 2 * sizeof(float)); vertexArray.SetElementArrayBuffer(indexBuffer); texture = textureLoader.LoadTexture(context, "Cursor.png"); sampler = context.Create.Sampler(); sampler.SetMagFilter(TextureMagFilter.Nearest); }
public void SetState(bool isReload) { if (!isReload) { _maxFragmentShaderTextureUnits = GL.GetInteger(GetPName.MaxTextureImageUnits); _shaderProgram = _shaderCompiler.Compile(GetVertexShaderCode(), GetFragmentShaderCode()); } if (_activeTextures.Count > _maxFragmentShaderTextureUnits) { _logger.LogInformation($"Maximum number of fragment shader texture image units = {_maxFragmentShaderTextureUnits}"); _logger.LogError("Maximum number of active textures exceeded."); return; } _renderTextures = new List <Textures.Texture>(); foreach (var texture in _activeTextures) { _renderTextures.Add(_textureLoader.LoadTexture(texture)); } if (isReload) { GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferObject); GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, _vertexBuffer.Value.Length * sizeof(float), _vertexBuffer.Value); } else { var stride = ComponentsPerVertex * sizeof(float); var vertexArrays = new int[1]; GL.CreateVertexArrays(1, vertexArrays); GL.BindVertexArray(vertexArrays[0]); _vertexArrayObject = vertexArrays[0]; var buffers = new int[1]; GL.CreateBuffers(1, buffers); GL.BindBuffer(BufferTarget.ArrayBuffer, buffers[0]); GL.BufferData(BufferTarget.ArrayBuffer, _vertexBuffer.Value.Length * sizeof(float), _vertexBuffer.Value, BufferUsageHint.DynamicCopy); _vertexBufferObject = buffers[0]; // Vertex ID const int layout_pos_vertexId = 0; GL.EnableVertexArrayAttrib(_vertexArrayObject, layout_pos_vertexId); GL.VertexAttribPointer(layout_pos_vertexId, 3, VertexAttribPointerType.Float, false, stride, 0); // Surface ID const int layout_pos_surfaceId = 1; GL.EnableVertexArrayAttrib(_vertexArrayObject, layout_pos_surfaceId); GL.VertexAttribPointer(layout_pos_surfaceId, 3, VertexAttribPointerType.Float, false, stride, sizeof(float)); // Vertex position const int layout_pos_vertexPosition = 2; GL.EnableVertexArrayAttrib(_vertexArrayObject, layout_pos_vertexPosition); GL.VertexAttribPointer(layout_pos_vertexPosition, 3, VertexAttribPointerType.Float, false, stride, 2 * sizeof(float)); // Vertex colour const int layout_pos_vertexColor = 3; GL.EnableVertexArrayAttrib(_vertexArrayObject, layout_pos_vertexColor); GL.VertexAttribPointer(layout_pos_vertexColor, 4, VertexAttribPointerType.Float, false, stride, 5 * sizeof(float)); // Texture coordinates const int layout_tex_coords = 4; GL.EnableVertexArrayAttrib(_vertexArrayObject, layout_tex_coords); GL.VertexAttribPointer(layout_tex_coords, 2, VertexAttribPointerType.Float, false, stride, 9 * sizeof(float)); // Sampler index const int layout_sampler_index = 5; GL.EnableVertexArrayAttrib(_vertexArrayObject, layout_sampler_index); GL.VertexAttribPointer(layout_sampler_index, 1, VertexAttribPointerType.Float, false, stride, 11 * sizeof(float)); // Create vertex index buffer for drawing triangles var indexBuffers = new int[1]; GL.CreateBuffers(1, indexBuffers); GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffers[0]); GL.BufferData(BufferTarget.ElementArrayBuffer, _vertexIndexBuffer.Value.Length * sizeof(uint), _vertexIndexBuffer.Value, BufferUsageHint.DynamicDraw); _vertexIndexBufferId = indexBuffers[0]; // Log stats _logger.LogInformation($"Vertex buffer size = {_vertexBuffer.Value.Length * sizeof(float)} bytes"); _logger.LogInformation($"Vertex index buffer size = {_vertexIndexBuffer.Value.Length * sizeof(uint)} bytes"); } }