SimulatorRenderer.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @author zz85 https://github.com/zz85 / http://www.lab4games.net/zz85/blog
  3. *
  4. * Bird Simulation Render
  5. *
  6. * A simple scene rendering a quad of the following shaders
  7. * 1. Pass-thru Shader,
  8. * 2. Bird Position Update Shader,
  9. * 3. Bird Velocity Update Shader
  10. *
  11. */
  12. function SimulatorRenderer(WIDTH, renderer) {
  13. WIDTH = WIDTH || 4;
  14. var camera = new THREE.Camera();
  15. camera.position.z = 1;
  16. // Init RTT stuff
  17. gl = renderer.getContext();
  18. if( !gl.getExtension( "OES_texture_float" )) {
  19. alert( "No OES_texture_float support for float textures!" );
  20. return;
  21. }
  22. if( gl.getParameter(gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS) == 0) {
  23. alert( "No support for vertex shader textures!" );
  24. return;
  25. }
  26. var scene = new THREE.Scene();
  27. var uniforms = {
  28. time: { type: "f", value: 1.0 },
  29. resolution: { type: "v2", value: new THREE.Vector2( WIDTH, WIDTH ) },
  30. texture: { type: "t", value: null }
  31. };
  32. var passThruShader = new THREE.ShaderMaterial( {
  33. uniforms: uniforms,
  34. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  35. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  36. } );
  37. var mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), passThruShader );
  38. var positionShader = new THREE.ShaderMaterial( {
  39. uniforms: {
  40. time: { type: "f", value: 1.0 },
  41. delta: { type: "f", value: 0.0 },
  42. resolution: { type: "v2", value: new THREE.Vector2( WIDTH, WIDTH ) },
  43. texturePosition: { type: "t", value: null },
  44. textureVelocity: { type: "t", value: null },
  45. },
  46. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  47. fragmentShader: document.getElementById( 'fragmentShaderPosition' ).textContent
  48. } );
  49. this.positionShader = positionShader;
  50. var velocityShader = new THREE.ShaderMaterial( {
  51. uniforms: {
  52. time: { type: "f", value: 1.0 },
  53. delta: { type: "f", value: 0.0 },
  54. resolution: { type: "v2", value: new THREE.Vector2( WIDTH, WIDTH ) },
  55. texturePosition: { type: "t", value: null },
  56. textureVelocity: { type: "t", value: null },
  57. testing: { type: "f", value: 1.0 },
  58. seperationDistance: { type: "f", value: 1.0 },
  59. alignmentDistance: { type: "f", value: 1.0 },
  60. cohesionDistance: { type: "f", value: 1.0 },
  61. freedomFactor: { type: "f", value: 1.0 },
  62. predator: { type: "v3", value: new THREE.Vector3() }
  63. },
  64. defines: {
  65. WIDTH: WIDTH.toFixed(2)
  66. },
  67. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  68. fragmentShader: document.getElementById( 'fragmentShaderVelocity' ).textContent
  69. } );
  70. this.velocityUniforms = velocityShader.uniforms;
  71. scene.add( mesh );
  72. this.getRenderTarget = function() {
  73. var renderTarget = new THREE.WebGLRenderTarget(WIDTH, WIDTH, {
  74. wrapS: THREE.RepeatWrapping,
  75. wrapT: THREE.RepeatWrapping,
  76. minFilter: THREE.NearestFilter,
  77. magFilter: THREE.NearestFilter,
  78. format: THREE.RGBAFormat,
  79. type: THREE.FloatType,
  80. stencilBuffer: false
  81. });
  82. return renderTarget;
  83. }
  84. // Takes a texture, and render out as another texture
  85. this.renderTexture = function( input, output ) {
  86. mesh.material = passThruShader;
  87. uniforms.texture.value = input;
  88. renderer.render( scene, camera, output );
  89. }
  90. this.renderPosition = function(position, velocity, output, delta) {
  91. mesh.material = positionShader;
  92. positionShader.uniforms.texturePosition.value = position;
  93. positionShader.uniforms.textureVelocity.value = velocity;
  94. positionShader.uniforms.time.value = performance.now();
  95. positionShader.uniforms.delta.value = delta;
  96. renderer.render( scene, camera, output );
  97. }
  98. this.renderVelocity = function(position, velocity, output, delta) {
  99. mesh.material = velocityShader;
  100. velocityShader.uniforms.texturePosition.value = position;
  101. velocityShader.uniforms.textureVelocity.value = velocity;
  102. velocityShader.uniforms.time.value = performance.now();
  103. velocityShader.uniforms.delta.value = delta;
  104. renderer.render( scene, camera, output );
  105. }
  106. }