~holger-seelig/cobweb.js/trunk

« back to all changes in this revision

Viewing changes to src/cobweb/Components/Texturing/PixelTexture.js

  • Committer: Holger Seelig
  • Date: 2017-08-22 04:53:24 UTC
  • Revision ID: holger.seelig@yahoo.de-20170822045324-4of4xxgt79669gbt
Switched to npm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
 
2
 *******************************************************************************
 
3
 *
 
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
5
 *
 
6
 * Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
 
7
 *
 
8
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
 
9
 *
 
10
 * The copyright notice above does not evidence any actual of intended
 
11
 * publication of such source code, and is an unpublished work by create3000.
 
12
 * This material contains CONFIDENTIAL INFORMATION that is the property of
 
13
 * create3000.
 
14
 *
 
15
 * No permission is granted to copy, distribute, or create derivative works from
 
16
 * the contents of this software, in whole or in part, without the prior written
 
17
 * permission of create3000.
 
18
 *
 
19
 * NON-MILITARY USE ONLY
 
20
 *
 
21
 * All create3000 software are effectively free software with a non-military use
 
22
 * restriction. It is free. Well commented source is provided. You may reuse the
 
23
 * source in any way you please with the exception anything that uses it must be
 
24
 * marked to indicate is contains 'non-military use only' components.
 
25
 *
 
26
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
27
 *
 
28
 * Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
 
29
 *
 
30
 * This file is part of the Cobweb Project.
 
31
 *
 
32
 * Cobweb is free software: you can redistribute it and/or modify it under the
 
33
 * terms of the GNU General Public License version 3 only, as published by the
 
34
 * Free Software Foundation.
 
35
 *
 
36
 * Cobweb is distributed in the hope that it will be useful, but WITHOUT ANY
 
37
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 
38
 * A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
 
39
 * details (a copy is included in the LICENSE file that accompanied this code).
 
40
 *
 
41
 * You should have received a copy of the GNU General Public License version 3
 
42
 * along with Cobweb.  If not, see <http://www.gnu.org/licenses/gpl.html> for a
 
43
 * copy of the GPLv3 License.
 
44
 *
 
45
 * For Silvio, Joy and Adi.
 
46
 *
 
47
 ******************************************************************************/
 
