Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: o3djs/effect.js

Issue 3358020: o3djs: Multiple simple lights, and a new demo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/o3d/samples/
Patch Set: added var declarations Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « o3d-webgl-samples/multiple-lights.html ('k') | o3djs/material.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2009, Google Inc. 2 * Copyright 2009, Google Inc.
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 6 * modification, are permitted provided that the following conditions are
7 * met: 7 * met:
8 * 8 *
9 * * Redistributions of source code must retain the above copyright 9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 p.VARYING_DECLARATION_PREFIX + 'position' + 459 p.VARYING_DECLARATION_PREFIX + 'position' +
460 p.semanticSuffix('POSITION') + ';\n' + 460 p.semanticSuffix('POSITION') + ';\n' +
461 p.buildTexCoords(material, true) + 461 p.buildTexCoords(material, true) +
462 p.buildBumpOutputCoords(bumpSampler); 462 p.buildBumpOutputCoords(bumpSampler);
463 if (diffuse || specular) { 463 if (diffuse || specular) {
464 str += p.VARYING + p.FLOAT3 + ' ' + 464 str += p.VARYING + p.FLOAT3 + ' ' +
465 p.VARYING_DECLARATION_PREFIX + 'normal' + 465 p.VARYING_DECLARATION_PREFIX + 'normal' +
466 p.semanticSuffix('TEXCOORD' + 466 p.semanticSuffix('TEXCOORD' +
467 p.interpolant_++ + '') + ';\n' + 467 p.interpolant_++ + '') + ';\n' +
468 p.VARYING + p.FLOAT3 + ' ' + 468 p.VARYING + p.FLOAT3 + ' ' +
469 p.VARYING_DECLARATION_PREFIX + 'surfaceToLight' + 469 p.VARYING_DECLARATION_PREFIX + 'surfacePosition' +
470 p.semanticSuffix( 470 p.semanticSuffix(
471 'TEXCOORD' + p.interpolant_++ + '') + ';\n'; 471 'TEXCOORD' + p.interpolant_++ + '') + ';\n';
472 } 472 }
473 if (specular) { 473 if (specular) {
474 str += p.VARYING + p.FLOAT3 + ' ' + 474 str += p.VARYING + p.FLOAT3 + ' ' +
475 p.VARYING_DECLARATION_PREFIX + 'surfaceToView' + 475 p.VARYING_DECLARATION_PREFIX + 'surfaceToView' +
476 p.semanticSuffix( 476 p.semanticSuffix(
477 'TEXCOORD' + p.interpolant_++ + '') + ';\n'; 477 'TEXCOORD' + p.interpolant_++ + '') + ';\n';
478 } 478 }
479 str += p.END_STRUCT; 479 str += p.END_STRUCT;
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 effect.name = url; 829 effect.name = url;
830 } 830 }
831 return effect; 831 return effect;
832 }; 832 };
833 833
834 /** 834 /**
835 * Builds a shader string for a given standard COLLADA material type. 835 * Builds a shader string for a given standard COLLADA material type.
836 * 836 *
837 * @param {!o3d.Material} material Material for which to build the shader. 837 * @param {!o3d.Material} material Material for which to build the shader.
838 * @param {string} effectType Type of effect to create ('phong', 'lambert', 838 * @param {string} effectType Type of effect to create ('phong', 'lambert',
839 * 'constant'). 839 * 'constant', 'blinn').
840 * @param {Object} opt_options Parameters to customize shader code generation.
841 See o3djs.effect.getStandardShader for details on the possible values.
840 * @return {{description: string, shader: string}} A description and the shader 842 * @return {{description: string, shader: string}} A description and the shader
841 * string. 843 * string.
842 */ 844 */
843 o3djs.effect.buildStandardShaderString = function(material, 845 o3djs.effect.buildStandardShaderString = function(material,
844 effectType) { 846 effectType,
847 opt_options) {
848 if (!opt_options) {
849 opt_options = {};
850 }
851 var numLights = 0;
852 var currentLightWorldPos = 'lightWorldPos';
853 if (opt_options.lights) {
854 numLights = opt_options.lights;
855 currentLightWorldPos = 'lightWorldPosList[i]';
856 }
845 var p = o3djs.effect; 857 var p = o3djs.effect;
846 var bumpSampler = material.getParam('bumpSampler'); 858 var bumpSampler = material.getParam('bumpSampler');
847 var bumpUVInterpolant; 859 var bumpUVInterpolant;
848 var skinning; 860 var skinning;
849 861
850 var maxSkinInfluences = 4; 862 var maxSkinInfluences = 4;
851 863
852 // Hardcode reasonable maximum for number of skinning uniforms. 864 // Hardcode reasonable maximum for number of skinning uniforms.
853 // glsl: Table 6.19: minimum MAX_VERTEX_UNIFORM_VECTORS is 128. 865 // glsl: Table 6.19: minimum MAX_VERTEX_UNIFORM_VECTORS is 128.
854 // (DX9 requires a minimum of 256, so not a problem in o3d). 866 // (DX9 requires a minimum of 256, so not a problem in o3d).
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 return getTextureType(textureParam); 906 return getTextureType(textureParam);
895 else 907 else
896 return '2D'; 908 return '2D';
897 }; 909 };
898 910
899 /** 911 /**
900 * Builds uniform variables common to all standard lighting types. 912 * Builds uniform variables common to all standard lighting types.
901 * @return {string} The effect code for the common shader uniforms. 913 * @return {string} The effect code for the common shader uniforms.
902 */ 914 */
903 var buildCommonVertexUniforms = function() { 915 var buildCommonVertexUniforms = function() {
916 //var size = numLights ? '['+numLights+']' : '';
904 return 'uniform ' + p.MATRIX4 + ' worldViewProjection' + 917 return 'uniform ' + p.MATRIX4 + ' worldViewProjection' +
905 p.semanticSuffix('WORLDVIEWPROJECTION') + ';\n' + 918 p.semanticSuffix('WORLDVIEWPROJECTION') + ';\n';
906 'uniform ' + p.FLOAT3 + ' lightWorldPos;\n';
907 }; 919 };
908 920
909 /** 921 /**
910 * Builds uniform variables common to all standard lighting types. 922 * Builds uniform variables common to all standard lighting types.
911 * @return {string} The effect code for the common shader uniforms. 923 * @return {string} The effect code for the common shader uniforms.
912 */ 924 */
913 var buildCommonPixelUniforms = function() { 925 var buildCommonPixelUniforms = function() {
914 return 'uniform ' + p.FLOAT4 + ' lightColor;\n'; 926 if (numLights > 0) {
927 return 'uniform ' + p.FLOAT4 + ' lightColorList[' + numLights + '];\n' +
928 'uniform ' + p.FLOAT3 + ' lightWorldPosList[' + numLights + '];\n';
929 } else {
930 return 'uniform ' + p.FLOAT4 + ' lightColor;\n' +
931 'uniform ' + p.FLOAT3 + ' lightWorldPos' + ';\n';
932 }
915 }; 933 };
916 934
917 /** 935 /**
918 * Builds uniform variables common to lambert, phong and blinn lighting types. 936 * Builds uniform variables common to lambert, phong and blinn lighting types.
919 * @return {string} The effect code for the common shader uniforms. 937 * @return {string} The effect code for the common shader uniforms.
920 */ 938 */
921 var buildLightingUniforms = function() { 939 var buildLightingUniforms = function() {
922 return 'uniform ' + p.MATRIX4 + ' world' + 940 return 'uniform ' + p.MATRIX4 + ' world' +
923 p.semanticSuffix('WORLD') + ';\n' + 941 p.semanticSuffix('WORLD') + ';\n' +
924 'uniform ' + p.MATRIX4 + 942 'uniform ' + p.MATRIX4 +
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
984 var type = getSamplerType(samplerParam); 1002 var type = getSamplerType(samplerParam);
985 return ' ' + p.FLOAT4 + ' ' + name + ' = ' + p.TEXTURE + type + 1003 return ' ' + p.FLOAT4 + ' ' + name + ' = ' + p.TEXTURE + type +
986 '(' + name + 'Sampler, ' + 1004 '(' + name + 'Sampler, ' +
987 p.PIXEL_VARYING_PREFIX + name + 'UV);\n' 1005 p.PIXEL_VARYING_PREFIX + name + 'UV);\n'
988 } else { 1006 } else {
989 return ''; 1007 return '';
990 } 1008 }
991 }; 1009 };
992 1010
993 /** 1011 /**
1012 * Begins a section of code which is to be run once for each light.
1013 * @return {string} The effect code for the for loop, or the empty string if
1014 * not using multiple lights.
1015 */
1016 var beginLightLoop = function() {
1017 if (numLights) {
1018 return ' ' + p.FLOAT4 + ' lightColorDiffuse = ' + p.FLOAT4 + '(0);\n' +
1019 ' for (int i = 0; i < ' + numLights + '; i++) {\n';
1020 } else {
1021 return '';
1022 }
1023 };
1024
1025 /**
1026 * Ends the block of code which is to be run for each light. Adds the current
1027 * light's color times (diffuseExpression) into lightColorDiffuse.
1028 * @param {string} diffuseExpression Expression to multiply by light color.
1029 * @return {string} The effect code to set lightColorDiffuse.
1030 */
1031 var endLightLoop = function(diffuseExpression) {
1032 if (numLights) {
1033 return ' lightColorDiffuse += ' +
1034 'lightColorList[i] * ( ' + diffuseExpression + ');\n' +
1035 ' }\n';
1036 } else {
1037 return ' ' + p.FLOAT4 + ' lightColorDiffuse = ' +
1038 'lightColor * (' + diffuseExpression + ');';
1039 }
1040 };
1041
1042 /**
994 * Builds vertex and fragment shader string for the Constant lighting type. 1043 * Builds vertex and fragment shader string for the Constant lighting type.
995 * @param {!o3d.Material} material The material for which to build 1044 * @param {!o3d.Material} material The material for which to build
996 * shaders. 1045 * shaders.
997 * @param {!Array.<string>} descriptions Array to add descriptions too. 1046 * @param {!Array.<string>} descriptions Array to add descriptions too.
998 * @return {string} The effect code for the shader, ready to be parsed. 1047 * @return {string} The effect code for the shader, ready to be parsed.
999 */ 1048 */
1000 var buildConstantShaderString = function(material, descriptions) { 1049 var buildConstantShaderString = function(material, descriptions) {
1001 descriptions.push('constant'); 1050 descriptions.push('constant');
1002 return buildCommonVertexUniforms() + 1051 return buildCommonVertexUniforms() +
1003 buildSkinningUniforms() + 1052 buildSkinningUniforms() +
(...skipping 23 matching lines...) Expand all
1027 var buildLambertShaderString = function(material, descriptions) { 1076 var buildLambertShaderString = function(material, descriptions) {
1028 descriptions.push('lambert'); 1077 descriptions.push('lambert');
1029 return buildCommonVertexUniforms() + 1078 return buildCommonVertexUniforms() +
1030 buildLightingUniforms() + 1079 buildLightingUniforms() +
1031 buildSkinningUniforms() + 1080 buildSkinningUniforms() +
1032 buildVertexDecls(material, true, false) + 1081 buildVertexDecls(material, true, false) +
1033 p.beginVertexShaderMain() + 1082 p.beginVertexShaderMain() +
1034 p.buildUVPassthroughs(material) + 1083 p.buildUVPassthroughs(material) +
1035 positionVertexShaderCode() + 1084 positionVertexShaderCode() +
1036 normalVertexShaderCode() + 1085 normalVertexShaderCode() +
1037 surfaceToLightVertexShaderCode() + 1086 surfacePositionVertexShaderCode() +
1038 bumpVertexShaderCode() + 1087 bumpVertexShaderCode() +
1039 p.endVertexShaderMain() + 1088 p.endVertexShaderMain() +
1040 p.pixelShaderHeader(material, true, false) + 1089 p.pixelShaderHeader(material, true, false) +
1041 buildCommonPixelUniforms() + 1090 buildCommonPixelUniforms() +
1042 p.repeatVaryingDecls() + 1091 p.repeatVaryingDecls() +
1043 buildColorParam(material, descriptions, 'emissive') + 1092 buildColorParam(material, descriptions, 'emissive') +
1044 buildColorParam(material, descriptions, 'ambient') + 1093 buildColorParam(material, descriptions, 'ambient') +
1045 buildColorParam(material, descriptions, 'diffuse') + 1094 buildColorParam(material, descriptions, 'diffuse') +
1046 buildColorParam(material, descriptions, 'bump', false) + 1095 buildColorParam(material, descriptions, 'bump', false) +
1047 p.utilityFunctions() + 1096 p.utilityFunctions() +
1048 p.beginPixelShaderMain() + 1097 p.beginPixelShaderMain() +
1049 getColorParam(material, 'emissive') + 1098 getColorParam(material, 'emissive') +
1050 getColorParam(material, 'ambient') + 1099 getColorParam(material, 'ambient') +
1051 getColorParam(material, 'diffuse') + 1100 getColorParam(material, 'diffuse') +
1052 getNormalShaderCode() + 1101 getNormalShaderCode() +
1053 ' ' + p.FLOAT3 + ' surfaceToLight = normalize(' + 1102 beginLightLoop() +
1054 p.PIXEL_VARYING_PREFIX + 'surfaceToLight);\n' + 1103 ' ' + p.FLOAT3 + ' surfaceToLight = normalize(' +
1055 ' ' + p.FLOAT4 + 1104 currentLightWorldPos + ' - ' +
1056 ' litR = lit(dot(normal, surfaceToLight), 0.0, 0.0);\n' + 1105 p.PIXEL_VARYING_PREFIX + 'surfacePosition);\n' +
1106 ' ' + p.FLOAT4 +
1107 ' litR = lit(dot(normal, surfaceToLight), 0.0, 0.0);\n' +
1108 endLightLoop('ambient * diffuse + diffuse * litR.y') +
1057 p.endPixelShaderMain(p.FLOAT4 + 1109 p.endPixelShaderMain(p.FLOAT4 +
1058 '((emissive +\n' + 1110 '((emissive +\n' +
1059 ' lightColor *' + 1111 ' lightColorDiffuse).rgb,\n' +
1060 ' (ambient * diffuse + diffuse * litR.y)).rgb,\n' +
1061 ' diffuse.a)') + 1112 ' diffuse.a)') +
1062 p.entryPoints() + 1113 p.entryPoints() +
1063 p.matrixLoadOrder(); 1114 p.matrixLoadOrder();
1064 }; 1115 };
1065 1116
1066 /** 1117 /**
1067 * Builds vertex and fragment shader string for the Blinn lighting type. 1118 * Builds vertex and fragment shader string for the Blinn lighting type.
1068 * @param {!o3d.Material} material The material for which to build 1119 * @param {!o3d.Material} material The material for which to build
1069 * shaders. 1120 * shaders.
1070 * @param {!Array.<string>} descriptions Array to add descriptions too. 1121 * @param {!Array.<string>} descriptions Array to add descriptions too.
1071 * @return {string} The effect code for the shader, ready to be parsed. 1122 * @return {string} The effect code for the shader, ready to be parsed.
1072 * TODO: This is actually just a copy of the Phong code. 1123 * TODO: This is actually just a copy of the Phong code.
1073 * Change to Blinn. 1124 * Change to Blinn.
1074 */ 1125 */
1075 var buildBlinnShaderString = function(material, descriptions) { 1126 var buildBlinnShaderString = function(material, descriptions) {
1076 descriptions.push('phong'); 1127 descriptions.push('phong');
1077 return buildCommonVertexUniforms() + 1128 return buildCommonVertexUniforms() +
1078 buildLightingUniforms() + 1129 buildLightingUniforms() +
1079 buildSkinningUniforms() + 1130 buildSkinningUniforms() +
1080 buildVertexDecls(material, true, true) + 1131 buildVertexDecls(material, true, true) +
1081 p.beginVertexShaderMain() + 1132 p.beginVertexShaderMain() +
1082 p.buildUVPassthroughs(material) + 1133 p.buildUVPassthroughs(material) +
1083 positionVertexShaderCode() + 1134 positionVertexShaderCode() +
1084 normalVertexShaderCode() + 1135 normalVertexShaderCode() +
1085 surfaceToLightVertexShaderCode() + 1136 surfacePositionVertexShaderCode() +
1086 surfaceToViewVertexShaderCode() + 1137 surfaceToViewVertexShaderCode() +
1087 bumpVertexShaderCode() + 1138 bumpVertexShaderCode() +
1088 p.endVertexShaderMain() + 1139 p.endVertexShaderMain() +
1089 p.pixelShaderHeader(material, true, true) + 1140 p.pixelShaderHeader(material, true, true) +
1090 buildCommonPixelUniforms() + 1141 buildCommonPixelUniforms() +
1091 p.repeatVaryingDecls() + 1142 p.repeatVaryingDecls() +
1092 buildColorParam(material, descriptions, 'emissive') + 1143 buildColorParam(material, descriptions, 'emissive') +
1093 buildColorParam(material, descriptions, 'ambient') + 1144 buildColorParam(material, descriptions, 'ambient') +
1094 buildColorParam(material, descriptions, 'diffuse') + 1145 buildColorParam(material, descriptions, 'diffuse') +
1095 buildColorParam(material, descriptions, 'specular') + 1146 buildColorParam(material, descriptions, 'specular') +
1096 buildColorParam(material, descriptions, 'bump', false) + 1147 buildColorParam(material, descriptions, 'bump', false) +
1097 'uniform float shininess;\n' + 1148 'uniform float shininess;\n' +
1098 'uniform float specularFactor;\n' + 1149 'uniform float specularFactor;\n' +
1099 p.utilityFunctions() + 1150 p.utilityFunctions() +
1100 p.beginPixelShaderMain() + 1151 p.beginPixelShaderMain() +
1101 getColorParam(material, 'emissive') + 1152 getColorParam(material, 'emissive') +
1102 getColorParam(material, 'ambient') + 1153 getColorParam(material, 'ambient') +
1103 getColorParam(material, 'diffuse') + 1154 getColorParam(material, 'diffuse') +
1104 getColorParam(material, 'specular') + 1155 getColorParam(material, 'specular') +
1105 getNormalShaderCode() + 1156 getNormalShaderCode() +
1106 ' ' + p.FLOAT3 + ' surfaceToLight = normalize(' +
1107 p.PIXEL_VARYING_PREFIX + 'surfaceToLight);\n' +
1108 ' ' + p.FLOAT3 + ' surfaceToView = normalize(' + 1157 ' ' + p.FLOAT3 + ' surfaceToView = normalize(' +
1109 p.PIXEL_VARYING_PREFIX + 'surfaceToView);\n' + 1158 p.PIXEL_VARYING_PREFIX + 'surfaceToView);\n' +
1110 ' ' + p.FLOAT3 + 1159 beginLightLoop() +
1111 ' halfVector = normalize(surfaceToLight + ' + 1160 ' ' + p.FLOAT3 + ' surfaceToLight = normalize(' +
1112 p.PIXEL_VARYING_PREFIX + 'surfaceToView);\n' + 1161 currentLightWorldPos + ' - ' +
1113 ' ' + p.FLOAT4 + 1162 p.PIXEL_VARYING_PREFIX + 'surfacePosition);\n' +
1163 ' ' + p.FLOAT3 +
1164 ' halfVector = normalize(surfaceToLight + surfaceToView);\n' +
1165 ' ' + p.FLOAT4 +
1114 ' litR = lit(dot(normal, surfaceToLight), \n' + 1166 ' litR = lit(dot(normal, surfaceToLight), \n' +
1115 ' dot(normal, halfVector), shininess);\n' + 1167 ' dot(normal, halfVector), shininess);\n' +
1168 endLightLoop('ambient * diffuse + diffuse * litR.y\n' +
1169 ' + specular * litR.z * specularFactor') +
1116 p.endPixelShaderMain( p.FLOAT4 + 1170 p.endPixelShaderMain( p.FLOAT4 +
1117 '((emissive +\n' + 1171 '((emissive + lightColorDiffuse).rgb,\n' +
1118 ' lightColor *' +
1119 ' (ambient * diffuse + diffuse * litR.y +\n' +
1120 ' + specular * litR.z *' +
1121 ' specularFactor)).rgb,\n' +
1122 ' diffuse.a)') + 1172 ' diffuse.a)') +
1123 p.entryPoints() + 1173 p.entryPoints() +
1124 p.matrixLoadOrder(); 1174 p.matrixLoadOrder();
1125 }; 1175 };
1126 1176
1127 /** 1177 /**
1128 * Builds vertex and fragment shader string for the Phong lighting type. 1178 * Builds vertex and fragment shader string for the Phong lighting type.
1129 * @param {!o3d.Material} material The material for which to build 1179 * @param {!o3d.Material} material The material for which to build
1130 * shaders. 1180 * shaders.
1131 * @param {!Array.<string>} descriptions Array to add descriptions too. 1181 * @param {!Array.<string>} descriptions Array to add descriptions too.
1132 * @return {string} The effect code for the shader, ready to be parsed. 1182 * @return {string} The effect code for the shader, ready to be parsed.
1133 */ 1183 */
1134 var buildPhongShaderString = function(material, descriptions) { 1184 var buildPhongShaderString = function(material, descriptions) {
1135 descriptions.push('phong'); 1185 descriptions.push('phong');
1136 return buildCommonVertexUniforms() + 1186 return buildCommonVertexUniforms() +
1137 buildLightingUniforms() + 1187 buildLightingUniforms() +
1138 buildSkinningUniforms() + 1188 buildSkinningUniforms() +
1139 buildVertexDecls(material, true, true) + 1189 buildVertexDecls(material, true, true) +
1140 p.beginVertexShaderMain() + 1190 p.beginVertexShaderMain() +
1141 p.buildUVPassthroughs(material) + 1191 p.buildUVPassthroughs(material) +
1142 positionVertexShaderCode() + 1192 positionVertexShaderCode() +
1143 normalVertexShaderCode() + 1193 normalVertexShaderCode() +
1144 surfaceToLightVertexShaderCode() + 1194 surfacePositionVertexShaderCode() +
1145 surfaceToViewVertexShaderCode() + 1195 surfaceToViewVertexShaderCode() +
1146 bumpVertexShaderCode() + 1196 bumpVertexShaderCode() +
1147 p.endVertexShaderMain() + 1197 p.endVertexShaderMain() +
1148 p.pixelShaderHeader(material, true, true) + 1198 p.pixelShaderHeader(material, true, true) +
1149 buildCommonPixelUniforms() + 1199 buildCommonPixelUniforms() +
1150 p.repeatVaryingDecls() + 1200 p.repeatVaryingDecls() +
1151 buildColorParam(material, descriptions, 'emissive') + 1201 buildColorParam(material, descriptions, 'emissive') +
1152 buildColorParam(material, descriptions, 'ambient') + 1202 buildColorParam(material, descriptions, 'ambient') +
1153 buildColorParam(material, descriptions, 'diffuse') + 1203 buildColorParam(material, descriptions, 'diffuse') +
1154 buildColorParam(material, descriptions, 'specular') + 1204 buildColorParam(material, descriptions, 'specular') +
1155 buildColorParam(material, descriptions, 'bump', false) + 1205 buildColorParam(material, descriptions, 'bump', false) +
1156 'uniform float shininess;\n' + 1206 'uniform float shininess;\n' +
1157 'uniform float specularFactor;\n' + 1207 'uniform float specularFactor;\n' +
1158 p.utilityFunctions() + 1208 p.utilityFunctions() +
1159 p.beginPixelShaderMain() + 1209 p.beginPixelShaderMain() +
1160 getColorParam(material, 'emissive') + 1210 getColorParam(material, 'emissive') +
1161 getColorParam(material, 'ambient') + 1211 getColorParam(material, 'ambient') +
1162 getColorParam(material, 'diffuse') + 1212 getColorParam(material, 'diffuse') +
1163 getColorParam(material, 'specular') + 1213 getColorParam(material, 'specular') +
1164 getNormalShaderCode() + 1214 getNormalShaderCode() +
1165 ' ' + p.FLOAT3 + ' surfaceToLight = normalize(' +
1166 p.PIXEL_VARYING_PREFIX + 'surfaceToLight);\n' +
1167 ' ' + p.FLOAT3 + ' surfaceToView = normalize(' + 1215 ' ' + p.FLOAT3 + ' surfaceToView = normalize(' +
1168 p.PIXEL_VARYING_PREFIX + 'surfaceToView);\n' + 1216 p.PIXEL_VARYING_PREFIX + 'surfaceToView);\n' +
1169 ' ' + p.FLOAT3 + 1217 beginLightLoop() +
1218 ' ' + p.FLOAT3 + ' surfaceToLight = normalize(' +
1219 currentLightWorldPos + ' - ' +
1220 p.PIXEL_VARYING_PREFIX + 'surfacePosition);\n' +
1221 ' ' + p.FLOAT3 +
1170 ' halfVector = normalize(surfaceToLight + surfaceToView);\n' + 1222 ' halfVector = normalize(surfaceToLight + surfaceToView);\n' +
1171 ' ' + p.FLOAT4 + 1223 ' ' + p.FLOAT4 +
1172 ' litR = lit(dot(normal, surfaceToLight), \n' + 1224 ' litR = lit(dot(normal, surfaceToLight), \n' +
1173 ' dot(normal, halfVector), shininess);\n' + 1225 ' dot(normal, halfVector), shininess);\n' +
1226 endLightLoop('ambient * diffuse + diffuse * litR.y +\n' +
1227 ' + specular * litR.z * specularFactor') +
1174 p.endPixelShaderMain(p.FLOAT4 + 1228 p.endPixelShaderMain(p.FLOAT4 +
1175 '((emissive +\n' + 1229 '((emissive + lightColorDiffuse).rgb,\n' +
1176 ' lightColor * (ambient * diffuse + diffuse * litR.y +\n' +
1177 ' + specular * litR.z *' +
1178 ' specularFactor)).rgb,\n' +
1179 ' diffuse.a)') + 1230 ' diffuse.a)') +
1180 p.entryPoints() + 1231 p.entryPoints() +
1181 p.matrixLoadOrder(); 1232 p.matrixLoadOrder();
1182 }; 1233 };
1183 1234
1184 /** 1235 /**
1185 * Builds the position code for the vertex shader. 1236 * Builds the position code for the vertex shader.
1186 * @return {string} The code for the vertex shader. 1237 * @return {string} The code for the vertex shader.
1187 */ 1238 */
1188 var positionVertexShaderCode = function() { 1239 var positionVertexShaderCode = function() {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1233 'worldInverseTranspose') + '.xyz;\n'; 1284 'worldInverseTranspose') + '.xyz;\n';
1234 } else { 1285 } else {
1235 return ' ' + p.VERTEX_VARYING_PREFIX + 'normal = ' + 1286 return ' ' + p.VERTEX_VARYING_PREFIX + 'normal = ' +
1236 p.mul(p.FLOAT4 + '(' + 1287 p.mul(p.FLOAT4 + '(' +
1237 attribute_normal + ', 0)', 'worldInverseTranspose') + 1288 attribute_normal + ', 0)', 'worldInverseTranspose') +
1238 '.xyz;\n'; 1289 '.xyz;\n';
1239 } 1290 }
1240 }; 1291 };
1241 1292
1242 /** 1293 /**
1243 * Builds the surface to light code for the vertex shader. 1294 * Builds the surface position code for the vertex shader. To support
1295 * multiple lights, the dot product with each light should then be
1296 * computed in the fragment shader.
1244 * @return {string} The code for the vertex shader. 1297 * @return {string} The code for the vertex shader.
1245 */ 1298 */
1246 var surfaceToLightVertexShaderCode = function() { 1299 var surfacePositionVertexShaderCode = function() {
1247 return ' ' + p.VERTEX_VARYING_PREFIX + 1300 return ' ' + p.VERTEX_VARYING_PREFIX +
1248 'surfaceToLight = lightWorldPos - \n' + 1301 'surfacePosition = \n' +
1249 ' ' +
1250 p.mul(p.ATTRIBUTE_PREFIX + 'position', 1302 p.mul(p.ATTRIBUTE_PREFIX + 'position',
1251 'world') + '.xyz;\n'; 1303 'world') + '.xyz;\n';
1252 }; 1304 };
1253 1305
1254 /** 1306 /**
1255 * Builds the surface to view code for the vertex shader. 1307 * Builds the surface to view code for the vertex shader.
1256 * @return {string} The code for the vertex shader. 1308 * @return {string} The code for the vertex shader.
1257 */ 1309 */
1258 var surfaceToViewVertexShaderCode = function() { 1310 var surfaceToViewVertexShaderCode = function() {
1259 return ' ' + p.VERTEX_VARYING_PREFIX + 1311 return ' ' + p.VERTEX_VARYING_PREFIX +
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1339 1391
1340 /** 1392 /**
1341 * Gets or builds a shader for given standard COLLADA material type. 1393 * Gets or builds a shader for given standard COLLADA material type.
1342 * 1394 *
1343 * Looks at the material passed in and assigns it an Effect that matches its 1395 * Looks at the material passed in and assigns it an Effect that matches its
1344 * Params. If a suitable Effect already exists in pack it will use that Effect. 1396 * Params. If a suitable Effect already exists in pack it will use that Effect.
1345 * 1397 *
1346 * @param {!o3d.Pack} pack Pack in which to create the new Effect. 1398 * @param {!o3d.Pack} pack Pack in which to create the new Effect.
1347 * @param {!o3d.Material} material Material for which to build the shader. 1399 * @param {!o3d.Material} material Material for which to build the shader.
1348 * @param {string} effectType Type of effect to create ('phong', 'lambert', 1400 * @param {string} effectType Type of effect to create ('phong', 'lambert',
1349 * 'constant'). 1401 * 'constant', 'blinn').
1402 * @param {{lights: number}} opt_options Extra options.
1403 * If 'lights' is non-zero, creates an array of light params; otherwise
1404 * only a single light is supported.
1350 * @return {o3d.Effect} The created effect. 1405 * @return {o3d.Effect} The created effect.
1351 */ 1406 */
1352 o3djs.effect.getStandardShader = function(pack, 1407 o3djs.effect.getStandardShader = function(pack,
1353 material, 1408 material,
1354 effectType) { 1409 effectType,
1410 opt_options) {
1355 var record = o3djs.effect.buildStandardShaderString(material, 1411 var record = o3djs.effect.buildStandardShaderString(material,
1356 effectType); 1412 effectType,
1413 opt_options);
1357 var effects = pack.getObjectsByClassName('o3d.Effect'); 1414 var effects = pack.getObjectsByClassName('o3d.Effect');
1358 for (var ii = 0; ii < effects.length; ++ii) { 1415 for (var ii = 0; ii < effects.length; ++ii) {
1359 if (effects[ii].name == record.description && 1416 if (effects[ii].name == record.description &&
1360 effects[ii].source == record.shader) { 1417 effects[ii].source == record.shader) {
1361 return effects[ii]; 1418 return effects[ii];
1362 } 1419 }
1363 } 1420 }
1364 var effect = pack.createObject('Effect'); 1421 var effect = pack.createObject('Effect');
1365 if (effect) { 1422 if (effect) {
1366 effect.name = record.description; 1423 effect.name = record.description;
1367 if (effect.loadFromFXString(record.shader)) { 1424 if (effect.loadFromFXString(record.shader)) {
1368 return effect; 1425 return effect;
1369 } 1426 }
1370 pack.removeObject(effect); 1427 pack.removeObject(effect);
1371 } 1428 }
1372 return null; 1429 return null;
1373 }; 1430 };
1374 1431
1375 /** 1432 /**
1376 * Attaches a shader for a given standard COLLADA material type to the 1433 * Attaches a shader for a given standard COLLADA material type to the
1377 * material. 1434 * material.
1378 * 1435 *
1379 * Looks at the material passed in and assigns it an Effect that matches its 1436 * Looks at the material passed in and assigns it an Effect that matches its
1380 * Params. If a suitable Effect already exists in pack it will use that Effect. 1437 * Params. If a suitable Effect already exists in pack it will use that Effect.
1381 * 1438 *
1382 * @param {!o3d.Pack} pack Pack in which to create the new Effect. 1439 * @param {!o3d.Pack} pack Pack in which to create the new Effect.
1383 * @param {!o3d.Material} material Material for which to build the shader. 1440 * @param {!o3d.Material} material Material for which to build the shader.
1384 * @param {!o3djs.math.Vector3} lightPos Position of the default light. 1441 * @param {!o3djs.math.Vector3} lightPos Position of the default light.
1385 * @param {string} effectType Type of effect to create ('phong', 'lambert', 1442 * @param {string} effectType Type of effect to create ('phong', 'lambert',
1386 * 'constant'). 1443 * 'constant', 'blinn').
1444 * @param {Object} opt_options Extra options for effect.getStandardShader
1387 * @return {boolean} True on success. 1445 * @return {boolean} True on success.
1388 */ 1446 */
1389 o3djs.effect.attachStandardShader = function(pack, 1447 o3djs.effect.attachStandardShader = function(pack,
1390 material, 1448 material,
1391 lightPos, 1449 lightPos,
1392 effectType) { 1450 effectType,
1451 opt_options) {
1393 var effect = o3djs.effect.getStandardShader(pack, 1452 var effect = o3djs.effect.getStandardShader(pack,
1394 material, 1453 material,
1395 effectType); 1454 effectType,
1455 opt_options);
1396 if (effect) { 1456 if (effect) {
1397 material.effect = effect; 1457 material.effect = effect;
1398 effect.createUniformParameters(material); 1458 effect.createUniformParameters(material);
1399 1459
1400 // Set a couple of the default parameters in the hopes that this will 1460 // Set a couple of the default parameters in the hopes that this will
1401 // help the user get something on the screen. We check to make sure they 1461 // help the user get something on the screen. We check to make sure they
1402 // are not connected to something otherwise we'll get an error. 1462 // are not connected to something otherwise we'll get an error.
1403 var param = material.getParam('lightWorldPos'); 1463 var param = material.getParam('lightWorldPos');
1404 if (param && !param.inputConnection) { 1464 if (param && !param.inputConnection) {
1405 param.value = lightPos; 1465 param.value = lightPos;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1465 effect.name = o3djs.effect.TWO_COLOR_CHECKER_EFFECT_NAME; 1525 effect.name = o3djs.effect.TWO_COLOR_CHECKER_EFFECT_NAME;
1466 return effect; 1526 return effect;
1467 }; 1527 };
1468 1528
1469 1529
1470 // For compatability with o3d code, the default language is o3d shading 1530 // For compatability with o3d code, the default language is o3d shading
1471 // language. 1531 // language.
1472 o3djs.effect.setLanguage('o3d'); 1532 o3djs.effect.setLanguage('o3d');
1473 1533
1474 1534
OLDNEW
« no previous file with comments | « o3d-webgl-samples/multiple-lights.html ('k') | o3djs/material.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698