提交 e604036b 编写于 作者: A alteredq

Changed normal map examples to use new normal map shader with standard lights.

Removed old normal map shader with custom lights.
上级 457b7a60
因为 它太大了无法显示 source diff 。你可以改为 查看blob
此差异已折叠。
......@@ -54,7 +54,6 @@
</div>
<script type="text/javascript" src="../build/Three.js"></script>
<script type="text/javascript" src="js/Detector.js"></script>
<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
<script type="text/javascript" src="js/Stats.js"></script>
......@@ -89,8 +88,8 @@
function init() {
container = document.createElement('div');
document.body.appendChild(container);
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 100000 );
camera.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
......@@ -141,22 +140,14 @@
uniforms[ "tDisplacement" ].texture = THREE.ImageUtils.loadTexture( "textures/normal/ninja/displacement.jpg" );
uniforms[ "uDisplacementBias" ].value = - 0.428408 * scale;
uniforms[ "uDisplacementScale" ].value = 2.436143 * scale;
uniforms[ "uPointLightPos" ].value = pointLight.position;
uniforms[ "uPointLightColor" ].value = pointLight.color;
uniforms[ "uDirLightPos" ].value = directionalLight.position;
uniforms[ "uDirLightColor" ].value = directionalLight.color;
uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
uniforms[ "uSpecularColor" ].value.setHex( specular );
uniforms[ "uAmbientColor" ].value.setHex( ambient );
uniforms[ "uShininess" ].value = shininess;
var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms };
var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true };
var material1 = new THREE.MeshShaderMaterial( parameters );
var material2 = new THREE.MeshPhongMaterial( { color: diffuse, specular: specular, ambient: ambient, shininess: shininess } );
......
......@@ -84,8 +84,8 @@
function init() {
container = document.createElement('div');
document.body.appendChild(container);
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 900;
......@@ -125,21 +125,13 @@
uniforms[ "enableDiffuse" ].value = true;
uniforms[ "enableSpecular" ].value = false;
uniforms[ "uPointLightPos" ].value = pointLight.position;
uniforms[ "uPointLightColor" ].value = pointLight.color;
uniforms[ "uDirLightPos" ].value = directionalLight.position;
uniforms[ "uDirLightColor" ].value = directionalLight.color;
uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
uniforms[ "uSpecularColor" ].value.setHex( specular );
uniforms[ "uAmbientColor" ].value.setHex( ambient );
uniforms[ "uShininess" ].value = shininess;
var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms };
var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true };
var material = new THREE.MeshShaderMaterial( parameters );
loader = new THREE.JSONLoader( true );
......
......@@ -86,217 +86,11 @@ THREE.ShaderUtils = {
// Normal map shader
// - Blinn-Phong
// - normal + diffuse + specular + AO + displacement maps
// - 1 point and 1 directional lights
// - point and directional lights (use with "lights: true" material option)
------------------------------------------------------------------------- */
'normal' : {
uniforms: {
"enableAO" : { type: "i", value: 0 },
"enableDiffuse" : { type: "i", value: 0 },
"enableSpecular": { type: "i", value: 0 },
"tDiffuse" : { type: "t", value: 0, texture: null },
"tNormal" : { type: "t", value: 2, texture: null },
"tSpecular" : { type: "t", value: 3, texture: null },
"tAO" : { type: "t", value: 4, texture: null },
"uNormalScale": { type: "f", value: 1.0 },
"tDisplacement": { type: "t", value: 5, texture: null },
"uDisplacementBias": { type: "f", value: -0.5 },
"uDisplacementScale": { type: "f", value: 2.5 },
"uPointLightPos": { type: "v3", value: new THREE.Vector3() },
"uPointLightColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
"uDirLightPos": { type: "v3", value: new THREE.Vector3() },
"uDirLightColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
"uAmbientLightColor": { type: "c", value: new THREE.Color( 0x050505 ) },
"uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
"uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
"uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
"uShininess": { type: "f", value: 30 }
},
fragmentShader: [
"uniform vec3 uDirLightPos;",
"uniform vec3 uAmbientLightColor;",
"uniform vec3 uDirLightColor;",
"uniform vec3 uPointLightColor;",
"uniform vec3 uAmbientColor;",
"uniform vec3 uDiffuseColor;",
"uniform vec3 uSpecularColor;",
"uniform float uShininess;",
"uniform bool enableDiffuse;",
"uniform bool enableSpecular;",
"uniform bool enableAO;",
"uniform sampler2D tDiffuse;",
"uniform sampler2D tNormal;",
"uniform sampler2D tSpecular;",
"uniform sampler2D tAO;",
"uniform float uNormalScale;",
"varying vec3 vTangent;",
"varying vec3 vBinormal;",
"varying vec3 vNormal;",
"varying vec2 vUv;",
"varying vec3 vPointLightVector;",
"varying vec3 vViewPosition;",
"void main() {",
"vec3 diffuseTex = vec3( 1.0 );",
"vec3 aoTex = vec3( 1.0 );",
"vec3 specularTex = vec3( 1.0, 1.0, 1.0 );",
"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
"normalTex.xy *= uNormalScale;",
"normalTex = normalize( normalTex );",
"if( enableDiffuse )",
"diffuseTex = texture2D( tDiffuse, vUv ).xyz;",
"if( enableAO )",
"aoTex = texture2D( tAO, vUv ).xyz;",
"if( enableSpecular )",
"specularTex = texture2D( tSpecular, vUv ).xyz;",
"mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
"vec3 finalNormal = tsb * normalTex;",
"vec3 normal = normalize( finalNormal );",
"vec3 viewPosition = normalize( vViewPosition );",
// point light
"vec4 pointDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec4 pointSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec3 pointVector = normalize( vPointLightVector );",
"vec3 pointHalfVector = normalize( vPointLightVector + vViewPosition );",
"float pointDotNormalHalf = dot( normal, pointHalfVector );",
"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
"float pointSpecularWeight = 0.0;",
"if ( pointDotNormalHalf >= 0.0 )",
"pointSpecularWeight = specularTex.r * pow( pointDotNormalHalf, uShininess );",
"pointDiffuse += vec4( uDiffuseColor, 1.0 ) * pointDiffuseWeight;",
"pointSpecular += vec4( uSpecularColor, 1.0 ) * pointSpecularWeight * pointDiffuseWeight;",
// directional light
"vec4 dirDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec4 dirSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );",
"vec4 lDirection = viewMatrix * vec4( uDirLightPos, 0.0 );",
"vec3 dirVector = normalize( lDirection.xyz );",
"vec3 dirHalfVector = normalize( lDirection.xyz + vViewPosition );",
"float dirDotNormalHalf = dot( normal, dirHalfVector );",
"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
"float dirSpecularWeight = 0.0;",
"if ( dirDotNormalHalf >= 0.0 )",
"dirSpecularWeight = specularTex.r * pow( dirDotNormalHalf, uShininess );",
"dirDiffuse += vec4( uDiffuseColor, 1.0 ) * dirDiffuseWeight;",
"dirSpecular += vec4( uSpecularColor, 1.0 ) * dirSpecularWeight * dirDiffuseWeight;",
// all lights contribution summation
"vec4 totalLight = vec4( uAmbientLightColor * uAmbientColor, 1.0 );",
"totalLight += vec4( uDirLightColor, 1.0 ) * ( dirDiffuse + dirSpecular );",
"totalLight += vec4( uPointLightColor, 1.0 ) * ( pointDiffuse + pointSpecular );",
"gl_FragColor = vec4( totalLight.xyz * aoTex * diffuseTex, 1.0 );",
"}"
].join("\n"),
vertexShader: [
"attribute vec4 tangent;",
"uniform vec3 uPointLightPos;",
"#ifdef VERTEX_TEXTURES",
"uniform sampler2D tDisplacement;",
"uniform float uDisplacementScale;",
"uniform float uDisplacementBias;",
"#endif",
"varying vec3 vTangent;",
"varying vec3 vBinormal;",
"varying vec3 vNormal;",
"varying vec2 vUv;",
"varying vec3 vPointLightVector;",
"varying vec3 vViewPosition;",
"void main() {",
"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
"vViewPosition = cameraPosition - mPosition.xyz;",
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
"vNormal = normalize( normalMatrix * normal );",
// tangent and binormal vectors
"vTangent = normalize( normalMatrix * tangent.xyz );",
"vBinormal = cross( vNormal, vTangent ) * tangent.w;",
"vBinormal = normalize( vBinormal );",
"vUv = uv;",
// point light
"vec4 lPosition = viewMatrix * vec4( uPointLightPos, 1.0 );",
"vPointLightVector = normalize( lPosition.xyz - mvPosition.xyz );",
// displacement mapping
"#ifdef VERTEX_TEXTURES",
"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
"float df = uDisplacementScale * dv.x + uDisplacementBias;",
"vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
"gl_Position = projectionMatrix * displacedPosition;",
"#else",
"gl_Position = projectionMatrix * mvPosition;",
"#endif",
"}"
].join("\n")
},
'normal2' : {
uniforms: THREE.UniformsUtils.merge( [
THREE.UniformsLib[ "lights" ],
......@@ -551,7 +345,7 @@ THREE.ShaderUtils = {
].join("\n")
},
/* -------------------------------------------------------------------------
// Cube map shader
------------------------------------------------------------------------- */
......
......@@ -690,7 +690,7 @@ THREE.SceneLoader.prototype = {
if ( m.parameters.normalMap ) {
var shader = THREE.ShaderUtils.lib[ "normal2" ];
var shader = THREE.ShaderUtils.lib[ "normal" ];
var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
var diffuse = m.parameters.color;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册