summaryrefslogtreecommitdiffstats
path: root/src/com/android/noisefield/NoiseFieldRS.java
blob: 3962e0a5e08c1ad23bfc72216e0a7fc86b0ffc3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package com.android.noisefield;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.renderscript.Allocation;
import android.renderscript.Float3;
import android.renderscript.Float4;
import android.renderscript.Matrix4f;
import android.renderscript.Mesh;
import android.renderscript.Program;
import android.renderscript.ProgramFragment;
import android.renderscript.ProgramFragmentFixedFunction;
import android.renderscript.ProgramRaster;
import android.renderscript.ProgramStore;
import android.renderscript.ProgramVertex;
import android.renderscript.ProgramVertexFixedFunction;
import android.renderscript.RenderScriptGL;
import android.renderscript.Sampler;
import android.renderscript.Mesh.Primitive;
import android.renderscript.ProgramStore.BlendDstFunc;
import android.renderscript.ProgramStore.BlendSrcFunc;
import android.util.Log;

public class NoiseFieldRS {

    private Resources mRes;
    private RenderScriptGL mRS;
    private ScriptC_noisefield mScript;
    int mHeight;
    int mWidth;
    private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();

    private ScriptField_VpConsts mPvConsts;
    private Allocation mDotAllocation;
    private ScriptField_VertexColor_s mVertexColors;
    private ScriptField_Particle mDotParticles;
    private Mesh mDotMesh;
    private int mDensityDPI;

    public void init(int dpi, RenderScriptGL rs,
                     Resources res, int width, int height) {
        mDensityDPI = dpi;

        mRS = rs;
        mRes = res;

        mWidth = width;
        mHeight = height;

        mOptionsARGB.inScaled = false;
        mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888;

        Mesh.AllocationBuilder smb2 = new Mesh.AllocationBuilder(mRS);

        mDotParticles = new ScriptField_Particle(mRS, 150);
        smb2.addVertexAllocation(mDotParticles.getAllocation());

        smb2.addIndexSetType(Mesh.Primitive.POINT);
        mScript = new ScriptC_noisefield(mRS, mRes, R.raw.noisefield);


        mDotMesh = smb2.create();
        mScript.set_dotMesh(mDotMesh);
        mScript.bind_dotParticles(mDotParticles);

        mPvConsts = new ScriptField_VpConsts(mRS, 1);

        createProgramVertex();
        createProgramRaster();
        createProgramFragmentStore();
        createProgramFragment();
        createBackgroundMesh();
        loadTextures();

        mScript.set_densityDPI(mDensityDPI);
        mScript.invoke_positionParticles();
    }

    private Matrix4f getProjectionNormalized(int w, int h) {
        // range -1,1 in the narrow axis at z = 0.
        Matrix4f m1 = new Matrix4f();
        Matrix4f m2 = new Matrix4f();

        if (w > h) {
            float aspect = ((float) w) / h;
            m1.loadFrustum(-aspect, aspect, -1, 1, 1, 100);
        } else {
            float aspect = ((float) h) / w;
            m1.loadFrustum(-1, 1, -aspect, aspect, 1, 100);
        }

        m2.loadRotate(180, 0, 1, 0);
        m1.loadMultiply(m1, m2);

        m2.loadScale(-1, 1, 1);
        m1.loadMultiply(m1, m2);

        m2.loadTranslate(0, 0, 1);
        m1.loadMultiply(m1, m2);
        return m1;
    }

    private void updateProjectionMatrices() {
        Matrix4f projNorm = getProjectionNormalized(mWidth, mHeight);
        ScriptField_VpConsts.Item i = new ScriptField_VpConsts.Item();
        i.MVP = projNorm;
        i.scaleSize = mDensityDPI/240.0f;
        mPvConsts.set(i, 0, true);
    }

