mirror of
https://github.com/DaanVandenBosch/phantasmal-world.git
synced 2025-04-05 15:28:29 +08:00
1 line
14 KiB
JavaScript
1 line
14 KiB
JavaScript
![]() |
(window.webpackJsonp=window.webpackJsonp||[]).push([[4],{"/QjG":function(t,e,r){"use strict";r.r(e);var n=r("tDPm"),o=r("y3fX");class i{constructor(t,e,r){this.gl=t;const n=t.createProgram();if(null==n)throw new Error("Failed to create program.");this.program=n;let i=null,s=null;try{if(i=a(t,t.VERTEX_SHADER,e),t.attachShader(n,i),s=a(t,t.FRAGMENT_SHADER,r),t.attachShader(n,s),t.bindAttribLocation(n,o.b,"pos"),t.bindAttribLocation(n,o.a,"normal"),t.bindAttribLocation(n,o.c,"tex"),t.linkProgram(n),!t.getProgramParameter(n,t.LINK_STATUS)){const e=t.getProgramInfoLog(n);throw new Error("Shader linking failed. Program log:\n"+e)}this.mat_projection_loc=this.get_required_uniform_location(n,"mat_projection"),this.mat_camera_loc=this.get_required_uniform_location(n,"mat_camera"),this.mat_normal_loc=t.getUniformLocation(n,"mat_normal"),this.tex_sampler_loc=t.getUniformLocation(n,"tex_sampler"),t.detachShader(n,i),t.detachShader(n,s)}catch(e){throw t.deleteProgram(n),e}finally{t.deleteShader(i),t.deleteShader(s)}}set_mat_projection_uniform(t){this.gl.uniformMatrix4fv(this.mat_projection_loc,!1,t.data)}set_mat_camera_uniform(t){this.gl.uniformMatrix4fv(this.mat_camera_loc,!1,t.data)}set_mat_normal_uniform(t){this.gl.uniformMatrix3fv(this.mat_normal_loc,!1,t.data)}set_texture_uniform(t){this.gl.uniform1i(this.tex_sampler_loc,t-this.gl.TEXTURE0)}bind(){this.gl.useProgram(this.program)}unbind(){this.gl.useProgram(null)}delete(){this.gl.deleteProgram(this.program)}get_required_uniform_location(t,e){const r=this.gl.getUniformLocation(t,e);if(null==r)throw new Error(`Couldn't get ${e} uniform location.`);return r}}function a(t,e,r){const n=t.createShader(e);if(null==n)throw new Error(`Failed to create shader of type ${e}.`);if(t.shaderSource(n,r),t.compileShader(n),!t.getShaderParameter(n,t.COMPILE_STATUS)){const e=t.getShaderInfoLog(n);throw t.deleteShader(n),new Error("Vertex shader compilation failed. Shader log:\n"+e)}return n}var s="#version 300 es\r\n\r\nprecision mediump float;\r\n\r\nuniform mat4 mat_projection;\r\nuniform mat4 mat_camera;\r\nuniform mat3 mat_normal;\r\n\r\nin vec4 pos;\r\nin vec3 normal;\r\n\r\nout vec3 frag_normal;\r\n\r\nvoid main() {\r\n gl_Position = mat_projection * mat_camera * pos;\r\n frag_normal = normalize(mat_normal * normal);\r\n}\r\n",c="#version 300 es\r\n\r\nprecision mediump float;\r\n\r\nconst vec3 light_pos = normalize(vec3(-1, 1, 1));\r\nconst vec4 sky_color = vec4(1, 1, 1, 1);\r\nconst vec4 ground_color = vec4(0.1, 0.1, 0.1, 1);\r\n\r\nin vec3 frag_normal;\r\n\r\nout vec4 frag_color;\r\n\r\nvoid main() {\r\n float cos0 = dot(frag_normal, light_pos);\r\n float a = 0.5 + 0.5 * cos0;\r\n float a_back = 1.0 - a;\r\n\r\n if (gl_FrontFacing) {\r\n frag_color = mix(ground_color, sky_color, a);\r\n } else {\r\n frag_color = mix(ground_color, sky_color, a_back);\r\n }\r\n}\r\n",h="#version 300 es\r\n\r\nprecision mediump float;\r\n\r\nuniform mat4 mat_projection;\r\nuniform mat4 mat_camera;\r\n\r\nin vec4 pos;\r\nin vec2 tex;\r\n\r\nout vec2 f_tex;\r\n\r\nvoid main() {\r\n gl_Position = mat_projection * mat_camera * pos;\r\n f_tex = tex;\r\n}\r\n",d="#version 300 es\r\n\r\nprecision mediump float;\r\n\r\nuniform sampler2D tex_sampler;\r\n\r\nin vec2 f_tex;\r\n\r\nout vec4 frag_color;\r\n\r\nvoid main() {\r\n frag_color = texture(tex_sampler, f_tex);\r\n}\r\n",u=r("FLbM"),l=r("7/+l");class _{constructor(t){this.gl=t}create_gfx_mesh(t,e,r,n){var i;const a=this.gl;let s=null,c=null,h=null;try{if(s=a.createVertexArray(),null==s)throw new Error("Failed to create VAO.");if(c=a.createBuffer(),null==c)throw new Error("Failed to create vertex buffer.");if(h=a.createBuffer(),null==h)throw new Error("Failed to create index buffer.");a.bindVertexArray(s),a.bindBuffer(a.ARRAY_BUFFER,c),a.bufferData(a.ARRAY_BUFFER,e,a.STATIC_DRAW);const d=Object(o.f)(t);a.vertexAttribPointer(o.b,3,a.FLOAT,!0,d,0),a.enableVertexAttribArray(o.b);const u=Object(o.e)(t);-1!==u&&(a.vertexAttribPointer(o.a,3,a.FLOAT,!0,d,u),a.enableVertexAttribArray(o.a));const l=Object(o.g)(t);return-1!==l
|