Is it possible to change the color of a particle after creating in the spritekit editor? I tried setParticleColor but it doesn't appear to do anything. Basically I want to have one particle file and a way to programmatically change the color on the fly...
The particleColor property isn't working because of the color ramp settings in the Particle Editor. These are actually stored in the particleColorSequence, which ignores all other particle color properties.
So, to make it work, you need to override the particleColorSequence setter and make it nil first. Then, you need to set the particleColorBlendFactor to fully blend your chosen color with the particle texture (full blending is 1.0). From then on, any explicit particle color setting should work:
emitter.particleColorSequence = nil;
emitter.particleColorBlendFactor = 1.0;
emitter.particleColor = [SKColor redColor];
Related
I am making a 3D puzzle video game and I wish to show a translucent copy of the puzzle piece on its original position when the puzzle piece is near its original position. In other words, to show the player that they are on the right track.
I have done so and it is working fine, here's a 4 second quick gif for demonstration.
However, it does not work when I deploy the game to WebGL. The copy of the object which should be translucent simply isn't.
Here is the function which I use to create a translucent copy of the object. I found the code in this forum thread.
Basically what I am doing is instantiating a copy, deleting uncecessary components, manually changing the material settings to make the object use the Transparent rendering mode, changing the opacity to 0, and then disabling the renderer because for some reason the object is not fully transparent when opacity is set to 0.
GameObject CreateHighlight(GameObject gameObject)
{
GameObject highlight = Instantiate(gameObject);
Destroy(highlight.GetComponent<Rigidbody>());
Destroy(highlight.GetComponent<MeshCollider>());
Destroy(highlight.GetComponent<StayInside>());
Destroy(highlight.GetComponent<ObjectControl>());
// Change render mode to Transparent
Material material = highlight.GetComponent<Renderer>().material;
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
// Change alpha to 0
highlight.GetComponent<MeshRenderer>().material.color = new Color(1.0f, 1.0f, 1.0f, 0.0f);
// Stop rendering the object because setting alpha to 0 does not make the object fully transparent
highlight.GetComponent<MeshRenderer>().enabled = false;
return highlight;
}
I have stumbled upon this Reddit thread and the guy had the same problem as I did. He said he switched the rendering path from Deferred to Forward. I found somewhere one is supposed to change that setting in the MainCamera, so I did. Mine was set on Use Graphics Settings so I explicitly set it to Forward, but nothing changed.
OctangularPRISM Redditor saved the day on Reddit.
What he suggested was creating a new material, making it transparent and then in the code instantiating that material and applying it to the highlighted object instead of changing the rendering mode to Transparent as I tried.
Here are the steps in more detail:
I first created public Material matTransparentMat; as a public variable in my ObjectControl script. This means an additional menu to add a Material was created on all the objects that have the ObjectControl script.
Then, in the project window I created a new Material and made it transparent. You just go Right Click -> Create -> Material, and then choose the material and in the inspector press where it says Opaque and choose Transparent in the drop down menu, and then click the white color and change the alpha from 255 to 0.
Then, drag and drop that Material to the Mat Transparent Mat public variable in the editor.
Now, I changed the code which was changing the rendering mode to Transparent, to /u/OctangularPRISM's code which will instantiate a new material and apply it to the copy of the object.
public Material matTransparentMat;
GameObject CreateHighlight(GameObject GO)
{
GameObject highlight = Instantiate(GO);
Destroy(highlight.GetComponent<Rigidbody>());
Destroy(highlight.GetComponent<MeshCollider>());
Destroy(highlight.GetComponent<StayInside>());
Destroy(highlight.GetComponent<ObjectControl>());
Renderer targetRend = highlight.GetComponent<Renderer>();
Renderer srcRend = GO.GetComponent<Renderer>();
Material newMat = Instantiate(matTransparentMat);
newMat.mainTexture = srcRend.material.mainTexture;
targetRend.material = newMat;
return highlight;
}
Done!
I want to give a glow effect to my cubes in the game. I got success in applying glow effect in the cubes those are already present in the gameplay with different colors.
Below image to show you my current progress and represent my problem through graphics:
I want to change Normal and Emission Color of runtime spawned cubes and up to now I didn't able to get success.
I have tried this code:
GetComponent<MeshRenderer>().material.color = cubeColors[cubeColorIndex];
//GetComponent<MeshRenderer>().material.SetColor("_Color", cubeColors[cubeColorIndex]);
GetComponent<MeshRenderer>().material.SetColor("_EmissionColor", cubeColors[cubeColorIndex] * 2f);
Through the above code, I can able to apply new emission color but still not get success in applying the main color to the cube material.
One more, what is the proper way to change the intensity of emission color?
This is how my cube inspector looks like:
The first: In the shader you are using there is no _Color but the _BaseColor you want to set.
Second: You were almost there. In fact the emission intensity is just a multiplication but using Mathf.LinearToGammaSpace
var meshRenderer = GetComponent<MeshRenderer>();
var material = meshRenderer.material;
material.SetColor("_BaseColor", cubeColors[cubeColorIndex]);
material.SetColor("_EmissionColor", cubeColors[cubeColorIndex] * Mathf.LinearToGammaSpace(2f));
How can I have a white background and use SKEmitterNode in SpriteKit?
The emitter disappears if I set the background color of the SKScene. On the default black background I can see the red particles (when I comment out "backgroundColor = .white" in my SKScene). If I use a CAEmitterLayer then I can see the particles with a white background, but would like to use SKEmitterNode. I've spent a couple hours trying to find any reference to this problem.
Code is very simple -- adds child to SKScene
if let particles = SKEmitterNode(fileNamed: "Sparks.sks") {
particles.position = position
scene.addChild(particles)
}
instead of the default .white, you may want to try setting the color with a color close to white... i.e. a UIColor where the RGB is .9,.9,.9
Background:
I created a dissolve Shader using the lightweight render pipiline, in Shader Graph by drag & drop. My object "dissolves" over time & fades in again. (Youtube "Brackeys Dissolve Shader")
Goal:
I want to change the _edgecolor as soon the object has been dissolved.
I can change the color via:
gameObject.GetComponent<Renderer>().material.SetColor("_edgecolor", Color.blue);
Problem:
How do i access the "Alpha" which seems to be modified by the shader, and would be a good indicator if the object is dissolved, right ?!
I tried to get the Alpha
Color c = gameObject.GetComponent<Renderer>().material.GetColor("_Color");
c.a = ....
won't work, because -> Material doesn't have a color property '_Color'
Here's a Screenshot of my ShaderGraph
I am trying to programatically adjust a particleColorSequence of an SKEmitterNode in SpriteKit. I don't want to replace the entire SKKeyframeSequence, but just alter the color of one of the keyframes. I think the way to do this is to use the setKeyframeValue method, but I cannot work out how to use it. Loading the emitter and trying to change the color like below does not work.
let emitter = SKEmitterNode(fileNamed: "FireWork_Flare_Standard")
emitter?.particleColorSequence?.setKeyframeValue(UIColor.blue, for: 1)
Weirdly, when calling getKeyframeValue, I do see a color is associated with that keyframe, but I can't seem to change it.
How can I change individual keyframes like this?