ShadowMapPlugin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.ShadowMapPlugin = function ( _renderer, _lights, _webglObjects, _webglObjectsImmediate ) {
  5. var _gl = _renderer.context;
  6. var _depthMaterial, _depthMaterialMorph, _depthMaterialSkin, _depthMaterialMorphSkin,
  7. _frustum = new THREE.Frustum(),
  8. _projScreenMatrix = new THREE.Matrix4(),
  9. _min = new THREE.Vector3(),
  10. _max = new THREE.Vector3(),
  11. _matrixPosition = new THREE.Vector3(),
  12. _renderList = [];
  13. // init
  14. var depthShader = THREE.ShaderLib[ "depthRGBA" ];
  15. var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms );
  16. _depthMaterial = new THREE.ShaderMaterial( {
  17. uniforms: depthUniforms,
  18. vertexShader: depthShader.vertexShader,
  19. fragmentShader: depthShader.fragmentShader
  20. } );
  21. _depthMaterialMorph = new THREE.ShaderMaterial( {
  22. uniforms: depthUniforms,
  23. vertexShader: depthShader.vertexShader,
  24. fragmentShader: depthShader.fragmentShader,
  25. morphTargets: true
  26. } );
  27. _depthMaterialSkin = new THREE.ShaderMaterial( {
  28. uniforms: depthUniforms,
  29. vertexShader: depthShader.vertexShader,
  30. fragmentShader: depthShader.fragmentShader,
  31. skinning: true
  32. } );
  33. _depthMaterialMorphSkin = new THREE.ShaderMaterial( {
  34. uniforms: depthUniforms,
  35. vertexShader: depthShader.vertexShader,
  36. fragmentShader: depthShader.fragmentShader,
  37. morphTargets: true,
  38. skinning: true
  39. } );
  40. _depthMaterial._shadowPass = true;
  41. _depthMaterialMorph._shadowPass = true;
  42. _depthMaterialSkin._shadowPass = true;
  43. _depthMaterialMorphSkin._shadowPass = true;
  44. this.render = function ( scene, camera ) {
  45. if ( _renderer.shadowMapEnabled === false ) return;
  46. var i, il, j, jl, n,
  47. shadowMap, shadowMatrix, shadowCamera,
  48. buffer, material,
  49. webglObject, object, light,
  50. lights = [],
  51. k = 0,
  52. fog = null;
  53. // set GL state for depth map
  54. _gl.clearColor( 1, 1, 1, 1 );
  55. _gl.disable( _gl.BLEND );
  56. _gl.enable( _gl.CULL_FACE );
  57. _gl.frontFace( _gl.CCW );
  58. if ( _renderer.shadowMapCullFace === THREE.CullFaceFront ) {
  59. _gl.cullFace( _gl.FRONT );
  60. } else {
  61. _gl.cullFace( _gl.BACK );
  62. }
  63. _renderer.state.setDepthTest( true );
  64. // preprocess lights
  65. // - skip lights that are not casting shadows
  66. // - create virtual lights for cascaded shadow maps
  67. for ( i = 0, il = _lights.length; i < il; i ++ ) {
  68. light = _lights[ i ];
  69. if ( ! light.castShadow ) continue;
  70. if ( ( light instanceof THREE.DirectionalLight ) && light.shadowCascade ) {
  71. for ( n = 0; n < light.shadowCascadeCount; n ++ ) {
  72. var virtualLight;
  73. if ( ! light.shadowCascadeArray[ n ] ) {
  74. virtualLight = createVirtualLight( light, n );
  75. virtualLight.originalCamera = camera;
  76. var gyro = new THREE.Gyroscope();
  77. gyro.position.copy( light.shadowCascadeOffset );
  78. gyro.add( virtualLight );
  79. gyro.add( virtualLight.target );
  80. camera.add( gyro );
  81. light.shadowCascadeArray[ n ] = virtualLight;
  82. //console.log( "Created virtualLight", virtualLight );
  83. } else {
  84. virtualLight = light.shadowCascadeArray[ n ];
  85. }
  86. updateVirtualLight( light, n );
  87. lights[ k ] = virtualLight;
  88. k ++;
  89. }
  90. } else {
  91. lights[ k ] = light;
  92. k ++;
  93. }
  94. }
  95. // render depth map
  96. for ( i = 0, il = lights.length; i < il; i ++ ) {
  97. light = lights[ i ];
  98. if ( ! light.shadowMap ) {
  99. var shadowFilter = THREE.LinearFilter;
  100. if ( _renderer.shadowMapType === THREE.PCFSoftShadowMap ) {
  101. shadowFilter = THREE.NearestFilter;
  102. }
  103. var pars = { minFilter: shadowFilter, magFilter: shadowFilter, format: THREE.RGBAFormat };
  104. light.shadowMap = new THREE.WebGLRenderTarget( light.shadowMapWidth, light.shadowMapHeight, pars );
  105. light.shadowMapSize = new THREE.Vector2( light.shadowMapWidth, light.shadowMapHeight );
  106. light.shadowMatrix = new THREE.Matrix4();
  107. }
  108. if ( ! light.shadowCamera ) {
  109. if ( light instanceof THREE.SpotLight ) {
  110. light.shadowCamera = new THREE.PerspectiveCamera( light.shadowCameraFov, light.shadowMapWidth / light.shadowMapHeight, light.shadowCameraNear, light.shadowCameraFar );
  111. } else if ( light instanceof THREE.DirectionalLight ) {
  112. light.shadowCamera = new THREE.OrthographicCamera( light.shadowCameraLeft, light.shadowCameraRight, light.shadowCameraTop, light.shadowCameraBottom, light.shadowCameraNear, light.shadowCameraFar );
  113. } else {
  114. THREE.error( "THREE.ShadowMapPlugin: Unsupported light type for shadow", light );
  115. continue;
  116. }
  117. scene.add( light.shadowCamera );
  118. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  119. }
  120. if ( light.shadowCameraVisible && ! light.cameraHelper ) {
  121. light.cameraHelper = new THREE.CameraHelper( light.shadowCamera );
  122. scene.add( light.cameraHelper );
  123. }
  124. if ( light.isVirtual && virtualLight.originalCamera == camera ) {
  125. updateShadowCamera( camera, light );
  126. }
  127. shadowMap = light.shadowMap;
  128. shadowMatrix = light.shadowMatrix;
  129. shadowCamera = light.shadowCamera;
  130. //
  131. shadowCamera.position.setFromMatrixPosition( light.matrixWorld );
  132. _matrixPosition.setFromMatrixPosition( light.target.matrixWorld );
  133. shadowCamera.lookAt( _matrixPosition );
  134. shadowCamera.updateMatrixWorld();
  135. shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
  136. //
  137. if ( light.cameraHelper ) light.cameraHelper.visible = light.shadowCameraVisible;
  138. if ( light.shadowCameraVisible ) light.cameraHelper.update();
  139. // compute shadow matrix
  140. shadowMatrix.set(
  141. 0.5, 0.0, 0.0, 0.5,
  142. 0.0, 0.5, 0.0, 0.5,
  143. 0.0, 0.0, 0.5, 0.5,
  144. 0.0, 0.0, 0.0, 1.0
  145. );
  146. shadowMatrix.multiply( shadowCamera.projectionMatrix );
  147. shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
  148. // update camera matrices and frustum
  149. _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  150. _frustum.setFromMatrix( _projScreenMatrix );
  151. // render shadow map
  152. _renderer.setRenderTarget( shadowMap );
  153. _renderer.clear();
  154. // set object matrices & frustum culling
  155. _renderList.length = 0;
  156. projectObject( scene, scene, shadowCamera );
  157. // render regular objects
  158. var objectMaterial, useMorphing, useSkinning;
  159. for ( j = 0, jl = _renderList.length; j < jl; j ++ ) {
  160. webglObject = _renderList[ j ];
  161. object = webglObject.object;
  162. buffer = webglObject.buffer;
  163. // culling is overriden globally for all objects
  164. // while rendering depth map
  165. // need to deal with MeshFaceMaterial somehow
  166. // in that case just use the first of material.materials for now
  167. // (proper solution would require to break objects by materials
  168. // similarly to regular rendering and then set corresponding
  169. // depth materials per each chunk instead of just once per object)
  170. objectMaterial = getObjectMaterial( object );
  171. useMorphing = object.geometry.morphTargets !== undefined && object.geometry.morphTargets.length > 0 && objectMaterial.morphTargets;
  172. useSkinning = object instanceof THREE.SkinnedMesh && objectMaterial.skinning;
  173. if ( object.customDepthMaterial ) {
  174. material = object.customDepthMaterial;
  175. } else if ( useSkinning ) {
  176. material = useMorphing ? _depthMaterialMorphSkin : _depthMaterialSkin;
  177. } else if ( useMorphing ) {
  178. material = _depthMaterialMorph;
  179. } else {
  180. material = _depthMaterial;
  181. }
  182. _renderer.setMaterialFaces( objectMaterial );
  183. if ( buffer instanceof THREE.BufferGeometry ) {
  184. _renderer.renderBufferDirect( shadowCamera, _lights, fog, material, buffer, object );
  185. } else {
  186. _renderer.renderBuffer( shadowCamera, _lights, fog, material, buffer, object );
  187. }
  188. }
  189. // set matrices and render immediate objects
  190. for ( j = 0, jl = _webglObjectsImmediate.length; j < jl; j ++ ) {
  191. webglObject = _webglObjectsImmediate[ j ];
  192. object = webglObject.object;
  193. if ( object.visible && object.castShadow ) {
  194. object._modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
  195. _renderer.renderImmediateObject( shadowCamera, _lights, fog, _depthMaterial, object );
  196. }
  197. }
  198. }
  199. // restore GL state
  200. var clearColor = _renderer.getClearColor(),
  201. clearAlpha = _renderer.getClearAlpha();
  202. _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha );
  203. _gl.enable( _gl.BLEND );
  204. if ( _renderer.shadowMapCullFace === THREE.CullFaceFront ) {
  205. _gl.cullFace( _gl.BACK );
  206. }
  207. _renderer.resetGLState();
  208. };
  209. function projectObject( scene, object, shadowCamera ) {
  210. if ( object.visible ) {
  211. var webglObjects = _webglObjects[ object.id ];
  212. if ( webglObjects && object.castShadow && (object.frustumCulled === false || _frustum.intersectsObject( object ) === true) ) {
  213. for ( var i = 0, l = webglObjects.length; i < l; i ++ ) {
  214. var webglObject = webglObjects[ i ];
  215. object._modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
  216. _renderList.push( webglObject );
  217. }
  218. }
  219. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  220. projectObject( scene, object.children[ i ], shadowCamera );
  221. }
  222. }
  223. }
  224. function createVirtualLight( light, cascade ) {
  225. var virtualLight = new THREE.DirectionalLight();
  226. virtualLight.isVirtual = true;
  227. virtualLight.onlyShadow = true;
  228. virtualLight.castShadow = true;
  229. virtualLight.shadowCameraNear = light.shadowCameraNear;
  230. virtualLight.shadowCameraFar = light.shadowCameraFar;
  231. virtualLight.shadowCameraLeft = light.shadowCameraLeft;
  232. virtualLight.shadowCameraRight = light.shadowCameraRight;
  233. virtualLight.shadowCameraBottom = light.shadowCameraBottom;
  234. virtualLight.shadowCameraTop = light.shadowCameraTop;
  235. virtualLight.shadowCameraVisible = light.shadowCameraVisible;
  236. virtualLight.shadowDarkness = light.shadowDarkness;
  237. virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
  238. virtualLight.shadowMapWidth = light.shadowCascadeWidth[ cascade ];
  239. virtualLight.shadowMapHeight = light.shadowCascadeHeight[ cascade ];
  240. virtualLight.pointsWorld = [];
  241. virtualLight.pointsFrustum = [];
  242. var pointsWorld = virtualLight.pointsWorld,
  243. pointsFrustum = virtualLight.pointsFrustum;
  244. for ( var i = 0; i < 8; i ++ ) {
  245. pointsWorld[ i ] = new THREE.Vector3();
  246. pointsFrustum[ i ] = new THREE.Vector3();
  247. }
  248. var nearZ = light.shadowCascadeNearZ[ cascade ];
  249. var farZ = light.shadowCascadeFarZ[ cascade ];
  250. pointsFrustum[ 0 ].set( - 1, - 1, nearZ );
  251. pointsFrustum[ 1 ].set( 1, - 1, nearZ );
  252. pointsFrustum[ 2 ].set( - 1, 1, nearZ );
  253. pointsFrustum[ 3 ].set( 1, 1, nearZ );
  254. pointsFrustum[ 4 ].set( - 1, - 1, farZ );
  255. pointsFrustum[ 5 ].set( 1, - 1, farZ );
  256. pointsFrustum[ 6 ].set( - 1, 1, farZ );
  257. pointsFrustum[ 7 ].set( 1, 1, farZ );
  258. return virtualLight;
  259. }
  260. // Synchronize virtual light with the original light
  261. function updateVirtualLight( light, cascade ) {
  262. var virtualLight = light.shadowCascadeArray[ cascade ];
  263. virtualLight.position.copy( light.position );
  264. virtualLight.target.position.copy( light.target.position );
  265. virtualLight.lookAt( virtualLight.target );
  266. virtualLight.shadowCameraVisible = light.shadowCameraVisible;
  267. virtualLight.shadowDarkness = light.shadowDarkness;
  268. virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
  269. var nearZ = light.shadowCascadeNearZ[ cascade ];
  270. var farZ = light.shadowCascadeFarZ[ cascade ];
  271. var pointsFrustum = virtualLight.pointsFrustum;
  272. pointsFrustum[ 0 ].z = nearZ;
  273. pointsFrustum[ 1 ].z = nearZ;
  274. pointsFrustum[ 2 ].z = nearZ;
  275. pointsFrustum[ 3 ].z = nearZ;
  276. pointsFrustum[ 4 ].z = farZ;
  277. pointsFrustum[ 5 ].z = farZ;
  278. pointsFrustum[ 6 ].z = farZ;
  279. pointsFrustum[ 7 ].z = farZ;
  280. }
  281. // Fit shadow camera's ortho frustum to camera frustum
  282. function updateShadowCamera( camera, light ) {
  283. var shadowCamera = light.shadowCamera,
  284. pointsFrustum = light.pointsFrustum,
  285. pointsWorld = light.pointsWorld;
  286. _min.set( Infinity, Infinity, Infinity );
  287. _max.set( - Infinity, - Infinity, - Infinity );
  288. for ( var i = 0; i < 8; i ++ ) {
  289. var p = pointsWorld[ i ];
  290. p.copy( pointsFrustum[ i ] );
  291. p.unproject( camera );
  292. p.applyMatrix4( shadowCamera.matrixWorldInverse );
  293. if ( p.x < _min.x ) _min.x = p.x;
  294. if ( p.x > _max.x ) _max.x = p.x;
  295. if ( p.y < _min.y ) _min.y = p.y;
  296. if ( p.y > _max.y ) _max.y = p.y;
  297. if ( p.z < _min.z ) _min.z = p.z;
  298. if ( p.z > _max.z ) _max.z = p.z;
  299. }
  300. shadowCamera.left = _min.x;
  301. shadowCamera.right = _max.x;
  302. shadowCamera.top = _max.y;
  303. shadowCamera.bottom = _min.y;
  304. // can't really fit near/far
  305. //shadowCamera.near = _min.z;
  306. //shadowCamera.far = _max.z;
  307. shadowCamera.updateProjectionMatrix();
  308. }
  309. // For the moment just ignore objects that have multiple materials with different animation methods
  310. // Only the first material will be taken into account for deciding which depth material to use for shadow maps
  311. function getObjectMaterial( object ) {
  312. return object.material instanceof THREE.MeshFaceMaterial
  313. ? object.material.materials[ 0 ]
  314. : object.material;
  315. };
  316. };