Changing Sin frequency smoothly in shader
In my vertex shader I am using a sin function to offset vertices. But as I change the frequency of my sin function I notice some "flickering". I guess that this comes from the way that the phase is not synchronised anymore.
float s = sin(frequency * _Time);
Is there a way to avoid those flickering effect while changing the frequency ?
See also questions close to this topic

In C++, how do I make a program that can take as an input and use a mathematical expression in runtime?
In C++ it is quite easy to define mathematical expressions preruntime in order to make some calculations. We can define an integer variable, and we can tell the computer to take that integer, and make some mathematical operations on it according to the mathematical expression we write in the program.
For example, if I want to make a program that calculates the square of a userinputted number, I can write:
double x; cin >> x; cout << x*x;
But the problem here is that it is I who defined what to do with that inputted number, and I chose to take the square of that number. What I want to be able to do is to allow THE USER to define his own expression with his own desired variable(s) and make the program calculate the value of that expression at the specified value(s) of the variable(s).
I want to be able to tell my program (in runtime) what expression to use. For example, I will input this:
x*x
And then the program will somehow take that string that I give to it, and turn in into a mathematical expression, and then tell me the value of that expression at the x value that I specify.
Perhaps the short version of the question is: How do I convert a string to a mathematical operation?
Apologies if you didn't like the way I asked my question.

I cannot find general formula for the least perimeter when specific number of tiles are given
Through drawing all the possible cases, I got that
a. For a square number N^2, the least perimeter is 4*N.
b. For N^21, the least perimeter isn't greater than 4*N.
But this does not satisfy for example 8 number of tiles. So it seems to work as long as we keep the shape convex.
At last I can only say that for K tiles, where K is less than or equal to N^2, the least perimeter isn't greater than 4*N.

How to set initial position of draggable points in JSXGraph in an equation graph?
I'm developing a web software that plots an interactive polynomial equation with 2 variables on JSXGraph, I need that every point where the derivative of the equation is 0 to have a draggable point, and that when I drag one point, only the interval between 1 point after and 1 before the dragged point of the curve goes together does anyone knows how I could do that?

Flickering particles when using THREE.js and GPGPU
I have a particles project built in
THREE.js
andGPGPU
.When you type words the stars formulate the words. It works pretty well, but I have two small issues with it.
 When you type words and / or delete letters, the stars moving around flicker in and out of view.
 When you type out a word, there is a small grid on empty stars which make up the words.
I am not much of a
WebGL
expert, but I have a feeling this issue is to do with theGLSL
shaders.Can anyone help with this? I have tried fixing these issues for a while but not got anywhere. thank you.

How to create a screen rectangle from scratch in teh geometry shader?
Is it possible to generate the 4 vertices needed to create a full screen rectangle directly in the geometry shader?
So assuming that there are no input vertices, can you do something like?
for(int i=0; i<4; i++) { gl_position = corner; }

How to handle incorrect index calculation for discretized ray tracing?
The situation si as follows. I am trying to implement a linear voxel search in a glsl shader for efficient voxel ray tracing. In toehr words, I have a 3D texture and I am ray tracing on it but I am trying to ray trace such that I only ever check voxels intersected by the ray once.
To this effect I have written a program with the following results:
Not efficient but correct:
The above image was obtained by adding a small epsilon ray multiple times and sampling from the texture on each iteration. WHich produces the correct results but it's very inefficient.
That would look like:
loop{ start += direction*0.01; sample(start); }
To make it efficient I decided to instead implement the following lookup function:
float bound(float val) { if(val >= 0) return voxel_size; return 0; } float planeIntersection(vec3 ray, vec3 origin, vec3 n, vec3 q) { n = normalize(n); if(dot(ray,n)!=0) return (dot(q,n)dot(n,origin))/dot(ray,n); return 1; } vec3 get_voxel(vec3 start, vec3 direction) { direction = normalize(direction); vec3 discretized_pos = ivec3((start*1.f/(voxel_size))) * voxel_size; vec3 n_x = vec3(sign(direction.x), 0,0); vec3 n_y = vec3(0, sign(direction.y),0); vec3 n_z = vec3(0, 0,sign(direction.z)); float bound_x, bound_y, bound_z; bound_x = bound(direction.x); bound_y = bound(direction.y); bound_z = bound(direction.z); float t_x, t_y, t_z; t_x = planeIntersection(direction, start, n_x, discretized_pos+vec3(bound_x,0,0)); t_y = planeIntersection(direction, start, n_y, discretized_pos+vec3(0,bound_y,0)); t_z = planeIntersection(direction, start, n_z, discretized_pos+vec3(0,0,bound_z)); if(t_x < 0) t_x = 1.f/0.f; if(t_y < 0) t_y = 1.f/0.f; if(t_z < 0) t_z = 1.f/0.f; float t = min(t_x, t_y); t = min(t, t_z); return start + direction*t; }
Which produces the following result:
Notice the triangle aliasing on the left side of some surfaces.
It seems this aliasing occurs because some coordinates are not being set to their correct voxel.
For example modifying the truncation part as follows:
vec3 discretized_pos = ivec3((start*1.f/(voxel_size))  vec3(0.1)) * voxel_size;
Creates:
So it has fixed the issue for some surfaces and caused it for others.
I wanted to know if there is a way in which I can correct tis truncation so that this error does not happen.

