import Kernel from '../Kernel' ; import Program from '../Program' ; import Graphic from './Graphic' ; import Mesh from '../geometries/Mesh' ; import MeshVertice from '../geometries/MeshVertice' ; import ColorMaterial from '../materials/ColorMaterial' ; import Camera from '../Camera' ; const vs = `
attribute vec3 aPosition;
attribute vec3 aColor;
varying vec4 vColor;
uniform mat4 uPMVMatrix;

void main()
{
	gl_Position = uPMVMatrix * vec4(aPosition,1.0);
	vColor = vec4(aColor,1.0);
}
` ; const fs = `
precision mediump float;
varying vec4 vColor;

void main()
{
	gl_FragColor = vColor;
}
` ; export default class MeshColorGraphic extends Graphic { constructor ( public geometry , public material ) { super ( geometry , material ) ; this . setGeometry ( geometry ) ; } setGeometry ( geometry ) { if ( this . geometry ) { this . geometry . destroy ( ) ; } this . geometry = geometry ; if ( this . geometry ) { this . geometry . vertices . forEach ( ( vertice ) => { vertice . c = this . material . color ; } ) ; this . geometry . calculateVBO ( ) ; this . geometry . calculateIBO ( ) ; this . geometry . calculateCBO ( ) ; } } isGeometryReady ( ) { return ! ! this . geometry . vbo && ! ! this . geometry . ibo && ! ! this . geometry . cbo ; } isReady ( ) { return this . isGeometryReady ( ) && super . isReady ( ) ; } 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 ( ) ) ; } 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 locColor = this . program . getAttribLocation ( 'aColor' ) ; this . program . enableVertexAttribArray ( 'aColor' ) ; this . geometry . cbo . bind ( ) ; gl . vertexAttribPointer ( locColor , 3 , Kernel . gl . FLOAT , false , 0 , 0 ) ; this . geometry . ibo . bind ( ) ; var count = this . geometry . triangles . length * 3 ; gl . drawElements ( Kernel . gl . TRIANGLES , count , Kernel . gl . UNSIGNED_SHORT , 0 ) ; } } ;