Function to read pixel colors from model's skin?

Posted By: PHeMoX

Function to read pixel colors from model's skin? - 03/20/08 15:49

Hi there,

I know the function c_trace and SCAN_TEXTURE can output tex_color, but this only works on levelblocks not models and only gives the color of the light source, not the pixel color of the model's skin.

I would like to adjust a particle function depending on the pixel color on the terrain below it. I take it I should use;

Code:
pixel_for_bmap(skinpointer, coords_x, coords_y);



and use the entity's skin intead, but I'm stuck on converting the 3D position of my player to the correct 2D positions in the texture.

Anyone got any hints or ideas?

Cheers
Posted By: testDummy

Re: Function to read pixel colors from model's ski - 03/21/08 13:45

?hist recall?
?
*example of model ambiance change by terrain pixel sampling offered
--most likely applies to terrain, not model
--ref user: Guitar(sp) / Disciple(sp) / Loopix
*pixel sampling: perhaps issues with models, but not terrain
*uv of terrain may be simpler (obvious)
--ref user: Ventilator (model uv info)
?
Posted By: PHeMoX

Re: Function to read pixel colors from model's ski - 03/21/08 13:58

Thanks for your suggestions, I appreciate it!

The model ambiance example I know of uses tex_color and 'SCAN_TEXTURE' or are you talking about a certain shader? I need to know the color of the (pixels of the) terrain underneath a model for a certain particle trail effect.

I think I can handle the reading of the pixels through pixel_for_bitmap on the UV of the terrain, but getting the coordinates right is a bit tricky.

I'll look into the Loopix projects, perhaps there's valuable info I've overlooked indeed!

Cheers
Posted By: testDummy

Re: Function to read pixel colors from model's ski - 03/21/08 14:52

You are probably already familiar with the following example or something similar.
However, given the rate that historical data seems to deteriorate in these parts, I'll dump it anyway:

(note: I am not the author.
Modifications might have been made from original.)
Code:

var terrain_chunk = 0; // won't work in 6.40.5 unless you do this

// (Loopix-ShadowmapAmbient0)
// Terrain Brightness
// Credit appreciated...
// src author: Josh Miller (xXxGuitar511)
// tD arc date: 2006-06-09
// tD mod possible
// ver: 6.60, 6.40.5?

define TraceDist, 100; // Distance to scan down...
define FloorOffs, 10; // Height above entities feet to trace from
define IsTerrain, flag5; // Set this flag for all terrain entities
//define SkinNumber, 3; // Skin to scan for brightness (red channel)
define SkinNumber, 2;
define BrightScale,100; // Percentage (0 - 100) of brightness
define BrightOffs, 0; // Offset of brightness (from 0)

var ScanFrom[3];
var ScanTo[3];
var ScanDist;
var TargetX;
var TargetY;
var TargetPixel;
var PixelColor[3];
var BMapFormat;
bmap* TerrainSkin;

var scanFloorEnable = 0;

action terrain0 {
my.skin = SkinNumber;
my.IsTerrain = on;
}

function ScanFloor(); // *** Prototype function...