    private void createBackgroundMesh() {
        // The composition and colors of the background mesh were plotted on paper and photoshop
        // first then translated to the code you see below. Points and colors are not random.

        mVertexColors = new ScriptField_VertexColor_s(mRS, 48);
        Float3 a = new Float3(-1.5f, 1.0f, 0.0f);
        Float3 b = new Float3(0.0f, 1.0f, 0.0f);
        Float3 c = new Float3(1.5f, 1.0f, 0.0f);
        Float3 d = new Float3(-1.05f, 0.3f, 0.0f);
        Float3 e = new Float3(-0.6f, 0.4f, 0.0f);
        Float3 f = new Float3(0.3f, 0.4f, 0.0f);
        Float3 g = new Float3(0.0f, 0.2f, 0.0f);
        Float3 h = new Float3(-0.6f, 0.1f, 0.0f);
        Float3 i = new Float3(-1.5f, -0.2f, 0.0f);
        Float3 j = new Float3(-0.45f, -0.3f, 0.0f);
        Float3 k = new Float3(-1.5f, -1.0f, 0.0f);
        Float3 l = new Float3(1.5f, -1.0f, 0.0f);
        mVertexColors.set_position(0, a, false);
        mVertexColors.set_color(0, (new Float4(0.08f,0.335f,0.406f, 1.0f)), false);
        mVertexColors.set_position(1, i, false);
        mVertexColors.set_color(1, (new Float4(0.137f,0.176f,0.225f, 1.0f)), false);
        mVertexColors.set_position(2, d, false);
        mVertexColors.set_color(2, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(3, a, false);
        mVertexColors.set_color(3, (new Float4(0.08f,0.335f,0.406f, 1.0f)), false);
        mVertexColors.set_position(4, d, false);
        mVertexColors.set_color(4, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(5, e, false);
        mVertexColors.set_color(5, (new Float4(0.0f,0.184f,0.233f, 1.0f)), false);
        mVertexColors.set_position(6, a, false);
        mVertexColors.set_color(6, (new Float4(0.08f,0.335f,0.406f, 1.0f)), false);
        mVertexColors.set_position(7, e, false);
        mVertexColors.set_color(7, (new Float4(0.0f,0.184f,0.233f, 1.0f)), false);
        mVertexColors.set_position(8, b, false);
        mVertexColors.set_color(8, (new Float4(0.133f,0.404f,0.478f, 1.0f)), false);
        mVertexColors.set_position(9, b, false);
        mVertexColors.set_color(9, (new Float4(0.133f,0.404f,0.478f, 1.0f)), false);
        mVertexColors.set_position(10, e, false);
        mVertexColors.set_color(10, (new Float4(0.0f,0.184f,0.233f, 1.0f)), false);
        mVertexColors.set_position(11, f, false);
        mVertexColors.set_color(11, (new Float4(0.0f,0.124f,0.178f, 1.0f)), false);
        mVertexColors.set_position(12, b, false);
        mVertexColors.set_color(12, (new Float4(0.133f,0.404f,0.478f, 1.0f)), false);
        mVertexColors.set_position(13, f, false);
        mVertexColors.set_color(13, (new Float4(0.0f,0.124f,0.178f, 1.0f)), false);
        mVertexColors.set_position(14, c, false);
        mVertexColors.set_color(14, (new Float4(0.002f,0.173f,0.231f, 1.0f)), false);
        mVertexColors.set_position(15, c, false);
        mVertexColors.set_color(15, (new Float4(0.002f,0.173f,0.231f, 1.0f)), false);
        mVertexColors.set_position(16, f, false);
        mVertexColors.set_color(16, (new Float4(0.0f,0.124f,0.178f, 1.0f)), false);
        mVertexColors.set_position(17, l, false);
        mVertexColors.set_color(17, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(18, f, false);
        mVertexColors.set_color(18, (new Float4(0.0f,0.124f,0.178f, 1.0f)), false);
        mVertexColors.set_position(19, e, false);
        mVertexColors.set_color(19, (new Float4(0.0f,0.184f,0.233f, 1.0f)), false);
        mVertexColors.set_position(20, g, false);
        mVertexColors.set_color(20, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(21, f, false);
        mVertexColors.set_color(21, (new Float4(0.0f,0.124f,0.178f, 1.0f)), false);
        mVertexColors.set_position(22, g, false);
        mVertexColors.set_color(22, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(23, l, false);
        mVertexColors.set_color(23, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(24, g, false);
        mVertexColors.set_color(24, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(25, e, false);
        mVertexColors.set_color(25, (new Float4(0.0f,0.184f,0.233f, 1.0f)), false);
        mVertexColors.set_position(26, h, false);
        mVertexColors.set_color(26, (new Float4(0.002f,0.196f,0.233f, 1.0f)), false);
        mVertexColors.set_position(27, h, false);
        mVertexColors.set_color(27, (new Float4(0.002f,0.196f,0.233f, 1.0f)), false);
        mVertexColors.set_position(28, e, false);
        mVertexColors.set_color(28, (new Float4(0.0f,0.184f,0.233f, 1.0f)), false);
        mVertexColors.set_position(29, d, false);
        mVertexColors.set_color(29, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(30, d, false);
        mVertexColors.set_color(30, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(31, i, false);
        mVertexColors.set_color(31, (new Float4(0.137f,0.176f,0.225f, 1.0f)), false);
        mVertexColors.set_position(32, h, false);
        mVertexColors.set_color(32, (new Float4(0.002f,0.196f,0.233f, 1.0f)), false);
        mVertexColors.set_position(33, j, false);
        mVertexColors.set_color(33, (new Float4(0.002f,0.059f,0.09f, 1.0f)), false);
        mVertexColors.set_position(34, h, false);
        mVertexColors.set_color(34, (new Float4(0.002f,0.196f,0.233f, 1.0f)), false);
        mVertexColors.set_position(35, i, false);
        mVertexColors.set_color(35, (new Float4(0.137f,0.176f,0.225f, 1.0f)), false);
        mVertexColors.set_position(36, j, false);
        mVertexColors.set_color(36, (new Float4(0.002f,0.059f,0.09f, 1.0f)), false);
        mVertexColors.set_position(37, i, false);
        mVertexColors.set_color(37, (new Float4(0.137f,0.176f,0.225f, 1.0f)), false);
        mVertexColors.set_position(38, k, false);
        mVertexColors.set_color(38, (new Float4(0.204f,0.212f,0.218f, 1.0f)), false);
        mVertexColors.set_position(39, l, false);
        mVertexColors.set_color(39, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(40, j, false);
        mVertexColors.set_color(40, (new Float4(0.002f,0.059f,0.09f, 1.0f)), false);
        mVertexColors.set_position(41, k, false);
        mVertexColors.set_color(41, (new Float4(0.204f,0.212f,0.218f, 1.0f)), false);
        mVertexColors.set_position(42, g, false);
        mVertexColors.set_color(42, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(43, h, false);
        mVertexColors.set_color(43, (new Float4(0.002f,0.196f,0.233f, 1.0f)), false);
        mVertexColors.set_position(44, j, false);
        mVertexColors.set_color(44, (new Float4(0.002f,0.059f,0.09f, 1.0f)), false);
        mVertexColors.set_position(45, l, false);
        mVertexColors.set_color(45, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(46, g, false);
        mVertexColors.set_color(46, (new Float4(0.0f,0.088f,0.135f, 1.0f)), false);
        mVertexColors.set_position(47, j, false);
        mVertexColors.set_color(47, (new Float4(0.002f,0.059f,0.09f, 1.0f)), false);

        mVertexColors.copyAll();

        Mesh.AllocationBuilder backgroundBuilder = new Mesh.AllocationBuilder(mRS);
        backgroundBuilder.addIndexSetType(Primitive.TRIANGLE);
        backgroundBuilder.addVertexAllocation(mVertexColors.getAllocation());
        mScript.set_gBackgroundMesh(backgroundBuilder.create());

        mScript.bind_vertexColors(mVertexColors);
    }

    private Allocation loadTexture(int id) {
        final Allocation allocation = Allocation.createFromBitmapResource(mRS, mRes, id);
        return allocation;
    }

    private void loadTextures() {
        mDotAllocation = loadTexture(R.drawable.dot);
        mScript.set_textureDot(mDotAllocation);
    }

    private void createProgramVertex() {
        ProgramVertex.Builder backgroundBuilder = new ProgramVertex.Builder(mRS);
        backgroundBuilder.setShader(mRes, R.raw.bg_vs);
        backgroundBuilder.addInput(ScriptField_VertexColor_s.createElement(mRS));
        ProgramVertex programVertexBackground = backgroundBuilder.create();
        mScript.set_vertBg(programVertexBackground);


        updateProjectionMatrices();

        ProgramVertex.Builder builder = new ProgramVertex.Builder(mRS);
        builder = new ProgramVertex.Builder(mRS);
        builder.setShader(mRes, R.raw.noisefield_vs);
        builder.addConstant(mPvConsts.getType());
        builder.addInput(mDotMesh.getVertexAllocation(0).getType().getElement());

        ProgramVertex pvs = builder.create();
        pvs.bindConstants(mPvConsts.getAllocation(), 0);
        mRS.bindProgramVertex(pvs);
        mScript.set_vertDots(pvs);
    }

    private void createProgramFragment() {
        ProgramFragment.Builder backgroundBuilder = new ProgramFragment.Builder(mRS);
        backgroundBuilder.setShader(mRes, R.raw.bg_fs);
        ProgramFragment programFragmentBackground = backgroundBuilder.create();
        mScript.set_fragBg(programFragmentBackground);

        ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS);
        builder.setShader(mRes, R.raw.noisefield_fs);
        builder.addTexture(Program.TextureType.TEXTURE_2D);
        ProgramFragment pf = builder.create();
        pf.bindSampler(Sampler.CLAMP_LINEAR(mRS), 0);
        mScript.set_fragDots(pf);
    }

    private void createProgramRaster() {
        ProgramRaster.Builder builder = new ProgramRaster.Builder(mRS);
        builder.setPointSpriteEnabled(true);
        ProgramRaster pr = builder.create();
        mRS.bindProgramRaster(pr);
    }

    private void createProgramFragmentStore() {
        ProgramStore.Builder builder = new ProgramStore.Builder(mRS);
        builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE );
        mRS.bindProgramStore(builder.create());
    }

    public void start() {
        mRS.bindRootScript(mScript);
    }

    public void stop() {
        mRS.bindRootScript(null);

    }

    public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) {
        mScript.set_xOffset(xOffset - 0.5f);
    }

    public void resize(int w, int h) {

    }

}