aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/pulse-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/pulse-material.js')
-rw-r--r--js/lib/rdge/materials/pulse-material.js343
1 files changed, 178 insertions, 165 deletions
diff --git a/js/lib/rdge/materials/pulse-material.js b/js/lib/rdge/materials/pulse-material.js
index 4d5a158d..a1b28d66 100644
--- a/js/lib/rdge/materials/pulse-material.js
+++ b/js/lib/rdge/materials/pulse-material.js
@@ -1,24 +1,25 @@
1/* <copyright> 1/* <copyright>
2Copyright (c) 2012, Motorola Mobility, Inc 2Copyright (c) 2012, Motorola Mobility LLC.
3All Rights Reserved. 3All Rights Reserved.
4BSD License.
5 4
6Redistribution and use in source and binary forms, with or without 5Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met: 6modification, are permitted provided that the following conditions are met:
8 7
9 - Redistributions of source code must retain the above copyright notice, 8* Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer. 9 this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright 10
12 notice, this list of conditions and the following disclaimer in the 11* Redistributions in binary form must reproduce the above copyright notice,
13 documentation and/or other materials provided with the distribution. 12 this list of conditions and the following disclaimer in the documentation
14 - Neither the name of Motorola Mobility nor the names of its contributors 13 and/or other materials provided with the distribution.
15 may be used to endorse or promote products derived from this software 14
16 without specific prior written permission. 15* Neither the name of Motorola Mobility LLC nor the names of its
16 contributors may be used to endorse or promote products derived from this
17 software without specific prior written permission.
17 18
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
@@ -37,176 +38,188 @@ var Texture = require("js/lib/rdge/texture").Texture;
37/////////////////////////////////////////////////////////////////////// 38///////////////////////////////////////////////////////////////////////
38var PulseMaterial = function PulseMaterial() 39var PulseMaterial = function PulseMaterial()
39{ 40{
40 var MaterialLibrary = require("js/models/materials-model").MaterialsModel; 41 var MaterialLibrary = require("js/models/materials-model").MaterialsModel;
41 42
42 // initialize the inherited members 43 // initialize the inherited members
43 this.inheritedFrom = Material; 44 this.inheritedFrom = Material;
44 this.inheritedFrom(); 45 this.inheritedFrom();
45 46
46 /////////////////////////////////////////////////////////////////////// 47 ///////////////////////////////////////////////////////////////////////
47 // Instance variables 48 // Instance variables
48 /////////////////////////////////////////////////////////////////////// 49 ///////////////////////////////////////////////////////////////////////
49 this._name = "Pulse"; 50 this._name = "Pulse";
50 this._shaderName = "pulse"; 51 this._shaderName = "pulse";
51 52
52 this._texMap = 'assets/images/cubelight.png'; 53 this._texMap = 'assets/images/cubelight.png';
53 54
54 this._time = 0.0; 55 this._time = 0.0;
55 this._dTime = 0.01; 56 this._dTime = 0.01;
56 57
57 this._glTextures = []; 58 this._glTextures = [];
58 59
59 /////////////////////////////////////////////////////////////////////// 60 ///////////////////////////////////////////////////////////////////////
60 // Property Accessors 61 // Property Accessors
61 /////////////////////////////////////////////////////////////////////// 62 ///////////////////////////////////////////////////////////////////////
62 this.isAnimated = function() { return true; }; 63 this.isAnimated = function() { return true; };
63 this.getShaderDef = function() { return pulseMaterialDef; } 64 this.getShaderDef = function() { return pulseMaterialDef; }
64 65
65 /////////////////////////////////////////////////////////////////////// 66 ///////////////////////////////////////////////////////////////////////
66 // Material Property Accessors 67 // Material Property Accessors
67 /////////////////////////////////////////////////////////////////////// 68 ///////////////////////////////////////////////////////////////////////
68 69
69 var u_tex0_index = 0, 70 var u_tex0_index = 0,
70 u_xScale_index = 1, 71 u_xScale_index = 1,
71 u_yScale_index = 2, 72 u_yScale_index = 2,
72 u_speed_index = 3; 73 u_speed_index = 3;
73 74
74 this._propNames = ["u_tex0", "u_xscale", "u_yscale", "u_speed" ]; 75 this._propNames = ["u_tex0", "u_xscale", "u_yscale", "u_speed" ];
75 this._propLabels = ["Texture map", "X Range", "Y Range", "Speed" ]; 76 this._propLabels = ["Texture map", "X Range", "Y Range", "Speed" ];
76 this._propTypes = ["file", "float", "float", "float"]; 77 this._propTypes = ["file", "float", "float", "float"];
77 this._propValues = []; 78 this._propValues = [];
78 79
79 this._propValues[ this._propNames[ u_tex0_index] ] = this._texMap.slice(0); 80 this._propValues[ this._propNames[ u_tex0_index] ] = this._texMap.slice(0);
80 this._propValues[ this._propNames[u_xScale_index] ] = 0.5; 81 this._propValues[ this._propNames[u_xScale_index] ] = 0.5;
81 this._propValues[ this._propNames[u_yScale_index] ] = 0.4; 82 this._propValues[ this._propNames[u_yScale_index] ] = 0.4;
82 this._propValues[ this._propNames[ u_speed_index] ] = 1.0; 83 this._propValues[ this._propNames[ u_speed_index] ] = 1.0;
83 /////////////////////////////////////////////////////////////////////// 84 ///////////////////////////////////////////////////////////////////////
84 85
85 86
86 /////////////////////////////////////////////////////////////////////// 87 ///////////////////////////////////////////////////////////////////////
87 // Methods 88 // Methods
88 /////////////////////////////////////////////////////////////////////// 89 ///////////////////////////////////////////////////////////////////////
89 // duplicate method required 90 // duplicate method required
90 91
91 this.init = function( world ) 92 this.init = function( world )
92 { 93 {
93 // save the world 94 // save the world
94 if (world) this.setWorld( world ); 95 if (world) this.setWorld( world );
95 96
96 // this variable declared above is inherited set to a smaller delta. 97 // this variable declared above is inherited set to a smaller delta.
97 // the pulse material runs a little faster 98 // the pulse material runs a little faster
98 this._dTime = 0.01; 99 this._dTime = 0.01;
99 100
100 // set up the shader 101 // set up the shader
101 this._shader = new RDGE.jshader(); 102 this._shader = new RDGE.jshader();
102 this._shader.def = pulseMaterialDef; 103 this._shader.def = pulseMaterialDef;
103 this._shader.init(); 104 this._shader.init();
104 105
105 // set up the material node 106 // set up the material node
106 this._materialNode = RDGE.createMaterialNode("pulseMaterial" + "_" + world.generateUniqueNodeID()); 107 this._materialNode = RDGE.createMaterialNode("pulseMaterial" + "_" + world.generateUniqueNodeID());
107 this._materialNode.setShader(this._shader); 108 this._materialNode.setShader(this._shader);
108 109
109 this._time = 0; 110 this._time = 0;
110 if (this._shader && this._shader['default']) { 111 if (this._shader && this._shader['default']) {
111 this._shader['default'].u_time.set( [this._time] ); 112 this._shader['default'].u_time.set( [this._time] );
112 } 113 }
113 114
114 // set the shader values in the shader 115 // set the shader values in the shader
115 this.setShaderValues(); 116 this.setShaderValues();
116 this.setResolution( [world.getViewportWidth(),world.getViewportHeight()] ); 117 this.setResolution( [world.getViewportWidth(),world.getViewportHeight()] );
117 this.update( 0 ); 118 this.update( 0 );
118 }; 119 };
119 120
120 this.update = function( time ) 121 this.resetToDefault = function()
121 { 122 {
122 var material = this._materialNode; 123 this._propValues[ this._propNames[ u_tex0_index] ] = this._texMap.slice(0);
123 if (material) 124 this._propValues[ this._propNames[u_xScale_index] ] = 0.5;
124 { 125 this._propValues[ this._propNames[u_yScale_index] ] = 0.4;
125 var technique = material.shaderProgram['default']; 126 this._propValues[ this._propNames[ u_speed_index] ] = 1.0;
126 var renderer = RDGE.globals.engine.getContext().renderer; 127
127 if (renderer && technique) 128 var nProps = this._propNames.length;
128 { 129 for (var i=0; i<nProps; i++)
129 var glTex = this._glTextures["u_tex0"]; 130 this.setProperty( this._propNames[i], this._propValues[this._propNames[i]] );
130 if (glTex)
131 {
132 //this.updateTexture();
133 if (glTex.isAnimated())
134 glTex.render();
135 tex = glTex.getTexture();
136 if (tex)
137 technique.u_tex0.set( tex );
138 }
139
140 if (this._shader && this._shader['default']) {
141 this._shader['default'].u_time.set( [this._time] );
142 }
143 this._time += this._dTime;
144 }
145 }
146 }; 131 };
147 132
148 this.setResolution = function( res ) { 133 this.update = function( time )
149 var material = this._materialNode; 134 {
150 if (material) { 135 var material = this._materialNode;
151 var technique = material.shaderProgram['default']; 136 if (material)