Need help for adding border on dotted line shader
I've got a script in Unity for dotted line shader like in that example
The problem is I want to make a black border around each circle and I'm new in using shaders. Anyone can help me or teach me? Here is the script, in case anyone needs it:
Shader "Unlit/DottedLineShader" { Properties { _RepeatCount("Repeat Count", float) = 5 _Spacing("Spacing", float) = 0.5 _Offset("Offset", float) = 0 } SubShader { Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" } LOD 100 Blend SrcAlpha OneMinusSrcAlpha ZWrite Off Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" float _RepeatCount; float _Spacing; float _Offset; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; fixed4 color : COLOR0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; fixed4 color : COLOR0; }; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; o.uv.x = (o.uv.x + _Offset) * _RepeatCount * (1.0f + _Spacing); o.color = v.color; return o; } fixed4 frag(v2f i) : SV_Target { i.uv.x = fmod(i.uv.x, 1.0f + _Spacing); float r = length(i.uv  float2(1.0f + _Spacing, 1.0f) * 0.5f) * 2.0f; fixed4 color = i.color; color.a *= saturate((0.99f  r) * 100.0f); return color; } ENDCG } } }

implementing pixiv4 filters in phaser CE
hej,
I'm trying to add the zoomblurfilter of pixi (https://github.com/pixijs/pixifilters/tree/master/filters/zoomblur) in phaser CE. It works to some extend (no crash), but renders black.
Here's the filter: https://gist.github.com/anonymous/70eed573ebd25206b3403cb428838b89
and how i apply it:
f = new Phaser.Filter.ZoomBlur(game); f.strength = 0.1; f.innerRadius = 80; f.radius = 1; f.center = [500,300]; game.world.filterArea = new Phaser.Rectangle(); game.world.filters = [f];
Any clues how to get it working?
PS. here's the intended result: http://pixijs.io/pixifilters/tools/demo/ (enable ZoomBlurFilter at the bottom)

Iterating surf in shader
Shader "Custom/Mask" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} [Space(10)] _Transparency ("Transparency", Range(0, 1)) = 0 _Alpha ("Alpha", Range(0, 1)) = 0 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Standard fullforwardshadows alpha:fade #pragma target 3.0 sampler2D _MainTex; fixed3 _worldPos; struct Input { float2 uv_MainTex; float3 worldPos; }; fixed4 _Color; half _Transparency; half _Alpha; uniform float4 FILTERmask_Position, FILTERmask_Position2, FILTERmask_Position3, FILTERmask_Position4, FILTERmask_Position5, FILTERmask_Position6, FILTERmask_Position7, FILTERmask_Position8; uniform half FILTERmask_Radius, FILTERmask_Radius2, FILTERmask_Radius3, FILTERmask_Radius4, FILTERmask_Radius5, FILTERmask_Radius6, FILTERmask_Radius7, FILTERmask_Radius8; uniform half FILTERmask_Softness, FILTERmask_Softness2, FILTERmask_Softness3, FILTERmask_Softness4, FILTERmask_Softness5, FILTERmask_Softness6, FILTERmask_Softness7, FILTERmask_Softness8; void surf (Input IN, inout SurfaceOutputStandard o) { fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; half dist = distance(FILTERmask_Position, IN.worldPos); half sum = saturate((dist  FILTERmask_Radius) / FILTERmask_Softness); half dist2 = distance(FILTERmask_Position2, IN.worldPos); half sum2 = saturate((dist  FILTERmask_Radius2) / FILTERmask_Softness2); half dist3 = distance(FILTERmask_Position3, IN.worldPos); half sum3 = saturate((dist  FILTERmask_Radius3) / FILTERmask_Softness3); half dist4 = distance(FILTERmask_Position4, IN.worldPos); half sum4 = saturate((dist  FILTERmask_Radius4) / FILTERmask_Softness4); half dist5 = distance(FILTERmask_Position5, IN.worldPos); half sum5 = saturate((dist  FILTERmask_Radius5) / FILTERmask_Softness5); half dist6 = distance(FILTERmask_Position6, IN.worldPos); half sum6 = saturate((dist  FILTERmask_Radius6) / FILTERmask_Softness6); half dist7 = distance(FILTERmask_Position7, IN.worldPos); half sum7 = saturate((dist  FILTERmask_Radius7) / FILTERmask_Softness7); half dist8 = distance(FILTERmask_Position8, IN.worldPos); half sum8 = saturate((dist  FILTERmask_Radius8) / FILTERmask_Softness8); o.Albedo = c.rgb * float4(c.r, c.g, c.b, _Alpha); o.Alpha = 1  sum * _Transparency; } ENDCG } FallBack "Diffuse" }
Here I've got a shader to do
This. This is supposed to act like a camera filter imitating a 'light in the dark' scenario. I'm controlling this behaviour via:
public class FilterMaskOmitter : MonoBehaviour { GameObject _filter; Vector3 _maskPos; public float _radius = 15; public float _softness = .25f; private void Awake() { _filter = GameObject.Find("FilterFX"); } private void Update() { if (GetComponent<PlayerStatus>() != null) { _maskPos = new Vector3(transform.position.x, transform.position.y + 10, _filter.transform.position.z); } else { _maskPos = new Vector3(transform.position.x, transform.position.y, _filter.transform.position.z); } Shader.SetGlobalVector("FILTERmask_Position", _maskPos); Shader.SetGlobalFloat("FILTERmask_Radius", _radius); Shader.SetGlobalFloat("FILTERmask_Softness", _softness); } }
I had such a great help getting this far(my first shader coding) from the question before, and even got a really decent tutorial for reference but here I have stuck once again.... shame.
The issue is to iterate surf, and I've tried myself to do this following the given tutorial, but couldn't get something working. As of now, this shader only does the lighteffect for only one FilterMaskOmitter class. What do I tweak for my shader to also do the same transparency control for other gameObjects has the FilterMaskOmitter class?

