webgl_custom_attributes.html 4.6 KB

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