function ScanFloor()
{
vec_set(ScanFrom, my.x);
ScanFrom[2] += my.min_z + FloorOffs;
vec_set(ScanTo, ScanFrom);
ScanTo[2] -= TraceDist;
ScanDist = c_trace(ScanFrom, ScanTo, ignore_me | ignore_passable | ignore_passents);
//trace_mode = ignore_me + ignore_passable + ignore_passents;
//ScanDist = trace(ScanFrom, ScanTo);
if (ScanDist == 0)
{ScanDist = TraceDist;}
if (you != null) {
if (you.IsTerrain == on) {
// Is a terrain
TerrainSkin = bmap_for_entity(you, SkinNumber);
BMapFormat = bmap_lock(TerrainSkin, 0);
if (TerrainSkin == 0) { goto(NoTerrain); }
vec_for_vertex(ScanFrom, you, 1);
vec_for_vertex(ScanTo, you, ent_vertices(you));
TargetX = (my.x - ScanFrom.x) / (ScanTo.x - ScanFrom.x) * bmap_width(TerrainSkin);
TargetY = (my.y - ScanFrom.y) / (ScanTo.y - ScanFrom.y) * bmap_height(TerrainSkin);
TargetX = clamp(int(TargetX), 0, bmap_width(TerrainSkin));
TargetY = clamp(int(TargetY), 0, bmap_height(TerrainSkin));
TargetPixel = pixel_for_bmap(TerrainSkin, TargetX, TargetY);
if (TargetPixel == -1)
{goto(NoTerrain);}
pixel_to_vec(PixelColor, null, BMapFormat, TargetPixel);
bmap_unlock(TerrainSkin);
my.ambient = clamp((PixelColor.blue / 255 * BrightScale) + BrightOffs, 0, 100);
//my.ambient = (PixelColor.red + PixelColor.green + PixelColor.blue) / 765 * 100;
}
}
//
NoTerrain:
//
return(ScanDist);
}


Posted By: PHeMoX

Re: Function to read pixel colors from model's ski - 03/21/08 15:46

VERY much appreciated. A quick glance at the code makes me think this should be all I need. Thanks!
Posted By: xXxGuitar511

Re: Function to read pixel colors from model's ski - 03/21/08 16:49

TestDummy was exactly right. I wrote this code for loopix, which will work with reading the pixel from a terrains skin.

However, if you need to do this with a model, you will have to refer to Ventilators super trace plugin
Posted By: PHeMoX

Re: Function to read pixel colors from model's ski - 03/24/08 02:20

Hmmm, I see. Do you happen to have a project in which the code is running and working successfully? It's not that I've got the feeling this isn't working, it probably is working fine as is, but I'm getting a weird glitch every once in a while. Most likely the glitch is the result of the 'return(ScanDist);' part which causes the sprite that's moving to come to a full stop. Unfortunately that's a complication that I can't afford to have,

I'll look into that plug-in though, thanks,

Cheers
Posted By: testDummy

Re: Function to read pixel colors from model's ski - 03/24/08 04:44

Quoting PHeMoX.
Quote:

Do you happen to have a project in which the code is running and working successfully?



That question is asked of which party?
current stance: In some contexts, I prefer to try and avoid terms such as 'working successfully'.
The code sample three posts back should be a copy of the entire shadowMapAmbient.wdl file from an archive here.

/*
?additional specs?
?
*compiles / runs: yes 6.40.5
*terrain (hmp) not mdl
*terrain has black n white checker test texture
*linked with old template6 (Loopix goal related)
--function ScanFloor() was inserted at end of BipedPhy01_Move_Ground
*player ent moves with input (template6) across terrain
--ambiance seems to change accordingly
?
*/

particle functions:
*Sample implementation is not tied to particle functions.
*As is, the sample implementation is probably not particle function friendly.

?manual ref: Particle functions should be fast.?

"return(ScanDist)" is not mandatory
Posted By: PHeMoX

Re: Function to read pixel colors from model's ski - 03/25/08 12:58

Thanks, yeah, actually removing return(ScanDist) seems to solve the issue for the most part.

However I've found a much better solution to my problem in the mean time. Instead of looking at pixels, I just take heights into account, certain textures (and thus pixel colors) are only found on lower areas,

Cheers
Posted By: xXxGuitar511

Re: Function to read pixel colors from model's ski - 03/25/08 17:20

The code above was my script, but slightly modified. I had it set the models ".ambient" to the average of the rgb color of the pixel. Now it reads the brightness from the blue channel.

It only returns the height if a terrain is found...
Posted By: PHeMoX

Re: Function to read pixel colors from model's ski - 03/25/08 19:18

I know... I actually don't use this code for height determination. Just a quick trace.

Anyhow, thanks for the code, it will still be useful in other projects for sure.
© 2024 lite-C Forums