Punching a hole with shader
Shader "Custom/Mask" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} _ColorStrength("Color Strength", Range(1,4)) = 1 [Space(10)] _EmissionColor("Emission Color", Color) = (1,1,1,1) _EmissionTex("Emission (RGB)", 2D) = "white"{} _EmissionStrength("Emission Strength", Range(1,4)) = 1 [Space(10)] _Transparency("Transparency", Range(0, 1)) = 0 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Standard fullforwardshadows #pragma target 3.0 sampler2D _MainTex, _EmissionTex; struct Input { float2 uv_MainTex; float2 uv_EmissionTex; float3 worldPos; }; fixed4 _Color, _EmissionColor; half _ColorStrength, _EmissionStrength, _Transparency; uniform float4 FILTERmask_Position; uniform half FILTERmask_Radius; uniform half FILTERmask_Softness; void surf (Input IN, inout SurfaceOutputStandard o) { // Albedo fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; // Grayscale half grayscale = (c.r + c.g + c.b) * .3; fixed3 c_g = fixed3(grayscale,grayscale,grayscale); // Emission fixed4 e = tex2D(_EmissionTex, IN.uv_EmissionTex) * _EmissionColor * _EmissionStrength; // Transparency // Put Transparency here half dist = distance(FILTERmask_Position, IN.worldPos); half sum = saturate((dist  FILTERmask_Radius) / FILTERmask_Softness); fixed4 lerpColor = lerp(fixed4(c_g,1),c * _ColorStrength,sum); fixed4 lerpEmission = lerp(fixed4(0, 0, 0, 0), e, sum); o.Albedo = c.rgb/*lerpColor.rgb*/; o.Emission = lerpEmission.rgb; o.Alpha = c.a * _Transparency; } ENDCG } FallBack "Diffuse" }
First time coding shader here. I've found a pretty good tutorial to follow to achieve most of above. What it does now is making a spherical area glow and colorize, but everything else gray: Simple. However, I wanted to make some change of my own from there and really got stuck.
What I wanted to do was replace the colorization with the spherical area to just make a transparent hole on this surface. Am I on the right track? Or am I not even using the right shader type?

