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

Side by Side Diff: samples/o3djs/particles.js

Issue 125189: Added particle trails for things like exhaust on a car. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/o3d/
Patch Set: Created 11 years, 6 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 | « samples/o3djs/base.js ('k') | samples/particles.html » ('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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 '\n' + 120 '\n' +
121 ' float frame = fmod(floor(localTime / frameDuration + frameStart),\n' + 121 ' float frame = fmod(floor(localTime / frameDuration + frameStart),\n' +
122 ' numFrames);\n' + 122 ' numFrames);\n' +
123 ' float uOffset = frame / numFrames;\n' + 123 ' float uOffset = frame / numFrames;\n' +
124 ' float u = uOffset + (uv.x + 0.5) * (1 / numFrames);\n' + 124 ' float u = uOffset + (uv.x + 0.5) * (1 / numFrames);\n' +
125 '\n' + 125 '\n' +
126 ' output.texcoord = float2(u, uv.y + 0.5);\n' + 126 ' output.texcoord = float2(u, uv.y + 0.5);\n' +
127 ' output.colorMult = input.colorMult;\n' + 127 ' output.colorMult = input.colorMult;\n' +
128 '\n' + 128 '\n' +
129 ' float size = lerp(startSize, endSize, percentLife);\n' + 129 ' float size = lerp(startSize, endSize, percentLife);\n' +
130 ' size = (percentLife < 0 || percentLife > 1) ? 0 : size;\n' +
130 ' float s = sin(spinStart + spinSpeed * localTime);\n' + 131 ' float s = sin(spinStart + spinSpeed * localTime);\n' +
131 ' float c = cos(spinStart + spinSpeed * localTime);\n' + 132 ' float c = cos(spinStart + spinSpeed * localTime);\n' +
132 '\n' + 133 '\n' +
133 ' float4 rotatedPoint = float4((uv.x * c + uv.y * s) * size, 0,\n' + 134 ' float4 rotatedPoint = float4((uv.x * c + uv.y * s) * size, 0,\n' +
134 ' (uv.x * s - uv.y * c) * size, 1);\n' + 135 ' (uv.x * s - uv.y * c) * size, 1);\n' +
135 ' float3 center = velocity * localTime +\n' + 136 ' float3 center = velocity * localTime +\n' +
136 ' acceleration * localTime * localTime + \n' + 137 ' acceleration * localTime * localTime + \n' +
137 ' position;\n' + 138 ' position;\n' +
138 ' \n' + 139 ' \n' +
139 ' float4 q2 = input.orientation + input.orientation;\n' + 140 ' float4 q2 = input.orientation + input.orientation;\n' +
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 ' float uOffset = frame / numFrames;\n' + 233 ' float uOffset = frame / numFrames;\n' +
233 ' float u = uOffset + (uv.x + 0.5) * (1 / numFrames);\n' + 234 ' float u = uOffset + (uv.x + 0.5) * (1 / numFrames);\n' +
234 '\n' + 235 '\n' +
235 ' output.texcoord = float2(u, uv.y + 0.5);\n' + 236 ' output.texcoord = float2(u, uv.y + 0.5);\n' +
236 ' output.colorMult = input.colorMult;\n' + 237 ' output.colorMult = input.colorMult;\n' +
237 '\n' + 238 '\n' +
238 ' float3 basisX = viewInverse[0].xyz;\n' + 239 ' float3 basisX = viewInverse[0].xyz;\n' +
239 ' float3 basisZ = viewInverse[1].xyz;\n' + 240 ' float3 basisZ = viewInverse[1].xyz;\n' +
240 '\n' + 241 '\n' +
241 ' float size = lerp(startSize, endSize, percentLife);\n' + 242 ' float size = lerp(startSize, endSize, percentLife);\n' +
243 ' size = (percentLife < 0 || percentLife > 1) ? 0 : size;\n' +
242 ' float s = sin(spinStart + spinSpeed * localTime);\n' + 244 ' float s = sin(spinStart + spinSpeed * localTime);\n' +
243 ' float c = cos(spinStart + spinSpeed * localTime);\n' + 245 ' float c = cos(spinStart + spinSpeed * localTime);\n' +
244 '\n' + 246 '\n' +
245 ' float2 rotatedPoint = float2(uv.x * c + uv.y * s, \n' + 247 ' float2 rotatedPoint = float2(uv.x * c + uv.y * s, \n' +
246 ' -uv.x * s + uv.y * c);\n' + 248 ' -uv.x * s + uv.y * c);\n' +
247 ' float3 localPosition = float3(basisX * rotatedPoint.x +\n' + 249 ' float3 localPosition = float3(basisX * rotatedPoint.x +\n' +
248 ' basisZ * rotatedPoint.y) * size +\n' + 250 ' basisZ * rotatedPoint.y) * size +\n' +
249 ' velocity * localTime +\n' + 251 ' velocity * localTime +\n' +
250 ' acceleration * localTime * localTime + \n' + 252 ' acceleration * localTime * localTime + \n' +
251 ' position;\n' + 253 ' position;\n' +
(...skipping 10 matching lines...) Expand all
262 ' input.colorMult;\n' + 264 ' input.colorMult;\n' +
263 ' float4 color = tex2D(colorSampler, input.texcoord) * colorMult;\n' + 265 ' float4 color = tex2D(colorSampler, input.texcoord) * colorMult;\n' +
264 ' return color;\n' + 266 ' return color;\n' +
265 '}\n' + 267 '}\n' +
266 '\n' + 268 '\n' +
267 '// #o3d VertexShaderEntryPoint vertexShaderFunction\n' + 269 '// #o3d VertexShaderEntryPoint vertexShaderFunction\n' +
268 '// #o3d PixelShaderEntryPoint pixelShaderFunction\n' + 270 '// #o3d PixelShaderEntryPoint pixelShaderFunction\n' +
269 '// #o3d MatrixLoadOrder RowMajor\n'}]; 271 '// #o3d MatrixLoadOrder RowMajor\n'}];
270 272
271 /** 273 /**
274 * Corner values.
275 * @private
276 * @type {!Array.<!Array.<number>>}
277 */
278 o3djs.particles.CORNERS_ = [
279 [-0.5, -0.5],
280 [+0.5, -0.5],
281 [+0.5, +0.5],
282 [-0.5, +0.5]];
283
284
285 /**
272 * Creates a particle system. 286 * Creates a particle system.
273 * You only need one of these to run multiple emitters of different types 287 * You only need one of these to run multiple emitters of different types
274 * of particles. 288 * of particles.
275 * @param {!o3d.Pack} pack The pack for the particle system to manage resources. 289 * @param {!o3d.Pack} pack The pack for the particle system to manage resources.
276 * @param {!o3djs.rendergraph.viewInfo} viewInfo A viewInfo so the particle 290 * @param {!o3djs.rendergraph.viewInfo} viewInfo A viewInfo so the particle
277 * system can do the default setup. The only thing used from viewInfo 291 * system can do the default setup. The only thing used from viewInfo
278 * is the zOrderedDrawList. If that is not where you want your particles, 292 * is the zOrderedDrawList. If that is not where you want your particles,
279 * after you create the particleEmitter use 293 * after you create the particleEmitter use
280 * particleEmitter.material.drawList = myDrawList to set it to something 294 * particleEmitter.material.drawList = myDrawList to set it to something
281 * else. 295 * else.
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 * @param {!o3d.ParamFloat} opt_clockParam A ParamFloat to be the clock for 683 * @param {!o3d.ParamFloat} opt_clockParam A ParamFloat to be the clock for
670 * the emitter. 684 * the emitter.
671 * @return {!o3djs.particles.ParticleEmitter} The new emitter. 685 * @return {!o3djs.particles.ParticleEmitter} The new emitter.
672 */ 686 */
673 o3djs.particles.ParticleSystem.prototype.createParticleEmitter = 687 o3djs.particles.ParticleSystem.prototype.createParticleEmitter =
674 function(opt_texture, opt_clockParam) { 688 function(opt_texture, opt_clockParam) {
675 return new o3djs.particles.ParticleEmitter(this, opt_texture, opt_clockParam); 689 return new o3djs.particles.ParticleEmitter(this, opt_texture, opt_clockParam);
676 }; 690 };
677 691
678 /** 692 /**
693 * Creates a Trail particle emitter.
694 * You can use this for jet exhaust, etc...
695 * @param {!o3d.Transform} opt_parent The parent for the oneshot.
apatrick 2009/06/16 18:12:57 Missing @param comments.
696 * @return {!o3djs.particles.Trail} A Trail object.
697 */
698 o3djs.particles.ParticleSystem.prototype.createTrail = function(
699 parent,
apatrick 2009/06/16 18:12:57 it's called opt_parent in the @param comment
700 maxParticles,
701 parameters,
702 opt_texture,
703 opt_perParticleParamSetter,
704 opt_clockParam) {
705 return new o3djs.particles.Trail(
706 this,
707 parent,
708 maxParticles,
709 parameters,
710 opt_texture,
711 opt_perParticleParamSetter,
712 opt_clockParam);
713 };
714
715 /**
679 * A ParticleEmitter 716 * A ParticleEmitter
680 * @constructor 717 * @constructor
681 * @param {!o3djs.particles.ParticleSystem} particleSystem The particle system 718 * @param {!o3djs.particles.ParticleSystem} particleSystem The particle system
682 * to manage this emitter. 719 * to manage this emitter.
683 * @param {!o3d.Texture} opt_texture The texture to use for the particles. 720 * @param {!o3d.Texture} opt_texture The texture to use for the particles.
684 * If you don't supply a texture a default is provided. 721 * If you don't supply a texture a default is provided.
685 * @param {!o3d.ParamFloat} opt_clockParam A ParamFloat to be the clock for 722 * @param {!o3d.ParamFloat} opt_clockParam A ParamFloat to be the clock for
686 * the emitter. 723 * the emitter.
687 */ 724 */
688 o3djs.particles.ParticleEmitter = function(particleSystem, 725 o3djs.particles.ParticleEmitter = function(particleSystem,
689 opt_texture, 726 opt_texture,
690 opt_clockParam) { 727 opt_clockParam) {
691
692 opt_clockParam = opt_clockParam || particleSystem.clockParam; 728 opt_clockParam = opt_clockParam || particleSystem.clockParam;
693 729
694 var o3d = o3djs.base.o3d; 730 var o3d = o3djs.base.o3d;
695 var pack = particleSystem.pack; 731 var pack = particleSystem.pack;
696 var viewInfo = particleSystem.viewInfo; 732 var viewInfo = particleSystem.viewInfo;
697 var material = pack.createObject('Material'); 733 var material = pack.createObject('Material');
698 material.name = 'particles'; 734 material.name = 'particles';
699 material.drawList = viewInfo.zOrderedDrawList; 735 material.drawList = viewInfo.zOrderedDrawList;
700 material.effect = particleSystem.effects[1]; 736 material.effect = particleSystem.effects[1];
701 particleSystem.effects[1].createUniformParameters(material); 737 particleSystem.effects[1].createUniformParameters(material);
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 if (!this.rampTexture_) { 871 if (!this.rampTexture_) {
836 this.rampTexture_ = this.particleSystem.pack.createTexture2D( 872 this.rampTexture_ = this.particleSystem.pack.createTexture2D(
837 width, 1, o3djs.base.o3d.Texture.ARGB8, 1, false); 873 width, 1, o3djs.base.o3d.Texture.ARGB8, 1, false);
838 } 874 }
839 875
840 this.rampTexture_.set(0, colorRamp); 876 this.rampTexture_.set(0, colorRamp);
841 this.rampSampler_.texture = this.rampTexture_; 877 this.rampSampler_.texture = this.rampTexture_;
842 }; 878 };
843 879
844 /** 880 /**
845 * Sets the parameters of the particle emitter. 881 * Validates and adds missing particle parameters.
846 * 882 * @param {!o3djs.particles.ParticleSpec} parameters The parameters to validate.
847 * Each of these parameters are in pairs. The used to create a table 883 */
848 * of particle parameters. For each particle a specfic value is 884 o3djs.particles.ParticleEmitter.prototype.validateParameters = function(
849 * set like this 885 parameters) {
850 * 886 var defaults = new o3djs.particles.ParticleSpec();
851 * particle.field = value + Math.random() - 0.5 * valueRange * 2; 887 for (var key in parameters) {
852 * 888 if (typeof defaults[key] === 'undefined') {
853 * or in English 889 throw 'unknown particle parameter "' + key + '"';
854 * 890 }
855 * particle.field = value plus or minus valueRange. 891 }
856 * 892 for (var key in defaults) {
857 * So for example, if you wanted a value from 10 to 20 you'd pass 15 for value 893 if (typeof parameters[key] === 'undefined') {
858 * and 5 for valueRange because 894 parameters[key] = defaults[key];
859 * 895 }
860 * 15 + or - 5 = (10 to 20) 896 }
861 * 897 };
898
899 /**
900 * Creates particles.
901 * @private
902 * @param {number} firstParticleIndex Index of first particle to create.
903 * @param {number} numParticles The number of particles to create.
862 * @param {!o3djs.particles.ParticleSpec} parameters The parameters for the 904 * @param {!o3djs.particles.ParticleSpec} parameters The parameters for the
863 * emitters. 905 * emitters.
864 * @param {!function(number, !o3djs.particles.ParticleSpec): void} 906 * @param {!function(number, !o3djs.particles.ParticleSpec): void}
865 * opt_perParticleParamSetter A function that is called for each particle to 907 * opt_perParticleParamSetter A function that is called for each particle to
866 * allow it's parameters to be adjusted per particle. The number is the 908 * allow it's parameters to be adjusted per particle. The number is the
867 * index of the particle being created, in other words, if numParticles is 909 * index of the particle being created, in other words, if numParticles is
868 * 20 this value will be 0 to 19. The ParticleSpec is a spec for this 910 * 20 this value will be 0 to 19. The ParticleSpec is a spec for this
869 * particular particle. You can set any per particle value before returning. 911 * particular particle. You can set any per particle value before returning.
870 */ 912 */
871 o3djs.particles.ParticleEmitter.prototype.setParameters = function( 913 o3djs.particles.ParticleEmitter.prototype.createParticles_ = function(
914 firstParticleIndex,
915 numParticles,
872 parameters, 916 parameters,
873 opt_perParticleParamSetter) { 917 opt_perParticleParamSetter) {
874 var defaults = new o3djs.particles.ParticleSpec(); 918 var uvLifeTimeFrameStart = this.uvLifeTimeFrameStart_;
875 for (var key in parameters) { 919 var positionStartTime = this.positionStartTime_;
876 if (typeof defaults[key] === 'undefined') { 920 var velocityStartSize = this.velocityStartSize_;
877 throw 'unknown particle parameter "' + key + '"'; 921 var accelerationEndSize = this.accelerationEndSize_;
878 } 922 var spinStartSpinSpeed = this.spinStartSpinSpeed_;
879 defaults[key] = parameters[key]; 923 var orientation = this.orientation_;
880 } 924 var colorMults = this.colorMults_;
881
882 var numParticles = defaults.numParticles;
883
884 var uvLifeTimeFrameStart = [];
885 var positionStartTime = [];
886 var velocityStartSize = [];
887 var accelerationEndSize = [];
888 var spinStartSpinSpeed = [];
889 var orientation = [];
890 var colorMults = [];
891 925
892 // Set the globals. 926 // Set the globals.
893 this.material.effect = 927 this.material.effect =
894 this.particleSystem.effects[defaults.billboard ? 1 : 0]; 928 this.particleSystem.effects[parameters.billboard ? 1 : 0];
895 this.material.getParam('timeRange').value = defaults.timeRange; 929 this.material.getParam('timeRange').value = parameters.timeRange;
896 this.material.getParam('numFrames').value = defaults.numFrames; 930 this.material.getParam('numFrames').value = parameters.numFrames;
897 this.material.getParam('frameDuration').value = defaults.frameDuration; 931 this.material.getParam('frameDuration').value = parameters.frameDuration;
898 this.material.getParam('worldVelocity').value = defaults.worldVelocity; 932 this.material.getParam('worldVelocity').value = parameters.worldVelocity;
899 this.material.getParam('worldAcceleration').value = 933 this.material.getParam('worldAcceleration').value =
900 defaults.worldAcceleration; 934 parameters.worldAcceleration;
901
902 var corners = [
903 [-0.5, -0.5],
904 [+0.5, -0.5],
905 [+0.5, +0.5],
906 [-0.5, +0.5]];
907 935
908 var random = this.particleSystem.randomFunction_; 936 var random = this.particleSystem.randomFunction_;
909 937
910 var plusMinus = function(range) { 938 var plusMinus = function(range) {
911 return (random() - 0.5) * range * 2; 939 return (random() - 0.5) * range * 2;
912 }; 940 };
913 941
942 // TODO: change to not allocate.
914 var plusMinusVector = function(range) { 943 var plusMinusVector = function(range) {
915 var v = []; 944 var v = [];
916 for (var ii = 0; ii < range.length; ++ii) { 945 for (var ii = 0; ii < range.length; ++ii) {
917 v.push(plusMinus(range[ii])); 946 v.push(plusMinus(range[ii]));
918 } 947 }
919 return v; 948 return v;
920 }; 949 };
921 950
922 for (var ii = 0; ii < numParticles; ++ii) { 951 for (var ii = 0; ii < numParticles; ++ii) {
923 if (opt_perParticleParamSetter) { 952 if (opt_perParticleParamSetter) {
924 opt_perParticleParamSetter(ii, defaults); 953 opt_perParticleParamSetter(ii, parameters);
925 } 954 }
926 var pLifeTime = defaults.lifeTime; 955 var pLifeTime = parameters.lifeTime;
927 var pStartTime = (defaults.startTime === null) ? 956 var pStartTime = (parameters.startTime === null) ?
928 (ii * defaults.lifeTime / numParticles) : defaults.startTime; 957 (ii * parameters.lifeTime / numParticles) : parameters.startTime;
929 var pFrameStart = defaults.frameStart + plusMinus(defaults.frameStartRange); 958 var pFrameStart =
959 parameters.frameStart + plusMinus(parameters.frameStartRange);
930 var pPosition = o3djs.math.addVector( 960 var pPosition = o3djs.math.addVector(
931 defaults.position, plusMinusVector(defaults.positionRange)); 961 parameters.position, plusMinusVector(parameters.positionRange));
932 var pVelocity = o3djs.math.addVector( 962 var pVelocity = o3djs.math.addVector(
933 defaults.velocity, plusMinusVector(defaults.velocityRange)); 963 parameters.velocity, plusMinusVector(parameters.velocityRange));
934 var pAcceleration = o3djs.math.addVector( 964 var pAcceleration = o3djs.math.addVector(
935 defaults.acceleration, plusMinusVector(defaults.accelerationRange)); 965 parameters.acceleration,
966 plusMinusVector(parameters.accelerationRange));
936 var pColorMult = o3djs.math.addVector( 967 var pColorMult = o3djs.math.addVector(
937 defaults.colorMult, plusMinusVector(defaults.colorMultRange)); 968 parameters.colorMult, plusMinusVector(parameters.colorMultRange));
938 var pSpinStart = defaults.spinStart + plusMinus(defaults.spinStartRange); 969 var pSpinStart =
939 var pSpinSpeed = defaults.spinSpeed + plusMinus(defaults.spinSpeedRange); 970 parameters.spinStart + plusMinus(parameters.spinStartRange);
940 var pStartSize = defaults.startSize + plusMinus(defaults.startSizeRange); 971 var pSpinSpeed =
941 var pEndSize = defaults.endSize + plusMinus(defaults.endSizeRange); 972 parameters.spinSpeed + plusMinus(parameters.spinSpeedRange);
942 var pOrientation = defaults.orientation; 973 var pStartSize =
974 parameters.startSize + plusMinus(parameters.startSizeRange);
975 var pEndSize = parameters.endSize + plusMinus(parameters.endSizeRange);
976 var pOrientation = parameters.orientation;
943 977
944 // make each corner of the particle. 978 // make each corner of the particle.
945 for (var jj = 0; jj < 4; ++jj) { 979 for (var jj = 0; jj < 4; ++jj) {
946 uvLifeTimeFrameStart.push(corners[jj][0], corners[jj][1], pLifeTime, 980 var offset0 = (ii * 4 + jj) * 4;
947 pFrameStart); 981 var offset1 = offset0 + 1;
948 positionStartTime.push( 982 var offset2 = offset0 + 2;
949 pPosition[0], pPosition[1], pPosition[2], pStartTime); 983 var offset3 = offset0 + 3;
950 velocityStartSize.push( 984
951 pVelocity[0], pVelocity[1], pVelocity[2], pStartSize); 985 uvLifeTimeFrameStart[offset0] = o3djs.particles.CORNERS_[jj][0];
952 accelerationEndSize.push( 986 uvLifeTimeFrameStart[offset1] = o3djs.particles.CORNERS_[jj][1];
953 pAcceleration[0], pAcceleration[1], pAcceleration[2], pEndSize); 987 uvLifeTimeFrameStart[offset2] = pLifeTime;
954 spinStartSpinSpeed.push(pSpinStart, pSpinSpeed, 0, 0); 988 uvLifeTimeFrameStart[offset3] = pFrameStart;
955 orientation.push( 989
956 pOrientation[0], pOrientation[1], pOrientation[2], pOrientation[3]); 990 positionStartTime[offset0] = pPosition[0];
957 colorMults.push( 991 positionStartTime[offset1] = pPosition[1];
958 pColorMult[0], pColorMult[1], pColorMult[2], pColorMult[3]); 992 positionStartTime[offset2] = pPosition[2];
993 positionStartTime[offset3] = pStartTime;
994
995 velocityStartSize[offset0] = pVelocity[0];
996 velocityStartSize[offset1] = pVelocity[1];
997 velocityStartSize[offset2] = pVelocity[2];
998 velocityStartSize[offset3] = pStartSize;
999
1000 accelerationEndSize[offset0] = pAcceleration[0];
1001 accelerationEndSize[offset1] = pAcceleration[1];
1002 accelerationEndSize[offset2] = pAcceleration[2];
1003 accelerationEndSize[offset3] = pEndSize;
1004
1005 spinStartSpinSpeed[offset0] = pSpinStart;
1006 spinStartSpinSpeed[offset1] = pSpinSpeed;
1007 spinStartSpinSpeed[offset2] = 0;
1008 spinStartSpinSpeed[offset3] = 0;
1009
1010 orientation[offset0] = pOrientation[0];
1011 orientation[offset1] = pOrientation[1];
1012 orientation[offset2] = pOrientation[2];
1013 orientation[offset3] = pOrientation[3];
1014
1015 colorMults[offset0] = pColorMult[0];
1016 colorMults[offset1] = pColorMult[1];
1017 colorMults[offset2] = pColorMult[2];
1018 colorMults[offset3] = pColorMult[3];
959 } 1019 }
960 } 1020 }
961 1021
1022 firstParticleIndex *= 4;
1023 this.uvLifeTimeFrameStartField_.setAt(
1024 firstParticleIndex,
1025 uvLifeTimeFrameStart);
1026 this.positionStartTimeField_.setAt(
1027 firstParticleIndex,
1028 positionStartTime);
1029 this.velocityStartSizeField_.setAt(
1030 firstParticleIndex,
1031 velocityStartSize);
1032 this.accelerationEndSizeField_.setAt(
1033 firstParticleIndex,
1034 accelerationEndSize);
1035 this.spinStartSpinSpeedField_.setAt(
1036 firstParticleIndex,
1037 spinStartSpinSpeed);
1038 this.orientationField_.setAt(
1039 firstParticleIndex,
1040 orientation);
1041 this.colorMultField_.setAt(
1042 firstParticleIndex,
1043 colorMults);
1044 };
1045
1046 /**
1047 * Allocates particles.
1048 * @param {number} numParticles Number of particles to allocate.
apatrick 2009/06/16 18:12:57 @private
1049 */
1050 o3djs.particles.ParticleEmitter.prototype.allocateParticles_ = function(
1051 numParticles) {
962 if (this.vertexBuffer_.numElements != numParticles * 4) { 1052 if (this.vertexBuffer_.numElements != numParticles * 4) {
963 this.vertexBuffer_.allocateElements(numParticles * 4); 1053 this.vertexBuffer_.allocateElements(numParticles * 4);
964 1054
965 var indices = []; 1055 var indices = [];
966 for (var ii = 0; ii < numParticles; ++ii) { 1056 for (var ii = 0; ii < numParticles; ++ii) {
967 // Make 2 triangles for the quad. 1057 // Make 2 triangles for the quad.
968 var startIndex = ii * 4 1058 var startIndex = ii * 4
969 indices.push(startIndex + 0, startIndex + 1, startIndex + 2); 1059 indices.push(startIndex + 0, startIndex + 1, startIndex + 2);
970 indices.push(startIndex + 0, startIndex + 2, startIndex + 3); 1060 indices.push(startIndex + 0, startIndex + 2, startIndex + 3);
971 } 1061 }
972 this.indexBuffer_.set(indices); 1062 this.indexBuffer_.set(indices);
1063
1064 // We keep these around to avoid memory allocations for trails.
1065 this.uvLifeTimeFrameStart_ = [];
1066 this.positionStartTime_ = [];
1067 this.velocityStartSize_ = [];
1068 this.accelerationEndSize_ = [];
1069 this.spinStartSpinSpeed_ = [];
1070 this.orientation_ = [];
1071 this.colorMults_ = [];
973 } 1072 }
974 1073
975 this.uvLifeTimeFrameStartField_.setAt(0, uvLifeTimeFrameStart);
976 this.positionStartTimeField_.setAt(0, positionStartTime);
977 this.velocityStartSizeField_.setAt(0, velocityStartSize);
978 this.accelerationEndSizeField_.setAt(0, accelerationEndSize);
979 this.spinStartSpinSpeedField_.setAt(0, spinStartSpinSpeed);
980 this.orientationField_.setAt(0, orientation);
981 this.colorMultField_.setAt(0, colorMults);
982
983 this.primitive_.numberPrimitives = numParticles * 2; 1074 this.primitive_.numberPrimitives = numParticles * 2;
984 this.primitive_.numberVertices = numParticles * 4; 1075 this.primitive_.numberVertices = numParticles * 4;
985 }; 1076 };
986 1077
987 /** 1078 /**
1079 * Sets the parameters of the particle emitter.
1080 *
1081 * Each of these parameters are in pairs. The used to create a table
1082 * of particle parameters. For each particle a specfic value is
1083 * set like this
1084 *
1085 * particle.field = value + Math.random() - 0.5 * valueRange * 2;
1086 *
1087 * or in English
1088 *
1089 * particle.field = value plus or minus valueRange.
1090 *
1091 * So for example, if you wanted a value from 10 to 20 you'd pass 15 for value
1092 * and 5 for valueRange because
1093 *
1094 * 15 + or - 5 = (10 to 20)
1095 *
1096 * @param {!o3djs.particles.ParticleSpec} parameters The parameters for the
1097 * emitters.
1098 * @param {!function(number, !o3djs.particles.ParticleSpec): void}
1099 * opt_perParticleParamSetter A function that is called for each particle to
1100 * allow it's parameters to be adjusted per particle. The number is the
1101 * index of the particle being created, in other words, if numParticles is
1102 * 20 this value will be 0 to 19. The ParticleSpec is a spec for this
1103 * particular particle. You can set any per particle value before returning.
1104 */
1105 o3djs.particles.ParticleEmitter.prototype.setParameters = function(
1106 parameters,
1107 opt_perParticleParamSetter) {
1108 this.validateParameters(parameters);
1109
1110 var numParticles = parameters.numParticles;
1111
1112 this.allocateParticles_(numParticles);
1113
1114 this.createParticles_(
1115 0,
1116 numParticles,
1117 parameters,
1118 opt_perParticleParamSetter);
1119 };
1120
1121 /**
988 * Creates a OneShot particle emitter instance. 1122 * Creates a OneShot particle emitter instance.
989 * You can use this for dust puffs, explosions, fireworks, etc... 1123 * You can use this for dust puffs, explosions, fireworks, etc...
990 * @param {!o3d.Transform} opt_parent The parent for the oneshot. 1124 * @param {!o3d.Transform} opt_parent The parent for the oneshot.
991 * @return {!o3djs.particles.OneShot} A OneShot object. 1125 * @return {!o3djs.particles.OneShot} A OneShot object.
992 */ 1126 */
993 o3djs.particles.ParticleEmitter.prototype.createOneShot = function(opt_parent) { 1127 o3djs.particles.ParticleEmitter.prototype.createOneShot = function(opt_parent) {
994 return new o3djs.particles.OneShot(this, opt_parent); 1128 return new o3djs.particles.OneShot(this, opt_parent);
995 }; 1129 };
996 1130
997 /** 1131 /**
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1042 if (opt_parent) { 1176 if (opt_parent) {
1043 this.setParent(opt_parent); 1177 this.setParent(opt_parent);
1044 } 1178 }
1045 if (opt_position) { 1179 if (opt_position) {
1046 this.transform.identity(); 1180 this.transform.identity();
1047 this.transform.translate(opt_position); 1181 this.transform.translate(opt_position);
1048 } 1182 }
1049 this.transform.visible = true; 1183 this.transform.visible = true;
1050 this.timeOffsetParam_.value = this.emitter_.clockParam.value; 1184 this.timeOffsetParam_.value = this.emitter_.clockParam.value;
1051 }; 1185 };
1186
1187 /**
1188 * A type of emitter to use for particle effects that leave trails like exhaust.
1189 * @constructor
1190 * @param {!o3djs.particles.ParticleSystem} particleSystem The particle system
1191 * to manage this emitter.
1192 * @param {!o3d.Transform} parent Transform to put emitter on.
1193 * @param {number} maxParticles Maximum number of particles to appear at once.
1194 * @param {!o3djs.particles.ParticleSpec} parameters The parameters used to
1195 * generate particles.
1196 * @param {!o3d.Texture} opt_texture The texture to use for the particles.
1197 * If you don't supply a texture a default is provided.
1198 * @param {!o3d.ParamFloat} opt_clockParam A ParamFloat to be the clock for
apatrick 2009/06/16 18:12:57 you could swap this @param comment with the one fo
1199 * the emitter.
1200 * @param {!function(number, !o3djs.particles.ParticleSpec): void}
1201 * opt_perParticleParamSetter A function that is called for each particle to
1202 * allow it's parameters to be adjusted per particle. The number is the
1203 * index of the particle being created, in other words, if numParticles is
1204 * 20 this value will be 0 to 19. The ParticleSpec is a spec for this
1205 * particular particle. You can set any per particle value before returning.
1206 */
1207 o3djs.particles.Trail = function(
1208 particleSystem,
1209 parent,
1210 maxParticles,
1211 parameters,
1212 opt_texture,
1213 opt_perParticleParamSetter,
1214 opt_clockParam) {
1215 o3djs.particles.ParticleEmitter.call(
1216 this, particleSystem, opt_texture, opt_clockParam);
1217
1218 var pack = particleSystem.pack;
1219
1220 this.allocateParticles_(maxParticles);
1221 this.validateParameters(parameters);
1222
1223 this.parameters = parameters;
1224 this.perParticleParamSetter = opt_perParticleParamSetter;
1225 this.birthIndex_ = 0;
1226 this.maxParticles_ = maxParticles;
1227
1228 /**
1229 * Transform for OneShot.
1230 * @type {!o3d.Transform}
1231 */
1232 this.transform = pack.createObject('Transform');
1233 this.transform.addShape(this.shape);
1234
1235 this.transform.parent = parent;
1236 };
1237
1238 o3djs.base.inherit(o3djs.particles.Trail, o3djs.particles.ParticleEmitter);
1239
1240 /**
1241 * Births particles from this Trail.
1242 * @param {!o3djs.math.Vector3} position Position to birth particles at.
1243 */
1244 o3djs.particles.Trail.prototype.birthParticles = function(position) {
1245 var numParticles = this.parameters.numParticles;
1246 this.parameters.startTime = this.clockParam.value;
1247 this.parameters.position = position;
1248 while (this.birthIndex_ + numParticles >= this.maxParticles_) {
1249 var numParticlesToEnd = this.maxParticles_ - this.birthIndex_;
1250 this.createParticles_(this.birthIndex_,
1251 numParticlesToEnd,
1252 this.parameters,
1253 this.perParticleParamSetter);
1254 numParticles -= numParticlesToEnd;
1255 this.birthIndex_ = 0;
1256 }
1257 this.createParticles_(this.birthIndex_,
1258 numParticles,
1259 this.parameters,
1260 this.perParticleParamSetter);
1261 this.birthIndex_ += numParticles;
1262 };
1263
OLDNEW
« no previous file with comments | « samples/o3djs/base.js ('k') | samples/particles.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698