public static Material CreateTextureColor(Texture texture) { var mat = new Material(); var attributes = new List<ShaderAttribute>(); attributes.Add(new ShaderAttribute(ShaderAttributeType.Vertex, "in_vertex")); attributes.Add(new ShaderAttribute(ShaderAttributeType.Color, "in_color")); attributes.Add(new ShaderAttribute(ShaderAttributeType.UV, "in_uv")); var parameters = new List<ShaderParam>(); parameters.Add(new ShaderParam(ShaderParamType.Camera, "camera", "Camera Matrix", Matrix4.Identity)); parameters.Add(new ShaderParam(ShaderParamType.Model, "model", "Model Matrix", Matrix4.Identity)); parameters.Add(new ShaderParam(ShaderParamType.Texture, "tex", "Texture", TextureUnit.Texture0)); mat.Shader = new Shader( @"#version 150 uniform mat4 camera; uniform mat4 model; in vec3 in_vertex; in vec4 in_color; in vec2 in_uv; out vec2 frag_TexCoord; out vec4 frag_Color; void main() { gl_Position = camera * model * vec4(in_vertex, 1); frag_TexCoord = in_uv; frag_Color = in_color; }", @"#version 150 uniform sampler2D tex; in vec4 frag_Color; in vec2 frag_TexCoord; out vec4 final_color; void main() { final_color = texture(tex, frag_TexCoord) * frag_Color; }", attributes, parameters); mat.AddTexture(TextureUnit.Texture0, texture); mat.DepthTest = true; mat.AlphaBlend = true; mat.SmoothLines = true; return mat; }
protected override void OnLoad() { _material = new TokGL.Material(); var shader = Plugins.Container.ResolveNamed<TokED.ShaderDefinition>(_shader); if (shader == null) throw new Exception(string.Format("Did not find shader {0}!", _shader)); _material.Shader = new TokGL.Shader(shader.VertexProgram, shader.FragmentProgram, shader.Attributes, shader.Parameters); _material.Activate(); if (_shader != _parameterShader) { //Get default parameters _parameters.Clear(); foreach (var p in _material.Shader.Parameters) { _parameters.Add(p.Clone()); } _parameterShader = _shader; } else ApplyParameters(); //Load Textures if (_material.TextureCount > 0) throw new Exception("Textures not cleaned up!"); foreach (var param in _material.Shader.Parameters) { if (param.Type == ShaderParamType.Texture) { TokGL.Texture texture; if (!string.IsNullOrWhiteSpace(param.TextureName)) { var t = this.Root.FindChild<GameObject>(param.TextureName) as ITexture; texture = TokGL.Texture.FromBitmap(t.GetBitmap(), true, false); } else { texture = TokGL.Texture.FromBitmap(_notTexture, true, false); } texture.MinFilter = _minFilter; texture.MagFilter = _magFilter; _material.AddTexture(param.TexUnit, texture); } } _material.DepthTest = _depthTest; _material.AlphaBlend = _alphaBlend; _material.SmoothLines = _smoothLines; _material.Deactivate(); }