提交 13c4aa32 编写于 作者: A alteredq

Fixed standard materials shaders to use properly transformed positions and...

Fixed standard materials shaders to use properly transformed positions and normals with skinning and morphing.

Also while at it I did some shader snippets cleanup and optimizations here and there.

There were quite drastic changes, impacting almost everything. It should work, I went through all WebGL examples, but you never know, there are myriads of possible material combinations. Please let me know if you find something broken.
上级 bec9e83c
此差异已折叠。
此差异已折叠。
......@@ -275,6 +275,8 @@ THREE.ShaderSkin = {
"void main() {",
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
"vec4 mPosition = modelMatrix * vec4( position, 1.0 );",
"vViewPosition = -mvPosition.xyz;",
"vNormal = normalMatrix * normal;",
......
......@@ -10,7 +10,7 @@
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
......@@ -37,7 +37,7 @@
var camera, cubeCamera, scene, renderer;
var cube, sphere, torus;
var fov = 70,
isUserInteracting = false,
onMouseDownMouseX = 0, onMouseDownMouseY = 0,
......@@ -74,7 +74,7 @@
//
var material = new THREE.MeshBasicMaterial( { envMap: cubeCamera.renderTarget } );
sphere = new THREE.Mesh( new THREE.SphereGeometry( 20, 60, 40 ), material );
scene.add( sphere );
......@@ -90,11 +90,11 @@
document.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
document.addEventListener( 'DOMMouseScroll', onDocumentMouseWheel, false);
window.addEventListener( 'resize', onWindowResized, false );
onWindowResized( null );
}
function onWindowResized( event ) {
renderer.setSize( window.innerWidth, window.innerHeight );
......@@ -153,7 +153,7 @@
}
camera.projectionMatrix.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
}
function animate() {
......@@ -164,7 +164,7 @@
}
function render() {
var time = Date.now();
lon += .15;
......@@ -211,6 +211,6 @@
}
</script>
</body>
</html>
......@@ -89,6 +89,8 @@
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x000104, 0.0000675 );
camera.lookAt( scene.position );
//
aloader = new THREE.JSONLoader( );
......
......@@ -628,13 +628,13 @@ THREE.ShaderUtils = {
//
"vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
"vec4 wPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
"vec4 mPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
"gl_Position = projectionMatrix * mvPosition;",
//
"vWorldPosition = wPosition.xyz;",
"vWorldPosition = mPosition.xyz;",
// shadows
......@@ -642,7 +642,7 @@ THREE.ShaderUtils = {
"for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
"vShadowCoord[ i ] = shadowMatrix[ i ] * wPosition;",
"vShadowCoord[ i ] = shadowMatrix[ i ] * mPosition;",
"}",
......
......@@ -149,17 +149,37 @@ THREE.ShaderChunk = {
].join("\n"),
envmap_vertex : [
worldpos_vertex : [
"#ifdef USE_ENVMAP",
"#if defined( USE_ENVMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP )",
"#ifdef USE_SKINNING",
"vec4 mPosition = modelMatrix * vec4( position, 1.0 );",
"vec4 mPosition = modelMatrix * skinned;",
"#endif",
"#endif",
"#if defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )",
"vec4 mPosition = modelMatrix * vec4( morphed, 1.0 );",
"#endif",
"#if ! defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )",
"vec4 mPosition = modelMatrix * vec4( position, 1.0 );",
"#endif",
"#endif"
].join("\n"),
envmap_vertex : [
"#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP )",
"vec3 nWorld = mat3( modelMatrix[ 0 ].xyz, modelMatrix[ 1 ].xyz, modelMatrix[ 2 ].xyz ) * normal;",
"vec3 nWorld = mat3( modelMatrix[ 0 ].xyz, modelMatrix[ 1 ].xyz, modelMatrix[ 2 ].xyz ) * objectNormal;",
"if ( useRefract ) {",
......@@ -1200,35 +1220,33 @@ THREE.ShaderChunk = {
"morphed += position;",
//"gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );",
"#endif"
].join("\n"),
default_vertex : [
"vec4 mvPosition;",
"#ifdef USE_SKINNING",
"gl_Position = projectionMatrix * modelViewMatrix * skinned;",
"mvPosition = modelViewMatrix * skinned;",
"#endif",
"#ifndef USE_SKINNING",
"#ifdef USE_MORPHTARGETS",
"#if !defined( USE_SKINNING ) && defined( USE_MORPHTARGETS )",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );",
"mvPosition = modelViewMatrix * vec4( morphed, 1.0 );",
"#endif",
"#endif",
"#ifndef USE_MORPHTARGETS",
"#ifndef USE_SKINNING",
"#if !defined( USE_SKINNING ) && ! defined( USE_MORPHTARGETS )",
"gl_Position = projectionMatrix * mvPosition;",
"mvPosition = modelViewMatrix * vec4( position, 1.0 );",
"#endif",
"#endif"
"gl_Position = projectionMatrix * mvPosition;",
].join("\n"),
......@@ -1272,31 +1290,27 @@ THREE.ShaderChunk = {
defaultnormal_vertex: [
"vec3 transformedNormal;",
"vec3 objectNormal;",
"#ifdef USE_SKINNING",
"transformedNormal = skinnedNormal.xyz;",
"objectNormal = skinnedNormal.xyz;",
"#endif",
"#ifndef USE_SKINNING",
"#ifdef USE_MORPHNORMALS",
"#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )",
"transformedNormal = morphedNormal;",
"objectNormal = morphedNormal;",
"#endif",
"#endif",
"#ifndef USE_MORPHNORMALS",
"#ifndef USE_SKINNING",
"#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )",
"transformedNormal = normal;",
"objectNormal = normal;",
"#endif",
"#endif",
"transformedNormal = normalMatrix * transformedNormal;",
"vec3 transformedNormal = normalMatrix * objectNormal;",
].join("\n"),
......@@ -1516,28 +1530,9 @@ THREE.ShaderChunk = {
"#ifdef USE_SHADOWMAP",
"vec4 transformedPosition;",
"#ifdef USE_SKINNING",
"transformedPosition = modelMatrix * skinned;",
"#else",
"#ifdef USE_MORPHTARGETS",
"transformedPosition = modelMatrix * vec4( morphed, 1.0 );",
"#else",
"transformedPosition = modelMatrix * vec4( position, 1.0 );",
"#endif",
"#endif",
"for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
"vShadowCoord[ i ] = shadowMatrix[ i ] * transformedPosition;",
"vShadowCoord[ i ] = shadowMatrix[ i ] * mPosition;",
"}",
......@@ -1824,16 +1819,25 @@ THREE.ShaderLib = {
"void main() {",
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
THREE.ShaderChunk[ "map_vertex" ],
THREE.ShaderChunk[ "lightmap_vertex" ],
THREE.ShaderChunk[ "envmap_vertex" ],
THREE.ShaderChunk[ "color_vertex" ],
"#ifdef USE_ENVMAP",
THREE.ShaderChunk[ "morphnormal_vertex" ],
THREE.ShaderChunk[ "skinbase_vertex" ],
THREE.ShaderChunk[ "skinnormal_vertex" ],
THREE.ShaderChunk[ "defaultnormal_vertex" ],
"#endif",
THREE.ShaderChunk[ "morphtarget_vertex" ],
THREE.ShaderChunk[ "skinning_vertex" ],
THREE.ShaderChunk[ "default_vertex" ],
THREE.ShaderChunk[ "worldpos_vertex" ],
THREE.ShaderChunk[ "envmap_vertex" ],
THREE.ShaderChunk[ "shadowmap_vertex" ],
"}"
......@@ -1894,6 +1898,8 @@ THREE.ShaderLib = {
vertexShader: [
"#define LAMBERT",
"varying vec3 vLightFront;",
"#ifdef DOUBLE_SIDED",
......@@ -1913,11 +1919,8 @@ THREE.ShaderLib = {
"void main() {",
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
THREE.ShaderChunk[ "map_vertex" ],
THREE.ShaderChunk[ "lightmap_vertex" ],
THREE.ShaderChunk[ "envmap_vertex" ],
THREE.ShaderChunk[ "color_vertex" ],
THREE.ShaderChunk[ "morphnormal_vertex" ],
......@@ -1925,16 +1928,13 @@ THREE.ShaderLib = {
THREE.ShaderChunk[ "skinnormal_vertex" ],
THREE.ShaderChunk[ "defaultnormal_vertex" ],
"#ifndef USE_ENVMAP",
"vec4 mPosition = modelMatrix * vec4( position, 1.0 );",
"#endif",
THREE.ShaderChunk[ "lights_lambert_vertex" ],
THREE.ShaderChunk[ "morphtarget_vertex" ],
THREE.ShaderChunk[ "skinning_vertex" ],
THREE.ShaderChunk[ "default_vertex" ],
THREE.ShaderChunk[ "worldpos_vertex" ],
THREE.ShaderChunk[ "envmap_vertex" ],
THREE.ShaderChunk[ "lights_lambert_vertex" ],
THREE.ShaderChunk[ "shadowmap_vertex" ],
"}"
......@@ -2022,6 +2022,8 @@ THREE.ShaderLib = {
vertexShader: [
"#define PHONG",
"varying vec3 vViewPosition;",
"varying vec3 vNormal;",
......@@ -2036,21 +2038,10 @@ THREE.ShaderLib = {
"void main() {",
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
THREE.ShaderChunk[ "map_vertex" ],
THREE.ShaderChunk[ "lightmap_vertex" ],
THREE.ShaderChunk[ "envmap_vertex" ],
THREE.ShaderChunk[ "color_vertex" ],
"#ifndef USE_ENVMAP",
"vec4 mPosition = modelMatrix * vec4( position, 1.0 );",
"#endif",
"vViewPosition = -mvPosition.xyz;",
THREE.ShaderChunk[ "morphnormal_vertex" ],
THREE.ShaderChunk[ "skinbase_vertex" ],
THREE.ShaderChunk[ "skinnormal_vertex" ],
......@@ -2058,10 +2049,15 @@ THREE.ShaderLib = {
"vNormal = transformedNormal;",
THREE.ShaderChunk[ "lights_phong_vertex" ],
THREE.ShaderChunk[ "morphtarget_vertex" ],
THREE.ShaderChunk[ "skinning_vertex" ],
THREE.ShaderChunk[ "default_vertex" ],
"vViewPosition = -mvPosition.xyz;",
THREE.ShaderChunk[ "worldpos_vertex" ],
THREE.ShaderChunk[ "envmap_vertex" ],
THREE.ShaderChunk[ "lights_phong_vertex" ],
THREE.ShaderChunk[ "shadowmap_vertex" ],
"}"
......@@ -2144,6 +2140,7 @@ THREE.ShaderLib = {
"gl_Position = projectionMatrix * mvPosition;",
THREE.ShaderChunk[ "worldpos_vertex" ],
THREE.ShaderChunk[ "shadowmap_vertex" ],
"}"
......@@ -2195,8 +2192,6 @@ THREE.ShaderLib = {
"void main() {",
"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
THREE.ShaderChunk[ "skinbase_vertex" ],
THREE.ShaderChunk[ "morphtarget_vertex" ],
THREE.ShaderChunk[ "skinning_vertex" ],
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册