webgl_custom_attributes.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 noise = [];
  62. var WIDTH = window.innerWidth,
  63. HEIGHT = window.innerHeight;
  64. init();
  65. animate();
  66. function init() {
  67. camera = new THREE.Camera( 30, WIDTH / HEIGHT, 1, 10000 );
  68. camera.position.z = 300;
  69. scene = new THREE.Scene();
  70. attributes = {
  71. displacement: { type: 'f', value: [] }
  72. };
  73. uniforms = {
  74. amplitude: { type: "f", value: 1.0 },
  75. color: { type: "c", value: new THREE.Color( 0xff2200 ) },
  76. texture: { type: "t", value: 0, texture: THREE.ImageUtils.loadTexture( "textures/water.jpg" ) },
  77. };
  78. uniforms.texture.texture.wrapS = uniforms.texture.texture.wrapT = THREE.RepeatWrapping;
  79. var shaderMaterial = new THREE.MeshShaderMaterial( {
  80. uniforms: uniforms,
  81. attributes: attributes,
  82. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  83. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  84. });
  85. var radius = 50, segments = 128, rings = 64;
  86. var geometry = new THREE.SphereGeometry( radius, segments, rings );
  87. sphere = new THREE.Mesh( geometry, shaderMaterial );
  88. sphere.dynamic = true;
  89. var vertices = sphere.geometry.vertices;
  90. var values = attributes.displacement.value;
  91. for( var v = 0; v < vertices.length; v++ ) {
  92. values[ v ] = 0;
  93. noise[ v ] = Math.random() * 5;
  94. }
  95. scene.addChild( sphere );
  96. renderer = new THREE.WebGLRenderer( { clearColor: 0x050505, clearAlpha: 1 } );
  97. renderer.setSize( WIDTH, HEIGHT );
  98. var container = document.getElementById( 'container' );
  99. container.appendChild( renderer.domElement );
  100. stats = new Stats();
  101. stats.domElement.style.position = 'absolute';
  102. stats.domElement.style.top = '0px';
  103. container.appendChild( stats.domElement );
  104. }
  105. function cap( x, a, b ) {
  106. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  107. }
  108. var i, value;
  109. function animate() {
  110. requestAnimationFrame( animate );
  111. render();
  112. stats.update();
  113. }
  114. function render() {
  115. var time = new Date().getTime() * 0.01;
  116. sphere.rotation.y = 0.01 * time;
  117. sphere.rotation.z = 0.01 * time;
  118. uniforms.amplitude.value = 2.5 * Math.sin( sphere.rotation.y * 0.125 );
  119. THREE.ColorUtils.adjustHSV( uniforms.color.value, 0.0005, 0, 0 );
  120. for( i = 0; i < attributes.displacement.value.length; i++ ) {
  121. attributes.displacement.value[ i ] = Math.sin( 0.1*i + time );
  122. noise[ i ] += 0.5 * ( 0.5 - Math.random() );
  123. noise[ i ] = cap( noise[ i ], -5, 5 );
  124. attributes.displacement.value[ i ] += noise[ i ];
  125. }
  126. attributes.displacement.needsUpdate = true;
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>