webgl_custom_attributes.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>three.js webgl - custom attributes</title>
  6. <style>
  7. body {
  8. color: #ffffff;
  9. font-family:Monospace;
  10. font-size:13px;
  11. text-align:center;
  12. font-weight: bold;
  13. background-color: #000000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. color: #fff;
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. z-index:100;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - custom attributes example</div>
  28. <div id="container"></div>
  29. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  30. <script type="text/javascript" src="js/Stats.js"></script>
  31. <script src="../build/Three.js"></script>
  32. <script type="x-shader/x-vertex" id="vertexshader">
  33. uniform float amplitude;
  34. attribute float displacement;
  35. varying vec3 vNormal;
  36. varying vec2 vUv;
  37. void main() {
  38. vNormal = normal;
  39. vUv = ( 0.5 + amplitude ) * uv + vec2( amplitude );
  40. vec3 newPosition = position + amplitude * normal * vec3( displacement );
  41. gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
  42. }
  43. </script>
  44. <script type="x-shader/x-fragment" id="fragmentshader">
  45. varying vec3 vNormal;
  46. varying vec2 vUv;
  47. uniform vec3 color;
  48. uniform sampler2D texture;
  49. void main() {
  50. vec3 light = vec3( 0.5, 0.2, 1.0 );
  51. light = normalize( light );
  52. float dProd = dot( vNormal, light ) * 0.5 + 0.5;
  53. vec4 tcolor = texture2D( texture, vUv );
  54. vec4 gray = vec4( vec3( tcolor.r * 0.3 + tcolor.g * 0.59 + tcolor.b * 0.11 ), 1.0 );
  55. gl_FragColor = gray * vec4( vec3( dProd ) * vec3( color ), 1.0 );
  56. }
  57. </script>
  58. <script type="text/javascript">
  59. var renderer, scene, camera, stats;
  60. var sphere, uniforms, attributes;
  61. var WIDTH = window.innerWidth,
  62. HEIGHT = window.innerHeight;
  63. init();
  64. animate();
  65. function init() {
  66. camera = new THREE.Camera( 30, WIDTH / HEIGHT, 1, 10000 );
  67. camera.position.z = 300;
  68. scene = new THREE.Scene();
  69. attributes = {
  70. displacement: { type: 'f', value: [] }
  71. };
  72. uniforms = {
  73. amplitude: { type: "f", value: 1.0 },
  74. color: { type: "c", value: new THREE.Color( 0xff2200 ) },
  75. texture: { type: "t", value: 0, texture: THREE.ImageUtils.loadTexture( "textures/water.jpg" ) },
  76. };
  77. uniforms.texture.texture.wrapS = uniforms.texture.texture.wrapT = THREE.RepeatWrapping;
  78. var shaderMaterial = new THREE.MeshShaderMaterial( {
  79. uniforms: uniforms,
  80. attributes: attributes,
  81. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  82. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  83. });
  84. var radius = 50, segments = 128, rings = 64;
  85. var geometry = new THREE.SphereGeometry( radius, segments, rings );
  86. sphere = new THREE.Mesh( geometry, shaderMaterial );
  87. sphere.dynamic = true;
  88. var vertices = sphere.geometry.vertices;
  89. var values = attributes.displacement.value;
  90. for( var v = 0; v < vertices.length; v++ ) {
  91. values[ v ] = Math.random() * 5;
  92. }
  93. scene.addChild( sphere );
  94. renderer = new THREE.WebGLRenderer( { clearColor: 0x050505, clearAlpha: 1 } );
  95. renderer.setSize( WIDTH, HEIGHT );
  96. var container = document.getElementById( 'container' );
  97. container.appendChild( renderer.domElement );
  98. stats = new Stats();
  99. stats.domElement.style.position = 'absolute';
  100. stats.domElement.style.top = '0px';
  101. container.appendChild( stats.domElement );
  102. }
  103. var i, value;
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. render();
  107. stats.update();
  108. }
  109. function render() {
  110. sphere.rotation.y += 0.01;
  111. sphere.rotation.z += 0.01;
  112. uniforms.amplitude.value = 2.5 * Math.sin( sphere.rotation.y * 0.125 );
  113. THREE.ColorUtils.adjustHSV( uniforms.color.value, 0.0005, 0, 0 );
  114. for( i = 0; i < attributes.displacement.value.length; i++ ) {
  115. value = attributes.displacement.value[ i ];
  116. value[ i ] += 0.5 * ( 0.5 - Math.random() );
  117. if ( value[ i ] < -5 ) value[ i ] = -5;
  118. if ( value[ i ] > 5 ) value[ i ] = 5;
  119. }
  120. attributes.displacement.needsUpdate = true;
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>