Javascript ]=x;out[1]=y;返回;},乘法:函数(out,a,b){out[0]=a[0]*b[0];out[1]=a[1]*b[1];返回;},加法:函数(out,a,b){out[0]=a[0]+b[0];out[1]=a[1]+b[1];返回;}; c

Javascript ]=x;out[1]=y;返回;},乘法:函数(out,a,b){out[0]=a[0]*b[0];out[1]=a[1]*b[1];返回;},加法:函数(out,a,b){out[0]=a[0]+b[0];out[1]=a[1]+b[1];返回;}; c,javascript,canvas,webgl,Javascript,Canvas,Webgl,]=x;out[1]=y;返回;},乘法:函数(out,a,b){out[0]=a[0]*b[0];out[1]=a[1]*b[1];返回;},加法:函数(out,a,b){out[0]=a[0]+b[0];out[1]=a[1]+b[1];返回;}; const FRAGMENT_SHADER=`精度高浮点;可变高矢量2 vTextureCoord;可变低通vec4彩色;均匀取样器;统一布尔-奥斯特文本;void main(void){if(aUseText){gl_FragColor=tex

]=x;out[1]=y;返回;},乘法:函数(out,a,b){out[0]=a[0]*b[0];out[1]=a[1]*b[1];返回;},加法:函数(out,a,b){out[0]=a[0]+b[0];out[1]=a[1]+b[1];返回;}; const FRAGMENT_SHADER=`精度高浮点;可变高矢量2 vTextureCoord;可变低通vec4彩色;均匀取样器;统一布尔-奥斯特文本;void main(void){if(aUseText){gl_FragColor=texture2D(uSampler,vtexturecord);}else{gl_FragColor=vColor;}}}; const VERTEX_SHADER=`attribute vec4;属性向量4;颜色;属性向量2编码;均匀mat4 uModelViewMatrix;均匀mat4映射矩阵;一致mat3矩阵;均匀的浮点数;可变低通vec4彩色;可变高矢量2 vTextureCoord;void main(void){gl_PointSize=uPointSize;gl_Position=uProjectionMatrix*uModelViewMatrix*aVertexPosition;vColor=aVertexColor;vTextureCoord=(vec3(atexturecord,1)*uTextMatrix).xy;}; 阶级性{ 构造函数(){ this.matrix=mat4.create(); this.coords=vec3.create(); } 翻译(newCoords){ 常数{ 矩阵, 坐标 }=这个; 翻译(矩阵,矩阵,新坐标); vec3.复制(coords,[matrix[12],matrix[13],matrix[14]]; 归还这个; } 移动(新坐标){ 常数{ 矩阵, 坐标 }=这个; vec3.复制(coords,newCoords); mat4.moveToVec3(矩阵、坐标); 归还这个; } } 类摄影机扩展了WebglEntity{ 构造函数(fieldOfView、aspect、zNear、zFar){ 超级(); this.projection=mat4.perspective(mat4.create(),fieldOfView,aspect,zNear,zFar); } 瞧(瞧){ 常数{ 矩阵, 投影, 坐标 }=这个; mat4.注视(矩阵,坐标,注视,[0,1,0]); mat4.乘法(矩阵、投影、矩阵); 归还这个; } } 类Rect扩展了WebglEntity{ 构造函数(){ 超级(); this.positionsBuffer=未定义; this.fragColorPos=未定义; this.strokeColorPos=未定义; this.strokePositionBuffer=未定义; this.vertexAttribInfo=未定义; this.vertextColorAttribInfo=未定义; this.vertexCount=未定义; this.textureInfo=未定义; this.multiTextures=false; 这个.strokeSize=1; 此参数={ 填充:假, 纹理:假, 笔画:错 }; } 设置(矩阵、位置缓冲、行程位置缓冲、vertexAttribInfo、vertextColorAttribInfo、vertexCount){ 这个矩阵=矩阵; this.positionsBuffer=positionsBuffer; this.strokePositionBuffer=strokePositionBuffer; this.vertexAttribInfo=vertexAttribInfo; this.vertextColorAttribInfo=vertextColorAttribInfo; this.vertexCount=vertexCount; 归还这个; } } 课堂展示{ 构造函数(gl、programInfo、zAxis、纹理){ this.gl=gl; this.programInfo=programInfo; this.canvas=gl.canvas; this.currentCamera=新摄像头(45*Math.PI/180,gl.canvas.width/gl.canvas.height,0.1100.0); this.currentCamera.translate([0,0,zAxis])。lookAt([0,0,0]); this.zAxis=zAxis; 这个.drawZAxis=0; this.last={}; texture.textAttribInfo={ NUM组件:2, 类型:gl.FLOAT, 正常化:错误, 步幅:0, 偏移量:0 }; 这个纹理=纹理; this.spriteSheets=[]; const context=texture.context; const canvas=texture.canvas; this.images={}; } 透明(彩色){ const gl=this.gl; gl.clearColor(0.1,0.1,0.1,1); gl.净空深度(1.0); 总帐启用(总帐深度测试); 德普丰克总帐(莱夸尔总帐); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); } 矩形(x,y,w,h){ const{rect,stroke}=this.createRectPos(w,h); const square=new Rect(); 设置(…this.getRectInfo(x,y,rect,stroke)); 返回广场; } fillRect(矩形,颜色){ const{createStaticDrawBuffer,gl,parseColor}=this; rect.fillers.fill=true; 如果(颜色){ rect.fragColorPos=createStaticDrawBuffer(gl[0,1,0,1,0,1,0,1,0,1,0,1,0,1]); } } createRectPos(w,h){ 常数rect=[w/2,h/2,-w/2,h/2,w/2,-h/2,-w/2,-h/2]; 常数冲程=[-w/2,h/2,w/2,h/2,w/2,-h/2,-w/2,-h/2,]; 返回{rect,stroke}; } getRectInfo(x,y,rect,stroke){ 返回这个.createSquareBuffer(rect,stroke,[x,y,this.drawZAxis]); } createStaticDrawBuffer(总帐,数据){ const buffer=gl.createBuffer(); gl.bindBuffer(gl.ARRAY\u BUFFER,BUFFER); 总账缓冲数据(总账数组缓冲区、新Float32Array(数据)、总账静态绘图); 返回缓冲区; } createSquareBuffer(位置、行程位置、坐标){ const{gl,createStaticDrawBuffer}=this; const positionsBuffer=createStaticDrawBuffer(总帐,位置); 常量strokePositionBuffer=createStaticDrawBuffer(gl,strokePosition); const modelViewMatrix=mat4.create(); mat4.翻译(modelViewMatrix、modelViewMatrix、coords); 返回[modelViewMatrix,positionsBuffer,strokePositionBuffer,this.createAttribInfo(2,gl.FLOAT,false,0,0),this.createAttribInfo(4,gl.FLOAT,false,0,0),positions.length/2];} createAttribInfo(numComponents、类型、规格化、步幅、偏移){ 返回{numComponents,type,normalize,stride,offset}; } enableAttrib(缓冲区、属性、gl、{numComponents、类型、规格化、步幅、偏移量}){ gl.bindBuffer(gl.ARRAY\u BUFFER,BUFFER); gl.VertexAttributePointer(属性、numComponents、类型、规格化、步幅、偏移); gl.enableVertexAttribArray(attrib); } drawBuffer(缓冲区){ const{gl,drawTexture,enableAttrib,createStaticDrawBuffer,currentCamera,纹理:{context,canvas,textAttribInfo},programInfo:{uniformLocations,program,attribLocations:{VertexExposition,vertexColor,TextureCord}}=此; 常量cameraMatrix=currentCamera.matrix; const{positionsBuffer、fragColorPos、strokeColorPos、stro
const worldViewProjection = mat4.create();
buffer.canvas.width = width;
buffer.canvas.height = height;
for (let i = objects.length; i--;) {
  const {
    coords: [_x, _y],
    area: [_w, _h]
  } = objects[i];
  mat4.multiply(worldViewProjection, this.display.currentCamera.matrix, objects[i].matrix);
  const points = [
    [-_w / 2, -_h / 2, 0],
    [ _w / 2,  _h / 2, 0],
  ].map(p => {
    const ndc = vec3.transformMat4([], p, worldViewProjection);
    return [
      (ndc[0] *  0.5 + 0.5) * width,
      (ndc[1] * -0.5 + 0.5) * height,
    ];
  });
  const ww = points[1][0] - points[0][0];
  const hh = points[1][1] - points[0][1];

  buffer.strokeStyle = 'red';
  buffer.strokeRect(...points[0], ww, hh);
}