OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
generate.cs
Go to the documentation of this file.
1 #version 310 es
2 
3 /* Copyright (c) 2015-2017, ARM Limited and Contributors
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 precision highp float;
24 layout (local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
25 layout (binding = 0, r32f) writeonly highp uniform image3D outSurface;
26 
30 uniform float time;
31 
32 float snoise(vec3 v);
33 
34 float WeirdFloor(vec3 p)
35 {
36  float f = p.y;
37  if (abs(p.y) < 0.1)
38  {
39  vec3 offset = vec3(0.1, 0.0, 0.07) * time;
40  f += 0.4 * snoise(p * 1.7 + offset);
41  }
42  return f;
43 }
44 
45 float Sphere(vec3 p, float r2)
46 {
47  return dot(p, p) - r2;
48 }
49 
50 float Cloud(vec3 p, vec3 center)
51 {
52  vec3 q = p - center + vec3(0.03, 0.0, 0.02) * time;
53  q.x = mod(q.x + 1.0, 2.0) - 1.0;
54  q.z = mod(q.z + 1.5, 3.0) - 1.5;
55  q.y *= 4.0;
56  q.x *= 1.5;
57  q.z *= 1.5;
58  float f = Sphere(q, 0.3*0.3);
59  f -= 0.2 * snoise(q * 1.2 + center * 1024.0 - vec3(0.05, 0.03, 0.02) * time);
60  return f;
61 }
62 
63 float Scene(vec3 p)
64 {
65  float f = WeirdFloor(p);
66  if (p.y > 0.5)
67  {
68  f = min(f, Cloud(p, vec3(0.5, 0.7, -0.3)));
69  f = min(f, Cloud(p, vec3(-0.5, 0.7, 0.7)));
70  }
71  f = max(f, -Sphere(p - sphere_pos, sphere_radius));
72  return f;
73 }
74 
75 void main()
76 {
77  ivec3 texel = ivec3(gl_GlobalInvocationID.xyz);
78  ivec3 size = imageSize(outSurface);
79 
80  // Don't write outside texture
81  // (Needed for this texture, since we invoke more
82  // work units than the size of the texture).
83  if (texel.x >= size.x ||
84  texel.y >= size.y ||
85  texel.z >= size.z)
86  return;
87 
88  // Make sure that we sample the correct position in space here!
89  // Let's say our (1D) cell complex has a grid size of N=4, like so:
90  // | o | o | o | o |
91  // +---+---+---+---+---> x
92  // -1 -.5 0 .5 1
93  // The world space position of the centroids are -.75, -.25, .25, and .75.
94  // Each cell must sample its 2 adjacent edges. I.e. the first cell
95  // should sample the function at -1.0 and -0.5.
96  vec3 p = vec3(-1.0) + 2.0 * vec3(texel) / float(dimension - 1);
97  imageStore(outSurface, texel, vec4(Scene(p)));
98 }
99 
100 // Description : Array and textureless GLSL 2D/3D/4D simplex
101 // noise functions.
102 // Author : Ian McEwan, Ashima Arts.
103 // Maintainer : ijm
104 // Lastmod : 20110822 (ijm)
105 // License : Copyright (C) 2011 Ashima Arts. All rights reserved.
106 // Distributed under the MIT License. See LICENSE file.
107 // https://github.com/ashima/webgl-noise
108 //
109 
111  return x - floor(x * (1.0 / 289.0)) * 289.0;
112 }
113 
115  return x - floor(x * (1.0 / 289.0)) * 289.0;
116 }
117 
119  return mod289(((x*34.0)+1.0)*x);
120 }
121 
123 {
124  return 1.79284291400159 - 0.85373472095314 * r;
125 }
126 
127 float snoise(vec3 v)
128 {
129  const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
130  const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
131 
132  // First corner
133  vec3 i = floor(v + dot(v, C.yyy) );
134  vec3 x0 = v - i + dot(i, C.xxx) ;
135 
136  // Other corners
137  vec3 g = step(x0.yzx, x0.xyz);
138  vec3 l = 1.0 - g;
139  vec3 i1 = min( g.xyz, l.zxy );
140  vec3 i2 = max( g.xyz, l.zxy );
141 
142  // x0 = x0 - 0.0 + 0.0 * C.xxx;
143  // x1 = x0 - i1 + 1.0 * C.xxx;
144  // x2 = x0 - i2 + 2.0 * C.xxx;
145  // x3 = x0 - 1.0 + 3.0 * C.xxx;
146  vec3 x1 = x0 - i1 + C.xxx;
147  vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
148  vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
149 
150  // Permutations
151  i = mod289(i);
152  vec4 p = permute( permute( permute(
153  i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
154  + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
155  + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
156 
157  // Gradients: 7x7 points over a square, mapped onto an octahedron.
158  // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
159  float n_ = 0.142857142857; // 1.0/7.0
160  vec3 ns = n_ * D.wyz - D.xzx;
161 
162  vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
163 
164  vec4 x_ = floor(j * ns.z);
165  vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
166 
167  vec4 x = x_ *ns.x + ns.yyyy;
168  vec4 y = y_ *ns.x + ns.yyyy;
169  vec4 h = 1.0 - abs(x) - abs(y);
170 
171  vec4 b0 = vec4( x.xy, y.xy );
172  vec4 b1 = vec4( x.zw, y.zw );
173 
174  //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
175  //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
176  vec4 s0 = floor(b0)*2.0 + 1.0;
177  vec4 s1 = floor(b1)*2.0 + 1.0;
178  vec4 sh = -step(h, vec4(0.0));
179 
180  vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
181  vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
182 
183  vec3 p0 = vec3(a0.xy,h.x);
184  vec3 p1 = vec3(a0.zw,h.y);
185  vec3 p2 = vec3(a1.xy,h.z);
186  vec3 p3 = vec3(a1.zw,h.w);
187 
188  //Normalise gradients
189  vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
190  p0 *= norm.x;
191  p1 *= norm.y;
192  p2 *= norm.z;
193  p3 *= norm.w;
194 
195  // Mix final noise value
196  vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
197  m = m * m;
198  return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
199  dot(p2,x2), dot(p3,x3) ) );
200 }
const GLfloat * v
Definition: gl2ext.h:2231
uniform float sphere_radius
Definition: generate.cs:28
void main()
Definition: generate.cs:75
Definition: matrix.h:51
vec4 permute(vec4 x)
Definition: generate.cs:118
float Cloud(vec3 p, vec3 center)
Definition: generate.cs:50
GLboolean r
Definition: gl2ext.h:306
uniform vec3 sphere_pos
Definition: generate.cs:27
uniform int dimension
Definition: generate.cs:29
Definition: matrix.h:28
float min(float x, float y)
Definition: noise.cpp:34
GLenum GLuint GLintptr offset
Definition: gl2ext.h:629
float z
Definition: matrix.h:55
Definition: matrix.h:75
vec4 taylorInvSqrt(vec4 r)
Definition: generate.cs:122
vec3 mod289(vec3 x)
Definition: generate.cs:110
GLfloat GLfloat GLfloat w
Definition: gl2ext.h:2701
GLfloat GLfloat f
Definition: gl2ext.h:2707
GLboolean GLboolean g
Definition: gl2ext.h:306
float w
Definition: matrix.h:80
GLfloat GLfloat GLfloat GLfloat h
Definition: gl2ext.h:2701
float x
Definition: matrix.h:53
uniform float time
Definition: generate.cs:30
float snoise(vec3 v)
Definition: generate.cs:127
float WeirdFloor(vec3 p)
Definition: generate.cs:34
float y
Definition: matrix.h:54
float max(float x, float y)
Definition: noise.cpp:29
float Scene(vec3 p)
Definition: generate.cs:63
float Sphere(vec3 p, float r2)
Definition: generate.cs:45
GLenum GLuint GLintptr GLsizeiptr size
Definition: gl2ext.h:629
void uniform(string name, const mat4 &v)
Definition: glutil.cpp:97
precision highp float
Definition: generate.cs:23
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei imageSize
Definition: gl2ext.h:575
float z
Definition: matrix.h:79
const GLfloat * m
Definition: gl2ext.h:2508
float x
Definition: matrix.h:77
float y
Definition: matrix.h:78
GLint y
Definition: gl2ext.h:179
layout(local_size_x=4, local_size_y=4, local_size_z=4) in
precision highp image3D
Definition: centroid.cs:24