123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - instancing test</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <style>
- body {
- color: #ffffff;
- font-family: Monospace;
- font-size: 13px;
- text-align: center;
- font-weight: bold;
- background-color: #000000;
- margin: 0px;
- overflow: hidden;
- }
- #info {
- position: absolute;
- top: 0px;
- width: 100%;
- padding: 5px;
- }
- a {
- color: #ffffff;
- }
- #oldie a {
- color: #da0;
- }
- #notSupported {
- width: 50%;
- margin: auto;
- border: 2px red solid;
- margin-top: 20px;
- padding: 10px;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="http://threejs.org" target="_blank">three.js</a> - instancing demo
- <div id="notSupported" style="display:none">Sorry your graphics card + browser does not support hardware instancing</div>
- </div>
- <script src="js/libs/dat.gui.min.js"></script>
- <script src="../build/three.js"></script>
- <script src="js/Detector.js"></script>
- <script src="js/libs/stats.min.js"></script>
- <script src="js/controls/TrackballControls.js"></script>
- <script id="vertexShader" type="x-shader/x-vertex">
- precision highp float;
- attribute vec3 instancePosition;
- attribute vec4 instanceQuaternion;
- attribute vec3 instanceScale;
- vec3 applyTRS( vec3 position, vec3 translation, vec4 quaternion, vec3 scale ) {
- position *= scale;
- position += 2.0 * cross( quaternion.xyz, cross( quaternion.xyz, position ) + quaternion.w * position );
- return position + translation;
- }
- attribute vec3 color;
- varying vec3 vColor;
- void main(){
- vColor = color;
- vec3 transformed = applyTRS( position.xyz, instancePosition, instanceQuaternion, instanceScale );
- gl_Position = projectionMatrix * modelViewMatrix * vec4( transformed, 1.0 );
- }
- </script>
- <script id="fragmentShader" type="x-shader/x-fragment">
- precision highp float;
- varying vec3 vColor;
- void main() {
- gl_FragColor = vec4( vColor, 1.0 );
- }
- </script>
- <script>
- if ( !Detector.webgl ) Detector.addGetWebGLMessage();
- var container, stats;
- var camera, scene, renderer;
- var controls;
- init();
- animate();
- function init() {
- container = document.getElementById( 'container' );
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
- camera.position.z = 4;
- controls = new THREE.TrackballControls( camera );
- scene = new THREE.Scene();
- //
- // var geometry = new THREE.BoxBufferGeometry( 0.01, 0.01, 0.01 );
- var geometry = new THREE.IcosahedronBufferGeometry( 0.1, 1 );
- var colors = [];
- for ( var i = 0, l = geometry.attributes.position.count; i < l; i ++ ) {
- colors.push( Math.random(), Math.random(), Math.random() );
- }
- geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
- var material = new THREE.MeshBasicMaterial( { color: 0xff0000, vertexColors: THREE.VertexColors } );
- var mesh = new THREE.Mesh( geometry, material );
- // scene.add( mesh );
- //
- var INSTANCE_COUNT = 100;
- var geometry2 = new THREE.InstancedBufferGeometry().copy( geometry );
- var instancePositions = [];
- var instanceQuaternions = [];
- var instanceScales = [];
- // var position = new THREE.Vector3();
- // var quaternion = new THREE.Quaternion();
- for ( var i = 0; i < INSTANCE_COUNT; i ++ ) {
- var mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- var position = mesh.position;
- var quaternion = mesh.quaternion;
- var scale = mesh.scale;
- position.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
- quaternion.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
- quaternion.normalize();
- scale.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
- instancePositions.push( position.x, position.y, position.z );
- instanceQuaternions.push( quaternion.x, quaternion.y, quaternion.z, quaternion.w );
- instanceScales.push( scale.x, scale.y, scale.z );
- }
- var attribute = new THREE.InstancedBufferAttribute( new Float32Array( instancePositions ), 3 );
- geometry2.addAttribute( 'instancePosition', attribute );
- var attribute = new THREE.InstancedBufferAttribute( new Float32Array( instanceQuaternions ), 4 );
- geometry2.addAttribute( 'instanceQuaternion', attribute );
- var attribute = new THREE.InstancedBufferAttribute( new Float32Array( instanceScales ), 3 );
- geometry2.addAttribute( 'instanceScale', attribute );
- var material = new THREE.ShaderMaterial( {
- uniforms: {},
- vertexShader: document.getElementById( 'vertexShader' ).textContent,
- fragmentShader: document.getElementById( 'fragmentShader' ).textContent
- } );
- var mesh2 = new THREE.Mesh( geometry2, material );
- mesh2.position.x = 0.1;
- scene.add( mesh2 );
- /*
- // geometry
- var triangles = 1;
- var instances = 65000;
- var geometry = new THREE.InstancedBufferGeometry();
- geometry.maxInstancedCount = instances; // set so its initalized for dat.GUI, will be set in first draw otherwise
- var gui = new dat.GUI();
- gui.add( geometry, "maxInstancedCount", 0, instances );
- var vertices = new THREE.BufferAttribute( new Float32Array( triangles * 3 * 3 ), 3 );
- vertices.setXYZ( 0, 0.025, -0.025, 0 );
- vertices.setXYZ( 1, -0.025, 0.025, 0 );
- vertices.setXYZ( 2, 0, 0, 0.025 );
- geometry.addAttribute( 'position', vertices );
- var offsets = new THREE.InstancedBufferAttribute( new Float32Array( instances * 3 ), 3, 1 );
- for ( var i = 0, ul = offsets.count; i < ul; i++ ) {
- offsets.setXYZ( i, Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 );
- }
- geometry.addAttribute( 'offset', offsets );
- var colors = new THREE.InstancedBufferAttribute( new Float32Array( instances * 4 ), 4, 1 );
- for ( var i = 0, ul = colors.count; i < ul; i++ ) {
- colors.setXYZW( i, Math.random(), Math.random(), Math.random(), Math.random() );
- }
- geometry.addAttribute( 'color', colors );
- var vector = new THREE.Vector4();
- var orientationsStart = new THREE.InstancedBufferAttribute( new Float32Array( instances * 4 ), 4, 1 );
- for ( var i = 0, ul = orientationsStart.count; i < ul; i++ ) {
- vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
- vector.normalize();
- orientationsStart.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
- }
- geometry.addAttribute( 'orientationStart', orientationsStart );
- var orientationsEnd = new THREE.InstancedBufferAttribute( new Float32Array( instances * 4 ), 4, 1 );
- for ( var i = 0, ul = orientationsEnd.count; i < ul; i++ ) {
- vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
- vector.normalize();
- orientationsEnd.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
- }
- geometry.addAttribute( 'orientationEnd', orientationsEnd );
- // material
- var material = new THREE.RawShaderMaterial( {
- uniforms: {
- time: { value: 1.0 },
- sineTime: { value: 1.0 }
- },
- vertexShader: document.getElementById( 'vertexShader' ).textContent,
- fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
- side: THREE.DoubleSide,
- transparent: true
- } );
- var mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- */
- renderer = new THREE.WebGLRenderer();
- if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === false ) {
- document.getElementById( "notSupported" ).style.display = "";
- return;
- }
- renderer.setClearColor( 0x101010 );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize( event ) {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- controls.update();
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|