webgl_custom_attributes.html 4.5 KB

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