48
 
 
49
 
 
50
define ([
 
51
        "jquery",
 
52
        "cobweb/Fields",
 
53
        "cobweb/Basic/X3DFieldDefinition",
 
54
        "cobweb/Basic/FieldDefinitionArray",
 
55
        "cobweb/Components/Texturing/X3DTexture2DNode",
 
56
        "cobweb/Bits/X3DConstants",
 
57
        "standard/Math/Algorithm",
 
58
        "standard/Math/Numbers/Vector3",
 
59
        "standard/Math/Numbers/Matrix3",
 
60
],
 
61
function ($,
 
62
          Fields,
 
63
          X3DFieldDefinition,
 
64
          FieldDefinitionArray,
 
65
          X3DTexture2DNode, 
 
66
          X3DConstants,
 
67
          Algorithm)
 
68
{
 
69
"use strict";
 
70
 
 
71
        function PixelTexture (executionContext)
 
72
        {
 
73
                X3DTexture2DNode .call (this, executionContext);
 
74
 
 
75
                this .addType (X3DConstants .PixelTexture);
 
76
 
 
77
                this .addChildObjects ("loadState", new Fields .SFInt32 (X3DConstants .NOT_STARTED_STATE));
 
78
        }
 
79
 
 
80
        PixelTexture .prototype = $.extend (Object .create (X3DTexture2DNode .prototype),
 
81
        {
 
82
                constructor: PixelTexture,
 
83
                fieldDefinitions: new FieldDefinitionArray ([
 
84
                        new X3DFieldDefinition (X3DConstants .inputOutput,    "metadata",          new Fields .SFNode ()),
 
85
                        new X3DFieldDefinition (X3DConstants .inputOutput,    "image",             new Fields .SFImage (0, 0, 0, [ ])),
 
86
                        new X3DFieldDefinition (X3DConstants .initializeOnly, "repeatS",           new Fields .SFBool (true)),
 
87
                        new X3DFieldDefinition (X3DConstants .initializeOnly, "repeatT",           new Fields .SFBool (true)),
 
88
                        new X3DFieldDefinition (X3DConstants .initializeOnly, "textureProperties", new Fields .SFNode ()),
 
89
                ]),
 
90
                getTypeName: function ()
 
91
                {
 
92
                        return "PixelTexture";
 
93
                },
 
94
                getComponentName: function ()
 
95
                {
 
96
                        return "Texturing";
 
97
                },
 
98
                getContainerField: function ()
 
99
                {
 
100
                        return "texture";
 
101
                },
 
102
                initialize: function ()
 
103
                {
 
104
                        X3DTexture2DNode .prototype .initialize .call (this);
 
105
 
 
106
                        this .image_ .addInterest ("set_image__", this);
 
107
 
 
108
                        this .set_image__ ();
 
109
                },
 
110
                checkLoadState: function ()
 
111
                {
 
112
                        return this .loadState_ .getValue ();
 
113
                },
 
114
                convert: function (data, comp, array)
 
115
                {
 
116
                        switch (comp)
 
117
                        {
 
118
                                case 1:
 
119
                                {
 
120
                                        for (var i = 0, index = 0, length = array .length; i < length; ++ i, index += 4)
 
121
                                        {
 
122
                                                var pixel = array [i] .getValue ();
 
123
 
 
124
                                                data [index] =
 
125
                                                data [index + 1] =
 
126
                                                data [index + 2] = pixel & 255;
 
127
                                                data [index + 3] = 255;
 
128
                                        }
 
129
 
 
130
                                        break;
 
131
                                }
 
132
                                case 2:
 
133
                                {
 
134
                                        for (var i = 0, index = 0, length = array .length; i < length; ++ i, index += 4)
 
135
                                        {
 
136
                                                var pixel = array [i] .getValue ();
 
137
 
 
138
                                                data [index] =
 
139
                                                data [index + 1] =
 
140
                                                data [index + 2] = (pixel >>> 8) & 255;
 
141
                                                data [index + 3] = pixel & 255;
 
142
                                        }
 
143
 
 
144
                                        break;
 
145
                                }
 
146
                                case 3:
 
147
                                {
 
148
                                        for (var i = 0, index = 0, length = array .length; i < length; ++ i, index += 4)
 
149
                                        {
 
150
                                                var pixel = array [i] .getValue ();
 
151
 
 
152
                                                data [index]     = (pixel >>> 16) & 255;
 
153
                                                data [index + 1] = (pixel >>>  8) & 255;
 
154
                                                data [index + 2] = pixel & 255;
 
155
                                                data [index + 3] = 255;
 
156
                                        }
 
157
 
 
158
                                        break;
 
159
                                }
 
160
                                case 4:
 
161
                                {
 
162
                                        for (var i = 0, index = 0, length = array .length; i < length; ++ i, index += 4)
 
163
                                        {
 
164
                                                var pixel = array [i] .getValue ();
 
165
 
 
166
                                                data [index]     = (pixel >>> 24);
 
167
                                                data [index + 1] = (pixel >>> 16) & 255;
 
168
                                                data [index + 2] = (pixel >>>  8) & 255;
 
169
                                                data [index + 3] = pixel & 255;
 
170
                                        }
 
171
 
 
172
                                        break;
 
173
                                }
 
174
                        }
 
175
                },
 
176
                set_image__: function ()
 
177
                {
 
178
                        var
 
179
                                width       = this .image_ .width,
 
180
                                height      = this .image_ .height,
 
181
                                comp        = this .image_ .comp,
 
182
                                array       = this .image_ .array .getValue (),
 
183
                                transparent = ! (comp % 2),
 
184
                                data        = null;
 
185
                
 
186
                        if (width > 0 && height > 0 && comp > 0 && comp < 5)
 
187
                        {
 
188
                                if (Algorithm .isPowerOfTwo (width) && Algorithm .isPowerOfTwo (height))
 
189
                                {
 
190
                                        data = new Uint8Array (width * height * 4);
 
191
 
 
192
                                        this .convert (data, comp, array);
 
193
                                }
 
194
                                else if (Math .max (width, height) < this .getBrowser () .getMinTextureSize () && ! this .textureProperties_ .getValue ())
 
195
                                {
 
196
                                        data = new Uint8Array (width * height * 4);
 
197
 
 
198
                                        this .convert (data, comp, array);
 
199
 
 
200
                                        var
 
201
                                                inputWidth  = width,
 
202
                                                inputHeight = height;
 
203
 
 
204
                                        width  = Algorithm .nextPowerOfTwo (inputWidth)  * 4;
 
205
                                        height = Algorithm .nextPowerOfTwo (inputHeight) * 4;
 
206
 
 
207
                                        data = this .resize (data, inputWidth, inputHeight, width, height);
 
208
                                }
 
209
                                else
 
210
                                {
 
211
                                        var
 
212
                                                canvas1   = $("<canvas></canvas>") [0],
 
213
                                                canvas2   = $("<canvas></canvas>") [0],
 
214
                                                cx1       = canvas1 .getContext("2d"),
 
215
                                                cx2       = canvas2 .getContext("2d"),
 
216
                                                imageData = cx1 .createImageData (width, height);
 
217
 
 
218
                                        canvas1 .width  = width;
 
219
                                        canvas1 .height = height;
 
220
 
 
221
                                        this .convert (imageData .data, comp, array);
 
222
                                        cx1 .putImageData (imageData, 0, 0);
 
223
 
 
224
                                        width  = Algorithm .nextPowerOfTwo (width);
 
225
                                        height = Algorithm .nextPowerOfTwo (height);
 
226
 
 
227
                                        canvas2 .width  = width;
 
228
                                        canvas2 .height = height;
 
229
                                        
 
230
                                        cx2 .drawImage (canvas1, 0, 0, canvas1 .width, canvas1 .height, 0, 0, width, height);
 
231
        
 
232
                                        data = cx2 .getImageData (0, 0, width, height) .data;
 
233
                                }
 
234
 
 
235
                                this .setTexture (width, height, transparent, new Uint8Array (data), false);
 
236
                                this .loadState_ = X3DConstants .COMPLETE_STATE;
 
237
                        }
 
238
                        else
 
239
                        {
 
240
                                this .clear ();
 
241
                                this .loadState_ = X3DConstants .FAILED_STATE;
 
242
                        }
 
243
                },
 
244
        });
 
245
 
 
246
        return PixelTexture;
 
247
});
 
248
 
 
249