webgl_stencil.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - stencil shadow</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background:#fff;
  9. padding:0;
  10. margin:0;
  11. font-weight: bold;
  12. overflow:hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script type="text/javascript" src="../build/Three.js"></script>
  18. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  19. <script type="text/javascript" src="js/Stats.js"></script>
  20. <script type="text/javascript">
  21. var container, stats;
  22. var camera, scene, renderer;
  23. var mesh, boxMesh, light, lightCube, light2, lightCube2, zmesh, lightMesh, geometry;
  24. var mouseX = 0, mouseY = 0;
  25. var windowHalfX = window.innerWidth / 2;
  26. var windowHalfY = window.innerHeight / 2;
  27. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  28. init();
  29. animate();
  30. function init() {
  31. container = document.createElement( 'div' );
  32. document.body.appendChild( container );
  33. camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  34. camera.position.z = 250;
  35. scene = new THREE.Scene();
  36. // world
  37. var cube = new THREE.Cube( 300, 300, 10 );
  38. var material0 = new THREE.MeshPhongMaterial( { color:0xff00ff } );
  39. var material1 = new THREE.MeshLambertMaterial( { color:0x00ff00 } );
  40. var material2 = new THREE.MeshLambertMaterial( { color:0x0000ff } );
  41. var mesh1 = new THREE.Mesh( cube, material0 );
  42. mesh1.position.z = - 150;
  43. scene.addChild( mesh1 );
  44. var mesh2 = new THREE.Mesh( cube, material1 );
  45. mesh2.position.x = - 150;
  46. mesh2.rotation.y = 90 * Math.PI / 180;
  47. scene.addChild( mesh2 );
  48. var mesh3 = new THREE.Mesh( cube, material2 );
  49. mesh3.position.y = - 150;
  50. mesh3.rotation.x = 90 * Math.PI / 180;
  51. scene.addChild( mesh3 );
  52. new THREE.ShadowVolume( mesh1 )
  53. new THREE.ShadowVolume( mesh2 )
  54. new THREE.ShadowVolume( mesh3 )
  55. // moving objects
  56. var cube = new THREE.Cube( 40, 40, 40 );
  57. var torus = new THREE.Torus( 40, 10 );
  58. var sphere = new THREE.Sphere( 40 );
  59. var cylinder = new THREE.Cylinder( 10, 10, 20, 40, 0, 0 );
  60. mesh = new THREE.Mesh( torus, material1 );
  61. scene.addChild( mesh );
  62. boxMesh = new THREE.Mesh( cube, material2 );
  63. scene.addChild( boxMesh );
  64. new THREE.ShadowVolume( mesh );
  65. new THREE.ShadowVolume( boxMesh );
  66. // lights
  67. light = new THREE.DirectionalLight( 0xffffff );
  68. light.castShadow = true;
  69. light.position.set( 0, 1, 0 );
  70. scene.addChild( light );
  71. var cube = new THREE.Sphere( 5 );
  72. lightCube = new THREE.Mesh( cube, material2 );
  73. scene.addChild( lightCube );
  74. // renderer
  75. renderer = new THREE.WebGLRenderer();
  76. renderer.setClearColorHex( 0xaaaaaa, 1 );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. container.appendChild( renderer.domElement );
  79. stats = new Stats();
  80. stats.domElement.style.position = 'absolute';
  81. stats.domElement.style.top = '0px';
  82. stats.domElement.style.zIndex = 100;
  83. container.appendChild( stats.domElement );
  84. }
  85. function onDocumentMouseMove(event) {
  86. mouseX = ( event.clientX - windowHalfX );
  87. mouseY = ( event.clientY - windowHalfY );
  88. }
  89. function animate() {
  90. requestAnimationFrame( animate );
  91. render();
  92. stats.update();
  93. }
  94. var t = 0;
  95. function render() {
  96. mesh.position.x = Math.sin( t ) * 100;
  97. mesh.position.y = Math.cos( t ) * 100;
  98. mesh.rotation.x += 0.5 * Math.PI / 180;
  99. mesh.rotation.y += 1.0 * Math.PI / 180;
  100. mesh.rotation.z += 1.5 * Math.PI / 180;
  101. boxMesh.position.z = Math.sin( t ) * 100;
  102. boxMesh.rotation.x = Math.sin( t ) * 180 * Math.PI / 180;
  103. light.position.x = Math.sin( t );
  104. light.position.y = 1.5;
  105. light.position.z = Math.cos( t );
  106. lightCube.position.copy( light.position );
  107. lightCube.position.multiplyScalar( 200 );
  108. t += 0.02;
  109. camera.position.x += ( mouseX - camera.position.x ) * .05;
  110. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  111. renderer.render( scene, camera );
  112. }
  113. function log( text ) {
  114. var e = document.getElementById("log");
  115. e.innerHTML = text + "<br/>" + e.innerHTML;
  116. }
  117. </script>
  118. </body>
  119. </html>