webgl_materials_variations_basic.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #fff;
  10. font-family: Monospace;
  11. font-size: 13px;
  12. text-align: center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="container"></div>
  26. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Basic Material Variantions by <a href="http://clara.io/" target="_blank" rel="noopener">Ben Houston</a>.</div>
  27. <script src="../build/three.js"></script>
  28. <script src="js/controls/OrbitControls.js"></script>
  29. <script src="js/WebGL.js"></script>
  30. <script src="js/libs/stats.min.js"></script>
  31. <script>
  32. if ( WEBGL.isWebGLAvailable() === false ) {
  33. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  34. }
  35. var container, stats;
  36. var camera, scene, renderer, controls;
  37. var particleLight;
  38. var loader = new THREE.FontLoader();
  39. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  40. init( font );
  41. animate();
  42. } );
  43. function init( font ) {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  47. camera.position.set( 0.0, 400, 400 * 3.5 );
  48. //
  49. var reflectionCube = new THREE.CubeTextureLoader()
  50. .setPath( 'textures/cube/SwedishRoyalCastle/' )
  51. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  52. reflectionCube.format = THREE.RGBFormat;
  53. scene = new THREE.Scene();
  54. scene.background = reflectionCube;
  55. // Materials
  56. var imgTexture = new THREE.TextureLoader().load( "textures/planets/moon_1024.jpg" );
  57. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  58. imgTexture.anisotropy = 16;
  59. imgTexture = null;
  60. var cubeWidth = 400;
  61. var numberOfSphersPerSide = 5;
  62. var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  63. var stepSize = 1.0 / numberOfSphersPerSide;
  64. var geometry = new THREE.SphereBufferGeometry( sphereRadius, 32, 16 );
  65. for ( var alpha = 0; alpha <= 1.0; alpha += stepSize ) {
  66. for ( var beta = 0; beta <= 1.0; beta += stepSize ) {
  67. for ( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  68. var diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 );
  69. var material = new THREE.MeshBasicMaterial( {
  70. map: imgTexture,
  71. color: diffuseColor,
  72. reflectivity: beta,
  73. envMap: alpha < 0.5 ? reflectionCube : null
  74. } );
  75. var mesh = new THREE.Mesh( geometry, material );
  76. mesh.position.x = alpha * 400 - 200;
  77. mesh.position.y = beta * 400 - 200;
  78. mesh.position.z = gamma * 400 - 200;
  79. scene.add( mesh );
  80. }
  81. }
  82. }
  83. function addLabel( name, location ) {
  84. var textGeo = new THREE.TextBufferGeometry( name, {
  85. font: font,
  86. size: 20,
  87. height: 1,
  88. curveSegments: 1
  89. } );
  90. var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  91. var textMesh = new THREE.Mesh( textGeo, textMaterial );
  92. textMesh.position.copy( location );
  93. scene.add( textMesh );
  94. }
  95. addLabel( "+hue", new THREE.Vector3( - 350, 0, 0 ) );
  96. addLabel( "-hue", new THREE.Vector3( 350, 0, 0 ) );
  97. addLabel( "-reflectivity", new THREE.Vector3( 0, - 300, 0 ) );
  98. addLabel( "+reflectivity", new THREE.Vector3( 0, 300, 0 ) );
  99. addLabel( "-diffuse", new THREE.Vector3( 0, 0, - 300 ) );
  100. addLabel( "+diffuse", new THREE.Vector3( 0, 0, 300 ) );
  101. addLabel( "envMap", new THREE.Vector3( - 350, 300, 0 ) );
  102. addLabel( "no envMap", new THREE.Vector3( 350, 300, 0 ) );
  103. particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  104. scene.add( particleLight );
  105. // Lights
  106. scene.add( new THREE.AmbientLight( 0x222222 ) );
  107. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  108. directionalLight.position.set( 1, 1, 1 ).normalize();
  109. scene.add( directionalLight );
  110. var pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  111. particleLight.add( pointLight );
  112. //
  113. renderer = new THREE.WebGLRenderer( { antialias: true } );
  114. renderer.setPixelRatio( window.devicePixelRatio );
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. container.appendChild( renderer.domElement );
  117. renderer.gammaInput = true;
  118. renderer.gammaOutput = true;
  119. //
  120. stats = new Stats();
  121. container.appendChild( stats.dom );
  122. controls = new THREE.OrbitControls( camera );
  123. window.addEventListener( 'resize', onWindowResize, false );
  124. }
  125. function onWindowResize() {
  126. camera.aspect = window.innerWidth / window.innerHeight;
  127. camera.updateProjectionMatrix();
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. }
  130. //
  131. function animate() {
  132. requestAnimationFrame( animate );
  133. render();
  134. stats.update();
  135. }
  136. function render() {
  137. var timer = Date.now() * 0.00025;
  138. //camera.position.x = Math.cos( timer ) * 800;
  139. //camera.position.z = Math.sin( timer ) * 800;
  140. camera.lookAt( scene.position );
  141. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  142. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  143. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  144. renderer.render( scene, camera );
  145. }
  146. </script>
  147. </body>
  148. </html>