aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/twist-vert-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/twist-vert-material.js')
-rw-r--r--js/lib/rdge/materials/twist-vert-material.js300
1 files changed, 157 insertions, 143 deletions
diff --git a/js/lib/rdge/materials/twist-vert-material.js b/js/lib/rdge/materials/twist-vert-material.js
index 6b71593f..4f002e8f 100644
--- a/js/lib/rdge/materials/twist-vert-material.js
+++ b/js/lib/rdge/materials/twist-vert-material.js
@@ -37,159 +37,173 @@ var Texture = require("js/lib/rdge/texture").Texture;
37/////////////////////////////////////////////////////////////////////// 37///////////////////////////////////////////////////////////////////////
38var TwistVertMaterial = function TwistVertMaterial() 38var TwistVertMaterial = function TwistVertMaterial()
39{ 39{
40 // initialize the inherited members 40 // initialize the inherited members
41 this.inheritedFrom = Material; 41 this.inheritedFrom = Material;
42 this.inheritedFrom(); 42 this.inheritedFrom();
43 43
44 /////////////////////////////////////////////////////////////////////// 44 ///////////////////////////////////////////////////////////////////////
45 // Instance variables 45 // Instance variables
46 /////////////////////////////////////////////////////////////////////// 46 ///////////////////////////////////////////////////////////////////////
47 this._name = "Twist Vertex"; 47 this._name = "Twist Vertex";
48 this._shaderName = "twistVert"; 48 this._shaderName = "twistVert";
49 49
50 this._tex0 = 'assets/images/rocky-normal.jpg'; 50 this._tex0 = 'assets/images/rocky-normal.jpg';
51 this._tex1 = 'assets/images/metal.png'; 51 this._tex1 = 'assets/images/metal.png';
52 52
53 this._angle = 0.0; 53 this._angle = 0.0;
54 this._deltaTime = 0.01; 54 this._deltaTime = 0.01;
55 this._speed = 1.0; 55 this._speed = 1.0;
56 56
57 /////////////////////////////////////////////////////////////////////// 57 ///////////////////////////////////////////////////////////////////////
58 // Property Accessors 58 // Property Accessors
59 /////////////////////////////////////////////////////////////////////// 59 ///////////////////////////////////////////////////////////////////////
60 this.getShaderName = function () { return this._shaderName; }; 60 this.getShaderName = function () { return this._shaderName; };
61 this.isAnimated = function () { return true; }; 61 this.isAnimated = function () { return true; };
62 this.getShaderDef = function() { return twistVertShaderDef; }; 62 this.getShaderDef = function() { return twistVertShaderDef; };
63 this.getTechniqueName = function() { return 'twistMe' }; 63 this.getTechniqueName = function() { return 'twistMe' };
64 64
65 this.hasVertexDeformation = function () { return this._hasVertexDeformation; }; 65 this.hasVertexDeformation = function () { return this._hasVertexDeformation; };
66 this._hasVertexDeformation = true; 66 this._hasVertexDeformation = true;
67 this._vertexDeformationTolerance = 0.02; // should be a property 67 this._vertexDeformationTolerance = 0.02; // should be a property
68 68
69 /////////////////////////////////////////////////////////////////////// 69 ///////////////////////////////////////////////////////////////////////
70 // Material Property Accessors 70 // Material Property Accessors
71 /////////////////////////////////////////////////////////////////////// 71 ///////////////////////////////////////////////////////////////////////
72 this._propNames = [ "u_limit1", "u_limit2", "u_twistAmount", "speed", "u_tex0", "u_tex1"]; 72 this._propNames = [ "u_limit1", "u_limit2", "u_twistAmount", "speed", "u_tex0", "u_tex1"];
73 this._propLabels = [ "Start Parameter", "End Paramater", "Twist Amount", "Speed", "Front facing texture map", "Back facing texture map"]; 73 this._propLabels = [ "Start Parameter", "End Paramater", "Twist Amount", "Speed", "Front facing texture map", "Back facing texture map"];
74 this._propTypes = [ "float", "float", "angle", "float", "file", "file"]; 74 this._propTypes = [ "float", "float", "angle", "float", "file", "file"];
75 this._propValues = []; 75 this._propValues = [];
76 76
77 // initialize the property values 77 // initialize the property values
78 this._propValues[this._propNames[0]] = 0.0; 78 this._propValues[this._propNames[0]] = 0.0;
79 this._propValues[this._propNames[1]] = 1.0; 79 this._propValues[this._propNames[1]] = 1.0;
80 this._propValues[this._propNames[2]] = 2.0 * Math.PI; 80 this._propValues[this._propNames[2]] = 2.0 * Math.PI;
81 this._propValues[this._propNames[3]] = this._speed; 81 this._propValues[this._propNames[3]] = this._speed;
82 this._propValues[this._propNames[4]] = this._tex0.slice(); 82 this._propValues[this._propNames[4]] = this._tex0.slice();
83 this._propValues[this._propNames[5]] = this._tex1.slice(); 83 this._propValues[this._propNames[5]] = this._tex1.slice();
84 /////////////////////////////////////////////////////////////////////// 84 ///////////////////////////////////////////////////////////////////////
85 85
86 /////////////////////////////////////////////////////////////////////// 86 ///////////////////////////////////////////////////////////////////////
87 // Methods 87 // Methods
88 /////////////////////////////////////////////////////////////////////// 88 ///////////////////////////////////////////////////////////////////////
89 89
90 this.init = function (world) 90 this.init = function (world)
91 { 91 {
92 this.setWorld(world); 92 this.setWorld(world);
93 93
94 // set up the shader 94 // set up the shader
95 this._shader = new RDGE.jshader(); 95 this._shader = new RDGE.jshader();
96 this._shader.def = twistVertShaderDef; 96 this._shader.def = twistVertShaderDef;
97 this._shader.init(); 97 this._shader.init();
98 98
99 // set up the material node 99 // set up the material node
100 this._materialNode = RDGE.createMaterialNode("twistVertMaterial" + "_" + world.generateUniqueNodeID()); 100 this._materialNode = RDGE.createMaterialNode("twistVertMaterial" + "_" + world.generateUniqueNodeID());
101 this._materialNode.setShader(this._shader); 101 this._materialNode.setShader(this._shader);
102 102
103 103
104 // initialize the twist vert properties 104 // initialize the twist vert properties
105 this.setShaderValues(); 105 this.setShaderValues();
106 }; 106 };
107 107
108 this.update = function (time) 108 this.resetToDefault = function()
109 { 109 {
110 if (this._shader && this._shader.twistMe) 110 this._propValues[this._propNames[0]] = 0.0;
111 { 111 this._propValues[this._propNames[1]] = 1.0;
112 var technique = this._shader.twistMe; 112 this._propValues[this._propNames[2]] = 2.0 * Math.PI;
113 113 this._propValues[this._propNames[3]] = this._speed;
114 var angle = this._angle; 114 this._propValues[this._propNames[4]] = this._tex0.slice();
115 angle += this._deltaTime * this._propValues["speed"]; 115 this._propValues[this._propNames[5]] = this._tex1.slice();
116 if (angle > this._propValues["u_twistAmount"]) 116
117 { 117 var nProps = this._propNames.length;
118 angle = this._propValues["u_twistAmount"]; 118 for (var i=0; i<nProps; i++)
119 this._deltaTime = -this._deltaTime; 119 this.setProperty( this._propNames[i], this._propValues[this._propNames[i]] );
120 } 120 };
121 else if (angle < 0.0) 121
122 { 122 this.update = function (time)
123 angle = 0; 123 {
124 this._deltaTime = -this._deltaTime; 124 if (this._shader && this._shader.twistMe)
125 } 125 {
126 this._angle = angle; 126 var technique = this._shader.twistMe;
127 this._shader.twistMe["u_twistAmount"].set([angle]); 127
128 128 var angle = this._angle;
129 var tex; 129 angle += this._deltaTime * this._propValues["speed"];
130 var glTex = this._glTextures["u_tex0"]; 130 if (angle > this._propValues["u_twistAmount"])
131 if (glTex) 131 {
132 { 132 angle = this._propValues["u_twistAmount"];
133 //if (glTex.isAnimated()) 133 this._deltaTime = -this._deltaTime;
134 glTex.render(); 134 }
135 tex = glTex.getTexture(); 135 else if (angle < 0.0)
136 if (tex) 136 {
137 technique.u_tex0.set( tex ); 137 angle = 0;
138 } 138 this._deltaTime = -this._deltaTime;
139 139 }
140 glTex = this._glTextures["u_tex1"]; 140 this._angle = angle;
141 if (glTex) 141 this._shader.twistMe["u_twistAmount"].set([angle]);
142 { 142
143 //if (glTex.isAnimated()) 143 var tex;
144 glTex.render(); 144 var glTex = this._glTextures["u_tex0"];
145 tex = glTex.getTexture(); 145 if (glTex)
146 if (tex) 146 {
147 technique.u_tex1.set( tex ); 147 //if (glTex.isAnimated())
148 } 148 glTex.render();
149 149 tex = glTex.getTexture();
150 } 150 if (tex)
151 } 151 technique.u_tex0.set( tex );
152 }
153
154 glTex = this._glTextures["u_tex1"];
155 if (glTex)
156 {
157 //if (glTex.isAnimated())
158 glTex.render();
159 tex = glTex.getTexture();
160 if (tex)
161 technique.u_tex1.set( tex );
162 }
163
164 }
165 }
152}; 166};
153 167
154// shader spec (can also be loaded from a .JSON file, or constructed at runtime) 168// shader spec (can also be loaded from a .JSON file, or constructed at runtime)
155twistVertShaderDef = 169twistVertShaderDef =
156{ 170{
157 'shaders': { // shader files 171 'shaders': { // shader files
158 'defaultVShader': "assets/shaders/TwistVert.vert.glsl", 172 'defaultVShader': "assets/shaders/TwistVert.vert.glsl",
159 'defaultFShader': "assets/shaders/TwistVert.frag.glsl" 173 'defaultFShader': "assets/shaders/TwistVert.frag.glsl"
160 }, 174 },
161