init hw0
							parent
							
								
									f449127608
								
							
						
					
					
						commit
						94660cd684
					
				| 
						 | 
				
			
			@ -30,6 +30,7 @@
 | 
			
		|||
    <script src="src/shaders/InternalShader.js" defer></script>
 | 
			
		||||
    <script src="src/shaders/Shader.js" defer></script>
 | 
			
		||||
    <script src="src/materials/Material.js" defer></script>
 | 
			
		||||
    <script src="src/materials/PhongMaterial.js" defer></script>
 | 
			
		||||
    <script src="src/textures/Texture.js" defer></script>
 | 
			
		||||
 | 
			
		||||
    <script src="src/objects/Mesh.js" defer></script>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
						}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
`;
 | 
			
		||||
		Loading…
	
		Reference in New Issue