webgl_stencil.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/Stats.js"></script>
  19. <script type="text/javascript">
  20. var statsEnabled = true;
  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. function init() {
  30. container = document.createElement( 'div' );
  31. document.body.appendChild( container );
  32. camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  33. camera.position.z = 250;
  34. scene = new THREE.Scene();
  35. // world
  36. var cube = new Cube( 300, 300, 10 );
  37. var material0 = new THREE.MeshPhongMaterial( { color:0xff00ff } );
  38. var material1 = new THREE.MeshLambertMaterial( { color:0x00ff00 } );
  39. var material2 = new THREE.MeshLambertMaterial( { color:0x0000ff } );
  40. var mesh1 = new THREE.Mesh( cube, material0 );
  41. mesh1.position.z = -150;
  42. scene.addChild( mesh1 );
  43. var mesh2 = new THREE.Mesh( cube, material1 );
  44. mesh2.position.x = -150;
  45. mesh2.rotation.y = 90 * Math.PI / 180;
  46. scene.addChild( mesh2 );
  47. var mesh3 = new THREE.Mesh( cube, material2 );
  48. mesh3.position.y = -150;
  49. mesh3.rotation.x = 90 * Math.PI / 180;
  50. scene.addChild( mesh3 );
  51. new THREE.ShadowVolume( mesh1 )
  52. new THREE.ShadowVolume( mesh2 )
  53. new THREE.ShadowVolume( mesh3 )
  54. // moving objects
  55. var cube = new Cube( 40, 40, 40 );
  56. var torus = new Torus( 40, 10 );
  57. var sphere = new Sphere( 40 );
  58. var cylinder = new Cylinder( 10, 10, 20, 40, 0, 0 );
  59. mesh = new THREE.Mesh( torus, material1 );
  60. scene.addChild( mesh );
  61. boxMesh = new THREE.Mesh( cube, material2 );
  62. scene.addChild( boxMesh );
  63. new THREE.ShadowVolume( mesh );
  64. new THREE.ShadowVolume( boxMesh );
  65. // lights
  66. light = new THREE.DirectionalLight( 0xffffff );
  67. light.position.set( 0, 1, 0 );
  68. scene.addChild( light );
  69. var cube = new Sphere( 5 );
  70. lightCube = new THREE.Mesh( cube, material2 );
  71. scene.addChild( lightCube );
  72. // renderer
  73. renderer = new THREE.WebGLRenderer();
  74. renderer.setClearColorHex( 0xaaaaaa, 1 );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. container.appendChild( renderer.domElement );
  77. if ( statsEnabled ) {
  78. stats = new Stats();
  79. stats.domElement.style.position = 'absolute';
  80. stats.domElement.style.top = '0px';
  81. stats.domElement.style.zIndex = 100;
  82. container.appendChild( stats.domElement );
  83. }
  84. setInterval( loop, 1000 / 60 );
  85. }
  86. function onDocumentMouseMove(event) {
  87. mouseX = ( event.clientX - windowHalfX );
  88. mouseY = ( event.clientY - windowHalfY );
  89. }
  90. var t = 0;
  91. function loop() {
  92. mesh.position.x = Math.sin( t ) * 100;
  93. mesh.position.y = Math.cos( t ) * 100;
  94. mesh.rotation.x += 0.5 * Math.PI / 180;
  95. mesh.rotation.y += 1.0 * Math.PI / 180;
  96. mesh.rotation.z += 1.5 * Math.PI / 180;
  97. boxMesh.position.z = Math.sin( t ) * 100;
  98. boxMesh.rotation.x = Math.sin( t ) * 180 * Math.PI / 180;
  99. light.position.x = Math.sin( t );
  100. light.position.y = 1.5;
  101. light.position.z = Math.cos( t );
  102. lightCube.position.copy( light.position );
  103. lightCube.position.multiplyScalar( 200 );
  104. t += 0.02;
  105. camera.position.x += ( mouseX - camera.position.x ) * .05;
  106. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  107. renderer.render( scene, camera );
  108. if ( statsEnabled ) stats.update();
  109. }
  110. function log( text ) {
  111. var e = document.getElementById("log");
  112. e.innerHTML = text + "<br/>" + e.innerHTML;
  113. }
  114. </script>
  115. </body>
  116. </html>