import Kernel from '../Kernel' ; import Program from '../Program' ; import Graphic from './Graphic' ; import Mesh from '../geometries/Mesh' ; import MeshTextureMaterial from '../materials/MeshTextureMaterial' ; import Camera from '../Camera' ; import Line from '../math/Line' ; import { Drawable , Pickable , Attributes } from '../Definitions.d' ; const vs = `
attribute vec3 aPosition;
attribute vec2 aUV;
varying vec2 vUV;
uniform mat4 uPMVMatrix;

void main()
{
	gl_Position = uPMVMatrix * vec4(aPosition,1.0);
	vUV = aUV;
}
` ; const fs = `
precision mediump float;
varying vec2 vUV;
uniform sampler2D uSampler;

void main()
{
	gl_FragColor = texture2D(uSampler, vec2(vUV.s, vUV.t));
}
` ; export default class MeshTextureGraphic extends Graphic implements Pickable { constructor ( public geometry , public material , public attributes = null ) { super ( geometry , material , attributes ) ; this . geometry . calculateVBO ( ) ; this . geometry . calculateIBO ( ) ; this . geometry . calculateUVBO ( ) ; } isGeometryReady ( ) { return ! ! this . geometry . vbo && ! ! this . geometry . ibo && ! ! this . geometry . uvbo ; } isReady ( ) { return this . isGeometryReady ( ) && super . isReady ( ) ; } static findProgram ( ) { return Program . findProgram ( vs , fs ) ; } createProgram ( ) { return Program . getProgram ( vs , fs ) ; } protected updateShaderUniforms ( camera ) { var gl = Kernel . gl ; var pmvMatrix = camera . getProjViewMatrixForDraw ( ) . multiplyMatrix ( this . geometry . getMatrix ( ) ) ; var locPMVMatrix = this . program . getUniformLocation ( 'uPMVMatrix' ) ; gl . uniformMatrix4fv ( locPMVMatrix , false , pmvMatrix . getFloat32Array ( ) ) ; gl . activeTexture ( Kernel . gl . TEXTURE0 ) ; var locSampler = this . program . getUniformLocation ( 'uSampler' ) ; gl . uniform1i ( locSampler , 0 ) ; } protected onDraw ( camera ) { var gl = Kernel . gl ; this . updateShaderUniforms ( camera ) ; var locPosition = this . program . getAttribLocation ( 'aPosition' ) ; this . program . enableVertexAttribArray ( 'aPosition' ) ; this . geometry . vbo . bind ( ) ; gl . vertexAttribPointer ( locPosition , 3 , Kernel . gl . FLOAT , false , 0 , 0 ) ; var locUV = this . program . getAttribLocation ( 'aUV' ) ; this . program . enableVertexAttribArray ( 'aUV' ) ; this . geometry . uvbo . bind ( ) ; gl . vertexAttribPointer ( locUV , 2 , Kernel . gl . FLOAT , false , 0 , 0 ) ; gl . bindTexture ( Kernel . gl . TEXTURE_2D , this . material . texture ) ; this . geometry . ibo . bind ( ) ; var count = this . geometry . triangles . length * 3 ; gl . drawElements ( Kernel . gl . TRIANGLES , count , Kernel . gl . UNSIGNED_SHORT , 0 ) ; } ifIntersectLocalLine ( localLine ) { if ( this . geometry ) { return this . geometry . ifIntersectLocalLine ( localLine ) ; } return false ; } ifIntersectWorldLine ( worldLine ) { if ( this . geometry ) { return this . geometry . ifIntersectWorldLine ( worldLine ) ; } return false ; } } ;