aboutsummaryrefslogtreecommitdiff
path: root/js/lib
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib')
-rwxr-xr-xjs/lib/drawing/world.js1
-rwxr-xr-xjs/lib/rdge/materials/bump-metal-material.js58
-rw-r--r--js/lib/rdge/texture.js285
3 files changed, 341 insertions, 3 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js
index 73273adc..d314760b 100755
--- a/js/lib/drawing/world.js
+++ b/js/lib/drawing/world.js
@@ -30,6 +30,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
30 30
31 this._canvas = canvas; 31 this._canvas = canvas;
32 if (this._useWebGL) { 32 if (this._useWebGL) {
33 preserveDrawingBuffer = true;
33 if(preserveDrawingBuffer) { 34 if(preserveDrawingBuffer) {
34 this._glContext = canvas.getContext("experimental-webgl", {preserveDrawingBuffer: true}); 35 this._glContext = canvas.getContext("experimental-webgl", {preserveDrawingBuffer: true});
35 } else { 36 } else {
diff --git a/js/lib/rdge/materials/bump-metal-material.js b/js/lib/rdge/materials/bump-metal-material.js
index 2ef83227..9c9a189a 100755
--- a/js/lib/rdge/materials/bump-metal-material.js
+++ b/js/lib/rdge/materials/bump-metal-material.js
@@ -6,6 +6,8 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
6 6
7var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; 7var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser;
8var Material = require("js/lib/rdge/materials/material").Material; 8var Material = require("js/lib/rdge/materials/material").Material;
9var Texture = require("js/lib/rdge/texture").Texture;
10
9/////////////////////////////////////////////////////////////////////// 11///////////////////////////////////////////////////////////////////////
10// Class GLMaterial 12// Class GLMaterial
11// RDGE representation of a material. 13// RDGE representation of a material.
@@ -18,8 +20,13 @@ var BumpMetalMaterial = function BumpMetalMaterial() {
18 this._shaderName = "bumpMetal"; 20 this._shaderName = "bumpMetal";
19 21
20 this._lightDiff = [0.3, 0.3, 0.3, 1.0]; 22 this._lightDiff = [0.3, 0.3, 0.3, 1.0];
21 this._diffuseTexture = "assets/images/metal.png"; 23
22 this._specularTexture = "assets/images/silver.png"; 24 //this._diffuseTexture = "assets/images/metal.png";
25 this._diffuseTexture = "texture";
26 this._diffuseWorld = null; // the world that the texture is derived from (if there is one).
27 this._diffuseTextureObj = null;
28
29 this._specularTexture = "assets/images/silver.png";
23 this._normalTexture = "assets/images/normalMap.png"; 30 this._normalTexture = "assets/images/normalMap.png";
24 31
25 /////////////////////////////////////////////////////////////////////// 32 ///////////////////////////////////////////////////////////////////////
@@ -119,12 +126,53 @@ var BumpMetalMaterial = function BumpMetalMaterial() {
119 this._materialNode = createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() ); 126 this._materialNode = createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() );
120 this._materialNode.setShader(this._shader); 127 this._materialNode.setShader(this._shader);
121 128
129 // DEBUG CODE
130 this.initWorldTextures();
131
122 // set some image maps 132 // set some image maps
123 this.updateTexture(1); 133 this.updateTexture(1);
124 this.updateTexture(2); 134 this.updateTexture(2);
125 this.updateTexture(3); 135 this.updateTexture(3);
126 }; 136 };
127 137
138 this.initWorldTextures = function()
139 {
140 // find the world with the given id
141 var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils;
142 var root = viewUtils.application.ninja.currentDocument.documentRoot;
143 this._diffuseWorld = this.findWorld( this._diffuseTexture, root );
144 if (this._diffuseWorld)
145 {
146 var world = this.getWorld();
147 var tex = new Texture( world );
148 this._diffuseTextureObj = tex;
149 tex.loadFromCanvas( world.getCanvas() );
150 }
151 }
152
153 this.findWorld = function( id, elt )
154 {
155 if (elt.id && elt.id === id)
156 {
157 if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
158 {
159 var world = elt.elementModel.shapeModel.GLWorld;
160 return world;
161 }
162 }
163
164 if (elt.children)
165 {
166 var nKids = elt.children.length;
167 for (var i=0; i<nKids; i++)
168 {
169 var child = elt.children[i];
170 var world = this.findWorld( id, child );
171 if (world) return world;
172 }
173 }
174 }
175
128 this.updateTexture = function( index ) 176 this.updateTexture = function( index )
129 { 177 {
130 var material = this._materialNode; 178 var material = this._materialNode;
@@ -136,7 +184,11 @@ var BumpMetalMaterial = function BumpMetalMaterial() {
136 { 184 {
137 var texMapName = this._propValues[this._propNames[index]]; 185 var texMapName = this._propValues[this._propNames[index]];
138 var wrap = 'REPEAT', mips = true; 186 var wrap = 'REPEAT', mips = true;
139 var tex = this.loadTexture( texMapName, wrap, mips ); 187 var tex;
188 if ((index === 1) && this._diffuseTextureObj)
189 tex = this._diffuseTextureObj.getTexture();
190 else
191 tex = this.loadTexture( texMapName, wrap, mips );
140 192
141 if (tex) 193 if (tex)
142 { 194 {
diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js
new file mode 100644
index 00000000..c72864b8
--- /dev/null
+++ b/js/lib/rdge/texture.js
@@ -0,0 +1,285 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7var Material = require("js/lib/rdge/materials/material").Material;
8
9///////////////////////////////////////////////////////////////////////
10// Class GLTexture
11// GL representation of a texture.
12///////////////////////////////////////////////////////////////////////
13function Texture( dstWorld )
14{
15 ///////////////////////////////////////////////////////////////////////
16 // Instance variables
17 ///////////////////////////////////////////////////////////////////////
18 this._texture;
19
20 // texture attributes
21 this._texMapName;
22 this._wrap;
23 this._mips;
24
25 this._srcCanvas; // the canvas generating the texture map.
26 this._dstWorld; // the world that will use the texture map
27 this._dstWorld = dstWorld;
28
29 ///////////////////////////////////////////////////////////////////////
30 // Property Accessors
31 ///////////////////////////////////////////////////////////////////////
32 this.getTexture = function() { return this._texture; }
33
34 this.setSrcWorld = function(w) { this._srcWorld = w; }
35 this.getSrcWorld = function() { return this._srcWorld; }
36
37 this.setDstWorld = function(w) { this._dstWorld = w; }
38 this.getDstWorld = function() { return this._dstWorld; }
39
40 ///////////////////////////////////////////////////////////////////////
41 // Methods
42 ///////////////////////////////////////////////////////////////////////
43
44 this.loadFromFile = function( texMapName, wrap, mips )
45 {
46 var tex = this._texture;
47 this._srcCanvas = null;
48
49 // only load if something has changed
50 if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips?
51 {
52 this._texMapName = texMapName.slice();
53 this._wrap = wrap;
54 this._mips = mips;
55
56 var dstWorld = this.getDstWorld();
57 if (dstWorld)
58 {
59 var renderer = dstWorld.getRenderer();
60 tex = renderer.getTextureByName(texMapName, wrap, mips );
61 this._texture = tex;
62 dstWorld.textureToLoad( tex );
63 }
64 }
65
66 return tex;
67 }
68
69 var __texCounter = 0;
70
71 /*
72 this.loadFromCanvas = function( srcCanvas, wrap, mips )
73 {
74 this._texMapName = "GLTexture_" + __texCounter;
75 __texCounter++;
76
77 //if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
78 var world = this.getDstWorld();
79 var renderer = world.getRenderer();
80
81 // set default values for wrap and mips
82 if (wrap === undefined)
83 wrap = "REPEAT";
84 if (mips === undefined)
85 mips = true;
86
87 var imageData;
88 var width = srcCanvas.width, height = srcCanvas.height;
89 width = 64; height = 64; // some even power of 2 for now...
90
91 // create a canvas to be used as the image for the texture map
92 var doc = srcCanvas.ownerDocument;
93 var dstCanvas = doc.createElement("canvas");
94 dstCanvas.width = width;
95 dstCanvas.height = height;
96 var dstCtx = dstCanvas.getContext("2d");
97
98 var tex;
99 var srcCtx = srcCanvas.getContext("2d");
100 if (srcCtx)
101 {
102 tex = renderer.getTextureByName(this._texMapName, wrap, mips );
103 imageData = srcCtx.getImageData( 0, 0, width, height );
104 dstCtx.putImageData( imageData, 0, 0 );
105 }
106 else
107 {