Constant Buffer can not receive the correct value
I find the answer. Because I use the Graphics Pixel History for debug the HLSL. Only the variable have been used when rendering have a certain value. Because the SPower was not used in Pixel Shader, its value is NaN,NaN,NaN,NaN。
In .fx file , the struct is defined as this:
cbuffer Material : register(b1) { bool4 UseTexture; // 4 bytes 使用纹理 float4 sPower; // 4 bytes 反射光能 float4 Emissive; // 16 bytes 自发光 // (16 byte boundary) float4 Ambient; // 16 bytes 环境光 //(16 byte boundary) float4 Diffuse; // 16 bytes 漫反射 // (16 byte boundary) float4 Specular; // 16 bytes 镜面反射 // (16 byte boundary) };// Total: // 80bytes (5 * 16 byte boundary)
in .h, the corresponding struct is defined as this:
struct Material { Material() : SPower( 128.0f, 0.0f, 0.0f, 0.0f ) , UseTexture( false, false, false, false ) ,Emissive( 0.0f, 0.0f, 0.0f, 1.0f ) , Ambient( 0.1f, 0.1f, 0.1f, 1.0f ) , Diffuse( 1.0f, 1.0f, 1.0f, 1.0f ) , Specular( 1.0f, 1.0f, 1.0f, 1.0f ) {} XMINT4 UseTexture; // 4 bytes 使用纹理 XMFLOAT4 SPower; // 4 bytes 反射光能 XMFLOAT4 Emissive; // 16 bytes 自发光 // (16 byte boundary) XMFLOAT4 Ambient; // 16 bytes 环境光 //(16 byte boundary) XMFLOAT4 Diffuse; // 16 bytes 漫反射 // (16 byte boundary) XMFLOAT4 Specular; // 16 bytes 镜面反射 };
No matter how I adjust the position of Material.sPower in the struct, No matter what the value I pass into the struct, the Pixel Shader can not receive the correct value of Material.sPower(sPower is always NaN,NaN,NaN,NaN). I do not know why... Please help me.

Swift Sin function lines through UIView points uses too much CPU power
So, I am creating point animations using 1x1 UIViews, which act like points.
This is my code:
import UIKit var timer = Timer() var xx: CGFloat = 1 var shiftX: CGFloat = 0 var yy: CGFloat = 50 var red: Int = 0 var green: Int = 0 var blue: Int = 0 class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.black timer = Timer.scheduledTimer(timeInterval: 0.005, target: self, selector: #selector(self.draw), userInfo: nil, repeats: true) } @objc func draw() { colors() let line = UIView(frame: CGRect(x: x(xx) + 50, y: y(yy) + 50, width: CGFloat(2), height: CGFloat(2))) line.backgroundColor = UIColor.rgb(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue)) self.view.addSubview(line) xx = xx + 1 yy = yy + 1 if line.frame.origin.y > UIScreen.main.bounds.maxY { yy = 50 shiftX = shiftX + 25 } } func colors () { red = red + 1 if red >= 255 { green = green + 1 if green >= 255 { blue = blue + 1 if (blue >= 255) { red = 0 green = 0 blue = 0 } } } } func x(_ t: CGFloat) > CGFloat { return 10 * sin(4 * t) + shiftX  35 } func y(_ t: CGFloat) > CGFloat{ return t } }
And this is what it yields through time:
Every 0.005 seconds, the application generates a new UIView which acts like a point, in a different position. The problem which I get is that after 20/30 seconds of the application running, the point generator starts dropping drastically, generating one point every 0.3 seconds instead of 0.005. Is there any efficient way of doing what I am trying to do? Thanks!

sin(x) as triangle waveform
Example:
Triangle waveform of cos waveform has a period equal to 2𝜋;
1 −2t/𝜋 is a formula describing the shape of the triangle wave between 𝑡 = 0 and 𝑡 = 𝜋.
Task:
Triangle waveform of sin waveform has a period equal to 2𝜋;
What is the formula describing the shape of the triangle wave between t = 0 and t = 𝜋?
I do not understand how to find this formula. Thanks for help! 
How to move an object along a line given two points?
In my game I'm working on, I have a tower that will shoot at an enemy. I'm trying to get my tower shoot to the enemy when the enemy becomes in range. My fireball shows up, however I'm having problem moving the fireball to the enemy. My latest attempt just makes the ball dance around on the screen.
the ball has an x and y position (which resembles where the ball starts. The ball also has a newX and newY position in which is where the enemy is. I was thinking about doing the Pythagorean theorm, but that only gets me the length of the line I want to travel in. Then I was searching online and found out about sin, cos, and atan. I was using atan to get the angle of the slope that I calculated, then plugging the angle in for sin and cos.
My Ball::Update function looks like this:
int dx = newX  x; int dy = newY  y; int ang = atan(dy, dx); x += cos(ang); y += sin(ang);
That code there makes the ball appear to 'dance' on the screen. It doesn't move at all. I tested my code with these lines:
x += cos(45); y += sin(45);
And the ball moves in a 45 degree angle (heading south east) which is expected. So I'm guessing that my error is when calculating the angle. Any math experts that can help?