From 94660cd68452b231cb56b38821aeee1f9495e54b Mon Sep 17 00:00:00 2001 From: ink-soul Date: Fri, 16 Jun 2023 16:49:35 +0800 Subject: [PATCH] init hw0 --- hw0/index.html | 1 + hw0/src/loads/loadOBJ.js | 4 +- hw0/src/materials/PhongMaterial.js | 38 +++++++++++++++ hw0/src/shaders/InternalShader.js | 76 ++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 hw0/src/materials/PhongMaterial.js diff --git a/hw0/index.html b/hw0/index.html index 759473c..3d25ab7 100644 --- a/hw0/index.html +++ b/hw0/index.html @@ -30,6 +30,7 @@ + diff --git a/hw0/src/loads/loadOBJ.js b/hw0/src/loads/loadOBJ.js index 7dfeefb..dda9b87 100644 --- a/hw0/src/loads/loadOBJ.js +++ b/hw0/src/loads/loadOBJ.js @@ -37,6 +37,7 @@ function loadOBJ(renderer, path, name) { let colorMap = null; if (mat.map != null) colorMap = new Texture(renderer.gl, mat.map.image); + /* // MARK: You can change the myMaterial object to your own Material instance let textureSample = 0; @@ -54,7 +55,8 @@ function loadOBJ(renderer, path, name) { 'uKd': { type: '3fv', value: mat.color.toArray() } },[],VertexShader, FragmentShader); } - + */ + let myMaterial = new PhongMaterial(mat.color.toArray(), colorMap , mat.specular.toArray(),renderer.lights[0].entity.mat. intensity); let meshRender = new MeshRender(renderer.gl, mesh, myMaterial); renderer.addMesh(meshRender); } diff --git a/hw0/src/materials/PhongMaterial.js b/hw0/src/materials/PhongMaterial.js new file mode 100644 index 0000000..ddd53d4 --- /dev/null +++ b/hw0/src/materials/PhongMaterial.js @@ -0,0 +1,38 @@ +class PhongMaterial extends Material +{ + + /* + * Creates an instance of PhongMaterial. + * @param {vec3f} color The material color + * @param {Texture} colorMap The texture object of the material + * @param {vec3f} specular The material specular coefficient + * @param {float} intensity The light intensity + * @member of PhongMaterial + */ + + constructor(color,colorMap,specular,intensity) + { + let textureSample = 0; + + if( colorMap != null) + { + textureSample = 1; + super({ + 'uTextureSampler': {type:'li',value:textureSample}, + 'uSampler' : {type:'texture',value:colorMap}, + 'uKd' : {type:'3fv',value : color}, + 'uKs' : {type:'3fv',value: specular}, + 'uLightIntensity' : {type:'lf',value:intensity} + },[],PhongVertexShader,PhongFragmentShader); + }else{ + console.log(color); + super({ + 'uTextureSampler': {type:'li',value:textureSample}, + 'uKd' : {type:'3fv',value : color}, + 'uKs' : {type:'3fv',value: specular}, + 'uLightIntensity' : {type:'lf',value:intensity} + },[],PhongVertexShader,PhongFragmentShader); + } + } + +} \ No newline at end of file diff --git a/hw0/src/shaders/InternalShader.js b/hw0/src/shaders/InternalShader.js index 2c41de5..2eb4342 100644 --- a/hw0/src/shaders/InternalShader.js +++ b/hw0/src/shaders/InternalShader.js @@ -74,4 +74,80 @@ void main(void) { } } +`; + +const PhongVertexShader = ` + +attribute vec3 aVertexPosition; +attribute vec3 aNormalPosition; +attribute vec2 aTextureCoord; + +uniform mat4 uModelViewMatrix; +uniform mat4 uProjectionMatrix; + +varying highp vec2 vTextureCoord; +varying highp vec3 vFragPos; +varying highp vec3 vNormal; + +void main(void) +{ + vFragPos = aVertexPosition; + + vNormal = aNormalPosition; + + gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aVertexPosition,1.0); + + vTextureCoord = aTextureCoord; +} +`; + +const PhongFragmentShader = ` + +#ifdef GL_ES +precision mediump float; +#endif + +uniform sampler2D uSampler; +//binn + +uniform vec3 uKd; +uniform vec3 uKs; +uniform vec3 uLightPos; +uniform vec3 uCameraPos; +uniform float uLightIntensity; +uniform int uTextureSample; + +varying highp vec2 vTextureCoord; +varying highp vec3 vFragPos; +varying highp vec3 vNormal; + +void main(void) +{ + vec3 color; + if (uTextureSample == 1 ) + { + color = pow(texture2D(uSampler , vTextureCoord).rgb, vec3(2.2)); + + }else + { + color = uKd; + } + + vec3 ambient = 0.05 *color; + + vec3 lightDir = normalize(uLightPos - vFragPos); + vec3 normal = normalize(vNormal); + float diff = max(dot(lightDir,normal),0.0); + float light_atten_coff = uLightIntensity / length(uLightPos - vFragPos); + vec3 diffuse = diff * light_atten_coff * color; + + vec3 viewDir = normalize(uCameraPos - vFragPos); + float spec = 0.0; + vec3 reflectDir = reflect(-lightDir,normal); + spec = pow(max(dot(viewDir,reflectDir),0.0),35.0); + vec3 specular = uKs * light_atten_coff * spec; + + gl_FragColor = vec4(pow((ambient + diffuse + specular),vec3(1.0/2.2)),1.0); +} + `; \ No newline at end of file