WebGLShadowMap.js 13 KB

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