customMaterial.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import { TilesRenderer } from '../src/index.js';
  2. import {
  3. Scene,
  4. DirectionalLight,
  5. AmbientLight,
  6. WebGLRenderer,
  7. PerspectiveCamera,
  8. Box3,
  9. OrthographicCamera,
  10. sRGBEncoding,
  11. Group,
  12. ShaderMaterial,
  13. MeshStandardMaterial,
  14. PCFSoftShadowMap,
  15. Sphere,
  16. } from 'three';
  17. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  18. import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js';
  19. import Stats from 'three/examples/jsm/libs/stats.module.js';
  20. let camera, controls, scene, renderer, tiles, orthoCamera;
  21. let offsetParent, box, sphere, dirLight, statsContainer;
  22. let stats;
  23. const DEFAULT = 0;
  24. const GRADIENT = 1;
  25. const TOPOGRAPHIC_LINES = 2;
  26. const LIGHTING = 3;
  27. const params = {
  28. 'material': DEFAULT,
  29. 'orthographic': false,
  30. 'rebuild': initTiles,
  31. };
  32. const gradientShader = {
  33. vertexShader: /* glsl */`
  34. varying vec3 wPosition;
  35. void main() {
  36. #include <begin_vertex>
  37. #include <project_vertex>
  38. wPosition = ( modelMatrix * vec4( transformed, 1.0 ) ).xyz;
  39. }
  40. `,
  41. fragmentShader: /* glsl */`
  42. varying vec3 wPosition;
  43. void main() {
  44. float minVal = - 30.0;
  45. float maxVal = 30.0;
  46. float val = ( wPosition.y - minVal ) / ( maxVal - minVal );
  47. vec4 color1 = vec4( 0.149, 0.196, 0.219, 1.0 ) * 0.5;
  48. vec4 color2 = vec4( 1.0 );
  49. gl_FragColor = mix( color1, color2, val );
  50. }
  51. `,
  52. };
  53. const topoShader = {
  54. extensions: {
  55. derivatives: true,
  56. },
  57. vertexShader: /* glsl */`
  58. varying vec3 wPosition;
  59. varying vec3 vViewPosition;
  60. void main() {
  61. #include <begin_vertex>
  62. #include <project_vertex>
  63. wPosition = ( modelMatrix * vec4( transformed, 1.0 ) ).xyz;
  64. vViewPosition = - mvPosition.xyz;
  65. }
  66. `,
  67. fragmentShader: /* glsl */`
  68. varying vec3 wPosition;
  69. varying vec3 vViewPosition;
  70. void main() {
  71. // lighting
  72. vec3 fdx = vec3( dFdx( wPosition.x ), dFdx( wPosition.y ), dFdx( wPosition.z ) );
  73. vec3 fdy = vec3( dFdy( wPosition.x ), dFdy( wPosition.y ), dFdy( wPosition.z ) );
  74. vec3 worldNormal = normalize( cross( fdx, fdy ) );
  75. float lighting =
  76. 0.4 +
  77. clamp( dot( worldNormal, vec3( 1.0, 1.0, 1.0 ) ), 0.0, 1.0 ) * 0.5 +
  78. clamp( dot( worldNormal, vec3( - 1.0, 1.0, - 1.0 ) ), 0.0, 1.0 ) * 0.3;
  79. // thickness scale
  80. float upwardness = dot( worldNormal, vec3( 0.0, 1.0, 0.0 ) );
  81. float yInv = clamp( 1.0 - abs( upwardness ), 0.0, 1.0 );
  82. float thicknessScale = pow( yInv, 0.4 );
  83. thicknessScale *= 0.25 + 0.5 * ( vViewPosition.z + 1.0 ) / 2.0;
  84. // thickness
  85. float thickness = 0.01 * thicknessScale;
  86. float thickness2 = thickness / 2.0;
  87. float m = mod( wPosition.y, 3.0 );
  88. // soften edge
  89. float center = thickness2;
  90. float dist = clamp( abs( m - thickness2 ) / thickness2, 0.0, 1.0 );
  91. vec4 topoColor = vec4( 0.149, 0.196, 0.219, 1.0 ) * 0.5;
  92. gl_FragColor = mix( topoColor * lighting, vec4( lighting ), dist );
  93. }
  94. `,
  95. };
  96. init();
  97. animate();
  98. function updateMaterial( scene ) {
  99. const materialIndex = parseFloat( params.material );
  100. scene.traverse( c => {
  101. if ( c.isMesh ) {
  102. c.material.dispose();
  103. switch ( materialIndex ) {
  104. case DEFAULT:
  105. c.material = c.originalMaterial;
  106. c.material.side = 2;
  107. c.receiveShadow = false;
  108. c.castShadow = false;
  109. break;
  110. case GRADIENT:
  111. c.material = new ShaderMaterial( gradientShader );
  112. c.material.side = 2;
  113. c.receiveShadow = false;
  114. c.castShadow = false;
  115. break;
  116. case TOPOGRAPHIC_LINES:
  117. c.material = new ShaderMaterial( topoShader );
  118. c.material.side = 2;
  119. c.material.flatShading = true;
  120. c.receiveShadow = false;
  121. c.castShadow = false;
  122. break;
  123. case LIGHTING:
  124. c.material = new MeshStandardMaterial();
  125. c.material.side = 2;
  126. c.receiveShadow = true;
  127. c.castShadow = true;
  128. }
  129. }
  130. } );
  131. }
  132. function onLoadModel( scene ) {
  133. scene.traverse( c => {
  134. if ( c.isMesh ) {
  135. c.originalMaterial = c.material;
  136. }
  137. } );
  138. updateMaterial( scene );
  139. }
  140. function onDisposeModel( scene ) {
  141. scene.traverse( c => {
  142. if ( c.isMesh ) {
  143. c.material.dispose();
  144. }
  145. } );
  146. }
  147. function initTiles() {
  148. if ( tiles ) {
  149. tiles.group.parent.remove( tiles.group );
  150. tiles.dispose();
  151. }
  152. const url = window.location.hash.replace( /^#/, '' ) || '../data/tileset.json';
  153. tiles = new TilesRenderer( url );
  154. tiles.errorTarget = 2;
  155. tiles.onLoadModel = onLoadModel;
  156. tiles.onDisposeModel = onDisposeModel;
  157. offsetParent.add( tiles.group );
  158. }
  159. function init() {
  160. scene = new Scene();
  161. // primary camera view
  162. renderer = new WebGLRenderer( { antialias: true } );
  163. renderer.setPixelRatio( window.devicePixelRatio );
  164. renderer.setSize( window.innerWidth, window.innerHeight );
  165. renderer.setClearColor( 0x151c1f );
  166. renderer.shadowMap.enabled = true;
  167. renderer.shadowMap.type = PCFSoftShadowMap;
  168. renderer.outputEncoding = sRGBEncoding;
  169. document.body.appendChild( renderer.domElement );
  170. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
  171. camera.position.set( 400, 400, 400 );
  172. orthoCamera = new OrthographicCamera();
  173. // controls
  174. controls = new OrbitControls( camera, renderer.domElement );
  175. controls.screenSpacePanning = false;
  176. controls.minDistance = 1;
  177. controls.maxDistance = 2000;
  178. // lights
  179. dirLight = new DirectionalLight( 0xffffff, 1.25 );
  180. dirLight.position.set( 1, 2, 3 ).multiplyScalar( 40 );
  181. dirLight.castShadow = true;
  182. dirLight.shadow.bias = - 0.01;
  183. dirLight.shadow.mapSize.setScalar( 2048 );
  184. const shadowCam = dirLight.shadow.camera;
  185. shadowCam.left = - 200;
  186. shadowCam.bottom = - 200;
  187. shadowCam.right = 200;
  188. shadowCam.top = 200;
  189. shadowCam.updateProjectionMatrix();
  190. scene.add( dirLight );
  191. const ambLight = new AmbientLight( 0xffffff, 0.05 );
  192. scene.add( ambLight );
  193. box = new Box3();
  194. sphere = new Sphere();
  195. offsetParent = new Group();
  196. scene.add( offsetParent );
  197. initTiles();
  198. onWindowResize();
  199. window.addEventListener( 'resize', onWindowResize, false );
  200. // GUI
  201. const gui = new GUI();
  202. gui.width = 300;
  203. gui.add( params, 'orthographic' );
  204. gui.add( params, 'material', { DEFAULT, GRADIENT, TOPOGRAPHIC_LINES, LIGHTING } )
  205. .onChange( () => {
  206. tiles.forEachLoadedModel( updateMaterial );
  207. } );
  208. gui.add( params, 'rebuild' );
  209. gui.open();
  210. // Stats
  211. stats = new Stats();
  212. stats.showPanel( 0 );
  213. document.body.appendChild( stats.dom );
  214. statsContainer = document.createElement( 'div' );
  215. statsContainer.style.position = 'absolute';
  216. statsContainer.style.top = 0;
  217. statsContainer.style.left = 0;
  218. statsContainer.style.color = 'white';
  219. statsContainer.style.width = '100%';
  220. statsContainer.style.textAlign = 'center';
  221. statsContainer.style.padding = '5px';
  222. statsContainer.style.pointerEvents = 'none';
  223. statsContainer.style.lineHeight = '1.5em';
  224. document.body.appendChild( statsContainer );
  225. }
  226. function onWindowResize() {
  227. camera.aspect = window.innerWidth / window.innerHeight;
  228. renderer.setPixelRatio( window.devicePixelRatio );
  229. renderer.setSize( window.innerWidth, window.innerHeight );
  230. camera.updateProjectionMatrix();
  231. updateOrthoCamera();
  232. }
  233. function updateOrthoCamera() {
  234. orthoCamera.position.copy( camera.position );
  235. orthoCamera.rotation.copy( camera.rotation );
  236. const scale = camera.position.distanceTo( controls.target ) / 2.0;
  237. const aspect = window.innerWidth / window.innerHeight;
  238. orthoCamera.left = - aspect * scale;
  239. orthoCamera.right = aspect * scale;
  240. orthoCamera.bottom = - scale;
  241. orthoCamera.top = scale;
  242. orthoCamera.near = camera.near;
  243. orthoCamera.far = camera.far;
  244. orthoCamera.updateProjectionMatrix();
  245. }
  246. function animate() {
  247. requestAnimationFrame( animate );
  248. if ( params.orthographic ) {
  249. tiles.deleteCamera( camera );
  250. tiles.setCamera( orthoCamera );
  251. tiles.setResolutionFromRenderer( orthoCamera, renderer );
  252. } else {
  253. tiles.deleteCamera( orthoCamera );
  254. tiles.setCamera( camera );
  255. tiles.setResolutionFromRenderer( camera, renderer );
  256. }
  257. offsetParent.rotation.set( 0, 0, 0 );
  258. if ( params.up === '-Z' ) {
  259. offsetParent.rotation.x = Math.PI / 2;
  260. }
  261. offsetParent.updateMatrixWorld( true );
  262. // update tiles center
  263. if ( tiles.getBounds( box ) ) {
  264. box.getCenter( tiles.group.position );
  265. tiles.group.position.multiplyScalar( - 1 );
  266. } else if ( tiles.getBoundingSphere( sphere ) ) {
  267. tiles.group.position.copy( sphere.center );
  268. tiles.group.position.multiplyScalar( - 1 );
  269. }
  270. // update tiles
  271. window.tiles = tiles;
  272. camera.updateMatrixWorld();
  273. orthoCamera.updateMatrixWorld();
  274. tiles.update();
  275. render();
  276. stats.update();
  277. }
  278. function render() {
  279. updateOrthoCamera();
  280. statsContainer.innerText =
  281. `Geometries: ${ renderer.info.memory.geometries } ` +
  282. `Textures: ${ renderer.info.memory.textures } ` +
  283. `Programs: ${ renderer.info.programs.length } `;
  284. renderer.render( scene, params.orthographic ? orthoCamera : camera );
  285. }