API Docs for:
Show:

File: ../src/materials/standardMaterial.js

  1. //modes
  2. //- per texture
  3. //- texture coordinates
  4. //- vertex color and extras
  5. //- alpha test
  6.  
  7. //StandardMaterial class **************************
  8. /* Warning: a material is not a component, because it can be shared by multiple nodes */
  9.  
  10. /**
  11. * StandardMaterial class improves the material class
  12. * @namespace LS
  13. * @class StandardMaterial
  14. * @constructor
  15. * @param {Object} object [optional] to configure from
  16. */
  17.  
  18. function StandardMaterial(o)
  19. {
  20. ShaderMaterial.call(this,null); //do not pass the data object, it is called later
  21.  
  22. this.blend_mode = LS.Blend.NORMAL;
  23.  
  24. this.createProperty( "diffuse", new Float32Array([1.0,1.0,1.0]), "color" );
  25. this.createProperty( "ambient", new Float32Array([1.0,1.0,1.0]), "color" );
  26. this.createProperty( "emissive", new Float32Array([0,0,0,0]), "color" ); //fourth component to control if emissive is affected by albedo
  27.  
  28. this._specular_data = vec4.fromValues( 0.1, 10.0, 0.0, 0.0 ); //specular factor, glossiness, specular_on_top
  29. this.specular_on_top = false;
  30. this.specular_on_alpha = false;
  31.  
  32. this.backlight_factor = 0;
  33.  
  34. this.reflection_factor = 0.0;
  35. this.reflection_fresnel = 1.0;
  36. this.reflection_specular = false;
  37.  
  38. this.createProperty( "velvet", new Float32Array([0.5,0.5,0.5]), "color" );
  39. this.velvet_exp = 0.0;
  40. this.velvet_additive = false;
  41. this._velvet_info = vec4.create();
  42.  
  43. this._detail = new Float32Array([0.0, 10, 10]);
  44.  
  45. this.normalmap_factor = 1.0;
  46. this.normalmap_tangent = true;
  47. this.bumpmap_factor = 1.0;
  48.  
  49. this.displacementmap_factor = 0.1;
  50.  
  51. this.use_scene_ambient = true;
  52.  
  53. this.createProperty( "extra", new Float32Array([1,1,1,1]), "color" ); //used in special situations
  54.  
  55. //used to change the render state
  56. this.flags = {
  57. alpha_test: false,
  58. alpha_test_shadows: false,
  59. two_sided: false,
  60. flip_normals: false,
  61. depth_test: true,
  62. depth_write: true,
  63. ignore_lights: false,
  64. cast_shadows: true,
  65. receive_shadows: true,
  66. // flat_normals: false,
  67. ignore_frustum: false
  68. };
  69.  
  70. //used for special fx
  71. this._uniforms = {
  72. u_material_color: this._color,
  73. u_ambient_color: this._ambient,
  74. u_emissive_color: this._emissive,
  75. u_specular: this._specular_data,
  76. u_reflection_info: vec2.create(), //factor and fresnel
  77. u_velvet_info: vec4.create(),
  78. u_normal_info: vec2.create(),
  79. u_detail_info: this._detail,
  80. u_texture_matrix: this.uvs_matrix,
  81. u_extra_color: this._extra
  82. };
  83.  
  84. this._samplers = [];
  85.  
  86. this._allows_instancing = true;
  87. this.needsUpdate = true;
  88.  
  89. if(o)
  90. this.configure(o);
  91. }
  92.  
  93.  
  94. Object.defineProperty( StandardMaterial.prototype, 'detail_factor', {
  95. get: function() { return this._detail[0]; },
  96. set: function(v) { this._detail[0] = v; },
  97. enumerable: true
  98. });
  99.  
  100. Object.defineProperty( StandardMaterial.prototype, 'detail_scale', {
  101. get: function() { return this._detail.subarray(1,3); },
  102. set: function(v) { this._detail[1] = v[0]; this._detail[2] = v[1]; },
  103. enumerable: true
  104. });
  105.  
  106. Object.defineProperty( StandardMaterial.prototype, 'emissive_extra', {
  107. get: function() { return this._emissive[3]; },
  108. set: function(v) { this._emissive[3] = v; },
  109. enumerable: true
  110. });
  111.  
  112. Object.defineProperty( StandardMaterial.prototype, 'specular_factor', {
  113. get: function() { return this._specular_data[0]; },
  114. set: function(v) {
  115. if( v != null && v.constructor === Number)
  116. this._specular_data[0] = v;
  117. },
  118. enumerable: true
  119. });
  120.  
  121. Object.defineProperty( StandardMaterial.prototype, 'specular_gloss', {
  122. get: function() { return this._specular_data[1]; },
  123. set: function(v) { this._specular_data[1] = v; },
  124. enumerable: true
  125. });
  126.  
  127. StandardMaterial["@blend_mode"] = { type: "enum", values: LS.Blend };
  128. StandardMaterial.actions = {};
  129.  
  130. StandardMaterial.DETAIL_TEXTURE = "detail";
  131. StandardMaterial.NORMAL_TEXTURE = "normal";
  132. StandardMaterial.DISPLACEMENT_TEXTURE = "displacement";
  133. StandardMaterial.BUMP_TEXTURE = "bump";
  134. StandardMaterial.REFLECTIVITY_TEXTURE = "reflectivity";
  135. StandardMaterial.EXTRA_TEXTURE = "extra";
  136. StandardMaterial.IRRADIANCE_TEXTURE = "irradiance";
  137.  
  138. StandardMaterial.prototype.renderInstance = ShaderMaterial.prototype.renderInstance;
  139. StandardMaterial.prototype.renderShadowInstance = ShaderMaterial.prototype.renderShadowInstance;
  140. StandardMaterial.prototype.renderPickingInstance = ShaderMaterial.prototype.renderPickingInstance;
  141.  
  142.  
  143. StandardMaterial.prototype.prepare = function( scene )
  144. {
  145. var flags = this.flags;
  146.  
  147. var render_state = this._render_state;
  148.  
  149. //set flags in render state
  150. render_state.cull_face = !flags.two_sided;
  151. render_state.front_face = flags.flip_normals ? GL.CW : GL.CCW;
  152. render_state.depth_test = flags.depth_test;
  153. render_state.depth_mask = flags.depth_write;
  154.  
  155. render_state.blend = this.blend_mode != LS.Blend.NORMAL;
  156. if( this.blend_mode != LS.Blend.NORMAL )
  157. {
  158. var func = LS.BlendFunctions[ this.blend_mode ];
  159. if(func)
  160. {
  161. render_state.blendFunc0 = func[0];
  162. render_state.blendFunc1 = func[1];
  163. }
  164. }
  165.  
  166. this._light_mode = this.flags.ignore_lights ? Material.NO_LIGHTS : 1;
  167.  
  168. this.fillUniforms( scene ); //update uniforms
  169. }
  170.  
  171. //options vec4: channel, degamma, transform, contrast
  172.  
  173. StandardMaterial.FLAGS = {
  174. COLOR_TEXTURE: 1<<1,
  175. OPACITY_TEXTURE: 1<<2,
  176. SPECULAR_TEXTURE: 1<<3,
  177. REFLECTIVITY_TEXTURE: 1<<4,
  178. AMBIENT_TEXTURE: 1<<5,
  179. EMISSIVE_TEXTURE: 1<<6,
  180. DETAIL_TEXTURE: 1<<7,
  181. NORMAL_TEXTURE: 1<<8,
  182. DISPLACEMENT_TEXTURE: 1<<9,
  183. EXTRA_TEXTURE: 1<<10,
  184. ENVIRONMENT_TEXTURE: 1<<11,
  185. ENVIRONMENT_CUBEMAP: 1<<12,
  186. IRRADIANCE_CUBEMAP: 1<<13,
  187.  
  188. COLOR_TEXTURE_OPTIONS: 1<<16,
  189. OPACITY_TEXTURE_OPTIONS: 1<<17,
  190. SPECULAR_TEXTURE_OPTIONS: 1<<18,
  191. REFLECTIVITY_TEXTURE_OPTIONS: 1<<19,
  192. AMBIENT_TEXTURE_OPTIONS: 1<<20,
  193. EMISSIVE_TEXTURE_OPTIONS: 1<<21,
  194. NORMAL_TEXTURE_OPTIONS: 1<<22,
  195. DISPLACEMENT_TEXTURE_OPTIONS: 1<<23,
  196. EXTRA_TEXTURE_OPTIONS: 1<<24,
  197.  
  198. DEGAMMA_COLOR: 1<<26,
  199. SPEC_ON_ALPHA: 1<<27,
  200. SPEC_ON_TOP: 1<<28,
  201. ALPHA_TEST: 1<<29
  202. }; //max is 32
  203.  
  204. StandardMaterial.shader_codes = {};
  205.  
  206. //returns the LS.ShaderCode required to render
  207. //here we cannot filter by light pass because this is done before applying shaderblocks
  208. //in the StandardMaterial we cache versions of the ShaderCode according to the settings
  209. StandardMaterial.prototype.getShaderCode = function( instance, render_settings, pass )
  210. {
  211. var FLAGS = StandardMaterial.FLAGS;
  212.  
  213. //lets check which code flags are active according to the configuration of the shader
  214. var code_flags = 0;
  215. var scene = LS.Renderer._current_scene;
  216.  
  217. //TEXTURES
  218. if( this.textures.color )
  219. {
  220. code_flags |= FLAGS.COLOR_TEXTURE;
  221. if( this.textures.color.degamma )
  222. code_flags |= FLAGS.DEGAMMA_COLOR;
  223. }
  224. if( this.textures.opacity )
  225. code_flags |= FLAGS.OPACITY_TEXTURE;
  226.  
  227. if( this.textures.displacement )
  228. code_flags |= FLAGS.DISPLACEMENT_TEXTURE;
  229.  
  230. //color textures are not necessary
  231. if( this.textures.normal )
  232. code_flags |= FLAGS.NORMAL_TEXTURE;
  233. if( this.textures.specular )
  234. code_flags |= FLAGS.SPECULAR_TEXTURE;
  235. if( this.reflection_factor > 0 )
  236. {
  237. //code_flags |= FLAGS.REFLECTION;
  238. if( this.textures.reflectivity )
  239. code_flags |= FLAGS.REFLECTIVITY_TEXTURE;
  240. }
  241. if( this.textures.emissive )
  242. code_flags |= FLAGS.EMISSIVE_TEXTURE;
  243. if( this.textures.ambient )
  244. code_flags |= FLAGS.AMBIENT_TEXTURE;
  245. if( this.textures.detail )
  246. code_flags |= FLAGS.DETAIL_TEXTURE;
  247. if( this.textures.extra )
  248. code_flags |= FLAGS.EXTRA_TEXTURE;
  249. if( this.specular_on_alpha )
  250. code_flags |= FLAGS.SPEC_ON_ALPHA;
  251. if( this.specular_on_top )
  252. code_flags |= FLAGS.SPEC_ON_TOP;
  253.  
  254. //flags
  255. if( this.flags.alpha_test )
  256. code_flags |= FLAGS.ALPHA_TEST;
  257.  
  258. //check if we already have this ShaderCode created
  259. var shader_code = LS.StandardMaterial.shader_codes[ code_flags ];
  260.  
  261. //reuse shader codes when possible
  262. if(shader_code)
  263. return shader_code;
  264.  
  265. //generate code
  266. var code = {
  267. vs_local: "",
  268. fs: "",
  269. fs_shadows: ""
  270. };
  271.  
  272. if( code_flags & FLAGS.DISPLACEMENT_TEXTURE )
  273. code.vs_local += " vertex4.xyz += v_normal * texture2D( displacement_texture, v_uvs ).x * u_displacementmap_factor;\n";
  274.  
  275. //uvs
  276. code.fs += "vec2 uv0 = (vec3(IN.uv,1.0) * u_texture_matrix).xy;\n";
  277. code.fs_shadows += "vec2 uv0 = (vec3(IN.uv,1.0) * u_texture_matrix).xy;\n";
  278.  
  279. if( code_flags & FLAGS.NORMAL_TEXTURE )
  280. {
  281. code.fs += " vec3 normal_pixel = texture2D( normal_texture, uv0 ).xyz;\n\
  282. normal_pixel.xy = vec2(1.0) - normal_pixel.xy;\n\
  283. if( u_normal_info.y > 0.0 )\n\
  284. normal_pixel = normalize( perturbNormal( IN.worldNormal, IN.viewDir, uv0, normal_pixel ));\n\
  285. o.Normal = normalize( mix( o.Normal, normal_pixel, u_normal_info.x ) );\n";
  286. }
  287.  
  288. if( code_flags & FLAGS.COLOR_TEXTURE )
  289. {
  290. var str = " vec4 tex_color = texture2D( color_texture, uv0 );\n";
  291. code.fs += str;
  292. code.fs_shadows += str;
  293.  
  294. if( code_flags & FLAGS.DEGAMMA_COLOR )
  295. code.fs += " tex_color.xyz = pow( tex_color.xyz, vec3(2.0) );\n";
  296. str = " o.Albedo *= tex_color.xyz;\n\
  297. o.Alpha *= tex_color.w;\n";
  298. code.fs += str;
  299. code.fs_shadows += str;
  300. }
  301. if( code_flags & FLAGS.OPACITY_TEXTURE )
  302. {
  303. var str = " o.Alpha *= texture2D( opacity_texture, uv0 ).x;\n";
  304. code.fs += str;
  305. code.fs_shadows += str;
  306. }
  307. if( code_flags & FLAGS.SPECULAR_TEXTURE )
  308. {
  309. code.fs += " vec4 spec_info = texture2D( specular_texture, uv0 );\n\
  310. o.Specular *= spec_info.x;\n\
  311. o.Gloss *= spec_info.y;\n";
  312. }
  313. if( code_flags & FLAGS.REFLECTIVITY_TEXTURE )
  314. code.fs += " o.Reflectivity *= texture2D( reflectivity_texture, uv0 ).x;\n";
  315. if( code_flags & FLAGS.EMISSIVE_TEXTURE )
  316. code.fs += " o.Emission *= texture2D( emissive_texture, uv0 ).xyz;\n";
  317. if( code_flags & FLAGS.AMBIENT_TEXTURE )
  318. code.fs += " o.Ambient *= texture2D( ambient_texture, uv0 ).xyz;\n";
  319. if( code_flags & FLAGS.DETAIL_TEXTURE )
  320. code.fs += " o.Albedo += (texture2D( detail_texture, uv0 * u_detail_info.yz).xyz - vec3(0.5)) * u_detail_info.x;\n";
  321. if( code_flags & FLAGS.EXTRA_TEXTURE )
  322. code.fs += " if(u_light_info.z == 0.0) o.Extra = u_extra_color * texture2D( extra_texture, uv0 );\n";
  323.  
  324. //flags
  325. if( code_flags & FLAGS.ALPHA_TEST )
  326. {
  327. var str = " if(o.Alpha < 0.01) discard;\n";
  328. code.fs += str;
  329. code.fs_shadows += str;
  330. }
  331.  
  332. if( code_flags & FLAGS.SPEC_ON_TOP )
  333. code.fs += " #define SPEC_ON_TOP\n";
  334.  
  335. if( code_flags & FLAGS.SPEC_ON_ALPHA )
  336. code.fs += " #define SPEC_ON_ALPHA\n";
  337.  
  338. //if( code_flags & FLAGS.FLAT_NORMALS )
  339. // flat_normals += "";
  340.  
  341. //compile shader and cache
  342. shader_code = new LS.ShaderCode();
  343. var final_code = StandardMaterial.code_template;
  344.  
  345. if( StandardMaterial.onShaderCode )
  346. StandardMaterial.onShaderCode( code, this, code_flags );
  347.  
  348. shader_code.code = ShaderCode.replaceCode( final_code, code );
  349. /*
  350. shader_code.code = final_code.replace(/\{\{[a-zA-Z0-9_]*\}\}/g, function(v){
  351. v = v.replace( /[\{\}]/g, "" );
  352. return code[v] || "";
  353. });
  354. */
  355.  
  356. LS.StandardMaterial.shader_codes[ code_flags ] = shader_code;
  357. return shader_code;
  358. }
  359.  
  360. StandardMaterial.prototype.fillUniforms = function( scene, options )
  361. {
  362. var uniforms = this._uniforms;
  363.  
  364. uniforms.u_reflection_info[0] = this.reflection_factor;
  365. uniforms.u_reflection_info[1] = this.reflection_fresnel;
  366. uniforms.u_backlight_factor = this.backlight_factor;
  367. uniforms.u_normal_info[0] = this.normalmap_factor;
  368. uniforms.u_normal_info[1] = this.normalmap_tangent ? 1 : 0;
  369. uniforms.u_displacementmap_factor = this.displacementmap_factor;
  370. uniforms.u_velvet_info.set( this._velvet );
  371. uniforms.u_velvet_info[3] = this.velvet_additive ? this.velvet_exp : -this.velvet_exp;
  372.  
  373. //iterate through textures in the material
  374. var last_texture_slot = 0;
  375. var samplers = this._samplers;
  376. samplers.length = 0; //clear
  377. for(var i in this.textures)
  378. {
  379. var sampler = this.getTextureSampler(i);
  380. if(!sampler)
  381. continue;
  382.  
  383. var texture = sampler.texture;
  384. if(!texture)
  385. continue;
  386.  
  387. if(texture.constructor === String) //name of texture
  388. texture = LS.ResourcesManager.textures[texture];
  389. else if (texture.constructor != Texture)
  390. continue;
  391. if(!texture) //loading or non-existant
  392. sampler = { texture: ":missing" };
  393.  
  394. var slot = last_texture_slot;
  395. if( i == "environment" )
  396. slot = LS.Renderer.ENVIRONMENT_TEXTURE_SLOT;
  397. else if( i == "irradiance" )
  398. slot = LS.Renderer.IRRADIANCE_TEXTURE_SLOT;
  399. else
  400. last_texture_slot++;
  401.  
  402. samplers[ slot ] = sampler;
  403. //var uniform_name = i + ( (!texture || texture.texture_type == gl.TEXTURE_2D) ? "_texture" : "_cubemap");
  404. uniforms[ i + "_texture" ] = slot;
  405. }
  406. }
  407.  
  408. StandardMaterial.prototype.getTextureChannels = function()
  409. {
  410. return [ Material.COLOR_TEXTURE, Material.OPACITY_TEXTURE, Material.AMBIENT_TEXTURE, Material.SPECULAR_TEXTURE, Material.EMISSIVE_TEXTURE, StandardMaterial.DETAIL_TEXTURE, StandardMaterial.NORMAL_TEXTURE, StandardMaterial.DISPLACEMENT_TEXTURE, StandardMaterial.BUMP_TEXTURE, StandardMaterial.REFLECTIVITY_TEXTURE, StandardMaterial.EXTRA_TEXTURE, Material.ENVIRONMENT_TEXTURE, StandardMaterial.IRRADIANCE_TEXTURE ];
  411. }
  412.  
  413. /**
  414. * assign a value to a property in a safe way
  415. * @method setProperty
  416. * @param {Object} object to configure from
  417. */
  418. StandardMaterial.prototype.setProperty = function(name, value)
  419. {
  420. //redirect to base material
  421. if( Material.prototype.setProperty.call(this,name,value) )
  422. return true;
  423.  
  424. //regular
  425. switch(name)
  426. {
  427. //objects
  428. case "render_state":
  429. //numbers
  430. case "specular_factor":
  431. case "specular_gloss":
  432. case "backlight_factor":
  433. case "reflection_factor":
  434. case "reflection_fresnel":
  435. case "velvet_exp":
  436. case "velvet_additive":
  437. case "normalmap_tangent":
  438. case "normalmap_factor":
  439. case "bumpmap_factor":
  440. case "displacementmap_factor":
  441. case "detail_factor":
  442. case "emissive_extra":
  443. //strings
  444. case "shader_name":
  445. //bools
  446. case "specular_on_top":
  447. case "specular_on_alpha":
  448. case "normalmap_tangent":
  449. case "reflection_specular":
  450. case "use_scene_ambient":
  451. case "blend_mode":
  452. if(value !== null)
  453. this[name] = value;
  454. break;
  455. case "flags":
  456. if(value)
  457. {
  458. for(var i in value)
  459. this.flags[i] = value[i];
  460. }
  461. break;
  462. //vectors
  463. case "ambient":
  464. case "emissive":
  465. case "velvet":
  466. case "extra":
  467. case "detail_scale":
  468. if(this[name].length == value.length)
  469. this[name].set(value);
  470. break;
  471. default:
  472. return false;
  473. }
  474. return true;
  475. }
  476.  
  477. /**
  478. * gets all the properties and its types
  479. * @method getPropertiesInfo
  480. * @return {Object} object with name:type
  481. */
  482. StandardMaterial.prototype.getPropertiesInfo = function()
  483. {
  484. //get from the regular material
  485. var o = Material.prototype.getPropertiesInfo.call(this);
  486.  
  487. //add some more
  488. o.merge({
  489. shader_name: LS.TYPES.STRING,
  490.  
  491. blend_mode: LS.TYPES.NUMBER,
  492. specular_factor: LS.TYPES.NUMBER,
  493. specular_gloss: LS.TYPES.NUMBER,
  494. backlight_factor: LS.TYPES.NUMBER,
  495. reflection_factor: LS.TYPES.NUMBER,
  496. reflection_fresnel: LS.TYPES.NUMBER,
  497. velvet_exp: LS.TYPES.NUMBER,
  498.  
  499. normalmap_factor: LS.TYPES.NUMBER,
  500. bumpmap_factor: LS.TYPES.NUMBER,
  501. displacementmap_factor: LS.TYPES.NUMBER,
  502. emissive_extra: LS.TYPES.NUMBER,
  503.  
  504. ambient: LS.TYPES.VEC3,
  505. emissive: LS.TYPES.VEC3,
  506. velvet: LS.TYPES.VEC3,
  507. extra: LS.TYPES.VEC4,
  508. detail_factor: LS.TYPES.NUMBER,
  509. detail_scale: LS.TYPES.VEC2,
  510.  
  511. specular_on_top: LS.TYPES.BOOLEAN,
  512. normalmap_tangent: LS.TYPES.BOOLEAN,
  513. reflection_specular: LS.TYPES.BOOLEAN,
  514. use_scene_ambient: LS.TYPES.BOOLEAN,
  515. velvet_additive: LS.TYPES.BOOLEAN
  516. });
  517.  
  518. return o;
  519. }
  520.  
  521. StandardMaterial.prototype.getPropertyInfoFromPath = function( path )
  522. {
  523. if( path.length < 1)
  524. return;
  525.  
  526. var info = Material.prototype.getPropertyInfoFromPath.call(this,path);
  527. if(info)
  528. return info;
  529.  
  530. var varname = path[0];
  531. var type;
  532.  
  533. switch(varname)
  534. {
  535. case "blend_mode":
  536. case "backlight_factor":
  537. case "reflection_factor":
  538. case "reflection_fresnel":
  539. case "velvet_exp":
  540. case "normalmap_factor":
  541. case "bumpmap_factor":
  542. case "displacementmap_factor":
  543. case "emissive_extra":
  544. case "detail_factor":
  545. type = LS.TYPES.NUMBER; break;
  546. case "extra":
  547. type = LS.TYPES.VEC4; break;
  548. case "ambient":
  549. case "emissive":
  550. case "velvet":
  551. type = LS.TYPES.VEC3; break;
  552. case "detail_scale":
  553. type = LS.TYPES.VEC2; break;
  554. case "specular_on_top":
  555. case "specular_on_alpha":
  556. case "normalmap_tangent":
  557. case "reflection_specular":
  558. case "use_scene_ambient":
  559. case "velvet_additive":
  560. type = LS.TYPES.BOOLEAN; break;
  561. default:
  562. return null;
  563. }
  564.  
  565. return {
  566. node: this._root,
  567. target: this,
  568. name: varname,
  569. value: this[varname],
  570. type: type
  571. };
  572. }
  573.  
  574. StandardMaterial.clearShadersCache = function()
  575. {
  576. LS.log("StandardMaterial ShaderCode cache cleared");
  577. StandardMaterial.shader_codes = {};
  578. }
  579.  
  580. LS.registerMaterialClass( StandardMaterial );
  581. LS.StandardMaterial = StandardMaterial;
  582.  
  583. //legacy
  584. LS.Classes["newStandardMaterial"] = StandardMaterial;
  585. //LS.newStandardMaterial = StandardMaterial;
  586. //LS.MaterialClasses.newStandardMaterial = StandardMaterial;
  587.  
  588. //**********************************************
  589.  
  590.  
  591. StandardMaterial.code_template = "\n\
  592. \n\
  593. \n\
  594. \\color.vs\n\
  595. \n\
  596. precision mediump float;\n\
  597. attribute vec3 a_vertex;\n\
  598. attribute vec3 a_normal;\n\
  599. attribute vec2 a_coord;\n\
  600. #ifdef USE_COLORS\n\
  601. attribute vec4 a_color;\n\
  602. #endif\n\
  603. \n\
  604. //varyings\n\
  605. varying vec3 v_pos;\n\
  606. varying vec3 v_normal;\n\
  607. varying vec2 v_uvs;\n\
  608. \n\
  609. //matrices\n\
  610. #ifdef BLOCK_INSTANCING\n\
  611. attribute mat4 u_model;\n\
  612. #else\n\
  613. uniform mat4 u_model;\n\
  614. #endif\n\
  615. uniform mat4 u_normal_model;\n\
  616. uniform mat4 u_view;\n\
  617. uniform mat4 u_viewprojection;\n\
  618. //material\n\
  619. uniform float u_displacementmap_factor;\n\
  620. uniform sampler2D displacement_texture;\n\
  621. \n\
  622. //globals\n\
  623. uniform float u_time;\n\
  624. uniform vec4 u_viewport;\n\
  625. uniform float u_point_size;\n\
  626. \n\
  627. #pragma shaderblock \"light\"\n\
  628. #pragma shaderblock \"morphing\"\n\
  629. #pragma shaderblock \"skinning\"\n\
  630. \n\
  631. //camera\n\
  632. uniform vec3 u_camera_eye;\n\
  633. uniform vec2 u_camera_planes;\n\
  634. \n\
  635. #pragma event \"vs_functions\"\n\
  636. \n\
  637. //special cases\n\
  638. {{vs_out}}\n\
  639. \n\
  640. void main() {\n\
  641. \n\
  642. vec4 vertex4 = vec4(a_vertex,1.0);\n\
  643. v_normal = a_normal;\n\
  644. v_uvs = a_coord;\n\
  645. \n\
  646. //local deforms\n\
  647. {{vs_local}}\n\
  648. applyMorphing( vertex4, v_normal );\n\
  649. applySkinning( vertex4, v_normal );\n\
  650. \n\
  651. //vertex\n\
  652. v_pos = (u_model * vertex4).xyz;\n\
  653. \n\
  654. applyLight(v_pos);\n\
  655. \n\
  656. //normal\n\
  657. #ifdef SHADERBLOCK_INSTANCING\n\
  658. v_normal = (u_model * vec4(v_normal,0.0)).xyz;\n\
  659. #else\n\
  660. v_normal = (u_normal_model * vec4(v_normal,0.0)).xyz;\n\
  661. #endif\n\
  662. //world deform\n\
  663. {{vs_global}}\n\
  664. \n\
  665. #pragma event \"vs_final_pass\"\n\
  666. \n\
  667. gl_Position = u_viewprojection * vec4(v_pos,1.0);\n\
  668. gl_PointSize = u_point_size;\n\
  669. #pragma event \"vs_final\"\n\
  670. }\n\
  671. \n\
  672. \\color.fs\n\
  673. \n\
  674. #ifdef DRAW_BUFFERS\n\
  675. #extension GL_EXT_draw_buffers : require \n\
  676. #endif\n\
  677. \n\
  678. precision mediump float;\n\
  679. \n\
  680. //varyings\n\
  681. varying vec3 v_pos;\n\
  682. varying vec3 v_normal;\n\
  683. varying vec2 v_uvs;\n\
  684. \n\
  685. //globals\n\
  686. uniform vec3 u_camera_eye;\n\
  687. uniform vec4 u_clipping_plane;\n\
  688. uniform vec4 u_background_color;\n\
  689. uniform vec4 u_material_color;\n\
  690. \n\
  691. uniform vec3 u_ambient_color;\n\
  692. uniform vec4 u_emissive_color;\n\
  693. uniform vec4 u_specular;\n\
  694. uniform vec2 u_reflection_info;\n\
  695. uniform vec4 u_velvet_info;\n\
  696. uniform vec2 u_normal_info;\n\
  697. uniform vec3 u_detail_info;\n\
  698. uniform mat3 u_texture_matrix;\n\
  699. uniform vec4 u_extra_color;\n\
  700. uniform float u_backlight_factor;\n\
  701. \n\
  702. uniform sampler2D color_texture;\n\
  703. uniform sampler2D opacity_texture;\n\
  704. uniform sampler2D specular_texture;\n\
  705. uniform sampler2D ambient_texture;\n\
  706. uniform sampler2D emissive_texture;\n\
  707. uniform sampler2D reflectivity_texture;\n\
  708. uniform sampler2D detail_texture;\n\
  709. uniform sampler2D normal_texture;\n\
  710. uniform sampler2D extra_texture;\n\
  711. \n\
  712. uniform vec4 u_color_texture_settings;\n\
  713. uniform vec4 u_opacity_texture_settings;\n\
  714. uniform vec4 u_specular_texture_settings;\n\
  715. uniform vec4 u_ambient_texture_settings;\n\
  716. uniform vec4 u_emissive_texture_settings;\n\
  717. uniform vec4 u_reflectivity_texture_settings;\n\
  718. uniform vec4 u_normal_texture_settings;\n\
  719. \n\
  720. \n\
  721. #pragma shaderblock \"light\"\n\
  722. #pragma shaderblock \"light_texture\"\n\
  723. #pragma shaderblock \"applyReflection\"\n\
  724. \n\
  725. #pragma snippet \"perturbNormal\"\n\
  726. \n\
  727. #pragma shaderblock \"extraBuffers\"\n\
  728. \n\
  729. void surf(in Input IN, out SurfaceOutput o)\n\
  730. {\n\
  731. o.Albedo = u_material_color.xyz;\n\
  732. o.Alpha = u_material_color.a;\n\
  733. o.Normal = normalize( v_normal );\n\
  734. o.Specular = u_specular.x;\n\
  735. o.Gloss = u_specular.y;\n\
  736. o.Ambient = u_ambient_color;\n\
  737. o.Emission = u_emissive_color.xyz;\n\
  738. o.Reflectivity = u_reflection_info.x;\n\
  739. o.Extra = u_extra_color;\n\
  740. \n\
  741. {{fs}}\n\
  742. \n\
  743. if(u_velvet_info.w > 0.0)\n\
  744. o.Albedo += u_velvet_info.xyz * ( 1.0 - pow( max(0.0, dot( IN.viewDir, o.Normal )), u_velvet_info.w ));\n\
  745. else if(u_velvet_info.w < 0.0)\n\
  746. o.Albedo = mix( o.Albedo, u_velvet_info.xyz, 1.0 - pow( max(0.0, dot( IN.viewDir, o.Normal )), abs(u_velvet_info.w) ) );\n\
  747. if(u_emissive_color.w > 0.0)\n\
  748. o.Emission *= o.Albedo;\n\
  749. o.Reflectivity *= max(0.0, pow( 1.0 - clamp(0.0, dot(IN.viewDir,o.Normal),1.0), u_reflection_info.y ));\n\
  750. }\n\
  751. \n\
  752. #pragma event \"fs_functions\"\n\
  753. \n\
  754. {{fs_out}}\n\
  755. \n\
  756. void main() {\n\
  757. Input IN = getInput();\n\
  758. SurfaceOutput o = getSurfaceOutput();\n\
  759. surf(IN,o);\n\
  760. Light LIGHT = getLight();\n\
  761. applyLightTexture( IN, LIGHT );\n\
  762. FinalLight FINALLIGHT = computeLight( o, IN, LIGHT );\n\
  763. FINALLIGHT.Diffuse += u_backlight_factor * max(0.0, dot(FINALLIGHT.Vector, -o.Normal));\n\
  764. vec4 final_color = vec4( 0.0,0.0,0.0, o.Alpha );\n\
  765. #ifdef SPEC_ON_ALPHA\n\
  766. final_color.a += FINALLIGHT.Specular;\n\
  767. #endif\n\
  768. #ifdef SPEC_ON_TOP\n\
  769. float specular = FINALLIGHT.Specular;\n\
  770. FINALLIGHT.Specular = 0.0;\n\
  771. #endif\n\
  772. final_color.xyz = applyLight( o, FINALLIGHT );\n\
  773. #ifdef SPEC_ON_TOP\n\
  774. final_color.xyz += specular * LIGHT.Color * FINALLIGHT.Shadow;\n\
  775. #endif\n\
  776. final_color = applyReflection( IN, o, final_color );\n\
  777. #pragma event \"fs_final_pass\"\n\
  778. {{fs_encode}}\n\
  779. #ifdef DRAW_BUFFERS\n\
  780. gl_FragData[0] = final_color;\n\
  781. if(u_light_info.z == 0.0)\n\
  782. gl_FragData[1] = o.Extra;\n\
  783. #else\n\
  784. gl_FragColor = final_color;\n\
  785. #endif\n\
  786. #pragma event \"fs_final\"\n\
  787. }\n\
  788. \\shadow.vs\n\
  789. \n\
  790. precision mediump float;\n\
  791. attribute vec3 a_vertex;\n\
  792. attribute vec3 a_normal;\n\
  793. attribute vec2 a_coord;\n\
  794. #ifdef USE_COLORS\n\
  795. attribute vec4 a_color;\n\
  796. #endif\n\
  797. \n\
  798. //varyings\n\
  799. varying vec3 v_pos;\n\
  800. varying vec3 v_normal;\n\
  801. varying vec2 v_uvs;\n\
  802. \n\
  803. //matrices\n\
  804. #ifdef BLOCK_INSTANCING\n\
  805. attribute mat4 u_model;\n\
  806. #else\n\
  807. uniform mat4 u_model;\n\
  808. #endif\n\
  809. uniform mat4 u_normal_model;\n\
  810. uniform mat4 u_view;\n\
  811. uniform mat4 u_viewprojection;\n\
  812. //material\n\
  813. uniform float u_displacementmap_factor;\n\
  814. uniform sampler2D displacement_texture;\n\
  815. \n\
  816. //globals\n\
  817. uniform float u_time;\n\
  818. uniform vec4 u_viewport;\n\
  819. uniform float u_point_size;\n\
  820. \n\
  821. #pragma shaderblock \"light\"\n\
  822. #pragma shaderblock \"morphing\"\n\
  823. #pragma shaderblock \"skinning\"\n\
  824. \n\
  825. //camera\n\
  826. uniform vec3 u_camera_eye;\n\
  827. \n\
  828. {{vs_out}}\n\
  829. \n\
  830. void main() {\n\
  831. \n\
  832. vec4 vertex4 = vec4(a_vertex,1.0);\n\
  833. v_normal = a_normal;\n\
  834. v_uvs = a_coord;\n\
  835. \n\
  836. //deforms\n\
  837. {{vs_local}}\n\
  838. applyMorphing( vertex4, v_normal );\n\
  839. applySkinning( vertex4, v_normal );\n\
  840. \n\
  841. //vertex\n\
  842. v_pos = (u_model * vertex4).xyz;\n\
  843. \n\
  844. applyLight(v_pos);\n\
  845. \n\
  846. //normal\n\
  847. #ifdef SHADERBLOCK_INSTANCING\n\
  848. v_normal = (u_model * vec4(v_normal,0.0)).xyz;\n\
  849. #else\n\
  850. v_normal = (u_normal_model * vec4(v_normal,0.0)).xyz;\n\
  851. #endif\n\
  852. {{vs_global}}\n\
  853. gl_Position = u_viewprojection * vec4(v_pos,1.0);\n\
  854. }\n\
  855. \\shadow.fs\n\
  856. \n\
  857. precision mediump float;\n\
  858. \n\
  859. //varyings\n\
  860. varying vec3 v_pos;\n\
  861. varying vec3 v_normal;\n\
  862. varying vec2 v_uvs;\n\
  863. \n\
  864. //globals\n\
  865. uniform vec3 u_camera_eye;\n\
  866. uniform vec4 u_clipping_plane;\n\
  867. uniform vec4 u_material_color;\n\
  868. \n\
  869. uniform mat3 u_texture_matrix;\n\
  870. \n\
  871. uniform sampler2D color_texture;\n\
  872. uniform sampler2D opacity_texture;\n\
  873. \n\
  874. #pragma snippet \"input\"\n\
  875. #pragma snippet \"surface\"\n\
  876. \n\
  877. void surf(in Input IN, out SurfaceOutput o)\n\
  878. {\n\
  879. o.Albedo = u_material_color.xyz;\n\
  880. o.Alpha = u_material_color.a;\n\
  881. \n\
  882. {{fs_shadows}}\n\
  883. }\n\
  884. \n\
  885. {{fs_shadow_out}}\n\
  886. \n\
  887. void main() {\n\
  888. Input IN = getInput();\n\
  889. SurfaceOutput o = getSurfaceOutput();\n\
  890. surf(IN,o);\n\
  891. {{fs_shadow_encode}}\n\
  892. gl_FragColor = vec4(o.Albedo,o.Alpha);\n\
  893. }\n\
  894. \\picking.vs\n\
  895. \n\
  896. precision mediump float;\n\
  897. attribute vec3 a_vertex;\n\
  898. attribute vec3 a_normal;\n\
  899. attribute vec2 a_coord;\n\
  900. #ifdef USE_COLORS\n\
  901. attribute vec4 a_color;\n\
  902. #endif\n\
  903. \n\
  904. //varyings\n\
  905. varying vec3 v_pos;\n\
  906. varying vec3 v_normal;\n\
  907. varying vec2 v_uvs;\n\
  908. \n\
  909. //matrices\n\
  910. #ifdef BLOCK_INSTANCING\n\
  911. attribute mat4 u_model;\n\
  912. #else\n\
  913. uniform mat4 u_model;\n\
  914. #endif\n\
  915. uniform mat4 u_normal_model;\n\
  916. uniform mat4 u_view;\n\
  917. uniform mat4 u_viewprojection;\n\
  918. //material\n\
  919. uniform float u_displacementmap_factor;\n\
  920. uniform sampler2D displacement_texture;\n\
  921. \n\
  922. //globals\n\
  923. uniform float u_time;\n\
  924. uniform vec4 u_viewport;\n\
  925. uniform float u_point_size;\n\
  926. \n\
  927. #pragma shaderblock \"light\"\n\
  928. #pragma shaderblock \"morphing\"\n\
  929. #pragma shaderblock \"skinning\"\n\
  930. \n\
  931. //camera\n\
  932. uniform vec3 u_camera_eye;\n\
  933. \n\
  934. {{vs_out}}\n\
  935. \n\
  936. #pragma event \"vs_functions\"\n\
  937. \n\
  938. void main() {\n\
  939. \n\
  940. vec4 vertex4 = vec4(a_vertex,1.0);\n\
  941. v_normal = a_normal;\n\
  942. v_uvs = a_coord;\n\
  943. \n\
  944. //deforms\n\
  945. {{vs_local}}\n\
  946. applyMorphing( vertex4, v_normal );\n\
  947. applySkinning( vertex4, v_normal );\n\
  948. \n\
  949. //vertex\n\
  950. v_pos = (u_model * vertex4).xyz;\n\
  951. \n\
  952. applyLight(v_pos);\n\
  953. \n\
  954. //normal\n\
  955. #ifdef SHADERBLOCK_INSTANCING\n\
  956. v_normal = (u_model * vec4(v_normal,0.0)).xyz;\n\
  957. #else\n\
  958. v_normal = (u_normal_model * vec4(v_normal,0.0)).xyz;\n\
  959. #endif\n\
  960. {{vs_global}}\n\
  961. gl_Position = u_viewprojection * vec4(v_pos,1.0);\n\
  962. #pragma event \"vs_final\"\n\
  963. }\n\
  964. \\picking.fs\n\
  965. precision mediump float;\n\
  966. uniform vec4 u_material_color;\n\
  967. void main() {\n\
  968. gl_FragColor = u_material_color;\n\
  969. }\n\
  970. ";
  971.  
  972.  
  973. /* example to inject code in the standardMaterial without having to edit it
  974. //hooks are vs_out (out of main), vs_local (vertex4 to deform vertices localy), vs_global (v_pos to deform final position), fs_out (out of main), fs_encode (final_color before being written)
  975. this.onStart = function()
  976. {
  977. LS.StandardMaterial.onShaderCode = function(code,mat)
  978. {
  979. code.fs_encode = "final_color.x = final_color.y;";
  980. }
  981. LS.StandardMaterial.clearShadersCache();
  982. }
  983. */