webgl_materials_compile.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - node material</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. #waitScreen {
  10. color: #fff;
  11. position: absolute;
  12. top: 50%;
  13. left: 50%;
  14. margin-top: -50px;
  15. margin-left: -50px;
  16. width: 100px;
  17. height: 100px;
  18. }
  19. .hide {
  20. display:none;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="container"></div>
  26. <div id="info">
  27. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Node-Based Material
  28. <br /><span class="button" id="preload">change preload</span>
  29. </div>
  30. <div id="waitScreen">
  31. Loading ...
  32. </div>
  33. <script type="module">
  34. import * as THREE from '../build/three.module.js';
  35. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  36. import { TeapotBufferGeometry } from './jsm/geometries/TeapotBufferGeometry.js';
  37. import { NodeFrame } from './jsm/nodes/core/NodeFrame.js';
  38. import { MathNode } from './jsm/nodes/math/MathNode.js';
  39. import { OperatorNode } from './jsm/nodes/math/OperatorNode.js';
  40. import { ConstNode } from './jsm/nodes/core/ConstNode.js';
  41. import { ColorNode } from './jsm/nodes/inputs/ColorNode.js';
  42. import { ExpressionNode } from './jsm/nodes/core/ExpressionNode.js';
  43. import { TimerNode } from './jsm/nodes/utils/TimerNode.js';
  44. import { FloatNode } from './jsm/nodes/inputs/FloatNode.js';
  45. import { PhongNodeMaterial } from './jsm/nodes/materials/PhongNodeMaterial.js';
  46. var container = document.getElementById( 'container' );
  47. var renderer, scene, camera, clock = new THREE.Clock(), fov = 50;
  48. var frame = new NodeFrame();
  49. var teapot;
  50. var controls;
  51. var rtTexture, rtMaterial;
  52. var meshes = [];
  53. document.getElementById( "preload" ).addEventListener( 'click', function () {
  54. var hash = document.location.hash.substr( 1 );
  55. if ( hash.length === 0 ) {
  56. window.location.hash = "#NoPreLoad";
  57. } else {
  58. window.location.hash = "";
  59. }
  60. location.reload( true );
  61. }, false );
  62. window.addEventListener( 'load', init );
  63. function init() {
  64. renderer = new THREE.WebGLRenderer( { antialias: true } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. container.appendChild( renderer.domElement );
  68. scene = new THREE.Scene();
  69. camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
  70. camera.position.x = 0;
  71. camera.position.z = - 300;
  72. camera.position.y = 200;
  73. camera.target = new THREE.Vector3();
  74. controls = new OrbitControls( camera, renderer.domElement );
  75. controls.minDistance = 50;
  76. controls.maxDistance = 400;
  77. scene.add( new THREE.AmbientLight( 0x464646 ) );
  78. var light = new THREE.DirectionalLight( 0xffddcc, 1 );
  79. light.position.set( 1, 0.75, 0.5 );
  80. scene.add( light );
  81. var light = new THREE.DirectionalLight( 0xccccff, 1 );
  82. light.position.set( - 1, 0.75, - 0.5 );
  83. scene.add( light );
  84. teapot = new TeapotBufferGeometry( 15, 18 );
  85. var itemsonrow = 10;
  86. for ( var i = 0; i < itemsonrow * itemsonrow; i ++ ) {
  87. var mesh = new THREE.Mesh( teapot );
  88. mesh.position.x = 50 * ( i % itemsonrow ) - 50 * itemsonrow / 2;
  89. mesh.position.z = 50 * Math.floor( i / itemsonrow ) - 150;
  90. updateMaterial( mesh );
  91. scene.add( mesh );
  92. meshes.push( mesh );
  93. }
  94. window.addEventListener( 'resize', onWindowResize, false );
  95. var hash = document.location.hash.substr( 1 );
  96. if ( hash.length === 0 ) {
  97. renderer.compile( scene, camera );
  98. }
  99. document.getElementById( "waitScreen" ).className = "hide";
  100. setTimeout( function () {
  101. onWindowResize();
  102. animate();
  103. }, 1 );
  104. }
  105. function updateMaterial( mesh ) {
  106. if ( mesh.material ) mesh.material.dispose();
  107. if ( rtTexture ) {
  108. rtTexture.dispose();
  109. rtTexture = null;
  110. }
  111. if ( rtMaterial ) {
  112. rtMaterial.dispose();
  113. rtMaterial = null;
  114. }
  115. var mtl = new PhongNodeMaterial();
  116. var time = new TimerNode();
  117. var speed = new FloatNode( Math.random() );
  118. var color = new ColorNode( Math.random() * 0xFFFFFF );
  119. var timeSpeed = new OperatorNode(
  120. time,
  121. speed,
  122. OperatorNode.MUL
  123. );
  124. var sinCycleInSecs = new OperatorNode(
  125. timeSpeed,
  126. new ConstNode( ConstNode.PI2 ),
  127. OperatorNode.MUL
  128. );
  129. var cycle = new MathNode( sinCycleInSecs, MathNode.SIN );
  130. var cycleColor = new OperatorNode(
  131. cycle,
  132. color,
  133. OperatorNode.MUL
  134. );
  135. var cos = new MathNode( cycleColor, MathNode.SIN );
  136. mtl.color = new ColorNode( 0 );
  137. mtl.emissive = cos;
  138. var transformer = new ExpressionNode( "position + 0.0 * " + Math.random(), "vec3", [ ] );
  139. mtl.transform = transformer;
  140. // build shader ( ignore auto build )
  141. mtl.build();
  142. // set material
  143. mesh.material = mtl;
  144. }
  145. function onWindowResize() {
  146. var width = window.innerWidth, height = window.innerHeight;
  147. camera.aspect = width / height;
  148. camera.updateProjectionMatrix();
  149. renderer.setSize( width, height );
  150. if ( rtTexture ) rtTexture.setSize( width, height );
  151. }
  152. function animate() {
  153. var delta = clock.getDelta();
  154. frame.update( delta );
  155. for ( var i = 0; i < meshes.length; i ++ ) {
  156. var mesh = meshes[ i ];
  157. frame.updateNode( mesh.material );
  158. }
  159. renderer.render( scene, camera );
  160. requestAnimationFrame( animate );
  161. }
  162. </script>
  163. </body>
  164. </html>