webgl_stencil.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - webgl</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 material = new THREE.MeshNormalMaterial();
  38. var mesh1 = new THREE.Mesh( cube, material );
  39. mesh1.position.z = -150;
  40. scene.addChild( mesh1 );
  41. new THREE.ShadowVolume( mesh1 )
  42. var mesh2 = new THREE.Mesh( cube, material );
  43. mesh2.position.x = -150;
  44. mesh2.rotation.y = 90 * Math.PI / 180;
  45. scene.addChild( mesh2 );
  46. new THREE.ShadowVolume( mesh2 )
  47. var mesh3 = new THREE.Mesh( cube, material );
  48. mesh3.position.y = -150;
  49. mesh3.rotation.x = 90 * Math.PI / 180;
  50. scene.addChild( mesh3 );
  51. new THREE.ShadowVolume( mesh3 )
  52. // shadow
  53. var cube = new Cube( 40, 40, 40 );
  54. var torus = new Torus( 40, 10 );
  55. var sphere = new Sphere( 40 );
  56. var cylinder = new Cylinder( 10, 10, 20, 40, 0, 0 );
  57. mesh = new THREE.Mesh( torus, material );
  58. scene.addChild( mesh );
  59. new THREE.ShadowVolume( mesh );
  60. boxMesh = new THREE.Mesh( cube, material );
  61. scene.addChild( boxMesh );
  62. new THREE.ShadowVolume( boxMesh );
  63. //shadowVolume.position.x = 40;
  64. light = new THREE.DirectionalLight();
  65. light.position.set( 0, 1, 0 );
  66. scene.addChild( light );
  67. light2 = new THREE.DirectionalLight();
  68. light2.position.set( 0, 1, 0 );
  69. scene.addChild( light2 );
  70. var cube = new Cube( 10, 10, 10 );
  71. lightCube = new THREE.Mesh( cube, material );
  72. scene.addChild( lightCube );
  73. lightCube2 = new THREE.Mesh( cube, material );
  74. scene.addChild( lightCube2 );
  75. renderer = new THREE.WebGLRenderer();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. container.appendChild( renderer.domElement );
  78. if ( statsEnabled ) {
  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. setInterval( loop, 1000 / 60 );
  86. }
  87. function onDocumentMouseMove(event) {
  88. mouseX = ( event.clientX - windowHalfX );
  89. mouseY = ( event.clientY - windowHalfY );
  90. }
  91. var t = 0;
  92. function loop() {
  93. mesh.position.x = Math.sin( t ) * 100;
  94. mesh.position.y = Math.cos( t ) * 100;
  95. mesh.rotation.x += 0.5 * Math.PI / 180;
  96. mesh.rotation.y += 1.0 * Math.PI / 180;
  97. mesh.rotation.z += 1.5 * Math.PI / 180;
  98. boxMesh.position.z = Math.sin( t ) * 100;
  99. boxMesh.rotation.x = Math.sin( t ) * 180 * Math.PI / 180;
  100. light.position.x = Math.sin( t );
  101. light.position.y = 1;
  102. light.position.z = Math.cos( t );
  103. light2.position.x = Math.cos( t + 1 );
  104. light2.position.y = 0.5;
  105. light2.position.z = Math.sin( t + 1 );
  106. lightCube.position.copy( light.position );
  107. lightCube.position.multiplyScalar( 200 );
  108. lightCube2.position.copy( light2.position );
  109. lightCube2.position.multiplyScalar( 250 );
  110. t += 0.02;
  111. camera.position.x += ( mouseX - camera.position.x ) * .05;
  112. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  113. renderer.render( scene, camera );
  114. if ( statsEnabled ) stats.update();
  115. }
  116. function log( text ) {
  117. var e = document.getElementById("log");
  118. e.innerHTML = text + "<br/>" + e.innerHTML;
  119. }
  120. </script>
  121. </body>
  122. </html>
粤ICP备19079148号